Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,19 @@ export function getIsInstanceClassTypes(

if (isInstantiableClass(subtype) && ClassType.isBuiltIn(subtype, 'Callable')) {
subtype = convertToInstantiable(getUnknownTypeForCallable());
} else if (TypeBase.isInstance(subtype) && ClassType.isBuiltIn(subtype, 'type')) {
if (subtype.priv.typeArgs && subtype.priv.typeArgs.length > 0) {
const typeArg = subtype.priv.typeArgs[0];
if (isAnyOrUnknown(typeArg)) {
foundNonClassType = true;
return;
}
if (isInstantiableClass(typeArg)) {
subtype = typeArg;
} else if (isClass(typeArg) && TypeBase.isInstance(typeArg)) {
subtype = convertToInstantiable(typeArg);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info · Optional note

The instantiable typeArg branch assigns the filter directly, while the instance-form branch converts it and thereby preserves includeSubclasses. Normalize the instantiable branch as well so any future representation reaching it cannot bypass the conservative non-final negative-narrowing behavior. [verified]

}
}
}
}

Expand Down Expand Up @@ -1540,8 +1553,12 @@ function narrowTypeForInstance(
// note this case specially so we don't do any narrowing, which
// will generate false positives.
if (filterIsSuperclass) {
if (!isTypeIsCheck && concreteFilterType.priv.includeSubclasses) {
// If the filter type includes subclasses, we can't eliminate
if (
!isTypeIsCheck &&
concreteFilterType.priv.includeSubclasses &&
!ClassType.isFinal(concreteFilterType)
) {
// If the filter type includes subclasses and is not final, we can't eliminate
// this type in the negative direction. We'll relax this for
// TypeIs checks.
isClassRelationshipIndeterminate = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This sample tests type narrowing for isinstance and issubclass when
# the class argument is passed as a type[T] variable or tuple of type[T].

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

The sample and PR claim coverage for both isinstance and issubclass, but this file exercises only isinstance. Add an issubclass(..., cls: type[A]) regression case so its narrowing consumer remains covered. [verified]


# pyright: reportMissingModuleSource=false

from typing import final
from typing_extensions import reveal_type


class A:
pass


class B:
pass


@final
class FinalClass:
pass


def test_positive_narrowing(x: A | B, cls: type[A]):
if isinstance(x, cls):
reveal_type(x, expected_text="A")


def test_positive_tuple_param(x: A | B, types: tuple[type[A], type[B]]):
if isinstance(x, types):
reveal_type(x, expected_text="A | B")


def test_final_class_param(x: FinalClass | B, cls: type[FinalClass]):
if isinstance(x, cls):
reveal_type(x, expected_text="FinalClass")
else:
reveal_type(x, expected_text="B")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

Please add a non-final negative case. cls: type[A] may hold a subclass of A, so if not isinstance(x, cls) must retain A and reveal A | B; this is the soundness boundary protected by the new final-class guard. [verified]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

Add regression coverage for type[Any] and type[Unknown]. The new foundNonClassType path intentionally rejects these as concrete filters, but the behavior is currently untested.

6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ test('TypeNarrowingIsinstance21', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('TypeNarrowingIsinstance22', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeNarrowingIsinstance22.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('TypeNarrowingTupleLength1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeNarrowingTupleLength1.py']);

Expand Down