Skip to content

Commit 2a53146

Browse files
committed
Try another approach to TypeGuard
1 parent 410ce41 commit 2a53146

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

mypy/binder.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
ProperType,
2121
TupleType,
2222
Type,
23-
TypeGuardedType,
2423
TypeOfAny,
2524
TypeType,
2625
TypeVarType,
@@ -278,10 +277,7 @@ def update_from_options(self, frames: list[Frame]) -> bool:
278277
possible_types = []
279278
for t in resulting_values:
280279
assert t is not None
281-
if isinstance(t.type, TypeGuardedType):
282-
possible_types.append(t.type.type_guard)
283-
else:
284-
possible_types.append(t.type)
280+
possible_types.append(t.type)
285281
if len(possible_types) == 1:
286282
# This is to avoid calling get_proper_type() unless needed, as this may
287283
# interfere with our (hacky) TypeGuard support.

mypy/meet.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
117117
"""Return the declared type narrowed down to another type."""
118118
# TODO: check infinite recursion for aliases here.
119119
if isinstance(narrowed, TypeGuardedType):
120-
# A type guard forces the new type even if it doesn't overlap the old.
120+
# A type guard forces the new type even if it doesn't overlap the old...
121+
if is_proper_subtype(declared, narrowed.type_guard, ignore_promotions=True):
122+
# ...unless it is a proper supertype of declared type.
123+
return declared
121124
return narrowed.type_guard
122125

123126
original_declared = declared

0 commit comments

Comments
 (0)