Check TypeAlias/TypeVar in class bodies against their own naming styles #11811
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.42) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 335 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.348
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #11811"
general
weight: 0.35
chrikrah
Type of Changes
| | Type |
| --- | ---------------------- |
| ✓ | :bug: Bug fix |
Description
Fix false positive
invalid-namefor typing constructs defined in a class body.Root cause. In
NameChecker.visit_assignname, theClassDefbranch sent everyassigned name to
_check_name("class_attribute", ...)(orclass_const). TheModulebranchalready checks
TypeAliasannotations andTypeVar/ParamSpec/TypeVarTupleassignmentsagainst their own styles, and function scope handles
X: TypeAlias = ...(#8536); the classbranch did neither. So with
class-attribute-naming-style=snake_case,MyReturnType: TypeAlias = intin a class body was reported as a class attribute.Fix. Before the class attribute/const checks, the class branch now:
X: TypeAlias = ...against thetypealiasstyle;X = TypeVar(...),ParamSpec(...),TypeVarTuple(...)againsttypevar,paramspec,typevartuple(reusing_assigns_typevar; only a plainname = Call(...)target, so unpacking a call is not treated as a TypeVar definition).Unannotated aliases such as
Alias = Union[int, str]in a class body are not changed here.How I reproduced it. The example from the issue (with
class-attribute-naming-style=snake_case) reportsinvalid-name"Class attribute name ..." forMyReturnTypeandMyCallType. A new functional testname_class_body_typing_namesfails onmain: 4 valid typing definitions are reported as class attributes and 4 expectedinvalid-namemessages are missing.How I verified it.
pytest tests/test_functional.py -k name_class_body_typing_names: fails before the fix, passes after.pytest tests/test_functional.py tests/checkers: 1398 passed, 1 failed (unbalanced_tuple_unpacking, which also fails on unmodifiedmainin my environment).pytest tests: 2362 passed, 2 failed:unbalanced_tuple_unpacking(same as above) andregression_4680, which passes when run alone and onmain(looks order/xdist dependent, unrelated to naming).--cov-branchontests/test_functional.py(new test alone, plusannotated: int = 1for the non-TypeAliasAnnAssignside): the new lines and branches inchecker.pyare covered; the full run also exercises them through existing tests.black --safe,isort,ruff check .,mypyonchecker.py,pylintonchecker.py(10.00/10),codespell,script.check_newsfragments: clean.Added news fragment
doc/whatsnew/fragments/11809.false_positive.Closes #11809
This PR was prepared with AI assistance (Claude Code). I reproduced the bug, reviewed the change, and ran the tests myself.