Skip to content

Import numpy, pandas and matplotlib under their conventional aliases - #98

Merged
mmacferrin merged 1 commit into
mainfrom
ruff-icn001
Sep 1, 2026
Merged

Import numpy, pandas and matplotlib under their conventional aliases#98
mmacferrin merged 1 commit into
mainfrom
ruff-icn001

Conversation

@mmacferrin

@mmacferrin mmacferrin commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Clears ICN001 from the Ruff ignore list, which goes from 59 entries to 58. Progress on #40.

19 imports and 262 usage sites across 12 files — 207 numpy, 69 pandas, 12 matplotlib identifier tokens in total, less the 26 that legitimately remain on import lines.

Why now

The package was already inconsistent: export_vector.py and plot_photon_clouds_v2.py used np/pd, and the latter aliased numpy and pandas while leaving import matplotlib bare. This wasn't a house style being defended, just drift, so it's settled the way the rule and the wider ecosystem expect.

How the rewrite was done

It targets ast.Name node positions, which by construction cover only real identifier references — never comments, docstrings, or the import statements. That distinction matters here:

  • 20 mentions sit in commented-out code (# center = numpy.mean(...)). These overlap heavily with ERA001 (425 still ignored); that text may simply be deleted later, so rewriting it now would be wasted work.
  • 5 sit in docstrings, where the full name is the correct prose form — "returns a pandas.DataFrame" should not become "pd.DataFrame".
  • import matplotlib.pyplot as plt and from matplotlib import ticker are untouched, since only bare import matplotlib was ever flagged. The awkward mpl.use("Agg") sitting between imports in plot_photon_clouds_v2.py came through correctly.

Equivalence was proven, not inspected

Both the old and new ASTs were normalised to a canonical form — aliases expanded back to full module names, alias bindings stripped — and compared with ast.dump(). All 12 files match. Because docstrings are AST constants, that also proves none were altered, and a separate diff check confirmed no comment line changed.

Two knock-on effects, both from names getting shorter

  • SIM108 fired: an if/else in validate_dem.py that was previously too long to express as a ternary now fits, so the already-enabled rule flagged it. Reverting just that ternary makes the file's AST match as well, confirming it is the only non-alias change anywhere in the diff.
  • The formatter repacked one file, whose wrapped expressions now fit on single lines. That's why the diff shows 260 insertions against 266 deletions rather than an even trade.

This is the same shape as the RET504 that appeared in #97, and is worth expecting again on the PTH* family.

Verification

ruff check, ruff format and the full prek suite pass. All touched modules import cleanly and ivert --help works, with one exception noted below.

Note

plot_results_slope_centrality.py does not import at all — it does import import_parent_dir, a module that does not exist. This is pre-existing and fails identically on main; it is not caused by this change. The file is included here only so the rule could be enabled, and is slated for removal separately.

No changelog entry: this is an internal refactor with no user-visible effect.


🔍 Docs preview: https://ivert--98.org.readthedocs.build/en/98/

Clears ICN001 from the ignore list (issue #40), taking it from 59
entries to 58. Nineteen imports and 262 usage sites across 12 files.

The package was already inconsistent here: export_vector.py and
plot_photon_clouds_v2.py used np and pd, and the latter aliased numpy
and pandas while leaving matplotlib bare. This settles it the way the
rule and the wider ecosystem expect.

The rewrite targets ast.Name node positions, which by construction cover
only real identifier references, so comments, docstrings and the import
statements themselves were left alone. That matters here: twenty
mentions sit in commented-out code, and five sit in docstrings where the
full name is the correct prose form -- "returns a pandas.DataFrame"
should not become "pd.DataFrame". `import matplotlib.pyplot as plt` and
`from matplotlib import ticker` are untouched, since only bare `import
matplotlib` was flagged.

Equivalence was proven rather than inspected: both the old and new ASTs
were normalised to a canonical form, with aliases expanded back to full
module names and alias bindings stripped, and compared. All twelve files
match. Since docstrings are AST constants, that also proves none were
altered, and a separate check confirmed no comment line changed.

Two knock-on effects came from the names simply getting shorter. An
if/else in validate_dem.py that was previously too long to express as a
ternary now fits, so SIM108 -- already enabled -- flagged it; reverting
just that ternary makes the file's AST match too, confirming it is the
only non-alias change in the diff. The formatter also repacked one file
whose wrapped expressions now fit on single lines.
@mmacferrin
mmacferrin merged commit be0db58 into main Sep 1, 2026
5 checks passed
@mmacferrin
mmacferrin deleted the ruff-icn001 branch September 1, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant