Skip to main content

typemallow

An elegant and automatic solution for generating/outputting Typescript interfaces from your Marshmallow Schemas

I created this Package out of necessity for one of my own projects. Rather than keeping it to myself, I thought it would be helpful for other developers and my own future projects if I uploaded it.

Usage:

Using typemallow is simple.

First, install the package pip install typemallow

Next, for your Marshmallow schemas that you wish to generate Typescript interfaces for, simply import ts_interface and generate_ts from the typemallow module, and prepend the @ts_interface() class decorator to your Marshmallow schema class.

All that is required to generate your Typescript interfaces is to call the generate_ts() function, and provide a filepath as a parameter to output the result.

main.py

from typemallow import ts_interface, generate_ts


@ts_interface()
class Foo(Schema):
    some_field = fields.Str()
    another_field = fields.Date()


generate_ts('./output.ts')

output.ts

export interface Foo {
    some_field: string;
    another_field: date;
}

typemallow even supports Nested Schema fields!

main.py

from typemallow import ts_interface, generate_ts

@ts_interface()
class Foo(Schema):
    some_field = fields.Str()
    another_field = fields.Date()

@ts_interface()
class Bar(Schema):
    foo = fields.Nested(Foo)
    foos = fields.Nested(Foo, many=True)
    bar_field = fields.Str()

output.ts

export interface Foo {
    some_field: string;
    another_field: date;
}

export interface Bar {
    foo: Foo;
    foos: Foo[];
    bar_field: string;
}

Extended Usage:

The @ts_interface() decorator function accepts an optional parameter, context, which defaults to... well... 'default'.

"Why is this the case?"

When a Marshmallow Schema is identified with with @ts_interface decorator, it is added to a list in a dictionary of schemas, with the dictionary key being the value provided to the context parameter. If you were to provide different contexts for each schema, additional keys will be created if they do not exist, or the schema will simply be appended to the list at the existing key.

This comes in handy, as the generate_ts() function also accepts an optional context parameter, which will filter only schemas in the dictionary at the specific key.

This is useful if you wish to output different contexts to different files, e.g.

main.py

...
from typemallow import ts_interface, generate_ts

@ts_interface(context='internal')
class Foo(Schema):
    foo = fields.Str()


@ts_interface(context='internal')
class Bar(Schema):
    bar = fields.Str()
 

@ts_interface(context='external')
class FooBar(Schema):
    foo_bar = fields.Str()


''' 
we're telling typemallow that we only want to generate interfaces from Schemas with 
an 'internal' context to './internal.ts' 
'''
generate_ts('./internal.ts', context='internal')

''' 
only generate interfaces from Schemas with an 'external' context to './external.ts' 
'''
generate_ts('./external.ts', context='external')

internal.ts

export interface Foo {
    foo: string;
}

export interface Bar {
    bar: string;
}

external.ts

export interface FooBar {
    foo_bar: string;
}

Metadata

Release files for typemallow 0.0.4

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

Source distribution (sdist)

Source distribution for typemallow 0.0.4
File Size Uploaded Provenance Actions
typemallow-0.0.4.tar.gz 3.5 kB Without provenance Details for typemallow-0.0.4.tar.gz

Release files / typemallow-0.0.4.tar.gz

Download URL typemallow-0.0.4.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2e065ed0a710d10047d62113f1874637a24af344f03267ac99f6c4615b08aecc
BLAKE2b-256 checksum
How to use checksums
56308e0870854f1fba34c46dbcff50d5de6f74612c4c564d2ed98e023f894f3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

Security for typemallow 0.0.4

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