From d52b0c3270cbea75c11d6c243fed419d49c3f78d Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Tue, 15 Sep 2026 16:32:22 -0700 Subject: [PATCH] Short-circuit failed TypedDict dictionary equivalence Return immediately when a field makes dictionary equivalence fail, avoiding unnecessary union construction and assignability checks for remaining fields. Credit @Wizzerinus for identifying this optimization in #11759. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- packages/pyright-internal/src/analyzer/typedDicts.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typedDicts.ts b/packages/pyright-internal/src/analyzer/typedDicts.ts index 21f7ca36d9c5..c2a68cb65282 100644 --- a/packages/pyright-internal/src/analyzer/typedDicts.ts +++ b/packages/pyright-internal/src/analyzer/typedDicts.ts @@ -953,10 +953,9 @@ export function getTypedDictDictEquivalent( let dictValueType = entries.extraItems.valueType; - let isEquivalentToDict = true; - entries.knownItems.forEach((entry) => { + for (const entry of entries.knownItems.values()) { if (entry.isReadOnly || entry.isRequired) { - isEquivalentToDict = false; + return undefined; } dictValueType = combineTypes([dictValueType, entry.valueType]); @@ -971,12 +970,8 @@ export function getTypedDictDictEquivalent( recursionCount + 1 ) ) { - isEquivalentToDict = false; + return undefined; } - }); - - if (!isEquivalentToDict) { - return undefined; } return dictValueType;