MyPy to TS generator
Project description
myts
In beta
Road to ready:
- Better test coverage, 80% baseline
- Support for generics, enums with auto, most MyPy typing.
Converts MyPy types to TS types. Uses MyPy's internal api to gather type info. Including your pydantic project if its MyPy friendly.
Call as a cli, optionally directory watchable. Or import and invoke directly from your Python code.
Installation
pip
pip install myts
uv
uv add myts
poetry
poetry add myts
Usage
In your code
Myts looks for any classes that inherit from MytsType to start building its dependency graph.
These by default will be included in the output TS.
Alternatively you can use the myts_export decorator on Enums and TypedDicts and classes that you would like to have exported without needing to be referenced by a MytsType class.
from enum import IntEnum
from myts import MytsType, myts_export
@myts_export
class Fruit(IntEnum): # Will be in the TS output
apple = 0
orange = 1
banana = 2
class Vegetable(IntEnum): # Will not be, because it is not referenced ever and is not decorated with myts_export
carrot = 0
celery = 1
lettuce = 2
class Drink(IntEnum): # Will be in the output, reference by a MytsType below
water = 0
milk = 1
soda = 2
class BeveragePreference(MytsType): # Will export and will also export the Drink enum
drink: Drink
person: Person
CLI
To see all cli options run
myts -h
Optionally a config file can be used.
By default myts will attempt to find a myts.toml in the specified root. Alternatively a config file can be supplied through the -c or --config argument to the cli.
You can also specify these options under a [tool.myts] section in your pyproject.toml found at the --root path.
Note: that arguments to the cli will override config settings found in the config.
The resolution of options is: (lowest → highest priority)
- pyproject
[tool.myts] myts.toml- command line arguments
Meaning an option defined in [tool.myts] can and will be overwritten by the same option if provided to myts.toml or the command line.
myts.toml or [tool.myts] is a flat toml that can optionally supply any or none of the following:
root = "/some/path" # If specifying root in the config make sure it is an absolute path
output = "./out/" # When output is relative, it is relative to root
group = "module" # "module" or "single"
preserve_structure = true # When group is "module" this will create folders matching the py structure
dry_run = false
#output_file_name = "out.ts" # When using group = "single" this will be the name of the output file
#trim_root = "myapp.has.a.long.root.path" # Optionally provide a python formatted module path to trim from all output
Simplest example
To generate the .ts files.
cd <myprojectroot>
myts --output your/output/dir/
A more complex example
Uses trim-root to remove a common py module path from the output.
Uses watch to automatically update generated ts when the py changes.
cd <myprojectroot>
myts --output your/output/dir/ -w
Py
import pathlib
import myts
from myts.cli.main import extract_ts
config = myts.MytsConfiguration(
root=pathlib.Path('someprojdir'), # defaults to os.cwd()
output=pathlib.Path('someoutputdir'),
group='module', # 'single' or 'module' - single will output a single .ts file, 'module' will output files matching the `py` files they come from
preserve_structure=True, # No effect if group == 'single', otherwise determines output structure. If true folders will be created to match py paths
dry_run=False, # If true, just prints the paths of output files that would be written
output_file_name='types.ts', # Only used if group == 'single', determines the name of the single file generated
trim_root="myapp.some.path" # When preserving structure, use this to trim a common root
)
extract_ts(config)
Roadmap
- Export to multiple files, flat folder or matching py module structure
- Support generics
- Watch for
pychanges and auto generatets - Config options for
- Variable and type naming options (camelCase, PascalCase, snake_case)
- Interface vs type output
- Special support for pydantics built-ins (might build a plugin architecture to support)
Exporter configuration
Currently, myts only has 1 built-in exporter for ts. However it has been designed to easily support multiple exporters in the future, including the potential for custom exporters(actually achievable today, just without a "formal" plugin architecture).
The TS exporter can be configured by adding a [exporter.ts] section to your myts config:
[exporter.ts]
enum_output_format = "typed_const_map" # or "std_enum" to use TS enums. Default is typed_const_map for DX
typeddict_output_format = "interface" # or "type". Default interface to support inheritance
class_output_format = "interface" # or "type". Default interface to support inheritance
use_declare = true # default true, for any `type` declarations will precede with 'declare' if true
indent = "tabs" # default tabs, because they're superior, "spaces" is an option, currently the number of spaces is 4 and not yet configurable
Example of functionality
Python (Mypy)
from myts import MytsType, myts_export
from pydantic import BaseModel
class APIModel(BaseModel, MytsType):
"""
All pydantic models that extend from this will be picked up by Myts
"""
...
class MyPydanticModel(APIModel): # Myts will grab this
name: str
class Fruits(Enum):
APPLE = auto()
BANANA = auto()
GRAPES = auto()
class MyGenericClass[T: str | int]:
my_var: T
fruits: list[Fruits]
Output (Typescript)
export interface MyPedanticModel {
name: string;
}
export const Fruits = {
APPLE: 1,
BANANA: 2,
GRAPES: 3,
} as const;
export type Fruits = typeof Fruits[keyof typeof Fruits];
export interface MyGenericClass<T extends string | number> {
myVar: T;
fruits: Array<Fruits>;
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file myts-0.3.12.tar.gz.
File metadata
- Download URL: myts-0.3.12.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ec24bee53f7c345280c4fbded2bddd73f8bda15039f93bed0647344524883d7
|
|
| MD5 |
2c0a11034979ab63df60c4cea024496b
|
|
| BLAKE2b-256 |
a2f4e53475eb7608240e03a8a0d687929ae4155c60f030a0532d3af2a8434e3c
|
Provenance
The following attestation bundles were made for myts-0.3.12.tar.gz:
Publisher:
release.yaml on dennmat/myts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
myts-0.3.12.tar.gz -
Subject digest:
9ec24bee53f7c345280c4fbded2bddd73f8bda15039f93bed0647344524883d7 - Sigstore transparency entry: 1700442813
- Sigstore integration time:
-
Permalink:
dennmat/myts@55b71385f6706397bf012131f249f27bca161d8b -
Branch / Tag:
refs/tags/v0.3.12 - Owner: https://github.com/dennmat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@55b71385f6706397bf012131f249f27bca161d8b -
Trigger Event:
push
-
Statement type:
File details
Details for the file myts-0.3.12-py3-none-any.whl.
File metadata
- Download URL: myts-0.3.12-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bf01e45232bdeeff08dd5cf667ad09803eeacba96303840c8bc3ae13d208751
|
|
| MD5 |
64b939396e823eb62bb011886c8f5385
|
|
| BLAKE2b-256 |
1ca76c2b9ab5fe60a8c5f5e2b8dd36c3a65305a69436d4ac0bc3c78115589a5e
|
Provenance
The following attestation bundles were made for myts-0.3.12-py3-none-any.whl:
Publisher:
release.yaml on dennmat/myts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
myts-0.3.12-py3-none-any.whl -
Subject digest:
1bf01e45232bdeeff08dd5cf667ad09803eeacba96303840c8bc3ae13d208751 - Sigstore transparency entry: 1700442885
- Sigstore integration time:
-
Permalink:
dennmat/myts@55b71385f6706397bf012131f249f27bca161d8b -
Branch / Tag:
refs/tags/v0.3.12 - Owner: https://github.com/dennmat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@55b71385f6706397bf012131f249f27bca161d8b -
Trigger Event:
push
-
Statement type: