Skip to content

python/import-alias

Pack Severity Fix Versions
Python warning none, edit by hand All versions

Renaming a symbol or module on import gives one definition two names across files, so a plain-text search for the alias never lands on its definition. Import the real name, or import the module and qualify the symbol.

from billing.invoices import Invoice as Bill
invoice = Bill(total=42)
from billing.invoices import Invoice
invoice = Invoice(total=42)

The rule reports three forms of import aliasing:

  • from ... import X as Y where X and Y differ, outside standard scientific ecosystem submodule aliases such as from plotly import graph_objects as go or from hypothesis import strategies as st.
  • import X as Y where X and Y differ, outside the standard ecosystem module aliases (pandas as pd, polars as pl, polars.selectors as cs, scipy as sp, matplotlib as mpl, matplotlib.pyplot as plt, seaborn as sns, plotly.express as px, plotly.graph_objects as go, plotly.io as pio, cvxpy as cp, lightgbm as lgb, xgboost as xgb, pymc as pm, arviz as az, pytensor.tensor as pt, statsmodels.api as sm, statsmodels.formula.api as smf, category_encoders as ce, hypothesis.strategies as st, networkx as nx, sqlalchemy as sa, datetime as dt, xml.etree.ElementTree as ET, tensorflow as tf, torch.nn as nn, torch.nn.functional as F, and jax.numpy as jnp). Non-standard numpy aliases are handled by numpy/numpy-import-alias.
  • Same-name X as X re-export aliases outside __init__.py.

The rule leaves alone the cases in the list:

  • Standard ecosystem aliases listed above, plus from array_api_compat import numpy as xp.
  • Same-name from .client import Client as Client re-exports inside __init__.py, which PEP 484 type checkers use to mark a package's public exports.

xpd has no automatic fix, so make the change by hand.

Import the symbol under its real name. When two modules define the same name, import both modules (import billing.config and import shipping.config) and qualify each reference with its module name, or rename the colliding symbol in your own codebase.

Renaming $NAME to $ALIAS on import gives one symbol two names and breaks plain-text search. Import $NAME under its real name or qualify it with its module.

See all rules in the Python pack.