← Back to Pull requests

Check TypeAlias/TypeVar in class bodies against their own naming styles #11811

Open
unknown opened this pull request with 335 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 12, 2026 19:37 UTC
Author

Type of Changes

| | Type |
| --- | ---------------------- |
| ✓ | :bug: Bug fix |

Description

Fix false positive invalid-name for typing constructs defined in a class body.

Root cause. In NameChecker.visit_assignname, the ClassDef branch sent every
assigned name to _check_name("class_attribute", ...) (or class_const). The Module branch
already checks TypeAlias annotations and TypeVar/ParamSpec/TypeVarTuple assignments
against their own styles, and function scope handles X: TypeAlias = ... (#8536); the class
branch did neither. So with class-attribute-naming-style=snake_case,
MyReturnType: TypeAlias = int in a class body was reported as a class attribute.

Fix. Before the class attribute/const checks, the class branch now:

  • checks X: TypeAlias = ... against the typealias style;

  • checks X = TypeVar(...), ParamSpec(...), TypeVarTuple(...) against typevar, paramspec, typevartuple (reusing _assigns_typevar; only a plain name = 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) reports invalid-name "Class attribute name ..." for MyReturnType and MyCallType. A new functional test name_class_body_typing_names fails on main: 4 valid typing definitions are reported as class attributes and 4 expected invalid-name messages 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 unmodified main in my environment).

  • pytest tests: 2362 passed, 2 failed: unbalanced_tuple_unpacking (same as above) and regression_4680, which passes when run alone and on main (looks order/xdist dependent, unrelated to naming).

  • --cov-branch on tests/test_functional.py (new test alone, plus annotated: int = 1 for the non-TypeAlias AnnAssign side): the new lines and branches in checker.py are covered; the full run also exercises them through existing tests.

  • black --safe, isort, ruff check ., mypy on checker.py, pylint on checker.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.

    Diff: 335 lines (+251 / -84) across 4 files Reviewer: chrikrah Review duration: 6498m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.2% (MEDIUM) · Higher = larger blast radius
    42.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%