Skip to content

Commit 14524fe

Browse files
committed
Improve error message
1 parent 2b711e2 commit 14524fe

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

mypy/binder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ def update_from_options(self, frames: list[Frame]) -> bool:
288288
# still equivalent to such type).
289289
if isinstance(type, UnionType):
290290
type = collapse_variadic_union(type)
291-
if old_semantics and isinstance(type, ProperType) and isinstance(type, UnionType):
291+
if (
292+
old_semantics
293+
and isinstance(type, ProperType)
294+
and isinstance(type, UnionType)
295+
):
292296
# Simplify away any extra Any's that were added to the declared
293297
# type when popping a frame.
294298
simplified = UnionType.make_union(

mypy/checker.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4566,6 +4566,12 @@ def check_simple_assignment(
45664566
rvalue_type = self.expr_checker.accept(
45674567
rvalue, type_context=lvalue_type, always_allow_any=always_allow_any
45684568
)
4569+
if not is_valid_inferred_type(rvalue_type, self.options) and inferred is not None:
4570+
self.msg.need_annotation_for_var(
4571+
inferred, context, self.options.python_version
4572+
)
4573+
rvalue_type = rvalue_type.accept(SetNothingToAny())
4574+
45694575
if (
45704576
isinstance(lvalue, NameExpr)
45714577
and inferred is not None

test-data/unit/check-redefine2.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,10 @@ def func() -> None:
10811081
l = None
10821082

10831083
if int():
1084-
l = []
1084+
l = [] # E: Need type annotation for "l"
10851085
l.append(1)
1086+
reveal_type(l) # N: Revealed type is "Union[None, builtins.list[Any]]"
1087+
[builtins fixtures/list.pyi]
10861088

10871089
[case testNewRedefineNarrowingSpecialCase]
10881090
# flags: --allow-redefinition-new --local-partial-types --warn-unreachable

0 commit comments

Comments
 (0)