Skip to content

Commit 0f89c59

Browse files
committed
This was too much of a shortcut
1 parent f8ecfe5 commit 0f89c59

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

mypy/meet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
170170
):
171171
# We put this branch early to get T(bound=Union[A, B]) instead of
172172
# Union[T(bound=A), T(bound=B)] that will be confusing for users.
173-
return declared.copy_modified(upper_bound=original_narrowed)
173+
return declared.copy_modified(
174+
upper_bound=narrow_declared_type(declared.upper_bound, original_narrowed)
175+
)
174176
elif not is_overlapping_types(declared, narrowed, prohibit_none_typevar_overlap=True):
175177
if state.strict_optional:
176178
return UninhabitedType()

test-data/unit/check-typeguard.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,26 @@ def handle(model: Model) -> int:
825825
return 0
826826
[builtins fixtures/tuple.pyi]
827827

828+
[case testTypeGuardedTypeDoesNotLeakTypeVar]
829+
# flags: --debug-serialize
830+
# https://github.com/python/mypy/issues/20015
831+
from typing import Generic, TypeVar, TypeGuard
832+
833+
class A: ...
834+
class B: ...
835+
836+
def is_a(_: object) -> TypeGuard[A]: return True
837+
def is_b(_: object) -> TypeGuard[B]: return True
838+
839+
_T = TypeVar("_T")
840+
841+
class Foo(Generic[_T]):
842+
def __init__(self, v: _T) -> None:
843+
if is_a(v) or is_b(v):
844+
self.v = v
845+
[builtins fixtures/tuple.pyi]
846+
[typing fixtures/typing-full.pyi]
847+
828848
[case testTypeGuardRestrictTypeVarUnion]
829849
from typing import Union, TypeVar
830850
from typing_extensions import TypeGuard

test-data/unit/fixtures/typing-full.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ no_type_check = 0
3434
ClassVar = 0
3535
Final = 0
3636
TypedDict = 0
37+
TypeGuard = 0
3738
NoReturn = 0
3839
NewType = 0
3940
Self = 0

0 commit comments

Comments
 (0)