Skip to content

fix: make lazy-import AttributeError test pass ruff B009/B018 - #30

Merged
cgfixit merged 2 commits into
mainfrom
kimi/fix-ruff-b009-test-tokenizer
Aug 26, 2026
Merged

fix: make lazy-import AttributeError test pass ruff B009/B018#30
cgfixit merged 2 commits into
mainfrom
kimi/fix-ruff-b009-test-tokenizer

Conversation

@cgfixit

@cgfixit cgfixit commented Aug 22, 2026

Copy link
Copy Markdown
Owner

What changed

tests/unit/test_tokenizer.py::test_lazy_root_imports — bind the probe attribute name to a local variable before calling getattr(insight_extractor, missing).

  • Root cause: the test probed the package's module-level __getattr__ with getattr(insight_extractor, "MissingThing") — a constant attribute name, which current ruff flags as B009 (getattr with constant attribute). The obvious rewrite (bare insight_extractor.MissingThing attribute access) trips B018 (useless expression) instead. CI's lint job runs pip install --upgrade ruff, i.e. unpinned latest ruff, so the lint gate on main goes 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 of main @ 3b88a7c).
  • Fix: missing = "MissingThing" + getattr(insight_extractor, missing) — a non-constant attribute name is exactly the pattern B009 prescribes, the pytest.raises(AttributeError) semantics are unchanged (the module __getattr__ raises AttributeError for unknown names either way), and no noqa is 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 AttributeError on 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 formatted
  • mypy src/insight_extractor (strict) → no issues in 10 source files
  • pytest tests/unit/ -v --tb=short → 118 passed, no skips
  • Integration tests skipped — no model download in this environment (gated behind [run-integration] in CI anyway)

Notes for the reviewer:

  • Recommend squash-merge: an intermediate commit on this branch mistyped a discarded kwarg name in FakeTokenizer.decode (del add_special_tokens → corrected back to del skip_special_tokens in the follow-up commit); the final tree was verified byte-exact against the locally gated file (blob SHA c6abd86a).
  • CI is verification of record for the full matrix (py3.12 + 3.13, CLI smoke).

- 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
cgfixit marked this pull request as ready for review August 25, 2026 19:50
@cgfixit
cgfixit merged commit 8097406 into main Aug 26, 2026
9 checks passed
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