Typing Stubs for typed-ast

1.5.8.7 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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`.

import typed_ast.ast3 as typed_ast

def analyze_node(node: typed_ast.AST) -> str:
    """Analyzes an AST node and returns its type name."""
    if isinstance(node, typed_ast.Expr):
        return f"Expression node: {typed_ast.dump(node)}"
    return f"Generic node: {type(node).__name__}"

source_code = "def example_func(a: int, b: int) -> int: return a + b"
parsed_module = typed_ast.parse(source_code, mode='exec')

# This call is type-checked by MyPy using the stubs provided by types-typed-ast
result = analyze_node(parsed_module)
print(result)

# To run type checking, save this as 'my_script.py' and run:
# mypy my_script.py

view raw JSON →