Skip to content

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)

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 = OrderClient or legacy_parse = parse_orders.
  • A PEP 695 type alias whose right-hand side is a single bare identifier, such as type OldOrder = Order.
  • An undecorated class with one superclass and an empty body (pass, ..., or only a docstring) that adds no behavior.
  • A @property whose body only returns a public attribute on self, such as return self.price.

The rule leaves alone the cases in the list:

  • The discard target _ = value and 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 type aliases that name a generic, union, or callable such as type Row = tuple[str, float], and nominal types created with typing.NewType.
  • Subclasses of exceptions, warnings, protocols, ABCs, enums, mixins, BaseModel, and TypedDict, where an empty subclass defines a distinct type in the hierarchy.
  • @property getters that expose a private backing attribute such as return self._price.

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.

$LEFT aliases $RIGHT under a second name. Use $RIGHT directly, update callers, and delete this alias.

See all rules in the Python pack.