refactor(util): modern type hints + drop vestigial py2 __future__ imports - #104
Open
randomizedcoder wants to merge 1 commit into
Open
randomizedcoder wants to merge 1 commit into
randomizedcoder wants to merge 1 commit into
Conversation
…mports These util/ scripts already run under python3 (all have a python3 shebang) but still carried Python-2-straddle idioms and had no type annotations. This modernizes them: - Remove `from __future__ import division, print_function` (both are the default in Python 3, so these lines were no-ops). ruff reports them as UP010; the count over these files goes from 14 to 0. - Add PEP 585 / PEP 604 type annotations to every function: built-in generics (list[...], dict[...], tuple[...]) and X | None unions rather than typing.List / Optional. Where a function returns a matplotlib Axes or a dict_keys view, typing.Any is used. A single `from __future__ import annotations` is added to each file that gains annotations, so the hints are lazy strings with zero runtime cost and work on older 3.x too. - avg.py: correct the stale `#!/usr/bin/env python` shebang to python3 to match the rest of util/. Also fixes three genuine Python-2 leftovers in diff_metrics.py that would raise NameError under python3 on their code paths (they are not exercised by the common path, which is why they survived): `long(...)` -> `int(...)` (x2) and `printf(...)` -> `print(...)`. diff_metrics.py now runs end-to-end (verified the changed-metric path, including its %lu format, which is valid in python3). Scope: the two large scripts, tthoma.py (14774 lines) and cperf.py (1984 lines), are intentionally left for follow-up PRs to keep this reviewable. strip_decl.py already had neither a __future__ line nor functions, so it is unchanged. Verification: all 18 changed files byte-compile; ruff UP010 is clean (14->0) and no legacy typing constructs are introduced (UP006/UP007/ UP035/UP045 clean); annotation names resolve (F821 clean apart from the py2 bugs above, which are fixed); several scripts smoke-tested via stdin. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Modernize the
util/Python: type hints + drop vestigial py2__future__These
util/scripts already run under Python 3 (every one has apython3shebang), but they still carried Python-2-straddle idioms and had no type annotations. This is a dedicated, review-focused modernization pass — separate from the W605 regex fix in #9 so each PR stays about one thing.What changed
from __future__ import division, print_function(14 files). Both features are the default in Python 3, so these lines were no-ops.ruffflags them asUP010.list[...],dict[...],tuple[...]) andX | Noneunions — nevertyping.List/Optional.typing.Anyis used where a function returns a matplotlibAxesor adict_keysview. Each file that gains annotations gets a singlefrom __future__ import annotations, so the hints are lazy strings with zero runtime cost (and work on older 3.x too).avg.py: corrected a stale#!/usr/bin/env pythonshebang topython3, matching the rest ofutil/.Bonus: three real Python-2 bugs fixed in
diff_metrics.pyWhile annotating I found genuine py2 leftovers that raise
NameErrorunder python3 on their code paths (they sit on the "metric changed" / usage-error paths, which is why they'd survived):diff_metrics.pynow runs end-to-end (verified the changed-metric path, including its%luformat — which is valid in python3, thellength modifier is accepted and ignored).Scope
tthoma.py(14,774 lines) andcperf.py(1,984) — are intentionally left for follow-up PRs to keep this one reviewable.strip_decl.pyalready had neither a__future__line nor functions, so it is unchanged.%,pathlib, removing unused imports) is deliberately out of scope here.Verification
python -m py_compile).ruff --select UP010over these files: 14 → 0.ruff --select UP006,UP007,UP035,UP045clean.ruff --select F821clean (the only F821 hits were thelong/printfpy2 bugs above, now fixed).avg,ttrange,ttoffset,ttgrep,diff_metrics).Type annotations are lazy here — a safety note
Because every annotated file carries
from __future__ import annotations(PEP 563), the annotations are never evaluated at runtime, so they cannot change behavior; they exist for readers, editors, and type checkers. Nothing in these scripts introspects annotations at runtime.