Expand description
Python SDK usage extraction.
§Soundness policy
A value or route reported with no gap is confirmed: a consumer acts on it
without asking. Python is too dynamic to make that hold by resolving every
shape, so this module resolves a small supported grammar exactly and
turns everything else into a gap. docs/SDK_USAGES.md states the grammar
in full; in short, a value or route has no gap only when it is:
- a string literal written at the call (never downgraded);
- a name with one reaching assignment of such a value, in the scope that binds it (module constant, local, enclosing local);
- a class attribute assigned once, read by class name or through
self/cls, in a class with no decorator, class keyword or second base, at most one same-file level of inheritance and at most 16 direct subclasses (a model value read throughself/clsin a class another module may subclass keeps asubclass_overridegap). Every binding of the name in a class body (def,class, an import, a loop target…) is an assignment of it, in the class, its base and its subclasses alike; and a method that binds itsself/clsname other than as that parameter reads nothing through it; - a parameter default of a function whose calls are all in this file (a
nested or
_privatefunction, a private class’s__init__, anything in a script), none of which passes another value; - an entry of a same-file dict literal read by literal key, when the dict
is only ever used through its one name and no
**expansion or computed key follows the entry (in a dict written at the call too); - a client from a tracked constructor with no
base_url, or one that resolves to a string (the provider’s own host is direct, another host iscustom_base_url), and for Gemini novertexai=True, endpointhttp_optionsor**expansion, in a file that does not configure the endpoint some other way; - the module-level API, configured only in the top-of-file setup block:
plain
=statements, each routing setting once, on a name only imports bind, before the firstdef,class, decorator, bare expression or compound statement (and before any module-level call in the block). No code of the file exists yet there, so none can run first; import-time execution is not followed any further.
The collection walk also records dynamic features that can change a
resolved name, class, dict or the environment (exec, globals(),
sys.modules, __dict__, setattr with a run-time name, attribute
hooks, decorators and metaclasses on a class whose attributes are read,
multiple or deep inheritance, a class used as a value, environment
writes with run-time names, …). A value or route one of them may affect
keeps a dynamic_feature gap naming the feature and its line; the most
dynamic ones affect the whole file (namespace builtins reached under
any name, builtins or sys imported under another name or used as a
value, a frame’s f_globals, scopes nested past
[MAX_SCOPE_DEPTH]). Replacing a precise but costly resolution with
such a gap is always acceptable, so each lookup does a bounded amount of
work (see the MAX_* constants), every walk up the scopes is bounded by
[MAX_SCOPE_DEPTH], and the whole pass is linear in the file.
§Passes
Two passes over the tree-sitter tree:
- Collect. One iterative walk records scopes (module, class,
function, lambda), imports, every assignment target (names,
self.x, class attributes,with ... as,except ... as,matchcaptures, walrus,global,nonlocal,globals()["NAME"],del, and a wildcard import from another module, which may bind any module-level name), parameters, every call, every attribute chain used as a value, where each binding takes effect, and the dynamic features. Parent kinds and field names travel down the walk, so no node is asked for its parent: tree-sitter answers that by descending from the root, which made deep trees quadratic. - Analyze. Each call’s callee is evaluated against those bindings.
A chain rooted at a tracked SDK binding becomes a construction or a
call; an SDK-shaped chain on an unknown receiver,
getattron a tracked binding and an unresolved model value become typed gaps.
Names resolve the way Python scopes them, approximately: function and
lambda locals, parameters and imports shadow enclosing functions, class
bodies are skipped from inside methods, comprehensions have their own
scope, global re-homes an assignment to the module and nonlocal to
the enclosing function that binds the name. A use sees its
reaching definitions: the nearest definition that dominates it in
straight-line code, every definition between that one and the use (in a
branch), later ones in a loop that runs the use again without passing
the dominating one, and global or nonlocal assignments made by
nested or other functions, which may run at any time. Class
bodies and list, set and dict comprehensions run where they are written,
so their uses are placed there. A use from a function, lambda or
generator sees what reaches its definition plus every later definition,
since it may run after any of them. With no dominating definition, or
past the search limits, every definition of the name counts.
An untyped parameter used as a client resolves when every call to its
function (or to a lambda assigned to a plain name) in this file passes
the same tracked client. Other modules may call a public function too, so
there the routing keeps an ambiguous_client gap (in_file_callers),
and a public function’s model or client default keeps a
parameter_default gap. The calls of __init__ are the calls of its
class; a method’s are every call of its name, on any object. A function
whose calls cannot all be seen (it is decorated, passed, stored or bound
twice, or instances are built without naming their class) keeps an
unseen_callers gap on a client or model default. A model parameter
default that an in-file caller overrides keeps a parameter_overridden
gap listing the values the callers pass.
Functions§
- extract
- Extracts SDK usages from a parsed Python file.
- may_
use_ sdk - Whether
sourcementions anything the extractor tracks. A file that does not reports nothing, so callers may skip parsing it.