Skip to content

Commit a23112c

Browse files
committed
narrow unions on initial assignment
1 parent 32f0502 commit a23112c

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

mypy/checker.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3231,7 +3231,18 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
32313231
# as X | Y.
32323232
if not (s.is_alias_def and self.is_stub):
32333233
with self.enter_final_context(s.is_final_def):
3234-
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
3234+
self.check_assignment(
3235+
s.lvalues[-1],
3236+
s.rvalue,
3237+
infer_lvalue_type=(
3238+
s.type is None
3239+
or (
3240+
(not isinstance(s.rvalue, TempNode) or not s.rvalue.no_rhs)
3241+
and isinstance(get_proper_type(s.type), UnionType)
3242+
)
3243+
),
3244+
new_syntax=s.new_syntax,
3245+
)
32353246

32363247
if s.is_alias_def:
32373248
self.check_type_alias_rvalue(s)

0 commit comments

Comments
 (0)