Skip to main content
CircleCI codecov

py2ts

Covert Python dataclass and Enum to TypeScript interface.

Mapping of Scalar Types

assert python_type_to_typescript(str) == "string"
assert python_type_to_typescript(int) == "number"
assert python_type_to_typescript(bool) == "boolean"
assert python_type_to_typescript(float) == "number"
assert python_type_to_typescript(datetime) == "string"
assert python_type_to_typescript(date) == "string"
assert python_type_to_typescript(Decimal) == "string"
assert python_type_to_typescript(dict) == "{}"
assert python_type_to_typescript(list) == "Array<any>"

Usage

Create a schema with Python dataclass:

class EnumFruit(Enum):
    APPLE = "APPLE"
    ORANGE = "ORANGE"


@dataclass
class NestedSchema:
    string_field: str
    nullable_datetime_field: Optional[datetime]
    recursively_nested_field: Optional["NestedSchema"]


@dataclass
class ComplexSchema:
    nullable_int_field: Optional[int]
    nullable_decimal_field: Optional[int]
    nullable_enum_field: Optional[EnumFruit]
    nullable_nested_field: Optional[NestedSchema]
    nested_list_field: List[NestedSchema]

blocks = []
for path, node in schemas2typescript([ComplexSchema]).items():
    blocks.append(node.to_typescript())

print("\n".join(blocks))

The output is:

export enum EnumFruit {
  APPLE = 'APPLE',
  ORANGE = 'ORANGE'
}
export interface NestedSchema {
  string_field: string;
  nullable_datetime_field: string | null;
  recursively_nested_field: NestedSchema | null;
}
export interface ComplexSchema {
  nullable_int_field: number | null;
  nullable_decimal_field: number | null;
  nullable_enum_field: EnumFruit | null;
  nullable_nested_field: NestedSchema | null;
  nested_list_field: Array<NestedSchema>;
}

Generate service registry

Dependency: ripgrep

Generate boilerplate service registry code.

Usage

python -m py2ts.generate_service_registry > service_registery.py

How it works

The command assumes that any classes named XXXService should be included in the service registry. For example:

class TestService:
    pass

will generate the following code:

# Generated code. DO NOT EDIT!

from dataclasses import dataclass

from tests.test_generate_service_registr import TestService


@dataclass
class ServiceRegistry:
    test_service: TestService = TestService()

service_registry = ServiceRegistry()

Metadata

Release files for py2ts 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py2ts 0.0.6
File Size Uploaded Provenance Actions
py2ts-0.0.6.tar.gz 7.8 kB Without provenance Details for py2ts-0.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py2ts 0.0.6
File Interpreter ABI Platform Actions
py2ts-0.0.6-py3-none-any.whl Python 3 none any Details for py2ts-0.0.6-py3-none-any.whl

Total release size: 15.3 kB

Release files / py2ts-0.0.6.tar.gz

Download URL py2ts-0.0.6.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
24ca30c8950ea378bdd98ee523d72988c3bae8435dca8b9dd8eb0bd0670e9827
BLAKE2b-256 checksum
How to use checksums
aa3879a2f62608ef9ce1b970d8fcaab3ca5fd34f8370886e2232d375a793f248
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.1.0 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.8.1

Release files / py2ts-0.0.6-py3-none-any.whl

Download URL py2ts-0.0.6-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb035f008f54edfc5343970f24f6fc83586d3c112e707acd25969045d7535019
BLAKE2b-256 checksum
How to use checksums
5d80d9740128060db31694c342699531a63446e796e41da0b5e4c4d45a36c09c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.1.0 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.8.1

Security for py2ts 0.0.6

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page