Skip to content

Commit 0738db3

Browse files
authored
Do not push partial types to the binder (#20202)
Fixes #19996. This was unearthed by #19400 where `is_subtype(Partial<X>, Partial<X>)` started returning True - before that `binder.assign_type(expr, Partial<None>, Partial<None>)` just returned early. If I understand correctly, that is how Partial should be handled here: we do not want to push partials to the binder. I do not think we should add a special case for that (both False and True make some sense for a partial type, I am not convinced that either one is marginally better), so I just add an explicit guard to skip adding partial types here.
1 parent 7e3fd24 commit 0738db3

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mypy/checker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3424,7 +3424,11 @@ def check_assignment(
34243424
and lvalue_type is not None
34253425
):
34263426
lvalue.node.type = remove_instance_last_known_values(lvalue_type)
3427-
elif self.options.allow_redefinition_new and lvalue_type is not None:
3427+
elif (
3428+
self.options.allow_redefinition_new
3429+
and lvalue_type is not None
3430+
and not isinstance(lvalue_type, PartialType)
3431+
):
34283432
# TODO: Can we use put() here?
34293433
self.binder.assign_type(lvalue, lvalue_type, lvalue_type)
34303434

test-data/unit/check-redefine2.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ class C4:
369369
reveal_type(self.x) # N: Revealed type is "builtins.list[builtins.str]"
370370

371371
reveal_type(C4().x) # N: Revealed type is "builtins.list[builtins.str]"
372+
373+
class C5:
374+
def __init__(self) -> None:
375+
if int():
376+
self.x = None
377+
return
378+
self.x = [""]
379+
reveal_type(self.x) # N: Revealed type is "builtins.list[builtins.str]"
380+
381+
reveal_type(C5().x) # N: Revealed type is "Union[builtins.list[builtins.str], None]"
372382
[builtins fixtures/list.pyi]
373383

374384
[case testNewRedefinePartialGenericTypes]

0 commit comments

Comments
 (0)