Skip to content

Commit

Permalink
Fixed bug that results in incorrect type isinstance or issubclass
Browse files Browse the repository at this point in the history
… type narrowing when using a type variable with an upper bound that includes a promotion type. This addresses #8238.
  • Loading branch information
erictraut committed Jun 26, 2024
1 parent 427ca11 commit b4ea6d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion packages/pyright-internal/src/tests/samples/isinstance2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]*")

0 comments on commit b4ea6d0

Please sign in to comment.