python/symbol-alias
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Python | warning | none, edit by hand | All versions |
Aliasing a class, function, type, or public attribute under a second name leaves two names for one thing in the codebase. Update callers to use the real name directly, and delete the alias.
class LegacyClient(OrderClient): pass
fetch_orders = load_orders
client = LegacyClient()orders = fetch_orders(client)client = OrderClient()orders = load_orders(client)What the rule reports
Section titled “What the rule reports”The rule reports four ways code creates an internal symbol alias:
- An untyped assignment at module or class level that binds one identifier directly to another, such as
OldClient = OrderClientorlegacy_parse = parse_orders. - A PEP 695
typealias whose right-hand side is a single bare identifier, such astype OldOrder = Order. - An undecorated class with one superclass and an empty body (
pass,..., or only a docstring) that adds no behavior. - A
@propertywhose body only returns a public attribute onself, such asreturn self.price.
The rule leaves alone the cases in the list:
- The discard target
_ = valueand dunder assignments such as__all__. - Constant-to-constant bindings where both names are uppercase (
TIMEOUT = DEFAULT_TIMEOUT), typed field defaults (limit: int = DEFAULT_LIMIT), and script variables assigned from another value variable bound earlier in the same module (result = df). - Compound
typealiases that name a generic, union, or callable such astype Row = tuple[str, float], and nominal types created withtyping.NewType. - Subclasses of exceptions, warnings, protocols, ABCs, enums, mixins,
BaseModel, andTypedDict, where an empty subclass defines a distinct type in the hierarchy. @propertygetters that expose a private backing attribute such asreturn self._price.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Rename every caller to use the canonical symbol or attribute directly, and delete the alias in the same commit. If a type alias needs distinct nominal checking, replace it with typing.NewType.
Diagnostic message
Section titled “Diagnostic message”$LEFT aliases $RIGHT under a second name. Use $RIGHT directly, update callers, and delete this alias.See all rules in the Python pack.