Skip to main content

A CLI tool that removes type annotations from Python source files.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

type_stripper-0.1.5.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

type_stripper-0.1.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file type_stripper-0.1.5.tar.gz.

File metadata

  • Download URL: type_stripper-0.1.5.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.16

File hashes

Hashes for type_stripper-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9c018b11e02fcecb99e7f3bbaeaeec1d5c7788f048d65e470117c384792b12a3
MD5 15416c264030910d34a2241397c15ab0
BLAKE2b-256 fe0537b0fe8f39fb1d38c9e9040af349e5922a6419c7556d29ab3a3a0211f8d2

See more details on using hashes here.

File details

Details for the file type_stripper-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for type_stripper-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 16e42a4fbb6a1b3984c9cf11cfe7b79ff06d1cca438c2497de1185a0946cc3c3
MD5 9a540879c9f53b9e91fd971cf1a86d94
BLAKE2b-256 496fd9c6858a476c8d75719099ece9ccd816b60838cad72bd24beed25cb70bff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page