Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with assignment to variable guarded with TypeGuard #10683

Merged
merged 1 commit into from Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions mypy/binder.py
Expand Up @@ -5,7 +5,7 @@
from typing_extensions import DefaultDict

from mypy.types import (
Type, AnyType, PartialType, UnionType, TypeOfAny, NoneType, get_proper_type
Type, AnyType, PartialType, UnionType, TypeOfAny, NoneType, TypeGuardType, get_proper_type
)
from mypy.subtypes import is_subtype
from mypy.join import join_simple
Expand Down Expand Up @@ -202,7 +202,9 @@ def update_from_options(self, frames: List[Frame]) -> bool:
else:
for other in resulting_values[1:]:
assert other is not None
type = join_simple(self.declarations[key], type, other)
# Ignore the error about using get_proper_type().
if not isinstance(other, TypeGuardType): # type: ignore[misc]
type = join_simple(self.declarations[key], type, other)
if current_value is None or not is_same_type(type, current_value):
self._put(key, type)
changed = True
Expand Down
49 changes: 49 additions & 0 deletions test-data/unit/check-typeguard.test
Expand Up @@ -82,6 +82,7 @@ def is_str_list(a: List[object]) -> TypeGuard[List[str]]: pass
def main(a: List[object]):
if is_str_list(a):
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(a) # N: Revealed type is "builtins.list[builtins.object]"
[builtins fixtures/tuple.pyi]

[case testTypeGuardUnionIn]
Expand All @@ -91,6 +92,7 @@ def is_foo(a: Union[int, str]) -> TypeGuard[str]: pass
def main(a: Union[str, int]) -> None:
if is_foo(a):
reveal_type(a) # N: Revealed type is "builtins.str"
reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.int]"
[builtins fixtures/tuple.pyi]

[case testTypeGuardUnionOut]
Expand Down Expand Up @@ -315,3 +317,50 @@ def coverage(obj: Any) -> bool:
return True
return False
[builtins fixtures/classmethod.pyi]

[case testAssignToTypeGuardedVariable1]
from typing_extensions import TypeGuard

class A: pass
class B(A): pass

def guard(a: A) -> TypeGuard[B]:
pass

a = A()
if not guard(a):
a = A()
[builtins fixtures/tuple.pyi]

[case testAssignToTypeGuardedVariable2]
from typing_extensions import TypeGuard

class A: pass
class B: pass

def guard(a: A) -> TypeGuard[B]:
pass

a = A()
if not guard(a):
a = A()
[builtins fixtures/tuple.pyi]

[case testAssignToTypeGuardedVariable3]
from typing_extensions import TypeGuard

class A: pass
class B: pass

def guard(a: A) -> TypeGuard[B]:
pass

a = A()
if guard(a):
reveal_type(a) # N: Revealed type is "__main__.B"
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
reveal_type(a) # N: Revealed type is "__main__.B"
a = A()
reveal_type(a) # N: Revealed type is "__main__.A"
reveal_type(a) # N: Revealed type is "__main__.A"
[builtins fixtures/tuple.pyi]