From c49ce2fb69f9641083e2a86264d1ea2f1f71f685 Mon Sep 17 00:00:00 2001 From: Bill Schnurr Date: Wed, 6 May 2026 16:28:41 -0700 Subject: [PATCH 1/3] Short-circuit isTypeVarSame subtype checks Fix allSubtypes callback handling and delegate union traversal directly to Array.every. Use it in isTypeVarSame to stop the subtype scan at the first incompatibility instead of invoking no-op callbacks for the remainder. Add direct allSubtypes and someSubtypes coverage. --- .../src/analyzer/typeUtils.ts | 21 ++--- .../src/tests/typeUtils.test.ts | 84 ++++++++++++++++++- 2 files changed, 90 insertions(+), 15 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typeUtils.ts b/packages/pyright-internal/src/analyzer/typeUtils.ts index a396b944af8f..d674cbc54512 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(callback); } + + 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 1a6b29129c54..8ea72380782f 100644 --- a/packages/pyright-internal/src/tests/typeUtils.test.ts +++ b/packages/pyright-internal/src/tests/typeUtils.test.ts @@ -8,7 +8,7 @@ import * as assert from 'assert'; -import { transformTypePair } from '../analyzer/typeUtils'; +import { allSubtypes, someSubtypes, transformTypePair } from '../analyzer/typeUtils'; import { AnyType, ClassType, @@ -22,6 +22,7 @@ import { Type, TypeVarScopeType, TypeVarType, + UnionableType, UnionType, UnknownType, Variance, @@ -164,3 +165,84 @@ function replacePair(source: Type, target: Type) { return (sourceNode: Type, targetNode: Type) => sourceNode === source && targetNode === target ? targetNode : undefined; } + +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('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 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: UnionableType[]) { + const unionType = UnionType.create(); + subtypes.forEach((subtype) => { + UnionType.addType(unionType, subtype); + }); + return unionType; +} From 7603cc829c80f4de3bc121e7c02a42a973036fdb Mon Sep 17 00:00:00 2001 From: Bill Schnurr Date: Fri, 20 Nov 2026 11:46:44 -0700 Subject: [PATCH 2/3] Preserve unary callbacks in allSubtypes Class B: Pyright helper contract. Pass only the subtype to each predicate so optional arguments retain their defaults. Preserve short-circuit behavior and literal-detection precision. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- packages/pyright-internal/src/analyzer/typeUtils.ts | 2 +- .../pyright-internal/src/tests/typeUtils.test.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/pyright-internal/src/analyzer/typeUtils.ts b/packages/pyright-internal/src/analyzer/typeUtils.ts index 3b769b2cb54e..d9b2ce2a3426 100644 --- a/packages/pyright-internal/src/analyzer/typeUtils.ts +++ b/packages/pyright-internal/src/analyzer/typeUtils.ts @@ -791,7 +791,7 @@ 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(callback); + return type.priv.subtypes.every((subtype) => callback(subtype)); } return callback(type); diff --git a/packages/pyright-internal/src/tests/typeUtils.test.ts b/packages/pyright-internal/src/tests/typeUtils.test.ts index 7104b67ab343..bae6cb28d9d1 100644 --- a/packages/pyright-internal/src/tests/typeUtils.test.ts +++ b/packages/pyright-internal/src/tests/typeUtils.test.ts @@ -422,6 +422,18 @@ test('AllSubtypes', () => { ); }); +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[] = []; From 82c1872fc0bee8a7f44582a3c30a7167f2a44133 Mon Sep 17 00:00:00 2001 From: Bill Schnurr Date: Fri, 20 Nov 2026 16:38:32 -0700 Subject: [PATCH 3/3] Keep subtype tests before helper declarations Classification: C (test maintenance). No type precision changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/tests/typeUtils.test.ts | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/pyright-internal/src/tests/typeUtils.test.ts b/packages/pyright-internal/src/tests/typeUtils.test.ts index bae6cb28d9d1..8d277a2323a2 100644 --- a/packages/pyright-internal/src/tests/typeUtils.test.ts +++ b/packages/pyright-internal/src/tests/typeUtils.test.ts @@ -365,34 +365,6 @@ test('Transform aligned class arguments but preserve mismatched wrappers', () => ); }); -function createFunction(parameterType: Type, defaultType: Type, returnType: Type) { - const functionType = FunctionType.createInstance('', '', '', FunctionTypeFlags.None); - FunctionType.addParam( - functionType, - FunctionParam.create(ParamCategory.Simple, parameterType, undefined, 'value', defaultType) - ); - functionType.shared.declaredReturnType = returnType; - return functionType; -} - -function createClass(name: string) { - return ClassType.createInstantiable( - name, - 'test', - `test.${name}`, - Uri.empty(), - ClassTypeFlags.None, - 0, - /* declaredMetaclass */ undefined, - /* effectiveMetaclass */ undefined - ); -} - -function replacePair(source: Type, target: Type) { - return (sourceNode: Type, targetNode: Type) => - sourceNode === source && targetNode === target ? targetNode : undefined; -} - test('AllSubtypes', () => { const unionType = createUnion(createClassType('A'), createClassType('B'), createClassType('C')); const visitedSubtypes: Type[] = []; @@ -463,6 +435,34 @@ test('SomeSubtypes', () => { ); }); +function createFunction(parameterType: Type, defaultType: Type, returnType: Type) { + const functionType = FunctionType.createInstance('', '', '', FunctionTypeFlags.None); + FunctionType.addParam( + functionType, + FunctionParam.create(ParamCategory.Simple, parameterType, undefined, 'value', defaultType) + ); + functionType.shared.declaredReturnType = returnType; + return functionType; +} + +function createClass(name: string) { + return ClassType.createInstantiable( + name, + 'test', + `test.${name}`, + Uri.empty(), + ClassTypeFlags.None, + 0, + /* declaredMetaclass */ undefined, + /* effectiveMetaclass */ undefined + ); +} + +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, @@ -478,7 +478,7 @@ function createClassType(name: string, flags = ClassTypeFlags.None) { return classType; } -function createUnion(...subtypes: UnionableType[]) { +function createUnion(...subtypes: readonly UnionableType[]) { const unionType = UnionType.create(); subtypes.forEach((subtype) => { UnionType.addType(unionType, subtype);