ast_scope
This package is an implementation of Python's lexical scoping rules. It's interface is simple, you pass in an AST object to the annotate function, and it provides a mapping from each node in the tree that represents a symbol to the containing scope.
Example Usage: Get Global Symbols
Let's say you have the code
code = """
def f():
x = 3
lambda z: theta
return x + y
"""
and you want to determine which global variables are referenced by it. All you need to do is run
import ast
import ast_scope
tree = ast.parse(code)
scope_info = ast_scope.annotate(tree)
global_variables = sorted(scope_info.global_scope.symbols_in_frame)
Once you have executed this code, global_variables will be bound to ['f', 'theta', 'y'].
Example Usage: Get Dependency Graph
Let's say you have the code
code = """
def hailstone(n):
if n == 1:
return 1
if n % 2 == 0:
return hailstone(n // 2)
if n % 2 == 1:
return hailstone(3 * n + 1)
def mapper(f, lst):
return list(map(f, lst))
def lrange(n):
return list(range(n))
def main():
return mapper(hailstone, lrange(20))
"""
and you want to find the dependency graph. You can run
import ast
import ast_scope
tree = ast.parse(code)
scope_info = ast_scope.annotate(tree)
graph = scope_info.static_dependency_graph
which results in the following directed graph of dependencies between top-level functions (rendering using networkx):
See the documentation for some caveats.
Example usage: find a specific symbol's scope
Take the following code:
code = """
def f(x):
def g(x): return x()
return g(lambda: x)
"""
First, parse the code and identify the node (in practice, you'd probably have this always).
import ast, ast_scope
tree = ast.parse(code)
last_x = tree.body[0].body[-1].value.args[0].body
If you want to find the scope in which the last x could be found, just run the annotator and look up it's scope!
# run the annotator
scope_info = ast_scope.annotate(tree)
scope_x = scope_info[last_x]
You should get a FunctionScope object which contains a bunch of information about the other variables, etc., in the scope, but also scope_x.function_node, a pointer to the node containing the def statement for f.
Metadata
Release files for ast-scope 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ast_scope-0.5.2.tar.gz | 11.4 kB | Without provenance | Details for ast_scope-0.5.2.tar.gz |
Release files / ast_scope-0.5.2.tar.gz
| Download URL | ast_scope-0.5.2.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e841bcf70ef804d502b3c81bf77b5547947f467f0dc724102905d78d3168291d
|
|
BLAKE2b-256 checksum How to use checksums |
9f186d3221e6f216eb9c2be4bffc3b999c3e88bf03cc0c91da837e84c4e10b8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Security for ast-scope 0.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.