typeshed-client

2.9.0 · active · verified Sat Apr 11

typeshed-client is a Python library (version 2.9.0) for programmatically accessing type stubs from typeshed and PEP 561 stub packages. It allows tools to find stub files, discover names defined in stubs, and retrieve AST nodes for definitions. The library frequently updates its bundled typeshed to stay current with the official type stub repository.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to find a stub file for a given module, retrieve its Abstract Syntax Tree (AST), and how to customize the stub search behavior using a `SearchContext`. It shows fetching stubs for 'typing' and attempts for 'math'.

from typeshed_client import get_stub_file, get_stub_ast
from typeshed_client.finder import SearchContext

# Find the stub file for a standard library module (e.g., 'typing')
typing_stub_path = get_stub_file('typing')
if typing_stub_path:
    print(f"Found 'typing' stub at: {typing_stub_path}")

    # Get the Abstract Syntax Tree (AST) for the stub
    typing_stub_ast = get_stub_ast('typing')
    if typing_stub_ast:
        print(f"AST for 'typing' module (first 3 nodes): {typing_stub_ast.body[:3]}")

# Example of using a custom SearchContext (e.g., allowing .py files in search)
# Note: allow_py_files was added in v2.8.0/v2.8.1/v2.8.2
custom_context = SearchContext(allow_py_files=True)
math_stub_path = get_stub_file('math', search_context=custom_context)
if math_stub_path:
    print(f"Found 'math' stub (with custom context) at: {math_stub_path}")
else:
    print("Could not find 'math' stub (this is normal if it's not in typeshed).")

view raw JSON →