Count keyword argument names as uses (#411) - #419
Open
jpab wants to merge 1 commit into
Open
Conversation
Treat keyword-argument names in calls (e.g. `cls(bar=...)`) as uses of same-named variables/attributes, mirroring the existing visit_MatchClass handler. This fixes false-positive "unused variable" reports for dataclass/pydantic/attrs fields that are only consumed via keyword arguments in a factory classmethod or constructor call. Attributes assigned on an instance without a corresponding read (e.g. `inst.bar4 = ...`) remain reported, as connecting them to the class would require type information vulture does not track. Developed using maieutic-tools (https://github.com/jpab/maieutic-tools). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #419 +/- ##
==========================================
+ Coverage 93.46% 93.50% +0.03%
==========================================
Files 9 9
Lines 597 600 +3
==========================================
+ Hits 558 561 +3
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.
Fixes #411.
Problem
vulture reports false-positive "unused variable" for class/dataclass/pydantic fields that are only consumed via keyword arguments in a constructor call, e.g. a factory
classmethodreturningcls(bar=...):Fix
Count keyword-argument names in calls (the
barincls(bar=...)/SomeClass(bar=...)) as uses of same-named variables/attributes. This is a small loop invisit_Callthat mirrors the existingvisit_MatchClasshandler, and fits vulture's name-based, framework-agnostic model — it covers dataclasses, pydantic, attrs, DRF serializers and plain classes uniformly, without special-casing any framework.Trade-off
As with the other name-based hooks (
visit_MatchClass,getattr/hasattr, format strings), this over-approximates: a name that only ever appears as a call keyword anywhere in the scanned source is now considered used. This is the accepted, intentional cost of vulture's untyped model.Deliberately out of scope
Attributes assigned on an instance without a corresponding read (e.g.
inst.bar4 = "extra") are still reported. Connecting such an assignment to the owning class would require type information vulture does not track. This limitation is captured in a test.Tests
tests/test_scavenging.pycovering: a field consumed viacls(field=...)and viaSomeClass(field=...)(no longer unused), the**kwargssplat adding no name, and the out-of-scopeinst.attr = ...case still being reported.tests/test_format_strings.py("{} {a} {b}".format(1, a=used_var, b=locals())now also recordsa/b).tests/test_script.py.