{"id":3283,"library":"strip-hints","title":"strip-hints: Python Type Hint Stripping Utility","description":"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.","status":"active","version":"0.1.13","language":"en","source_language":"en","source_url":"https://github.com/abarker/strip-hints","tags":["type hints","code transformation","static analysis","utility","python3"],"install":[{"cmd":"pip install strip-hints","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"symbol":"strip_string_hints","correct":"from strip_hints import strip_string_hints"},{"symbol":"strip_file_hints","correct":"from strip_hints import strip_file_hints"},{"note":"Used for automatic stripping on import. Relies on the deprecated `imp` module for Python 3 and is not recommended for new Python 3 projects.","symbol":"install_import_hook","correct":"from strip_hints.import_hooks import install_import_hook"}],"quickstart":{"code":"import os\nfrom strip_hints import strip_string_hints, strip_file_hints\n\n# Example 1: Stripping hints from a string\ncode_with_hints = \"\"\"\ndef greet(name: str) -> str:\n    return f\"Hello, {name}\"\n\"\"\"\nstripped_code = strip_string_hints(code_with_hints)\nprint(\"--- Stripped String ---\")\nprint(stripped_code)\n\n# Example 2: Stripping hints from a file (creating a dummy file first)\nfile_content = \"\"\"\n# my_module.py\ndef add(a: int, b: int) -> int:\n    return a + b\n\ndef subtract(x: float, y: float) -> float:\n    return x - y\n\"\"\"\n\nwith open(\"my_module_with_hints.py\", \"w\") as f:\n    f.write(file_content)\n\noutput_filename = \"my_module_stripped.py\"\n\n# Strip hints and write to a new file\nstrip_file_hints(\"my_module_with_hints.py\", outfile=output_filename)\n\nprint(\"\\n--- Stripped File Content (my_module_stripped.py) ---\")\nwith open(output_filename, \"r\") as f:\n    print(f.read())\n\n# Clean up dummy files\nos.remove(\"my_module_with_hints.py\")\nos.remove(output_filename)\n","lang":"python","description":"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."},"warnings":[{"fix":"Avoid `--strip-nl` and `--to-empty` if exact line/column number correspondence is critical for debugging. The default behavior attempts to preserve these.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Unless you specifically need to disallow the transformation, avoid using `--no-equal-move`. If an exception occurs, remove this flag or refactor your type hints to avoid newlines in annotated assignments.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully manage `# strip-hints: off` and `# strip-hints: on` directives, ensuring they are correctly placed and removed when no longer needed.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Assume Python 3 as the target environment. If using `install_import_hook`, be aware of the underlying deprecated `imp` module.","message":"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.","severity":"deprecated","affected_versions":"All versions (historical context, affects current expectations)"},{"fix":"Understand that `strip-hints` fundamentally alters the runtime availability of type annotations. Only use it when downstream consumers of the code do not require runtime type information, or if that information is recreated/managed by another mechanism.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}