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.1.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.1-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tsc_py-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 372240253a78dc75b41a40be5147fc13914a30687ba05e6e7a90b42d3f65681f
MD5 82b347e3a84954e44d051feb7cf7b8cf
BLAKE2b-256 f0175936a9ff2732db287adae5e94fc68d588d1a334da7746f8a99ac44d992d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tsc_py-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 530ebb3ae5c14b3cdf1a45d8251a40725ae6fc2b41dec9a249823fc9539031a4
MD5 9af7b1a853e886d16dcc90938685737e
BLAKE2b-256 73fdaf32c7af3f4049ab696745a07067ff48d5e38666c82172cf3cc8477ef9d9

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