strip-hints: Python Type Hint Stripping Utility

0.1.13 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

Demonstrates how to use `strip_string_hints` for in-memory string processing and `strip_file_hints` for processing Python files. The `strip_file_hints` function can output to stdout or a specified file.

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)

view raw JSON →