From 52ed54adce6bb3149bd77263ab92e910ef93ade5 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Fri, 11 Oct 2024 14:08:36 -0700 Subject: [PATCH] Fixed bug that results in a false positive when a NamedTuple field uses `...` as a default value in a stub file. This addresses #9191. --- .../src/analyzer/dataClasses.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/dataClasses.ts b/packages/pyright-internal/src/analyzer/dataClasses.ts index 98a213dfc872..a2b5f7589d68 100644 --- a/packages/pyright-internal/src/analyzer/dataClasses.ts +++ b/packages/pyright-internal/src/analyzer/dataClasses.ts @@ -572,11 +572,20 @@ export function synthesizeDataClassMethods( if (entry.isDefaultFactory || !entry.defaultExpr) { defaultType = entry.type; } else { - defaultType = evaluator.getTypeOfExpression( - entry.defaultExpr, - /* flags */ undefined, - makeInferenceContext(entry.type) - ).type; + const defaultExpr = entry.defaultExpr; + const fileInfo = AnalyzerNodeInfo.getFileInfo(node); + const flags = fileInfo.isStubFile ? EvalFlags.ConvertEllipsisToAny : EvalFlags.None; + + // Use speculative mode here so we don't cache the results. + // We'll want to re-evaluate this expression later, potentially + // with different evaluation flags. + defaultType = evaluator.useSpeculativeMode(defaultExpr, () => { + return evaluator.getTypeOfExpression( + defaultExpr, + flags, + makeInferenceContext(entry.type) + ).type; + }); } } }