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 uv

uv pip install tsc-py

Running without installing

uvx tsc-py schema.tsp

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.2.tar.gz (15.4 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.2-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tsc_py-0.1.2.tar.gz
  • Upload date:
  • Size: 15.4 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.2.tar.gz
Algorithm Hash digest
SHA256 3902db78c9ce00e97a06f32ef9a87b837c4a7eae0a3fe9190ee035a09d2414db
MD5 9d7e819892260499653bb77068d4d6e1
BLAKE2b-256 521a1df5cc5c0c0908051ef1ccd603e64c658fe07b9f67e331f29d77a6a62978

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tsc_py-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b1aba5c43381434a889a82047d2308131badac647ab7995b0e32f22c4e0ea4f4
MD5 a0af108a21224d14566345c62f5ceba6
BLAKE2b-256 2ad6a3852320e9ecf17df2c0d29edcbb3b5f09aa2cbde1d9c5ea46c1f523beb6

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