type-stripper
Python package that strips type annotations from a source file, printing the modified file to stdout.
Installation
Install from PyPI:
pip install type-stripper
Usage
Runs as a CLI tool. Give it a file path:
type-stripper /path/to/file.py
...and the results of the changes will be sent to stdout.
This output can be piped back to a file:
type-stripper /path/to/file.py > newfile.py
Use type-stripper --help for details.
What it does
-
Parameter annotations in function definitions are removed:
- def func(a: int, b: str = "30", c: int = 42, *args: str, **kwargs: dict[str, int]): + def func(a, b = "30", c = 42, *args, **kwargs):
-
Return-type annotations are removed:
- def func() -> str: + def func():
-
Annotations in variable assignments are removed:
- x: int = 10 + x = 10
-
Bare annotation statements are removed entirely:
- x: int
Formatting, comments, and all other syntax remains unchanged.
What it does not do
Some of the resulting code's format may be stylistically incorrect:
# Given:
- def foo(a: int = 4):
# Produces:
+ def foo(a = 4):
# What it probably should produce (note the spacing):
+ def foo(a=4):
You may wish to run results through a formatter first.
How?
This package uses libcst
to parse the Concrete Syntax Tree of an input module,
then modifies (or removes) nodes in that tree to strip type annotations away.
A similar task could be completed in Python's ast module,
however the AST does not preserve code structure or syntax the way CST does.
Why?
For fun and profit. 🙂
There are some potential use cases, however:
- Backwards compatibility: Code developed on modern Python versions with type hinting may not work on older versions of Python. While I would strongly recommend migrating to a more recent version of Python, this tool can get code working faster.
- Other Python variants: Some Python dialects (such as Starlark) may use the same Python syntax, but may not (now or ever) support type annotations. This tool can be used to transpile code meant for more "standard" Python flavors to work in these environments.
- Smaller file size: Need to shave precious bytes off a Docker image build and your type hints are not relevant to the runtime (i.e., not using FastAPI where type annotations are critical)? Strip those type hints off to reduce file sizes to their bare minimum.
- Reduced complexity when teaching: Modern Python code with type annotations may be difficult for the newest beginners to comprehend. While teaching how data types interact is important, some learners may benefit from the reduced noise in their code samples.
Absolutely none of these statements are qualified or tested: I just sort of made them up. Take it as you will!
Contributing
See CONTRIBUTING.md
Release files for type-stripper 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| type_stripper-0.1.5.tar.gz | 32.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| type_stripper-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 40.1 kB
Release files / type_stripper-0.1.5.tar.gz
| Download URL | type_stripper-0.1.5.tar.gz |
|---|---|
| Size | 32.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c018b11e02fcecb99e7f3bbaeaeec1d5c7788f048d65e470117c384792b12a3
|
|
BLAKE2b-256 checksum How to use checksums |
fe0537b0fe8f39fb1d38c9e9040af349e5922a6419c7556d29ab3a3a0211f8d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.5.16
|
Release files / type_stripper-0.1.5-py3-none-any.whl
| Download URL | type_stripper-0.1.5-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
16e42a4fbb6a1b3984c9cf11cfe7b79ff06d1cca438c2497de1185a0946cc3c3
|
|
BLAKE2b-256 checksum How to use checksums |
496fd9c6858a476c8d75719099ece9ccd816b60838cad72bd24beed25cb70bff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.5.16
|