{"id":7829,"library":"types-typed-ast","title":"Typing Stubs for typed-ast","description":"types-typed-ast provides static type annotations (stubs) for the `typed-ast` library, enabling type checkers like MyPy to understand its API and catch type-related errors. It is part of the `typeshed` project, a community-maintained repository of type stubs for Python packages. The current version is 1.5.8.7, and `typeshed` releases new stubs regularly as upstream libraries evolve.","status":"active","version":"1.5.8.7","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type-checking","typed-ast","mypy","typeshed"],"install":[{"cmd":"pip install types-typed-ast","lang":"bash","label":"Install stubs"},{"cmd":"pip install typed-ast mypy","lang":"bash","label":"Install runtime + type checker"}],"dependencies":[{"reason":"Provides the runtime functionality for which these stubs are created.","package":"typed-ast","optional":false},{"reason":"A common static type checker that utilizes these stubs.","package":"mypy","optional":true}],"imports":[{"note":"Stub packages like `types-typed-ast` are not meant for runtime import. You import symbols from the *actual* library (`typed-ast`), and the type checker uses the stubs implicitly.","wrong":"from types_typed_ast import AST","symbol":"AST","correct":"from typed_ast.ast3 import AST"},{"note":"Always import from the runtime library (`typed-ast`), not the stub package (`types-typed-ast`).","wrong":"import types_typed_ast; types_typed_ast.parse(...)","symbol":"parse","correct":"import typed_ast.ast3 as typed_ast; typed_ast.parse(...)"}],"quickstart":{"code":"import typed_ast.ast3 as typed_ast\n\ndef analyze_node(node: typed_ast.AST) -> str:\n    \"\"\"Analyzes an AST node and returns its type name.\"\"\"\n    if isinstance(node, typed_ast.Expr):\n        return f\"Expression node: {typed_ast.dump(node)}\"\n    return f\"Generic node: {type(node).__name__}\"\n\nsource_code = \"def example_func(a: int, b: int) -> int: return a + b\"\nparsed_module = typed_ast.parse(source_code, mode='exec')\n\n# This call is type-checked by MyPy using the stubs provided by types-typed-ast\nresult = analyze_node(parsed_module)\nprint(result)\n\n# To run type checking, save this as 'my_script.py' and run:\n# mypy my_script.py\n","lang":"python","description":"This example demonstrates how to use `typed-ast` (the runtime library) and how `types-typed-ast` (the stub package) provides type hints for it. The `mypy` command would use the installed stubs to verify type correctness of `analyze_node`'s arguments and return types without any explicit import of `types-typed-ast`."},"warnings":[{"fix":"Always import symbols from the *runtime library* (`typed-ast`), not the stub package. The stub package is consumed implicitly by type checkers like MyPy.","message":"Stub packages like `types-typed-ast` provide *only* type information and are not intended for direct import or runtime execution. Attempting to `import types_typed_ast` will result in a `ModuleNotFoundError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure both `pip install typed-ast` and `pip install types-typed-ast` are executed. `typed-ast` for runtime, `types-typed-ast` for type checking.","message":"For `types-typed-ast` to be effective, the actual runtime library `typed-ast` must also be installed. The stubs don't provide the implementation, only the type signatures.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When encountering unexpected type errors, ensure your `types-typed-ast` version is compatible with your `typed-ast` version. Consult the `typeshed` repository or `typed-ast` documentation for compatibility notes. Upgrading both packages might resolve the issue.","message":"Version compatibility between stub packages and their corresponding runtime libraries can be an issue. Stubs are generated for specific versions of the runtime, and mismatches can lead to incorrect type checking results or missing definitions.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"You should import from the actual library `typed_ast`, not the stub package `types_typed_ast`. Stubs are for type checkers, not runtime imports.","cause":"Attempting to import the stub package directly at runtime.","error":"ModuleNotFoundError: No module named 'types_typed_ast'"},{"fix":"Install the runtime library: `pip install typed-ast`. Also, ensure `mypy` is installed if using it for type checking.","cause":"The runtime library `typed-ast` is not installed, or the type checker (e.g., MyPy) cannot locate it.","error":"error: Cannot find module named 'typed_ast' [attr-defined]"},{"fix":"Verify `types-typed-ast` is installed: `pip show types-typed-ast`. Update both `typed-ast` and `types-typed-ast` to their latest compatible versions. Ensure MyPy is configured to check installed stubs (which it typically does by default).","cause":"MyPy or another type checker is not correctly picking up the `types-typed-ast` stubs, or they are missing/outdated for the specific `typed-ast` version.","error":"error: Missing type annotations for 'parse' in module 'typed_ast.ast3' [no-untyped-def]"}]}