Transformers
transformers
CST transformers for removing top-level symbols and attribute rewriting.
AttributeRewriter
Bases: CSTTransformer
Rewrite attribute chains rooted at old_module_alias for given symbols.
Given old_module_alias (either the dotted module, e.g. pkg.old, or a
local alias bound via import pkg.old as om) and a set of symbols, this
transformer rewrites attribute accesses of the form <alias>.<Symbol> to
new_module.<Symbol>. Chains beyond the symbol
(<alias>.<Symbol>.method() etc.) are preserved structurally. Uses
ScopeProvider to avoid rewriting references whose leftmost name is
shadowed by a local (non-import) binding. Exposes kept_usages: the count
of remaining <alias>.<other> references after rewriting, so callers can
decide whether to drop import old_module.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_Attribute(original_node, updated_node)
Rewrite alias.Symbol chain roots; count untouched alias.* refs.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
ProtectConditionalImports
Bases: _DepthTracker
Append # noqa: F401 to imports nested in top-level guard blocks.
Conditional imports (try/except or if guards at module
scope) must survive the post-move ruff --fix F401 pass even when no
remaining symbol references them — removing the fallback branch of a
try: import a / except: import b as a block silently changes runtime
behavior (AXM-1775 AC3). Marking the import lines with a per-line noqa
keeps the guard intact without disabling F401 for the whole file.
Only module-level guards are tagged; guards nested inside functions or classes are left untouched.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_If(original_node, updated_node)
Protect imports in a top-level if/else guard.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_Try(original_node, updated_node)
Protect imports in a top-level try/except/else guard.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
RemoveSymbols
Bases: _DepthTracker
Remove targeted top-level ClassDef, FunctionDef, or constant
assignments (Assign / AnnAssign) from a module.
Surrounding formatting (comments, blank lines, indentation of other
top-level symbols) is preserved thanks to libcst's lossless tree.
Non-assignment SimpleStatementLine nodes (imports, docstrings,
bare expressions) are left untouched.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_ClassDef(original_node, updated_node)
Drop the class when its top-level name matches a removal target.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_FunctionDef(original_node, updated_node)
Drop the function when its top-level name matches a removal target.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_SimpleStatementLine(original_node, updated_node)
Drop top-level statement lines whose assignments target a removed name.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
RenameSymbols
Bases: CSTTransformer
Rename Name nodes according to an old -> new mapping.
Every identifier whose value matches a key in mapping is rewritten
to the corresponding value. This covers definition names
(def OldName / class OldName), internal references (recursion,
self-references) and usage sites in caller modules. Attribute names
(the attr of an Attribute node, e.g. obj.OldName) are left
untouched so unrelated members are not renamed.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
| Python | |
|---|---|
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
leave_Annotation(original_node, updated_node)
Rewrite renamed identifiers inside a string forward-reference.
A string annotation ("OldName") is opaque to leave_Name — its
content is a literal, not a Name node. Parse the string body,
apply the same old -> new rename (whole-identifier match), and
re-emit the string when its parsed content actually referenced a
renamed symbol. Non-string annotations and strings that do not parse
are left untouched.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_Arg(original_node, updated_node)
Preserve a keyword-argument name, restoring it if renamed.
leave_Name fires for the keyword child of f(Old=1) too;
undo that rewrite so a parameter that merely shares the renamed
symbol's spelling stays put. The argument value (a genuine
reference) keeps its rename.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_Attribute(original_node, updated_node)
Preserve the attribute member name, restoring it if renamed.
leave_Name fires for the attr child too; undo that rewrite so
only the head of the expression (a real binding) is renamed.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_Name(original_node, updated_node)
Rewrite a bare name when it matches a rename key.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
| Python | |
|---|---|
SyncDunderAll
Bases: _DepthTracker
Synchronize a module's existing __all__ literal on a symbol move.
Removes the names in remove from the __all__ List/Tuple
and appends the names in add that are not already present
(idempotent). Existing Element nodes are reused untouched via
.with_changes() so quotes, trailing commas and comments of the
surviving entries are preserved. A module without a top-level
__all__ literal is left untouched — this transformer never
synthesizes a new assignment.
Source code in packages/axm-anvil/src/axm_anvil/_cst/transformers.py
leave_Assign(original_node, updated_node)
Rewrite the __all__ list/tuple elements in place when at module level.