fix: make lazy-import AttributeError test pass ruff B009/B018 - #30
Merged
Conversation
- test_lazy_root_imports called getattr() with a constant attribute name, which current ruff (B009) flags, and the naive fix (bare attribute access) trips B018 useless-expression. CI installs unpinned latest ruff, so main goes red on the lint gate as soon as the resolver picks up a ruff version enforcing these rules. - Root cause: the idioms used to probe the module-level __getattr__ are lint-fragile; binding the name to a local variable keeps the exact same AttributeError semantics while satisfying both rules.
- Previous commit mistyped the discarded kwarg name (del add_special_tokens instead of del skip_special_tokens); behavior-neutral but wrong. Recommend squash-merge so only the corrected content lands.
cgfixit
marked this pull request as ready for review
August 25, 2026 19:50
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.
What changed
tests/unit/test_tokenizer.py::test_lazy_root_imports— bind the probe attribute name to a local variable before callinggetattr(insight_extractor, missing).__getattr__withgetattr(insight_extractor, "MissingThing")— a constant attribute name, which current ruff flags as B009 (getattrwith constant attribute). The obvious rewrite (bareinsight_extractor.MissingThingattribute access) trips B018 (useless expression) instead. CI's lint job runspip install --upgrade ruff, i.e. unpinned latest ruff, so the lint gate onmaingoes red as soon as the resolver picks up a ruff version enforcing these rules (verified locally with ruff 0.16.4:ruff check src/ tests/fails on a clean checkout ofmain@ 3b88a7c).missing = "MissingThing"+getattr(insight_extractor, missing)— a non-constant attribute name is exactly the pattern B009 prescribes, thepytest.raises(AttributeError)semantics are unchanged (the module__getattr__raisesAttributeErrorfor unknown names either way), and nonoqais needed.Why it improves the signal
Keeps the lint gate green against future ruff releases without pinning ruff or weakening any rule — the test now uses the lint-stable idiom for asserting
AttributeErroron a lazy module attribute.Risk + verification
Test-only change; zero runtime impact. Verified locally against the pinned known-good dependency set (
constraints.txt: transformers 4.53.0 / sentence-transformers 3.4.1 / accelerate 1.14.0, Python 3.12):ruff check src/ tests/→ All checks passed (was 1 B009 error before the fix; an intermediate bare-access variant was also red/green-proofed against B018)ruff format --check src/ tests/→ 23 files already formattedmypy src/insight_extractor(strict) → no issues in 10 source filespytest tests/unit/ -v --tb=short→ 118 passed, no skips[run-integration]in CI anyway)Notes for the reviewer:
FakeTokenizer.decode(del add_special_tokens→ corrected back todel skip_special_tokensin the follow-up commit); the final tree was verified byte-exact against the locally gated file (blob SHAc6abd86a).