Detect __all__ exports via augmented and annotated assignments - #421
Open
gaoflow wants to merge 1 commit into
Open
Detect __all__ exports via augmented and annotated assignments#421gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
vulture only inspected `__all__ = [...]` (ast.Assign), so names exported via `__all__ += [...]` (AugAssign) or `__all__: list = [...]` (AnnAssign) were reported as unused, which flags a package's whole public API as dead code. Generalize the __all__ handling into a helper and add visit_AugAssign and visit_AnnAssign (the latter also added to the AST whitelist, since vulture scans itself).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #421 +/- ##
==========================================
+ Coverage 93.46% 93.54% +0.07%
==========================================
Files 9 9
Lines 597 604 +7
==========================================
+ Hits 558 565 +7
Misses 39 39 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
The bug
vulture marks names listed in
__all__as used (issue #172 / PR #282), but only when__all__is built with a plain assignment. If a module populates__all__with an augmented or annotated assignment, the exported names are reported as dead code:Both
__all__ += [...]and__all__: list[str] = [...]are idiomatic, semantically-identical ways to declare exports, so flagging them means vulture reports a package's entire public API as unused — the one thing it must never do. Plain__all__ = ["foo", "bar"]is handled correctly, so the behaviour is also internally inconsistent.Root cause
Vulture.visit_Assignwas the only place__all__was inspected, and_assigns_special_variable__all__asserts anast.Assignnode.__all__ += [...]is anast.AugAssignand__all__: list = [...]is anast.AnnAssign; with novisit_AugAssign/visit_AnnAssignhandlers, those nodes were never examined and the listed names were never added toused_names.Fix
Factor the list/tuple-of-strings handling into a small
_handle__all__helper and call it fromvisit_Assign,visit_AugAssign, andvisit_AnnAssign:visit_AugAssigncovers__all__ += [...],visit_AnnAssigncovers__all__: list = [...](a bare annotation hasvalue=None, which the helper ignores),__all__(e.g.other.__all__ = [...]) are still ignored, since the target check requires a bareast.Name.visit_AnnAssignis also added tovulture/whitelists/ast_whitelist.pybecause vulture scans itself and NodeVisitor methods are dispatched implicitly (visit_AugAssignwas already whitelisted).Tests
Added six cases to
tests/test_imports.py:+=with a list and with a tuple, an annotated assignment, a=-then-+=mix, and two negative controls —+=with a string (still unused, matching__all__ = "Foo") and a bare__all__: listannotation (must not crash). The four positive tests fail onmain; all pass with the fix, and the full suite stays green (297 → 303).