Skip to content

Count keyword argument names as uses (#411) - #419

Open
jpab wants to merge 1 commit into
jendrikseipp:mainfrom
jpab:fix-issue-411-classmethod-factory
Open

Count keyword argument names as uses (#411)#419
jpab wants to merge 1 commit into
jendrikseipp:mainfrom
jpab:fix-issue-411-classmethod-factory

Conversation

@jpab

@jpab jpab commented Jun 24, 2026

Copy link
Copy Markdown

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 classmethod returning cls(bar=...):

@dataclass(frozen=True)
class Foo:
    bar: str

    @classmethod
    def from_capitalized(cls, value: str) -> "Foo":
        return cls(bar=value.lower())   # `bar` was reported as unused

Fix

Count keyword-argument names in calls (the bar in cls(bar=...) / SomeClass(bar=...)) as uses of same-named variables/attributes. This is a small loop in visit_Call that mirrors the existing visit_MatchClass handler, 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.

for keyword in node.keywords:
    if keyword.arg is not None:  # keyword.arg is None for **kwargs.
        self.used_names.add(keyword.arg)

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

  • New tests in tests/test_scavenging.py covering: a field consumed via cls(field=...) and via SomeClass(field=...) (no longer unused), the **kwargs splat adding no name, and the out-of-scope inst.attr = ... case still being reported.
  • Updated one assertion in tests/test_format_strings.py ("{} {a} {b}".format(1, a=used_var, b=locals()) now also records a/b).
  • Full suite passes (301 tests), including the self-scan contract in tests/test_script.py.

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

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.50%. Comparing base (81fb2ac) to head (7b0c6b2).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Factory ClassMethod do not properly identify its own class

1 participant