{"id":2331,"library":"typed-ast","title":"Typed-AST","description":"typed-ast is a Python 3 package that provided parsers for Python 2.7 and Python 3 (up to 3.7) Abstract Syntax Trees (ASTs). Unlike the standard `ast` module before Python 3.8, it included support for PEP 484 type comments and was independent of the Python version it was run on. The project is currently at version 1.5.5 and is no longer actively maintained, with recommendations to use the built-in `ast` module for Python 3.8+.","status":"maintenance","version":"1.5.5","language":"en","source_language":"en","source_url":"https://github.com/python/typed_ast","tags":["AST","type comments","static analysis","parser","Python 2","Python 3"],"install":[{"cmd":"pip install typed-ast","lang":"bash","label":"PyPI"}],"dependencies":[],"imports":[{"note":"Use `typed_ast.ast3` for parsing Python 3 code with type comments, as the standard `ast` module did not support them prior to Python 3.8. For Python 2.7 code, use `typed_ast.ast27.parse`.","wrong":"import ast\nast.parse(code_string)","symbol":"typed_ast.ast3.parse","correct":"from typed_ast import ast3\n\nast3.parse(code_string)"}],"quickstart":{"code":"from typed_ast import ast3\n\ncode_to_parse = \"\"\"\ndef example_function(a: int, b) -> str:\n    result = a + int(b) # type: ignore\n    return str(result)\n\"\"\"\n\ntry:\n    tree = ast3.parse(code_to_parse)\n    print(\"Successfully parsed code with type comments.\")\n    # You can then traverse the AST, for example:\n    # import ast\n    # print(ast.dump(tree, indent=4))\nexcept SyntaxError as e:\n    print(f\"Syntax error during parsing: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates parsing Python 3 code containing PEP 484 type comments using `typed_ast.ast3.parse`. The `typed_ast` module was specifically designed to handle these comments, which the standard `ast` module in Python versions prior to 3.8 did not expose directly in the AST. For Python 3.8 and newer, the standard `ast` module can often be used instead."},"warnings":[{"fix":"For projects targeting Python 3.8 and newer, migrate to the standard library `ast` module. For projects requiring Python 3.7 or older, `typed-ast` may still be necessary, but be aware of its unmaintained status. Consider conditional imports or dropping support for older Python versions.","message":"The `typed-ast` project is no longer actively maintained. Its primary functionality (parsing type comments) was merged into the standard library `ast` module starting with Python 3.8. It does not support parsing syntax introduced in Python 3.8 or newer.","severity":"breaking","affected_versions":"All versions"},{"fix":"Ensure that the necessary C/C++ compiler toolchain is installed for your operating system (e.g., 'Desktop development with C++' workload for Visual Studio on Windows, `build-essential` on Debian/Ubuntu, or Xcode command line tools on macOS).","message":"Installation can fail with 'Failed building wheel for typed-ast' errors, especially on Windows, if C/C++ build tools (like Microsoft Visual C++ Build Tools) are not installed, as `typed-ast` contains C extensions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully review the `typed-ast` source code or documentation (if available) for the specific AST node classes you are working with to understand any additional fields that need to be managed during AST construction or transformation.","message":"When modifying or creating ASTs using `typed-ast`, it is not always a direct drop-in replacement for the standard `ast` module. Some `typed-ast` classes have additional fields (e.g., for type comments) that must be populated, which can lead to unexpected behavior or errors if not handled.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Do not use `typed-ast` in environments running PyPy. Use CPython or an alternative AST parsing solution if PyPy compatibility is required.","message":"The `typed-ast` library does not support PyPy and there are no plans for future support due to its reliance on specific CPython C APIs.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}