strip-hints: Python Type Hint Stripping Utility
strip-hints is a Python library and command-line program designed to remove type hints from Python code files. It aims to leave runnable code while making minimal changes to preserve line and column numbers, aiding in debugging of the stripped output. It supports stripping hints from strings, individual files, or via an import hook for automatic processing. The current version is 0.1.13, and it is actively maintained on a feature-driven release cadence.
Warnings
- gotcha Using options like `--strip-nl` (also strip non-logical newline tokens) or `--to-empty` (map removed code to empty strings) can alter the original line and column numbers. This can make debugging the stripped code harder by breaking correspondence with the original source.
- gotcha The `--no-equal-move` command-line option prevents the program from adjusting annotated assignments that include newlines in their type hints. If such a situation occurs with this option enabled, it will raise an exception rather than attempting a syntactically correct transformation.
- gotcha The library allows disabling stripping for specific code sections using `# strip-hints: off` and re-enabling with `# strip-hints: on`. Accidentally leaving these directives in or misplacing them can lead to unexpected behavior where hints are either retained or stripped in unintended areas.
- deprecated The primary use case of `strip-hints` was originally for Python 2 compatibility (developing with Python 3 hints, running on Python 2). Python 2 is no longer maintained, and the library is no longer tested against it. The `install_import_hook` mechanism also uses the `imp` module, which is deprecated for Python 3. Users should target Python 3.
- gotcha Stripping type hints removes runtime metadata. If other tools or frameworks in your ecosystem rely on runtime inspection of `__annotations__` or `typing.get_type_hints` (e.g., for dependency injection, API schema generation, or validation), using `strip-hints` will remove this crucial information, potentially causing runtime errors or unexpected behavior in those tools.
Install
-
pip install strip-hints
Imports
- strip_string_hints
from strip_hints import strip_string_hints
- strip_file_hints
from strip_hints import strip_file_hints
- install_import_hook
from strip_hints.import_hooks import install_import_hook
Quickstart
import os
from strip_hints import strip_string_hints, strip_file_hints
# Example 1: Stripping hints from a string
code_with_hints = """
def greet(name: str) -> str:
return f"Hello, {name}"
"""
stripped_code = strip_string_hints(code_with_hints)
print("--- Stripped String ---")
print(stripped_code)
# Example 2: Stripping hints from a file (creating a dummy file first)
file_content = """
# my_module.py
def add(a: int, b: int) -> int:
return a + b
def subtract(x: float, y: float) -> float:
return x - y
"""
with open("my_module_with_hints.py", "w") as f:
f.write(file_content)
output_filename = "my_module_stripped.py"
# Strip hints and write to a new file
strip_file_hints("my_module_with_hints.py", outfile=output_filename)
print("\n--- Stripped File Content (my_module_stripped.py) ---")
with open(output_filename, "r") as f:
print(f.read())
# Clean up dummy files
os.remove("my_module_with_hints.py")
os.remove(output_filename)