Skip to content

Commit 593d4e0

Browse files
committed
skip bare ClassVar
1 parent a72ba7a commit 593d4e0

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

mypy/checker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,8 +3271,12 @@ def check_redundant_annotation(self, s: AssignmentStmt) -> None:
32713271
and not is_same_type(s.type, AnyType(TypeOfAny.special_form))
32723272
and is_same_type(s.type, self.expr_checker.accept(s.rvalue))
32733273
):
3274-
# skip ClassVar
3275-
if any(isinstance(lvalue, NameExpr) and is_class_var(lvalue) for lvalue in s.lvalues):
3274+
# skip bare ClassVar
3275+
if (
3276+
any(isinstance(lvalue, NameExpr) and is_class_var(lvalue) for lvalue in s.lvalues)
3277+
and isinstance(s.unanalyzed_type, UnboundType)
3278+
and not s.unanalyzed_type.args
3279+
):
32763280
return
32773281

32783282
# skip dataclass and NamedTuple

test-data/unit/check-warnings.test

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ b: int = a
7878
[out]
7979
main:3: error: Annotation "int" is redundant (inferred type is the same)
8080

81-
[case testRedundantAnnotationSkips]
81+
[case testRedundantAnnotationClassVar]
8282
# flags: --warn-redundant-annotation
83-
from dataclasses import dataclass
84-
from typing import ClassVar, NamedTuple
83+
from typing import ClassVar
8584

8685
class a:
8786
b: ClassVar[int] = 1
88-
c: ClassVar = 1
87+
c: ClassVar = "test"
88+
[out]
89+
main:5: error: Annotation "int" is redundant (inferred type is the same)
90+
91+
[case testRedundantAnnotationSkips]
92+
# flags: --warn-redundant-annotation
93+
from dataclasses import dataclass
94+
from typing import NamedTuple
8995

9096
class d(NamedTuple):
9197
e: int = 1

0 commit comments

Comments
 (0)