diff --git a/packages/pyright-internal/src/analyzer/typeUtils.ts b/packages/pyright-internal/src/analyzer/typeUtils.ts index 1d92366f7e18..d9b2ce2a3426 100644 --- a/packages/pyright-internal/src/analyzer/typeUtils.ts +++ b/packages/pyright-internal/src/analyzer/typeUtils.ts @@ -336,12 +336,7 @@ export function isTypeVarSame(type1: TypeVarType, type2: Type) { return false; } - let isCompatible = true; - doForEachSubtype(type2, (subtype) => { - if (!isCompatible) { - return; - } - + return allSubtypes(type2, (subtype) => { if (!isTypeSame(type1, subtype)) { const conditions = getTypeCondition(subtype); @@ -349,12 +344,12 @@ export function isTypeVarSame(type1: TypeVarType, type2: Type) { !conditions || !conditions.some((condition) => condition.typeVar.priv.nameWithScope === type1.priv.nameWithScope) ) { - isCompatible = false; + return false; } } - }); - return isCompatible; + return true; + }); } export function makeInferenceContext( @@ -796,12 +791,10 @@ export function someSubtypes(type: Type, callback: (type: Type) => boolean): boo export function allSubtypes(type: Type, callback: (type: Type) => boolean): boolean { if (isUnion(type)) { - return type.priv.subtypes.every((subtype) => { - callback(subtype); - }); - } else { - return callback(type); + return type.priv.subtypes.every((subtype) => callback(subtype)); } + + return callback(type); } export function doForEachSignature( diff --git a/packages/pyright-internal/src/tests/typeUtils.test.ts b/packages/pyright-internal/src/tests/typeUtils.test.ts index 7d575adc113a..8d277a2323a2 100644 --- a/packages/pyright-internal/src/tests/typeUtils.test.ts +++ b/packages/pyright-internal/src/tests/typeUtils.test.ts @@ -10,9 +10,11 @@ import * as assert from 'assert'; import { ConstraintSolution } from '../analyzer/constraintSolution'; import { + allSubtypes, applySolvedTypeVars, containsLiteralType, mapSignatures, + someSubtypes, transformExpectedType, transformTypePair, } from '../analyzer/typeUtils'; @@ -35,6 +37,7 @@ import { TypeCategory, TypeVarScopeType, TypeVarType, + UnionableType, UnionType, UnknownType, Variance, @@ -362,6 +365,76 @@ test('Transform aligned class arguments but preserve mismatched wrappers', () => ); }); +test('AllSubtypes', () => { + const unionType = createUnion(createClassType('A'), createClassType('B'), createClassType('C')); + const visitedSubtypes: Type[] = []; + + const result = allSubtypes(unionType, (subtype) => { + visitedSubtypes.push(subtype); + return visitedSubtypes.length < 2; + }); + + assert.strictEqual(result, false); + assert.strictEqual(visitedSubtypes.length, 2); + + assert.strictEqual( + allSubtypes(unionType, () => { + return true; + }), + true + ); + + const singleType = createClassType('D'); + assert.strictEqual( + allSubtypes(singleType, (subtype) => { + assert.strictEqual(subtype, singleType); + return true; + }), + true + ); +}); + +test('AllSubtypes passes only the subtype to callbacks', () => { + const literal = ClassType.cloneWithLiteral(ClassType.cloneAsInstance(createClass('Value')), 1); + const nested = ClassType.specialize(ClassType.cloneAsInstance(createClass('Container')), [literal]); + + assert.strictEqual(containsLiteralType(literal), true); + assert.strictEqual(containsLiteralType(nested), false); + assert.strictEqual(containsLiteralType(nested, true), true); + assert.strictEqual(allSubtypes(createUnion(literal, nested), containsLiteralType), false); + assert.strictEqual(allSubtypes(createUnion(nested, literal), containsLiteralType), false); + assert.strictEqual(allSubtypes(nested, containsLiteralType), false); +}); + +test('SomeSubtypes', () => { + const unionType = createUnion(createClassType('A'), createClassType('B'), createClassType('C')); + const visitedSubtypes: Type[] = []; + + const result = someSubtypes(unionType, (subtype) => { + visitedSubtypes.push(subtype); + return visitedSubtypes.length === 2; + }); + + assert.strictEqual(result, true); + assert.strictEqual(visitedSubtypes.length, 2); + + assert.strictEqual( + someSubtypes(unionType, () => { + return false; + }), + false + ); + + const singleType = createClassType('D'); + assert.strictEqual( + someSubtypes(singleType, (subtype) => { + assert.strictEqual(subtype, singleType); + return false; + }), + false + ); +}); + function createFunction(parameterType: Type, defaultType: Type, returnType: Type) { const functionType = FunctionType.createInstance('', '', '', FunctionTypeFlags.None); FunctionType.addParam( @@ -389,3 +462,26 @@ function replacePair(source: Type, target: Type) { return (sourceNode: Type, targetNode: Type) => sourceNode === source && targetNode === target ? targetNode : undefined; } + +function createClassType(name: string, flags = ClassTypeFlags.None) { + const classType = ClassType.createInstantiable( + name, + name, + '', + Uri.empty(), + flags, + 0, + /* declaredMetaclass*/ undefined, + /* effectiveMetaclass */ undefined + ); + classType.shared.mro.push(classType); + return classType; +} + +function createUnion(...subtypes: readonly UnionableType[]) { + const unionType = UnionType.create(); + subtypes.forEach((subtype) => { + UnionType.addType(unionType, subtype); + }); + return unionType; +}