TypeSpec Parser for Python
A Python library that parses TypeSpec definitions and generates idiomatic data models for Python, C++, Rust, Go, Zig, and V.
Features
- Parse TypeSpec model definitions
- Generate Python dataclasses with type hints
- Generate C++ headers
- Generate Rust structs and enums
- Generate Go structs, string enum aliases, constants, and JSON tags
- Generate Zig structs and enums
- Generate V structs and enums
- 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
tsc-py schema.tsp
# Parse a TypeSpec file and save to a Python file
tsc-py schema.tsp -o models.py
# Generate other languages
tsc-py schema.tsp --language rust -o models.rs
tsc-py schema.tsp --language go -o models.go
tsc-py schema.tsp --language zig -o models.zig
tsc-py schema.tsp --language vlang -o models.v
# Use a custom Jinja template for the selected language
tsc-py schema.tsp --language rust --template custom-rust.j2 -o models.rs
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_python()
print(code)
# Generate Rust, Go, Zig, or V
rust_code = parser.generate_rust()
go_code = parser.generate_go(package_name="models")
zig_code = parser.generate_zig()
v_code = parser.generate_vlang(module_name="models")
# Override the built-in Jinja template
custom_rust = parser.generate_rust(template_path="custom-rust.j2")
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]
It can also generate idiomatic models for other languages:
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Status {
Active,
Inactive,
}
#[derive(Debug, Clone, PartialEq)]
pub struct User {
pub name: String,
pub age: i32,
pub email: Option<String>,
pub addresses: Vec<Address>,
}
package typespec
type Status string
const (
StatusActive Status = "active"
StatusInactive Status = "inactive"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Email *string `json:"email"`
Addresses []Address `json:"addresses"`
}
const std = @import("std");
pub const Status = enum {
active,
inactive,
};
pub const User = struct {
name: []const u8,
age: i32,
email: ?[]const u8,
addresses: []Address,
};
module typespec
pub enum Status {
active
inactive
}
pub struct User {
pub:
name string
age int
email ?string
addresses []Address
}
See Code Generation for language-specific notes and custom template context.
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
Metadata
Release files for tsc-py 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tsc_py-0.2.0.tar.gz | 24.2 kB | Without provenance | Details for tsc_py-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tsc_py-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for tsc_py-0.2.0-py3-none-any.whl |
Total release size: 52.8 kB
Release files / tsc_py-0.2.0.tar.gz
| Download URL | tsc_py-0.2.0.tar.gz |
|---|---|
| Size | 24.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
239ea2cc3030bd0ef8921ca713db110b17c34ff72cff342588943771ad64baa1
|
|
BLAKE2b-256 checksum How to use checksums |
912f182ecb8fff531a6ccb30bc4e538dfc817de3f976b02d6e0d51740c0ed79d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / tsc_py-0.2.0-py3-none-any.whl
| Download URL | tsc_py-0.2.0-py3-none-any.whl |
|---|---|
| Size | 28.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9610e4930b91100f0b38c34a772aed889c9d3c0fcee6d1f3895f8c612bac4c78
|
|
BLAKE2b-256 checksum How to use checksums |
3a59524a9d8331d77f48fdaee0ecfc153776c3056e620a942b74bf53a3cad282
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for tsc-py 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.