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
57 changes: 47 additions & 10 deletions packages/pyright-internal/src/analyzer/codeFlowEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ import { getPatternSubtypeNarrowingCallback } from './patternMatching';
import { SpeculativeTypeTracker } from './typeCacheUtils';
import { narrowForKeyAssignment, narrowForKeyDeletion } from './typedDicts';
import { EvalFlags, Reachability, TypeEvaluator, TypeResult } from './typeEvaluatorTypes';
import { applyTypeNarrowingCallback, getTypeNarrowingCallback } from './typeGuards';
import {
applyTypeNarrowingCallback,
getTypeNarrowingCallback,
narrowConstrainedTypeVarForSubclass,
} from './typeGuards';
import {
ClassType,
combineTypes,
Expand All @@ -74,6 +78,7 @@ import {
NeverType,
OverloadedType,
Type,
TypeBase,
TypeVarType,
UnboundType,
UnknownType,
Expand Down Expand Up @@ -1760,8 +1765,7 @@ export function getCodeFlowEngine(
}

// Determines whether the specified typeVar, which is assumed to be constrained,
// can be narrowed to one of its constrained types based on isinstance type
// guard checks.
// can be narrowed to one of its constrained types based on type guard checks.
function narrowConstrainedTypeVar(flowNode: FlowNode, typeVar: TypeVarType): ClassType | undefined {
assert(!isParamSpec(typeVar));
assert(!isTypeVarTuple(typeVar));
Expand Down Expand Up @@ -1862,7 +1866,7 @@ export function getCodeFlowEngine(
continue;
}

// Handle an isinstance type guard.
// Handle isinstance and issubclass type guards.
if (curFlowNode.flags & (FlowFlags.TrueCondition | FlowFlags.FalseCondition)) {
const conditionFlowNode = curFlowNode as FlowCondition;
const testExpression = conditionFlowNode.expression;
Expand All @@ -1871,14 +1875,37 @@ export function getCodeFlowEngine(
if (
testExpression.nodeType === ParseNodeType.Call &&
testExpression.d.leftExpr.nodeType === ParseNodeType.Name &&
testExpression.d.leftExpr.d.value === 'isinstance' &&
(testExpression.d.leftExpr.d.value === 'isinstance' ||
testExpression.d.leftExpr.d.value === 'issubclass') &&
testExpression.d.args.length === 2
) {
const arg0Expr = testExpression.d.args[0].d.valueExpr;

const arg0Type = evaluator.getTypeOfExpression(arg0Expr).type;
const [arg0, arg1] = testExpression.d.args;
const arg0Type = evaluator.getTypeOfExpression(arg0.d.valueExpr).type;
const isSubclassCheck = testExpression.d.leftExpr.d.value === 'issubclass';
const matchesTypeVar = isSubclassCheck
? isTypeVar(arg0Type) &&
TypeBase.isInstantiable(arg0Type) &&
isTypeSame(TypeVarType.cloneAsInstance(arg0Type), typeVar)
: isCompatibleWithConstrainedTypeVar(arg0Type, typeVar);

if (matchesTypeVar && isSubclassCheck) {
const callType = evaluator.getTypeOfExpression(
testExpression.d.leftExpr,
EvalFlags.CallBaseDefaults
).type;
if (
!isFunction(callType) ||
!FunctionType.isBuiltIn(callType, 'issubclass') ||
testExpression.d.args.some(
(arg) => arg.d.argCategory !== ArgCategory.Simple || arg.d.name
)
) {
curFlowNode = conditionFlowNode.antecedent;
continue;
}
}

if (isCompatibleWithConstrainedTypeVar(arg0Type, typeVar)) {
if (matchesTypeVar) {
// Prevent infinite recursion by noting that we've been here before.
visitedFlowNodeMap.add(curFlowNode.id);
const priorRemainingConstraints = narrowConstrainedTypeVarRecursive(
Expand All @@ -1887,7 +1914,7 @@ export function getCodeFlowEngine(
);
visitedFlowNodeMap.delete(curFlowNode.id);

const arg1Expr = testExpression.d.args[1].d.valueExpr;
const arg1Expr = arg1.d.valueExpr;
const arg1Type = evaluator.getTypeOfExpression(
arg1Expr,
EvalFlags.AllowMissingTypeArgs |
Expand All @@ -1899,6 +1926,16 @@ export function getCodeFlowEngine(
).type;

if (isInstantiableClass(arg1Type)) {
if (isSubclassCheck) {
return narrowConstrainedTypeVarForSubclass(
evaluator,
priorRemainingConstraints,
arg1Type,
isPositiveTest,
testExpression
);
}

return priorRemainingConstraints.filter((subtype) => {
if (ClassType.isSameGenericClass(subtype, ClassType.cloneAsInstance(arg1Type))) {
return isPositiveTest;
Expand Down
53 changes: 53 additions & 0 deletions packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,59 @@ function areDisjointBasesIncompatible(classType1: ClassType, classType2: ClassTy
return !!base1 && !!base2 && !ClassType.getMostDerivedDisjointBase([base1, base2]);
}

export function narrowConstrainedTypeVarForSubclass(
evaluator: TypeEvaluator,
constraints: ClassType[],
filter: ClassType,
isPositiveTest: boolean,
errorNode: ParseNode
): ClassType[] {
const metaclass = filter.shared.effectiveMetaclass;
// Only a known class with the default metaclass gives a reliable nominal test.
if (
(filter.priv.includeSubclasses && !ClassType.isFinal(filter)) ||
!metaclass ||
!isInstantiableClass(metaclass) ||
!ClassType.isBuiltIn(metaclass, 'type') ||
hasUnresolvedMetaclassInMro(filter) ||
ClassType.isIllegalIsinstanceClass(filter) ||
ClassType.isProtocolClass(filter) ||
ClassType.isTypedDictClass(filter)
) {
return constraints;
}

return constraints.filter((constraint) => {
// A promotion can select a constraint without satisfying a runtime subclass test.
return someSubtypes(evaluator.expandPromotionTypes(errorNode, constraint), (subtype) => {
if (
!isClassInstance(subtype) ||
ClassType.derivesFromAnyOrUnknown(subtype) ||
ClassType.isProtocolClass(subtype) ||
ClassType.isTypedDictClass(subtype)
) {
return true;
}

const subclass = ClassType.cloneAsInstantiable(subtype);
const derivesFromFilter = ClassType.isDerivedFrom(subclass, filter);
if (!isPositiveTest) {
return !derivesFromFilter;
}

// Unrelated classes can still share a subclass unless one of them
// is final or their disjoint bases are incompatible.
return (
derivesFromFilter ||
ClassType.isDerivedFrom(filter, subclass) ||
(!ClassType.isFinal(subclass) &&
!ClassType.isFinal(filter) &&
!areDisjointBasesIncompatible(subclass, filter))
);
});
});
}

// Fill gradual holes without replacing facts inferred from the guarded value.
function fillTypeGuardGradualArgs(
input: Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ def func5(t: _T3) -> _T3:
return B()
elif isinstance(t, C):
return C()


def func6(t: type[_T1]) -> _T1:
if issubclass(t, str):
return ""
else:
return 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Tests when issubclass can narrow a constrained return TypeVar.

from typing import TypeVar, final

T = TypeVar("T", int, str)


def exact_filter[U: (int, str)](cls: type[U]) -> U:
if issubclass(cls, int):
return 1
return ""


@final
class Exact: ...


class Other: ...


TFinal = TypeVar("TFinal", Exact, Other)


def final_filter(cls: type[TFinal], filter_cls: type[Exact]) -> TFinal:
if issubclass(cls, filter_cls):
return Exact()
return Other()


class Base: ...


class Derived(Base): ...


TRelated = TypeVar("TRelated", Base, Derived)


def related_constraints(cls: type[TRelated]) -> TRelated:
if issubclass(cls, Base):
return Base() # Error: TRelated could be Derived.
return cls()


def shadowed_builtin():
def issubclass(cls: type, filter_cls: type) -> bool:
return True

def select(cls: type[T]) -> T:
if issubclass(cls, str):
return "" # Error: this function does not narrow T.
return cls()


class FancyStr(str): ...


def dynamic_filter(cls: type[T], filter_cls: type[str]) -> T:
if issubclass(cls, filter_cls):
return cls()
return 0 # Error: filter_cls could be FancyStr while cls is str.


class CustomMeta(type):
def __subclasscheck__(cls, subclass: type, /) -> bool:
return True


class CustomStr(str, metaclass=CustomMeta): ...


def custom_subclass_check(cls: type[T]) -> T:
if issubclass(cls, CustomStr):
return "" # Error: the subclass hook can accept int.
return cls()


class MetaA(type): ...


class MetaB(type): ...


class ClassA(metaclass=MetaA): ...


class ClassB(metaclass=MetaB): ...


TMeta = TypeVar("TMeta", MetaA, MetaB)


def bare_metaclass_type_var(cls: TMeta) -> TMeta:
if issubclass(cls, MetaA):
return ClassA # Error: cls is not type[TMeta].
return ClassB # Error: cls is not type[TMeta].


TNumeric = TypeVar("TNumeric", float, str)


def promoted_constraint(cls: type[TNumeric]) -> TNumeric:
if issubclass(cls, float):
return 1.5
return "" # Error: int can solve as float but fails this subclass check.


class A: ...


class B: ...


class AB(A, B): ...


TOverlap = TypeVar("TOverlap", A, B)


def overlapping_constraints(cls: type[TOverlap]) -> TOverlap:
if issubclass(cls, B):
return B() # Error: AB can solve as A and satisfy this subclass check.
return A()
12 changes: 12 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,18 @@ test('ConstrainedTypeVar13', () => {
TestUtils.validateResults(analysisResults, 5);
});

test('ConstrainedTypeVarIssubclass', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['constrainedTypeVarIssubclass.py']);
TestUtils.validateResults(analysisResults, 8);
assert.deepStrictEqual(
analysisResults[0].errors
.slice()
.sort((left, right) => left.range.start.line - right.range.start.line)
.map((diagnostic) => [diagnostic.range.start.line + 1, diagnostic.getRule()]),
[41, 51, 61, 74, 95, 96, 105, 122].map((line) => [line, DiagnosticRule.reportReturnType])
);
});

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

Expand Down