Skip to main content

A TypeSpec parser that generates Python dataclasses

Project description

TypeSpec Parser for Python

A Python library that parses TypeSpec definitions and generates Python dataclasses.

Features

  • Parse TypeSpec model definitions
  • Generate Python dataclasses with type hints
  • Support for enums
  • Support for 1:1 and 1:n relationships
  • Command-line interface

Installation

Using pip

pip install typespec-parser

Using uv (recommended)

uv sync --dev

Usage

Command Line

# Parse a TypeSpec file and output to stdout
typespec-parser schema.tsp

# Parse a TypeSpec file and save to a Python file
typespec-parser schema.tsp -o models.py

Python API

from typespec_parser import TypeSpecParser

parser = TypeSpecParser()
parser.parse("""
model User {
  name: string;
  age: integer;
  email: string?;
}

enum Status {
  active,
  inactive,
}
""")

# Generate Python dataclasses
code = parser.generate_dataclasses()
print(code)

Example

Given the following TypeSpec:

model User {
  name: string;
  age: integer;
  email: string?;
  addresses: Address[];
}

model Address {
  street: string;
  city: string;
  country: string;
}

enum Status {
  active,
  inactive,
}

The parser will generate:

from dataclasses import dataclass
from typing import List, Optional
from enum import Enum

class Status(Enum):
    ACTIVE = 'active'
    INACTIVE = 'inactive'

@dataclass
class Address:
    street: str
    city: str
    country: str

@dataclass
class User:
    name: str
    age: int
    email: Optional[str]
    addresses: List[Address]

Development

This project uses uv for dependency management and packaging.

To install dependencies and set up the development environment:

uv sync --dev

To run tests:

uv run pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tsc_py-0.1.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tsc_py-0.1.0-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file tsc_py-0.1.0.tar.gz.

File metadata

  • Download URL: tsc_py-0.1.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for tsc_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a8ff929e04f3100271c092fa0ff83b6b683a45acc94c050e1f0469909b74129
MD5 2c9ee5d0aa21adc29c27ab07244c6969
BLAKE2b-256 721ed9e69dededbbc4e0b325dc41e8594c210bf923fdd88e10f1065cd53eb93a

See more details on using hashes here.

File details

Details for the file tsc_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tsc_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for tsc_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba81261788b50c33363929f8083b2cf72d2c18c6de83aaa418ffdb30e38f9190
MD5 78fb025d30fbf385875a5954ff954193
BLAKE2b-256 8987d433b6f07b2cc1447a12a0454da96c5a928251944856ca467fda8192dc43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page