Skip to content

symtable.symtable() should accept an AST object, like compile() #153844

Description

@serhiy-storchaka

Feature or enhancement

symtable.symtable() currently only accepts str or bytes source. I propose to also accept an AST object, with the same semantics as the builtin compile(): the compile_type argument selects the expected root node, and future statements found in the tree are honored.

>>> import ast, symtable
>>> tree = ast.parse("def f(): pass")
>>> symtable.symtable(tree, "<string>", "exec")
<SymbolTable for top in <string>>

The restriction is an artifact of the module wrapper: the C implementation builds the symbol table from an AST anyway, and compile() has accepted AST input since Python 2.6. But there is no way to get scope information for a tree you already have — a transformed or synthesized AST has no source, and the only workaround is ast.unparse() plus re-parsing.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions