Fixed regression in --verifytypes where variables assigned from calls to TypeVar, ParamSpec, TypeVarTuple, NewType, TypedDict, and NamedTuple were marked as ambiguous. This addresses https://github.com/microsoft/pyright/issues/4600.

This commit is contained in:
Eric Traut 2023-03-06 12:34:09 -07:00
parent 6f07019c89
commit f8edba07e5

View File

@ -20839,9 +20839,33 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
}
}
// Special-case constants, which are treated as unambiguous.
if (isFinalVariableDeclaration(resolvedDecl) || resolvedDecl.isConstant) {
isUnambiguousType = true;
}
// Special-case calls to certain built-in type functions.
if (resolvedDecl.inferredTypeSource?.nodeType === ParseNodeType.Call) {
const baseTypeResult = getTypeOfExpression(
resolvedDecl.inferredTypeSource.leftExpression,
EvaluatorFlags.DoNotSpecialize
);
const callType = baseTypeResult.type;
if (
isInstantiableClass(callType) &&
ClassType.isBuiltIn(callType, [
'TypeVar',
'ParamSpec',
'TypeVarTuple',
'TypedDict',
'NamedTuple',
'NewType',
])
) {
isUnambiguousType = true;
}
}
}
}