Consider this:
VAL = 42
def get() -> type(VAL):
return VAL
This is perfectly valid Python, but type checkers don’t accept it. Instead, you are supposed to write out all type references statically or use very limited global aliases.
macrotype makes this work exactly as you expect with all static type checkers, so your types can be as dynamic as the rest of your Python code.
How?
macrotype is a CLI tool intended to be run before static type checking:
macrotype your_module
macrotype imports your modules under normal Python and then generates corresponding .pyi files with all types pinned statically so the type checker can understand them.
In our example, macrotype would generate this:
VAL: int
def get() -> int: ...
macrotype is the bridge between static type checkers and your dynamic code. macrotype will import your modules as Python and then re-export the runtime types back out into a form that static type checkers can consume.
What else?
In addition to the CLI tool, there are also helpers for generating dynamic types. See macrotype.meta_types. These are intended for you to import to enable dynamic programming patterns which would be unthinkable without macrotype.
Type checking
Most users will want to run their static type checker through the macrotype.check entrypoint. This wrapper generates stub files and then invokes your checker with PYTHONPATH configured so the generated stubs are found. The console script is installed as macrotype-check and accepts the checker command followed by the paths to stub. Any additional arguments after -- are passed through to the checker:
macrotype-check mypy src/ -- --strict
Stubs are written to __macrotype__ by default; use -o to choose a different directory. When run with mypy, macrotype-check prepends the stub directory to MYPYPATH so the overlay stubs are picked up automatically. Other tools receive the stub directory on PYTHONPATH.
If you run mypy without macrotype-check, set MYPYPATH or pass --custom-typeshed-dir to point at the stub directory so it behaves the same way.
Watch mode
Use --watch (or -w) to regenerate stubs whenever the source files change. The command is re-run in a fresh Python process each time:
macrotype --watch your_module
The same flag is available for macrotype-check to rerun the wrapped type checker as files change.
Dogfooding
The macrotype project uses the CLI on itself. Running:
python -m macrotype macrotype
regenerates the stub files for the package in place. A CI job ensures that the checked in .pyi files are always in sync with this command.
Documentation
Full documentation is available on Read the Docs.
Release files for macrotype 0.3.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 | |
|---|---|---|---|
| macrotype-0.3.0.tar.gz | 85.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| macrotype-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 219.2 kB
Release files / macrotype-0.3.0.tar.gz
| Download URL | macrotype-0.3.0.tar.gz |
|---|---|
| Size | 85.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a3bbfe04e61439b6988c8c593611e5a4fad7da326991ed2de3378415fd25f603
|
|
BLAKE2b-256 checksum How to use checksums |
dce3ba52cf364e3d4d83a94043b2a0952899cab5d8063260283abbc369473146
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / macrotype-0.3.0-py3-none-any.whl
| Download URL | macrotype-0.3.0-py3-none-any.whl |
|---|---|
| Size | 134.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9bbf18c1fe1c028ff8610523b00ef94b5d36683405254fe88fb5903a85a20265
|
|
BLAKE2b-256 checksum How to use checksums |
19e037dd2f398982d007602ce2f89d215a019dc151a7177267f063d95d47d18b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|