Fixed bug that results in incorrect type isinstance or issubclass type narrowing when using a type variable with an upper bound that includes a promotion type. This addresses #8238. (#8243)

This commit is contained in:
Eric Traut 2024-06-26 16:30:26 -07:00 committed by GitHub
parent 427ca1143e
commit 05726120ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -1764,7 +1764,11 @@ function narrowTypeForIsInstanceInternal(
const filteredType = evaluator.mapSubtypesExpandTypeVars(
expandedTypes,
/* options */ undefined,
{
expandCallback: (type) => {
return evaluator.expandPromotionTypes(errorNode, type);
},
},
(subtype, unexpandedSubtype) => {
// If we fail to filter anything in the negative case, we need to decide
// whether to retain the original TypeVar or replace it with its specialized

View File

@ -4,7 +4,7 @@
# pyright: reportUnnecessaryIsInstance=true
from typing import Union
from typing import TypeVar, Union
# This should generate an error because "dummy" can't be resolved.
# The symbol Document should have an unknown type.
@ -33,3 +33,13 @@ def func3(obj: float):
reveal_type(obj, expected_text="float")
else:
reveal_type(obj, expected_text="int")
T = TypeVar("T", bound=float)
def func4(t: type[T]):
if issubclass(t, float):
reveal_type(t, expected_text="type[float]*")
else:
reveal_type(t, expected_text="type[int]*")