Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
skip bare ClassVar
  • Loading branch information
grayjk committed Nov 15, 2025
commit 593d4e097612e48b2169c443ae24f0ffa1bc09ea
8 changes: 6 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3271,8 +3271,12 @@ def check_redundant_annotation(self, s: AssignmentStmt) -> None:
and not is_same_type(s.type, AnyType(TypeOfAny.special_form))
and is_same_type(s.type, self.expr_checker.accept(s.rvalue))
):
# skip ClassVar
if any(isinstance(lvalue, NameExpr) and is_class_var(lvalue) for lvalue in s.lvalues):
# skip bare ClassVar
if (
any(isinstance(lvalue, NameExpr) and is_class_var(lvalue) for lvalue in s.lvalues)
and isinstance(s.unanalyzed_type, UnboundType)
and not s.unanalyzed_type.args
):
return

# skip dataclass and NamedTuple
Expand Down
14 changes: 10 additions & 4 deletions test-data/unit/check-warnings.test
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@ b: int = a
[out]
main:3: error: Annotation "int" is redundant (inferred type is the same)

[case testRedundantAnnotationSkips]
[case testRedundantAnnotationClassVar]
# flags: --warn-redundant-annotation
from dataclasses import dataclass
from typing import ClassVar, NamedTuple
from typing import ClassVar

class a:
b: ClassVar[int] = 1
c: ClassVar = 1
c: ClassVar = "test"
[out]
main:5: error: Annotation "int" is redundant (inferred type is the same)

[case testRedundantAnnotationSkips]
# flags: --warn-redundant-annotation
from dataclasses import dataclass
from typing import NamedTuple

class d(NamedTuple):
e: int = 1
Expand Down