Skip to content

Commit 32a696f

Browse files
committed
func local
1 parent ebf40a5 commit 32a696f

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

docs/source/command_line.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ potentially problematic or redundant in some way.
479479

480480
.. option:: --warn-redundant-annotation
481481

482-
This flag will make mypy report an error whenever your code uses
482+
This flag will make mypy report an error when a function local variable uses
483483
an unnecessary annotation in an assignment that can safely be removed.
484484

485485
.. option:: --warn-redundant-casts

docs/source/error_code_list2.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ Example:
7171
Check that annotation is not redundant [redundant-annotation]
7272
-------------------------------------------------------------
7373

74-
If you use :option:`--warn-redundant-annotation <mypy --warn-redundant-annotation>`, mypy will generate an error if the
75-
annotation type is the same as the inferred type.
74+
If you use :option:`--warn-redundant-annotation <mypy --warn-redundant-annotation>`, mypy will generate an error if a
75+
function local annotation type is the same as the inferred type.
7676

7777
Example:
7878

mypy/checker.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
WithStmt,
146146
YieldExpr,
147147
get_func_def,
148-
is_class_var,
149148
is_final_node,
150149
)
151150
from mypy.operators import flip_ops, int_op_to_method, neg_ops
@@ -3270,20 +3269,9 @@ def check_redundant_annotation(self, s: AssignmentStmt) -> None:
32703269
and s.type is not None
32713270
and not is_same_type(s.type, AnyType(TypeOfAny.special_form))
32723271
and is_same_type(s.type, self.expr_checker.accept(s.rvalue))
3272+
and (defn := self.scope.current_function()) is not None
3273+
and defn.name != "__init__"
32733274
):
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-
):
3280-
return
3281-
3282-
# skip dataclass and NamedTuple
3283-
cls = self.scope.active_class()
3284-
if cls and (dataclasses_plugin.is_processed_dataclass(cls) or cls.is_named_tuple):
3285-
return
3286-
32873275
self.msg.redundant_annotation(s.type, s.type)
32883276

32893277
def check_assignment(

test-data/unit/check-warnings.test

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,21 @@ a = 1
7878
b: int = a
7979
c: Literal[1] = 1
8080
d: list[str] = []
81-
[out]
82-
main:4: error: Annotation "int" is redundant (inferred type is the same)
83-
main:5: error: Annotation "Literal[1]" is redundant (inferred type is the same)
81+
def f() -> None:
82+
a = 1
83+
b: int = a # E: Annotation "int" is redundant (inferred type is the same)
84+
c: Literal[1] = 1 # E: Annotation "Literal[1]" is redundant (inferred type is the same)
85+
d: list[str] = []
8486

85-
[case testRedundantAnnotationClassVar]
86-
# flags: --warn-redundant-annotation
87-
from typing import ClassVar
87+
class x:
88+
def __init__(self) -> None:
89+
self.a: int = 1
8890

89-
class a:
90-
b: ClassVar[int] = 1
91-
c: ClassVar = "test"
92-
[out]
93-
main:5: error: Annotation "int" is redundant (inferred type is the same)
91+
def f(self) -> None:
92+
a = 1
93+
b: int = a # E: Annotation "int" is redundant (inferred type is the same)
94+
c: Literal[1] = 1 # E: Annotation "Literal[1]" is redundant (inferred type is the same)
95+
d: list[str] = []
9496

9597
[case testRedundantAnnotationTypeVar]
9698
# flags: --warn-redundant-annotation
@@ -101,13 +103,18 @@ T = TypeVar("T")
101103
def f(x: T) -> T:
102104
return x
103105

104-
x: Literal[1] = f(1)
105-
y: list[str] = f([])
106+
def g() -> None:
107+
x: Literal[1] = f(1)
108+
y: list[str] = f([])
106109

107110
[case testRedundantAnnotationSkips]
108111
# flags: --warn-redundant-annotation
109112
from dataclasses import dataclass
110-
from typing import NamedTuple
113+
from typing import ClassVar, NamedTuple
114+
115+
class a:
116+
b: ClassVar[int] = 1
117+
c: ClassVar = "test"
111118

112119
class d(NamedTuple):
113120
e: int = 1

0 commit comments

Comments
 (0)