Fixed a bug that results in incorrect type narrowing when using TypeIs form when the return type of the type guard function is a specialized generic class. This addresses #8460. (#8483)

This commit is contained in:
Eric Traut 2024-07-18 16:35:10 -07:00 committed by GitHub
parent 55f1f5f6a8
commit 405e6288cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 4 deletions

View File

@ -1433,8 +1433,7 @@ function narrowTypeForIsInstanceInternal(
filterType.shared.typeParameters.length > 0
) {
if (
!filterType.priv.typeArguments ||
!filterType.priv.isTypeArgumentExplicit ||
!filterType.priv.isTypeArgumentExplicit &&
!ClassType.isSameGenericClass(concreteVarType, filterType)
) {
const typeVarContext = new TypeVarContext(getTypeVarScopeId(filterType));

View File

@ -1,6 +1,6 @@
# This sample tests the TypeIs form.
from typing import Any, Callable, Literal, Mapping, Sequence, TypeVar, Union
from typing import Any, Callable, Collection, Literal, Mapping, Sequence, TypeVar, Union
from typing_extensions import TypeIs # pyright: ignore[reportMissingModuleSource]
@ -34,7 +34,7 @@ def is_list(val: object) -> TypeIs[list[Any]]:
def func3(val: dict[str, str] | list[str] | list[int] | Sequence[int]):
if is_list(val):
reveal_type(val, expected_text="list[str] | list[int]")
reveal_type(val, expected_text="list[str] | list[int] | list[Any]")
else:
reveal_type(val, expected_text="dict[str, str] | Sequence[int]")
@ -132,3 +132,14 @@ def is_int(obj: type) -> TypeIs[type[int]]:
def func8(x: type) -> None:
if is_int(x):
reveal_type(x, expected_text="type[int]")
def is_int_list(x: Collection[Any]) -> TypeIs[list[int]]:
raise NotImplementedError
def func9(val: Collection[object]) -> None:
if is_int_list(val):
reveal_type(val, expected_text="list[int]")
else:
reveal_type(val, expected_text="Collection[object]")