Overview
Codegen provides two main ways to track relationships between symbols:.dependencies/.get_dependencies(...)- What symbols does this symbol depend on?.usages/.usages(...)- Where is this symbol used?
A depends on B, then B is used by A. This relationship is tracked in both directions, allowing you to navigate the codebase from either perspective.
-
MyClass.dependenciesanswers the question: “which symbols in the codebase does MyClass depend on?” -
BaseClass.usagesanswers the question: “which symbols in the codebase use BaseClass?”
Usage Types
Both APIs use the UsageType enum to specify different kinds of relationships:DIRECT Usage
A direct usage occurs when a symbol is used in the same file where it’s defined, without going through any imports or attribute access.CHAINED Usage
A chained usage occurs when a symbol is accessed through module or object attribute access, using dot notation.INDIRECT Usage
An indirect usage happens when a symbol is used through a non-aliased import statement.ALIASED Usage
An aliased usage occurs when a symbol is used through an import with an alias.Dependencies API
The dependencies API lets you find what symbols a given symbol depends on.Basic Usage
Combining Usage Types
You can combine usage types using the bitwise OR operator:Common Patterns
- Finding dead code (symbols with no usages):
- Finding all imports that a symbol uses: