@zzzen/pyright-internal 1.2.0-dev.20230507 → 1.2.0-dev.20230514
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/backgroundAnalysisProgram.d.ts +1 -1
- package/dist/analyzer/checker.js +37 -2
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +14 -15
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructors.js +246 -189
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.js +2 -2
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/docStringConversion.js +1 -1
- package/dist/analyzer/docStringConversion.js.map +1 -1
- package/dist/analyzer/enums.js +8 -0
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/importResolver.js +47 -29
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/namedTuples.js +2 -5
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/program.d.ts +7 -17
- package/dist/analyzer/program.js +32 -247
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/protocols.js +1 -1
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/service.d.ts +4 -12
- package/dist/analyzer/service.js +12 -23
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.d.ts +2 -11
- package/dist/analyzer/sourceFile.js +11 -74
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/symbol.d.ts +3 -1
- package/dist/analyzer/symbol.js +5 -0
- package/dist/analyzer/symbol.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +362 -142
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +4 -0
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +3 -3
- package/dist/analyzer/typePrinter.js +247 -100
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +12 -7
- package/dist/analyzer/typeUtils.js +173 -48
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.d.ts +1 -2
- package/dist/analyzer/typeVarContext.js +14 -32
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/types.d.ts +3 -2
- package/dist/analyzer/types.js +13 -10
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +1 -1
- package/dist/backgroundAnalysisBase.js +16 -0
- package/dist/backgroundAnalysisBase.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +0 -1
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/extensibility.d.ts +26 -3
- package/dist/common/extensibility.js.map +1 -1
- package/dist/common/logTracker.d.ts +2 -0
- package/dist/common/logTracker.js +8 -1
- package/dist/common/logTracker.js.map +1 -1
- package/dist/common/lspUtils.d.ts +4 -1
- package/dist/common/lspUtils.js +38 -1
- package/dist/common/lspUtils.js.map +1 -1
- package/dist/common/pythonVersion.d.ts +2 -1
- package/dist/common/pythonVersion.js +1 -0
- package/dist/common/pythonVersion.js.map +1 -1
- package/dist/common/workspaceEditUtils.d.ts +3 -3
- package/dist/common/workspaceEditUtils.js +10 -10
- package/dist/common/workspaceEditUtils.js.map +1 -1
- package/dist/languageServerBase.d.ts +2 -5
- package/dist/languageServerBase.js +33 -60
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +50 -51
- package/dist/languageService/autoImporter.js +125 -210
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +6 -32
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.d.ts +40 -79
- package/dist/languageService/completionProvider.js +571 -811
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.d.ts +2 -2
- package/dist/languageService/documentSymbolCollector.js +33 -23
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/documentSymbolProvider.d.ts +13 -35
- package/dist/languageService/documentSymbolProvider.js +52 -264
- package/dist/languageService/documentSymbolProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.d.ts +1 -3
- package/dist/languageService/hoverProvider.js +11 -97
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.d.ts +3 -3
- package/dist/languageService/referencesProvider.js +5 -7
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/renameProvider.d.ts +0 -1
- package/dist/languageService/renameProvider.js +2 -6
- package/dist/languageService/renameProvider.js.map +1 -1
- package/dist/languageService/symbolIndexer.d.ts +31 -0
- package/dist/languageService/symbolIndexer.js +105 -0
- package/dist/languageService/symbolIndexer.js.map +1 -0
- package/dist/languageService/tooltipUtils.d.ts +8 -1
- package/dist/languageService/tooltipUtils.js +102 -1
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/languageService/workspaceSymbolProvider.d.ts +17 -0
- package/dist/languageService/workspaceSymbolProvider.js +133 -0
- package/dist/languageService/workspaceSymbolProvider.js.map +1 -0
- package/dist/localization/localize.d.ts +16 -1
- package/dist/localization/localize.js +9 -1
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +9 -1
- package/dist/parser/parser.js +3 -0
- package/dist/parser/parser.js.map +1 -1
- package/dist/pyright.js +26 -4
- package/dist/pyright.js.map +1 -1
- package/dist/tests/chainedSourceFiles.test.js +15 -20
- package/dist/tests/chainedSourceFiles.test.js.map +1 -1
- package/dist/tests/checker.test.js +14 -0
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/completions.test.js +11 -236
- package/dist/tests/completions.test.js.map +1 -1
- package/dist/tests/docStringConversion.test.js +36 -2
- package/dist/tests/docStringConversion.test.js.map +1 -1
- package/dist/tests/fourslash/completions.override2.fourslash.js +1 -16
- package/dist/tests/fourslash/completions.override2.fourslash.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.d.ts +14 -8
- package/dist/tests/harness/fourslash/testState.js +25 -36
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/importResolver.test.js +81 -1
- package/dist/tests/importResolver.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +12 -0
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +4 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +21 -9
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/workspaceEditUtils.test.js +15 -10
- package/dist/tests/workspaceEditUtils.test.js.map +1 -1
- package/package.json +1 -1
- package/dist/languageService/importAdder.d.ts +0 -40
- package/dist/languageService/importAdder.js +0 -388
- package/dist/languageService/importAdder.js.map +0 -1
- package/dist/tests/fourslash/completions.commitChars.fourslash.d.ts +0 -1
- package/dist/tests/fourslash/completions.commitChars.fourslash.js +0 -81
- package/dist/tests/fourslash/completions.commitChars.fourslash.js.map +0 -1
- package/dist/tests/importAdder.test.d.ts +0 -1
- package/dist/tests/importAdder.test.js +0 -1325
- package/dist/tests/importAdder.test.js.map +0 -1
@@ -446,7 +446,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
446
446
|
}
|
447
447
|
}
|
448
448
|
function getTypeOfExpression(node, flags = 0 /* None */, inferenceContext) {
|
449
|
-
var _a
|
449
|
+
var _a;
|
450
450
|
// Is this type already cached?
|
451
451
|
const cacheEntry = readTypeCacheEntry(node);
|
452
452
|
if (cacheEntry &&
|
@@ -646,6 +646,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
646
646
|
}
|
647
647
|
}
|
648
648
|
writeTypeCache(node, typeResult, flags, inferenceContext, /* allowSpeculativeCaching */ true);
|
649
|
+
// If there was an expected type, make sure that the result type is compatible.
|
649
650
|
if (inferenceContext &&
|
650
651
|
!(0, types_1.isAnyOrUnknown)(inferenceContext.expectedType) &&
|
651
652
|
!(0, types_1.isNever)(inferenceContext.expectedType)) {
|
@@ -653,9 +654,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
653
654
|
if (!typeResult.isIncomplete && !typeResult.expectedTypeDiagAddendum) {
|
654
655
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
655
656
|
// Make sure the resulting type is assignable to the expected type.
|
656
|
-
|
657
|
-
// it is provided.
|
658
|
-
if (!assignType(inferenceContext.expectedType, typeResult.type, diag, new typeVarContext_1.TypeVarContext((_b = inferenceContext.typeVarContext) === null || _b === void 0 ? void 0 : _b.getSolveForScopes()))) {
|
657
|
+
if (!assignType(inferenceContext.expectedType, typeResult.type, diag)) {
|
659
658
|
typeResult.typeErrors = true;
|
660
659
|
typeResult.expectedTypeDiagAddendum = diag;
|
661
660
|
diag.addTextRange(node);
|
@@ -1385,12 +1384,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1385
1384
|
const isObjectInit = !!methodType &&
|
1386
1385
|
(0, types_1.isFunction)(methodType) &&
|
1387
1386
|
methodType.details.fullName === 'builtins.object.__init__';
|
1388
|
-
const isSkipConstructor = !!methodType && (0, types_1.isFunction)(methodType) && types_1.FunctionType.isSkipConstructorCheck(methodType);
|
1389
1387
|
const isDefaultParams = methodType && (0, types_1.isFunction)(methodType) && types_1.FunctionType.hasDefaultParameters(methodType);
|
1390
1388
|
// If there was no `__init__` or the only `__init__` that was found was from
|
1391
1389
|
// the `object` class or accepts only default parameters(* args, ** kwargs),
|
1392
1390
|
// see if we can find a better signature from the `__new__` method.
|
1393
|
-
if (!methodType || isObjectInit ||
|
1391
|
+
if (!methodType || isObjectInit || isDefaultParams) {
|
1394
1392
|
const constructorType = getBoundMethod(subtype, '__new__',
|
1395
1393
|
/* recursionCount */ undefined,
|
1396
1394
|
/* treatConstructorAsClassMember */ true);
|
@@ -1534,8 +1532,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1534
1532
|
if ((0, types_1.isClassInstance)(declaredType)) {
|
1535
1533
|
const setterInfo = (0, typeUtils_1.lookUpClassMember)(declaredType, '__set__');
|
1536
1534
|
const setter = setterInfo ? getTypeOfMember(setterInfo) : undefined;
|
1537
|
-
if (setter && (0, types_1.isFunction)(setter) && setter.details.parameters.length >= 3) {
|
1535
|
+
if (setterInfo && setter && (0, types_1.isFunction)(setter) && setter.details.parameters.length >= 3) {
|
1538
1536
|
declaredType = setter.details.parameters[2].type;
|
1537
|
+
if ((0, types_1.isClass)(setterInfo.classType)) {
|
1538
|
+
const typeVarMap = (0, typeUtils_1.buildTypeVarContextFromSpecializedClass)(setterInfo.classType);
|
1539
|
+
declaredType = (0, typeUtils_1.applySolvedTypeVars)(declaredType, typeVarMap);
|
1540
|
+
}
|
1539
1541
|
if ((0, types_1.isAnyOrUnknown)(declaredType)) {
|
1540
1542
|
return undefined;
|
1541
1543
|
}
|
@@ -2021,7 +2023,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2021
2023
|
}
|
2022
2024
|
else {
|
2023
2025
|
// Constrain the resulting type to match the declared type.
|
2024
|
-
destType = narrowTypeBasedOnAssignment(declaredType, type);
|
2026
|
+
destType = narrowTypeBasedOnAssignment(nameNode, declaredType, type);
|
2025
2027
|
}
|
2026
2028
|
}
|
2027
2029
|
else {
|
@@ -2538,7 +2540,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2538
2540
|
// is a enum because the annotated type in an enum doesn't reflect
|
2539
2541
|
// the type of the symbol.
|
2540
2542
|
if (!(0, types_1.isClassInstance)(type) || !types_1.ClassType.isEnumClass(type)) {
|
2541
|
-
type = narrowTypeBasedOnAssignment(annotationType, type);
|
2543
|
+
type = narrowTypeBasedOnAssignment(target, annotationType, type);
|
2542
2544
|
}
|
2543
2545
|
}
|
2544
2546
|
}
|
@@ -3199,6 +3201,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3199
3201
|
container: typeAliasNode.name.value,
|
3200
3202
|
}), node);
|
3201
3203
|
}
|
3204
|
+
else {
|
3205
|
+
// If this is a TypeAliasType call, the recursiveTypeParameters will already
|
3206
|
+
// be populated, and we need to verify that the type parameter is in the
|
3207
|
+
// list of allowed type parameters.
|
3208
|
+
const allowedTypeParams = leftType.details.recursiveTypeParameters;
|
3209
|
+
if (allowedTypeParams) {
|
3210
|
+
if (!allowedTypeParams.some((param) => param.details.name === type.details.name)) {
|
3211
|
+
// Return the original type.
|
3212
|
+
return { type, isRescoped: false, foundInterveningClass: false };
|
3213
|
+
}
|
3214
|
+
}
|
3215
|
+
}
|
3202
3216
|
return {
|
3203
3217
|
type: types_1.TypeVarType.cloneForScopeId(type, leftType.details.recursiveTypeAliasScopeId, leftType.details.recursiveTypeAliasName, 2 /* TypeAlias */),
|
3204
3218
|
isRescoped: false,
|
@@ -3968,16 +3982,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3968
3982
|
// If this function is an instance member (e.g. a lambda that was
|
3969
3983
|
// assigned to an instance variable), don't perform any binding.
|
3970
3984
|
if (!isAccessedThroughObject || (memberInfo && !memberInfo.isInstanceMember)) {
|
3971
|
-
// Skip binding if the class appears to be a metaclass (i.e. a subclass of
|
3972
|
-
// `type`) because the first parameter of instance methods in a metaclass
|
3973
|
-
// are not `self` instances.
|
3974
|
-
const isMetaclass = !isAccessedThroughObject &&
|
3975
|
-
(0, types_1.isClass)(baseTypeClass) &&
|
3976
|
-
(flags & 16 /* TreatConstructorAsClassMethod */) === 0 &&
|
3977
|
-
baseTypeClass.details.mro.some((mroType) => (0, types_1.isClass)(mroType) && types_1.ClassType.isBuiltIn(mroType, 'type'));
|
3978
|
-
if (isMetaclass) {
|
3979
|
-
return concreteSubtype;
|
3980
|
-
}
|
3981
3985
|
return bindFunctionToClassOrObject(isAccessedThroughObject ? types_1.ClassType.cloneAsInstance(baseTypeClass) : baseTypeClass, concreteSubtype, memberInfo && (0, types_1.isInstantiableClass)(memberInfo.classType) ? memberInfo.classType : undefined, errorNode,
|
3982
3986
|
/* recursionCount */ undefined, treatConstructorAsClassMember, bindToType);
|
3983
3987
|
}
|
@@ -5059,7 +5063,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5059
5063
|
}
|
5060
5064
|
if (!matchingSubtype) {
|
5061
5065
|
const subtypeResult = useSpeculativeMode(node, () => {
|
5062
|
-
return getTypeOfTupleWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype
|
5066
|
+
return getTypeOfTupleWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype));
|
5063
5067
|
});
|
5064
5068
|
if (subtypeResult && assignType(subtype, subtypeResult.type)) {
|
5065
5069
|
matchingSubtype = subtype;
|
@@ -5070,7 +5074,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5070
5074
|
}
|
5071
5075
|
let expectedTypeDiagAddendum;
|
5072
5076
|
if (effectiveExpectedType) {
|
5073
|
-
const result = getTypeOfTupleWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType
|
5077
|
+
const result = getTypeOfTupleWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType));
|
5074
5078
|
if (result && !result.typeErrors) {
|
5075
5079
|
return result;
|
5076
5080
|
}
|
@@ -5246,6 +5250,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5246
5250
|
}
|
5247
5251
|
}
|
5248
5252
|
else {
|
5253
|
+
// Check for an attempt to invoke an abstract static or class method.
|
5254
|
+
if ((0, types_1.isFunction)(baseTypeResult.type) &&
|
5255
|
+
baseTypeResult.type.boundToType &&
|
5256
|
+
(0, types_1.isInstantiableClass)(baseTypeResult.type.boundToType) &&
|
5257
|
+
!baseTypeResult.type.boundToType.includeSubclasses) {
|
5258
|
+
if (types_1.FunctionType.isAbstractMethod(baseTypeResult.type)) {
|
5259
|
+
if (types_1.FunctionType.isStaticMethod(baseTypeResult.type) ||
|
5260
|
+
types_1.FunctionType.isClassMethod(baseTypeResult.type)) {
|
5261
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.abstractMethodInvocation().format({
|
5262
|
+
method: baseTypeResult.type.details.name,
|
5263
|
+
}), node.leftExpression);
|
5264
|
+
}
|
5265
|
+
}
|
5266
|
+
}
|
5249
5267
|
const callResult = validateCallArguments(node, argList, baseTypeResult,
|
5250
5268
|
/* typeVarContext */ undefined,
|
5251
5269
|
/* skipUnknownArgCheck */ false, inferenceContext);
|
@@ -6169,13 +6187,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6169
6187
|
return types_1.AnyType.create();
|
6170
6188
|
}
|
6171
6189
|
if (className === 'TypeVar') {
|
6172
|
-
return createTypeVarType(errorNode, argList);
|
6190
|
+
return createTypeVarType(errorNode, expandedSubtype, argList);
|
6173
6191
|
}
|
6174
6192
|
if (className === 'TypeVarTuple') {
|
6175
|
-
return createTypeVarTupleType(errorNode, argList);
|
6193
|
+
return createTypeVarTupleType(errorNode, expandedSubtype, argList);
|
6176
6194
|
}
|
6177
6195
|
if (className === 'ParamSpec') {
|
6178
|
-
return createParamSpecType(errorNode, argList);
|
6196
|
+
return createParamSpecType(errorNode, expandedSubtype, argList);
|
6197
|
+
}
|
6198
|
+
if (className === 'TypeAliasType') {
|
6199
|
+
const newTypeAlias = createTypeAliasType(errorNode, argList);
|
6200
|
+
if (newTypeAlias) {
|
6201
|
+
return newTypeAlias;
|
6202
|
+
}
|
6179
6203
|
}
|
6180
6204
|
if (className === 'NamedTuple') {
|
6181
6205
|
return (0, namedTuples_1.createNamedTupleType)(evaluatorInterface, errorNode, argList,
|
@@ -7215,18 +7239,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7215
7239
|
// Prepopulate the typeVarContext based on the specialized expected type if the
|
7216
7240
|
// callee has a declared return type. This will allow us to more closely match
|
7217
7241
|
// the expected type if possible.
|
7218
|
-
//
|
7219
|
-
|
7220
|
-
|
7221
|
-
|
7222
|
-
(0, types_1.
|
7223
|
-
|
7224
|
-
|
7225
|
-
|
7226
|
-
|
7227
|
-
|
7228
|
-
|
7229
|
-
|
7242
|
+
// Determine which type arguments are needed to match the expected type.
|
7243
|
+
if ((0, types_1.isClassInstance)(effectiveReturnType)) {
|
7244
|
+
// If the return type is a class and the expected type is a union that contains
|
7245
|
+
// that class, see if we can eliminate the other subtypes in the union.
|
7246
|
+
if ((0, types_1.isUnion)(effectiveExpectedType)) {
|
7247
|
+
const filteredType = (0, typeUtils_1.mapSubtypes)(effectiveExpectedType, (subtype) => {
|
7248
|
+
return (0, types_1.isClassInstance)(subtype) && types_1.ClassType.isSameGenericClass(effectiveReturnType, subtype)
|
7249
|
+
? subtype
|
7250
|
+
: undefined;
|
7251
|
+
});
|
7252
|
+
if ((0, types_1.isClassInstance)(filteredType)) {
|
7253
|
+
effectiveExpectedType = filteredType;
|
7254
|
+
}
|
7255
|
+
}
|
7256
|
+
if ((0, types_1.isClassInstance)(effectiveExpectedType) && !(0, types_1.isTypeSame)(effectiveReturnType, effectiveExpectedType)) {
|
7257
|
+
const tempTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(effectiveReturnType));
|
7258
|
+
(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, effectiveReturnType, effectiveExpectedType, tempTypeVarContext, ParseTreeUtils.getTypeVarScopesForNode(errorNode));
|
7259
|
+
const genericReturnType = types_1.ClassType.cloneForSpecialization(effectiveReturnType,
|
7260
|
+
/* typeArguments */ undefined,
|
7261
|
+
/* isTypeArgumentExplicit */ false);
|
7262
|
+
effectiveExpectedType = (0, typeUtils_1.applySolvedTypeVars)(genericReturnType, tempTypeVarContext);
|
7263
|
+
}
|
7230
7264
|
}
|
7231
7265
|
else if ((0, types_1.isFunction)(effectiveReturnType)) {
|
7232
7266
|
// If the return type is a callable and the expected type is a union that
|
@@ -7336,7 +7370,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7336
7370
|
// If we skipped a overload arg during the first pass,
|
7337
7371
|
// add another pass to ensure that we handle all of the
|
7338
7372
|
// type variables.
|
7339
|
-
if (i === 0 && argResult.skippedOverloadArg) {
|
7373
|
+
if (i === 0 && (argResult.skippedOverloadArg || argResult.skippedBareTypeVarExpectedType)) {
|
7340
7374
|
passCount++;
|
7341
7375
|
}
|
7342
7376
|
});
|
@@ -7444,11 +7478,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7444
7478
|
if (typeVarScopes.some((typeVarScope) => typeVarContext.hasSolveForScope(typeVarScope))) {
|
7445
7479
|
unknownIfNotFound = false;
|
7446
7480
|
}
|
7447
|
-
let specializedReturnType = (0, typeUtils_1.
|
7481
|
+
let specializedReturnType = (0, typeUtils_1.applySolvedTypeVars)(returnType, typeVarContext, {
|
7448
7482
|
unknownIfNotFound,
|
7449
7483
|
useUnknownOverDefault: skipUnknownArgCheck,
|
7450
7484
|
eliminateUnsolvedInUnions,
|
7451
|
-
|
7485
|
+
applyInScopePlaceholders: true,
|
7486
|
+
});
|
7487
|
+
specializedReturnType = (0, typeUtils_1.addConditionToType)(specializedReturnType, typeCondition);
|
7452
7488
|
// If the final return type is an unpacked tuple, turn it into a normal (unpacked) tuple.
|
7453
7489
|
if ((0, types_1.isUnpackedClass)(specializedReturnType)) {
|
7454
7490
|
specializedReturnType = types_1.ClassType.cloneForUnpacked(specializedReturnType, /* isUnpackedTuple */ false);
|
@@ -7559,6 +7595,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7559
7595
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.paramSpecNotBound().format({ type: printType(paramSpec) }), ((_a = argList[0]) === null || _a === void 0 ? void 0 : _a.valueExpression) || errorNode);
|
7560
7596
|
return false;
|
7561
7597
|
}
|
7598
|
+
const liveTypeVarScopes = ParseTreeUtils.getTypeVarScopesForNode(errorNode);
|
7562
7599
|
const srcTypeVarContext = new typeVarContext_1.TypeVarContext(paramSpecType.details.typeVarScopeId);
|
7563
7600
|
let reportedArgError = false;
|
7564
7601
|
// Build a map of all named parameters.
|
@@ -7621,7 +7658,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7621
7658
|
if (paramType) {
|
7622
7659
|
const argResult = validateArgType({
|
7623
7660
|
paramCategory: 0 /* Simple */,
|
7624
|
-
paramType,
|
7661
|
+
paramType: (0, typeUtils_1.transformExpectedType)(paramType, liveTypeVarScopes),
|
7625
7662
|
requiresTypeVarMatching: false,
|
7626
7663
|
argument: arg,
|
7627
7664
|
errorNode: arg.valueExpression || errorNode,
|
@@ -7672,7 +7709,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7672
7709
|
}
|
7673
7710
|
}
|
7674
7711
|
if (!reportedArgError) {
|
7675
|
-
|
7712
|
+
(0, typeUtils_1.applySourceContextTypeVarsToSignature)(typeVarContext, srcTypeVarContext);
|
7676
7713
|
}
|
7677
7714
|
return !reportedArgError;
|
7678
7715
|
}
|
@@ -7683,14 +7720,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7683
7720
|
let isTypeIncomplete = !!(typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete);
|
7684
7721
|
let isCompatible = true;
|
7685
7722
|
const functionName = typeResult === null || typeResult === void 0 ? void 0 : typeResult.type.details.name;
|
7723
|
+
let skippedBareTypeVarExpectedType = false;
|
7686
7724
|
if (argParam.argument.valueExpression) {
|
7687
7725
|
let expectedType;
|
7688
|
-
|
7689
|
-
|
7690
|
-
argParam.paramType
|
7691
|
-
|
7692
|
-
|
7693
|
-
|
7726
|
+
const isBareTypeVarExpectedType = (0, types_1.isTypeVar)(argParam.paramType) && argParam.paramType.scopeId === (typeResult === null || typeResult === void 0 ? void 0 : typeResult.type.details.typeVarScopeId);
|
7727
|
+
if (!options.skipBareTypeVarExpectedType || !isBareTypeVarExpectedType) {
|
7728
|
+
expectedType = argParam.paramType;
|
7729
|
+
// If the parameter type is a function with a ParamSpec, don't apply
|
7730
|
+
// the solved TypeVars if the typeVarContext has more than one signature.
|
7731
|
+
// This will expand the ParamSpec into an overload, which will cause problems.
|
7732
|
+
const skipApplySolvedTypeVars = (0, types_1.isFunction)(argParam.paramType) &&
|
7733
|
+
!!argParam.paramType.details.paramSpec &&
|
7734
|
+
typeVarContext.getSignatureContexts().length > 1;
|
7735
|
+
if (!skipApplySolvedTypeVars) {
|
7736
|
+
expectedType = (0, typeUtils_1.applySolvedTypeVars)(expectedType, typeVarContext, {
|
7737
|
+
useNarrowBoundOnly: !!options.useNarrowBoundOnly,
|
7738
|
+
});
|
7739
|
+
}
|
7740
|
+
}
|
7741
|
+
else {
|
7742
|
+
skippedBareTypeVarExpectedType = true;
|
7694
7743
|
}
|
7695
7744
|
// If the expected type is unknown, don't use an expected type. Instead,
|
7696
7745
|
// use default rules for evaluating the expression type.
|
@@ -7707,14 +7756,29 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7707
7756
|
32 /* DisallowParamSpec */ |
|
7708
7757
|
64 /* DisallowTypeVarTuple */
|
7709
7758
|
: 0 /* None */;
|
7710
|
-
const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedType,
|
7759
|
+
const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedType, !!(typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete)));
|
7711
7760
|
argType = exprTypeResult.type;
|
7761
|
+
// If the type includes multiple instances of a generic function
|
7762
|
+
// signature, force the type arguments for the duplicates to have
|
7763
|
+
// unique names.
|
7764
|
+
argType = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(argType, signatureTracker);
|
7712
7765
|
if (exprTypeResult.isIncomplete) {
|
7713
7766
|
isTypeIncomplete = true;
|
7714
7767
|
}
|
7715
7768
|
if (exprTypeResult.typeErrors) {
|
7716
7769
|
isCompatible = false;
|
7717
7770
|
}
|
7771
|
+
else if (expectedType && (0, typeUtils_1.requiresSpecialization)(expectedType)) {
|
7772
|
+
// Assign the argument type back to the expected type to assign
|
7773
|
+
// values to any in-scope placeholder type variables.
|
7774
|
+
const typeVarContextClone = typeVarContext.clone();
|
7775
|
+
if (assignType(expectedType, argType, /* diag */ undefined, typeVarContextClone)) {
|
7776
|
+
typeVarContext.copyFromClone(typeVarContextClone);
|
7777
|
+
}
|
7778
|
+
else {
|
7779
|
+
isCompatible = false;
|
7780
|
+
}
|
7781
|
+
}
|
7718
7782
|
expectedTypeDiag = exprTypeResult.expectedTypeDiagAddendum;
|
7719
7783
|
}
|
7720
7784
|
if (argParam.argument && argParam.argument.name && !isSpeculativeModeInUse(argParam.errorNode)) {
|
@@ -7743,10 +7807,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7743
7807
|
}
|
7744
7808
|
}
|
7745
7809
|
}
|
7746
|
-
// If the type includes multiple instances of a generic function
|
7747
|
-
// signature, force the type arguments for the duplicates to have
|
7748
|
-
// unique names.
|
7749
|
-
argType = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(argType, signatureTracker);
|
7750
7810
|
// If we're assigning to a var arg dictionary with a TypeVar type,
|
7751
7811
|
// strip literals before performing the assignment. This is used in
|
7752
7812
|
// places like a dict constructor.
|
@@ -7778,14 +7838,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7778
7838
|
// an overloaded callback protocol.
|
7779
7839
|
if (options.skipOverloadArg) {
|
7780
7840
|
if ((0, types_1.isOverloadedFunction)(argType)) {
|
7781
|
-
return {
|
7841
|
+
return {
|
7842
|
+
isCompatible,
|
7843
|
+
argType,
|
7844
|
+
isTypeIncomplete,
|
7845
|
+
skippedOverloadArg: true,
|
7846
|
+
skippedBareTypeVarExpectedType,
|
7847
|
+
condition,
|
7848
|
+
};
|
7782
7849
|
}
|
7783
7850
|
const concreteParamType = makeTopLevelTypeVarsConcrete(argParam.paramType);
|
7784
7851
|
if ((0, types_1.isFunction)(concreteParamType) || (0, types_1.isOverloadedFunction)(concreteParamType)) {
|
7785
7852
|
if ((0, types_1.isInstantiableClass)(argType)) {
|
7786
7853
|
const constructor = (0, constructors_1.createFunctionFromConstructor)(evaluatorInterface, argType);
|
7787
7854
|
if (constructor && (0, types_1.isOverloadedFunction)(constructor)) {
|
7788
|
-
return {
|
7855
|
+
return {
|
7856
|
+
isCompatible,
|
7857
|
+
argType,
|
7858
|
+
isTypeIncomplete,
|
7859
|
+
skippedOverloadArg: true,
|
7860
|
+
skippedBareTypeVarExpectedType,
|
7861
|
+
condition,
|
7862
|
+
};
|
7789
7863
|
}
|
7790
7864
|
}
|
7791
7865
|
if ((0, types_1.isClassInstance)(argType)) {
|
@@ -7793,7 +7867,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7793
7867
|
if (callMember) {
|
7794
7868
|
const memberType = getTypeOfMember(callMember);
|
7795
7869
|
if ((0, types_1.isOverloadedFunction)(memberType)) {
|
7796
|
-
return {
|
7870
|
+
return {
|
7871
|
+
isCompatible,
|
7872
|
+
argType,
|
7873
|
+
isTypeIncomplete,
|
7874
|
+
skippedOverloadArg: true,
|
7875
|
+
skippedBareTypeVarExpectedType,
|
7876
|
+
condition,
|
7877
|
+
};
|
7797
7878
|
}
|
7798
7879
|
}
|
7799
7880
|
}
|
@@ -7850,7 +7931,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7850
7931
|
}
|
7851
7932
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, message + diag.getString(), argParam.errorNode, (_a = diag.getEffectiveTextRange()) !== null && _a !== void 0 ? _a : argParam.errorNode);
|
7852
7933
|
}
|
7853
|
-
return { isCompatible: false, argType, isTypeIncomplete, condition };
|
7934
|
+
return { isCompatible: false, argType, isTypeIncomplete, skippedBareTypeVarExpectedType, condition };
|
7854
7935
|
}
|
7855
7936
|
if (!options.skipUnknownArgCheck) {
|
7856
7937
|
const simplifiedType = (0, types_1.removeUnbound)(argType);
|
@@ -7906,9 +7987,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7906
7987
|
}
|
7907
7988
|
}
|
7908
7989
|
}
|
7909
|
-
return { isCompatible, argType, isTypeIncomplete, condition };
|
7990
|
+
return { isCompatible, argType, isTypeIncomplete, skippedBareTypeVarExpectedType, condition };
|
7910
7991
|
}
|
7911
|
-
function createTypeVarType(errorNode, argList) {
|
7992
|
+
function createTypeVarType(errorNode, classType, argList) {
|
7912
7993
|
var _a, _b, _c, _d, _e, _f, _g;
|
7913
7994
|
let typeVarName = '';
|
7914
7995
|
let firstConstraintArg;
|
@@ -7981,8 +8062,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7981
8062
|
}
|
7982
8063
|
else if (paramName === 'default') {
|
7983
8064
|
defaultValueNode = argList[i].valueExpression;
|
7984
|
-
const argType = (_d = (_c = argList[i].typeResult) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : getTypeOfExpressionExpectingType(
|
8065
|
+
const argType = (_d = (_c = argList[i].typeResult) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : getTypeOfExpressionExpectingType(defaultValueNode, {
|
8066
|
+
allowTypeVarsWithoutScopeId: true,
|
8067
|
+
}).type;
|
7985
8068
|
typeVar.details.defaultType = (0, typeUtils_1.convertToInstance)(argType);
|
8069
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
8070
|
+
if (!fileInfo.isStubFile &&
|
8071
|
+
fileInfo.executionEnvironment.pythonVersion < pythonVersion_1.PythonVersion.V3_13 &&
|
8072
|
+
classType.details.moduleName !== 'typing_extensions') {
|
8073
|
+
addError(localize_1.Localizer.Diagnostic.typeVarDefaultIllegal(), defaultValueNode);
|
8074
|
+
}
|
7986
8075
|
}
|
7987
8076
|
else {
|
7988
8077
|
addError(localize_1.Localizer.Diagnostic.typeVarUnknownParam().format({ name: paramName }), ((_e = argList[i].node) === null || _e === void 0 ? void 0 : _e.name) || argList[i].valueExpression || errorNode);
|
@@ -8028,7 +8117,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8028
8117
|
}
|
8029
8118
|
return typeVar;
|
8030
8119
|
}
|
8031
|
-
function createTypeVarTupleType(errorNode, argList) {
|
8120
|
+
function createTypeVarTupleType(errorNode, classType, argList) {
|
8032
8121
|
var _a, _b;
|
8033
8122
|
let typeVarName = '';
|
8034
8123
|
if (argList.length === 0) {
|
@@ -8054,6 +8143,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8054
8143
|
if (expr) {
|
8055
8144
|
typeVar.details.defaultType = getTypeVarTupleDefaultType(expr);
|
8056
8145
|
}
|
8146
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
8147
|
+
if (!fileInfo.isStubFile &&
|
8148
|
+
fileInfo.executionEnvironment.pythonVersion < pythonVersion_1.PythonVersion.V3_13 &&
|
8149
|
+
classType.details.moduleName !== 'typing_extensions') {
|
8150
|
+
addError(localize_1.Localizer.Diagnostic.typeVarDefaultIllegal(), expr);
|
8151
|
+
}
|
8057
8152
|
}
|
8058
8153
|
else {
|
8059
8154
|
addError(localize_1.Localizer.Diagnostic.typeVarTupleUnknownParam().format({ name: ((_a = argList[i].name) === null || _a === void 0 ? void 0 : _a.value) || '?' }), ((_b = argList[i].node) === null || _b === void 0 ? void 0 : _b.name) || argList[i].valueExpression || errorNode);
|
@@ -8063,7 +8158,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8063
8158
|
return typeVar;
|
8064
8159
|
}
|
8065
8160
|
function getTypeVarTupleDefaultType(node) {
|
8066
|
-
const argType = getTypeOfExpressionExpectingType(node, {
|
8161
|
+
const argType = getTypeOfExpressionExpectingType(node, {
|
8162
|
+
allowUnpackedTuple: true,
|
8163
|
+
allowTypeVarsWithoutScopeId: true,
|
8164
|
+
}).type;
|
8067
8165
|
const isUnpackedTuple = (0, types_1.isClass)(argType) && (0, typeUtils_1.isTupleClass)(argType) && argType.isUnpacked;
|
8068
8166
|
const isUnpackedTypeVarTuple = (0, types_1.isUnpackedVariadicTypeVar)(argType);
|
8069
8167
|
if (!isUnpackedTuple && !isUnpackedTypeVarTuple) {
|
@@ -8072,7 +8170,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8072
8170
|
}
|
8073
8171
|
return (0, typeUtils_1.convertToInstance)(argType);
|
8074
8172
|
}
|
8075
|
-
function createParamSpecType(errorNode, argList) {
|
8173
|
+
function createParamSpecType(errorNode, classType, argList) {
|
8076
8174
|
if (argList.length === 0) {
|
8077
8175
|
addError(localize_1.Localizer.Diagnostic.paramSpecFirstArg(), errorNode);
|
8078
8176
|
return undefined;
|
@@ -8096,6 +8194,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8096
8194
|
if (expr) {
|
8097
8195
|
paramSpec.details.defaultType = getParamSpecDefaultType(expr);
|
8098
8196
|
}
|
8197
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
8198
|
+
if (!fileInfo.isStubFile &&
|
8199
|
+
fileInfo.executionEnvironment.pythonVersion < pythonVersion_1.PythonVersion.V3_13 &&
|
8200
|
+
classType.details.moduleName !== 'typing_extensions') {
|
8201
|
+
addError(localize_1.Localizer.Diagnostic.typeVarDefaultIllegal(), expr);
|
8202
|
+
}
|
8099
8203
|
}
|
8100
8204
|
else {
|
8101
8205
|
addError(localize_1.Localizer.Diagnostic.paramSpecUnknownParam().format({ name: paramName }), paramNameNode || argList[i].valueExpression || errorNode);
|
@@ -8117,7 +8221,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8117
8221
|
}
|
8118
8222
|
if (node.nodeType === 31 /* List */) {
|
8119
8223
|
node.entries.forEach((paramExpr, index) => {
|
8120
|
-
const typeResult = getTypeOfExpressionExpectingType(paramExpr);
|
8224
|
+
const typeResult = getTypeOfExpressionExpectingType(paramExpr, { allowTypeVarsWithoutScopeId: true });
|
8121
8225
|
types_1.FunctionType.addParameter(functionType, {
|
8122
8226
|
category: 0 /* Simple */,
|
8123
8227
|
name: `__p${index}`,
|
@@ -8132,7 +8236,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8132
8236
|
return functionType;
|
8133
8237
|
}
|
8134
8238
|
else {
|
8135
|
-
const typeResult = getTypeOfExpressionExpectingType(node, {
|
8239
|
+
const typeResult = getTypeOfExpressionExpectingType(node, {
|
8240
|
+
allowParamSpec: true,
|
8241
|
+
allowTypeVarsWithoutScopeId: true,
|
8242
|
+
});
|
8136
8243
|
if (typeResult.typeErrors) {
|
8137
8244
|
return undefined;
|
8138
8245
|
}
|
@@ -8144,6 +8251,91 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8144
8251
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.paramSpecDefaultNotTuple(), node);
|
8145
8252
|
return undefined;
|
8146
8253
|
}
|
8254
|
+
// Handles a call to TypeAliasType(). This special form allows a caller to programmatically
|
8255
|
+
// create a type alias as defined in PEP 695. If one or more of the arguments is incorrect,
|
8256
|
+
// it returns undefined so the normal constructor evaluation can be performed (and type errors
|
8257
|
+
// emitted).
|
8258
|
+
function createTypeAliasType(errorNode, argList) {
|
8259
|
+
if (errorNode.nodeType !== 9 /* Call */ || !errorNode.parent || argList.length < 2) {
|
8260
|
+
return undefined;
|
8261
|
+
}
|
8262
|
+
if (errorNode.parent.nodeType !== 3 /* Assignment */ ||
|
8263
|
+
errorNode.parent.rightExpression !== errorNode ||
|
8264
|
+
errorNode.parent.leftExpression.nodeType !== 38 /* Name */) {
|
8265
|
+
addError(localize_1.Localizer.Diagnostic.typeAliasTypeMustBeAssigned(), errorNode);
|
8266
|
+
return undefined;
|
8267
|
+
}
|
8268
|
+
const nameNode = errorNode.parent.leftExpression;
|
8269
|
+
const firstArg = argList[0];
|
8270
|
+
if (firstArg.valueExpression && firstArg.valueExpression.nodeType === 48 /* StringList */) {
|
8271
|
+
const typeAliasName = firstArg.valueExpression.strings.map((s) => s.value).join('');
|
8272
|
+
if (typeAliasName !== nameNode.value) {
|
8273
|
+
addError(localize_1.Localizer.Diagnostic.typeAliasTypeNameMismatch(), firstArg.valueExpression);
|
8274
|
+
}
|
8275
|
+
}
|
8276
|
+
else {
|
8277
|
+
addError(localize_1.Localizer.Diagnostic.typeAliasTypeNameArg(), firstArg.valueExpression || errorNode);
|
8278
|
+
return undefined;
|
8279
|
+
}
|
8280
|
+
let valueExpr;
|
8281
|
+
let typeParamsExpr;
|
8282
|
+
// Parse the remaining parameters.
|
8283
|
+
for (let i = 1; i < argList.length; i++) {
|
8284
|
+
const paramNameNode = argList[i].name;
|
8285
|
+
const paramName = paramNameNode ? paramNameNode.value : undefined;
|
8286
|
+
if (paramName) {
|
8287
|
+
if (paramName === 'type_params' && !typeParamsExpr) {
|
8288
|
+
typeParamsExpr = argList[i].valueExpression;
|
8289
|
+
}
|
8290
|
+
else if (paramName === 'value' && !valueExpr) {
|
8291
|
+
valueExpr = argList[i].valueExpression;
|
8292
|
+
}
|
8293
|
+
else {
|
8294
|
+
return undefined;
|
8295
|
+
}
|
8296
|
+
}
|
8297
|
+
else if (i === 1) {
|
8298
|
+
valueExpr = argList[i].valueExpression;
|
8299
|
+
}
|
8300
|
+
else {
|
8301
|
+
return undefined;
|
8302
|
+
}
|
8303
|
+
}
|
8304
|
+
// The value expression is not optional, so bail if it's not present.
|
8305
|
+
if (!valueExpr) {
|
8306
|
+
return undefined;
|
8307
|
+
}
|
8308
|
+
let typeParameters;
|
8309
|
+
if (typeParamsExpr) {
|
8310
|
+
if (typeParamsExpr.nodeType !== 52 /* Tuple */) {
|
8311
|
+
addError(localize_1.Localizer.Diagnostic.typeAliasTypeParamInvalid(), typeParamsExpr);
|
8312
|
+
return undefined;
|
8313
|
+
}
|
8314
|
+
typeParameters = [];
|
8315
|
+
let isTypeParamListValid = true;
|
8316
|
+
typeParamsExpr.expressions.map((expr) => {
|
8317
|
+
let entryType = getTypeOfExpression(expr, 128 /* ExpectingType */).type;
|
8318
|
+
if ((0, types_1.isTypeVar)(entryType)) {
|
8319
|
+
if (entryType.scopeId) {
|
8320
|
+
isTypeParamListValid = false;
|
8321
|
+
}
|
8322
|
+
else {
|
8323
|
+
entryType = types_1.TypeVarType.cloneForScopeId(entryType, ParseTreeUtils.getScopeIdForNode(nameNode), nameNode.value, 2 /* TypeAlias */);
|
8324
|
+
}
|
8325
|
+
typeParameters.push(entryType);
|
8326
|
+
}
|
8327
|
+
else {
|
8328
|
+
isTypeParamListValid = false;
|
8329
|
+
}
|
8330
|
+
});
|
8331
|
+
if (!isTypeParamListValid) {
|
8332
|
+
addError(localize_1.Localizer.Diagnostic.typeAliasTypeParamInvalid(), typeParamsExpr);
|
8333
|
+
return undefined;
|
8334
|
+
}
|
8335
|
+
}
|
8336
|
+
return getTypeOfTypeAliasCommon(nameNode, nameNode, valueExpr,
|
8337
|
+
/* typeParamNodes */ undefined, () => typeParameters);
|
8338
|
+
}
|
8147
8339
|
function getBooleanValue(node) {
|
8148
8340
|
if (node.nodeType === 11 /* Constant */) {
|
8149
8341
|
if (node.constType === 15 /* False */) {
|
@@ -8190,19 +8382,30 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8190
8382
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.newTypeBadName(), (_a = argList[0].node) !== null && _a !== void 0 ? _a : errorNode);
|
8191
8383
|
return undefined;
|
8192
8384
|
}
|
8193
|
-
|
8194
|
-
|
8195
|
-
|
8196
|
-
|
8197
|
-
|
8198
|
-
else if (baseClass.literalValue !== undefined) {
|
8199
|
-
addError(localize_1.Localizer.Diagnostic.newTypeLiteral(), argList[1].node || errorNode);
|
8385
|
+
let baseClass = getTypeOfArgumentExpectingType(argList[1]).type;
|
8386
|
+
let isBaseClassAny = false;
|
8387
|
+
if ((0, types_1.isAnyOrUnknown)(baseClass)) {
|
8388
|
+
if (objectType && (0, types_1.isClassInstance)(objectType)) {
|
8389
|
+
baseClass = types_1.ClassType.cloneAsInstantiable(objectType);
|
8200
8390
|
}
|
8201
|
-
|
8202
|
-
|
8203
|
-
|
8204
|
-
|
8205
|
-
|
8391
|
+
isBaseClassAny = true;
|
8392
|
+
}
|
8393
|
+
if (!(0, types_1.isInstantiableClass)(baseClass)) {
|
8394
|
+
addError(localize_1.Localizer.Diagnostic.newTypeNotAClass(), argList[1].node || errorNode);
|
8395
|
+
return undefined;
|
8396
|
+
}
|
8397
|
+
if (types_1.ClassType.isProtocolClass(baseClass)) {
|
8398
|
+
addError(localize_1.Localizer.Diagnostic.newTypeProtocolClass(), argList[1].node || errorNode);
|
8399
|
+
}
|
8400
|
+
else if (baseClass.literalValue !== undefined) {
|
8401
|
+
addError(localize_1.Localizer.Diagnostic.newTypeLiteral(), argList[1].node || errorNode);
|
8402
|
+
}
|
8403
|
+
const classFlags = baseClass.details.flags & ~(1 /* BuiltInClass */ | 2 /* SpecialBuiltIn */);
|
8404
|
+
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, classFlags, ParseTreeUtils.getTypeSourceId(errorNode),
|
8405
|
+
/* declaredMetaclass */ undefined, baseClass.details.effectiveMetaclass);
|
8406
|
+
classType.details.baseClasses.push(isBaseClassAny ? types_1.AnyType.create() : baseClass);
|
8407
|
+
(0, typeUtils_1.computeMroLinearization)(classType);
|
8408
|
+
if (!isBaseClassAny) {
|
8206
8409
|
// Synthesize an __init__ method that accepts only the specified type.
|
8207
8410
|
const initType = types_1.FunctionType.createSynthesizedInstance('__init__');
|
8208
8411
|
types_1.FunctionType.addParameter(initType, {
|
@@ -8230,12 +8433,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8230
8433
|
types_1.FunctionType.addDefaultParameters(newType);
|
8231
8434
|
newType.details.declaredReturnType = types_1.ClassType.cloneAsInstance(classType);
|
8232
8435
|
classType.details.fields.set('__new__', symbol_1.Symbol.createWithType(4 /* ClassMember */, newType));
|
8233
|
-
return classType;
|
8234
8436
|
}
|
8235
|
-
|
8236
|
-
addError(localize_1.Localizer.Diagnostic.newTypeNotAClass(), argList[1].node || errorNode);
|
8237
|
-
}
|
8238
|
-
return undefined;
|
8437
|
+
return classType;
|
8239
8438
|
}
|
8240
8439
|
// Implements the semantics of the multi-parameter variant of the "type" call.
|
8241
8440
|
function createType(errorNode, argList) {
|
@@ -8377,7 +8576,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8377
8576
|
return;
|
8378
8577
|
}
|
8379
8578
|
const subtypeResult = useSpeculativeMode(node, () => {
|
8380
|
-
return getTypeOfDictionaryWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype
|
8579
|
+
return getTypeOfDictionaryWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype));
|
8381
8580
|
});
|
8382
8581
|
if (subtypeResult && assignType(subtype, subtypeResult.type)) {
|
8383
8582
|
// If this is the first result we're seeing or it's the first result
|
@@ -8393,7 +8592,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8393
8592
|
let expectedTypeDiagAddendum = undefined;
|
8394
8593
|
if (effectiveExpectedType) {
|
8395
8594
|
expectedTypeDiagAddendum = new diagnostic_1.DiagnosticAddendum();
|
8396
|
-
const result = getTypeOfDictionaryWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType
|
8595
|
+
const result = getTypeOfDictionaryWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType), expectedTypeDiagAddendum);
|
8397
8596
|
if (result) {
|
8398
8597
|
return result;
|
8399
8598
|
}
|
@@ -8450,7 +8649,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8450
8649
|
const expectedValueType = specializedDict.typeArguments[1];
|
8451
8650
|
// Infer the key and value types if possible.
|
8452
8651
|
if (getKeyAndValueTypesFromDictionary(node, keyTypes, valueTypes,
|
8453
|
-
/* forceStrictInference */ true, expectedKeyType, expectedValueType, undefined, expectedDiagAddendum
|
8652
|
+
/* forceStrictInference */ true, expectedKeyType, expectedValueType, undefined, expectedDiagAddendum)) {
|
8454
8653
|
isIncomplete = true;
|
8455
8654
|
}
|
8456
8655
|
// Dict and MutableMapping types have invariant value types, so they
|
@@ -8459,9 +8658,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8459
8658
|
const isValueTypeInvariant = (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
|
8460
8659
|
(types_1.ClassType.isBuiltIn(inferenceContext.expectedType, 'dict') ||
|
8461
8660
|
types_1.ClassType.isBuiltIn(inferenceContext.expectedType, 'MutableMapping'));
|
8462
|
-
const specializedKeyType =
|
8661
|
+
const specializedKeyType = inferTypeArgFromExpectedEntryType((0, typeUtils_1.makeInferenceContext)(expectedKeyType), keyTypes.map((result) => result.type),
|
8463
8662
|
/* isNarrowable */ false);
|
8464
|
-
const specializedValueType =
|
8663
|
+
const specializedValueType = inferTypeArgFromExpectedEntryType((0, typeUtils_1.makeInferenceContext)(expectedValueType), valueTypes.map((result) => result.type), !isValueTypeInvariant);
|
8465
8664
|
if (!specializedKeyType || !specializedValueType) {
|
8466
8665
|
return undefined;
|
8467
8666
|
}
|
@@ -8518,7 +8717,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8518
8717
|
}
|
8519
8718
|
return { type, isIncomplete };
|
8520
8719
|
}
|
8521
|
-
function getKeyAndValueTypesFromDictionary(node, keyTypes, valueTypes, forceStrictInference, expectedKeyType, expectedValueType, expectedTypedDictEntries, expectedDiagAddendum
|
8720
|
+
function getKeyAndValueTypesFromDictionary(node, keyTypes, valueTypes, forceStrictInference, expectedKeyType, expectedValueType, expectedTypedDictEntries, expectedDiagAddendum) {
|
8522
8721
|
let isIncomplete = false;
|
8523
8722
|
// Infer the key and value types if possible.
|
8524
8723
|
node.entries.forEach((entryNode, index) => {
|
@@ -8526,7 +8725,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8526
8725
|
let addUnknown = true;
|
8527
8726
|
if (entryNode.nodeType === 17 /* DictionaryKeyEntry */) {
|
8528
8727
|
const keyTypeResult = getTypeOfExpression(entryNode.keyExpression,
|
8529
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedKeyType !== null && expectedKeyType !== void 0 ? expectedKeyType : (forceStrictInference ? types_1.NeverType.createNever() : undefined)
|
8728
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedKeyType !== null && expectedKeyType !== void 0 ? expectedKeyType : (forceStrictInference ? types_1.NeverType.createNever() : undefined)));
|
8530
8729
|
if (keyTypeResult.isIncomplete) {
|
8531
8730
|
isIncomplete = true;
|
8532
8731
|
}
|
@@ -8545,12 +8744,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8545
8744
|
expectedTypedDictEntries.has(keyType.literalValue)) {
|
8546
8745
|
const effectiveValueType = expectedTypedDictEntries.get(keyType.literalValue).valueType;
|
8547
8746
|
valueTypeResult = getTypeOfExpression(entryNode.valueExpression,
|
8548
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(effectiveValueType
|
8747
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(effectiveValueType));
|
8549
8748
|
}
|
8550
8749
|
else {
|
8551
8750
|
const effectiveValueType = expectedValueType !== null && expectedValueType !== void 0 ? expectedValueType : (forceStrictInference ? types_1.NeverType.createNever() : undefined);
|
8552
8751
|
valueTypeResult = getTypeOfExpression(entryNode.valueExpression,
|
8553
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(effectiveValueType
|
8752
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(effectiveValueType));
|
8554
8753
|
}
|
8555
8754
|
if (expectedDiagAddendum && valueTypeResult.expectedTypeDiagAddendum) {
|
8556
8755
|
expectedDiagAddendum.addAddendum(valueTypeResult.expectedTypeDiagAddendum);
|
@@ -8574,7 +8773,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8574
8773
|
}
|
8575
8774
|
}
|
8576
8775
|
const unexpandedTypeResult = getTypeOfExpression(entryNode.expandExpression,
|
8577
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedType
|
8776
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedType));
|
8578
8777
|
if (unexpandedTypeResult.isIncomplete) {
|
8579
8778
|
isIncomplete = true;
|
8580
8779
|
}
|
@@ -8661,7 +8860,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8661
8860
|
return;
|
8662
8861
|
}
|
8663
8862
|
const subtypeResult = useSpeculativeMode(node, () => {
|
8664
|
-
return getTypeOfListOrSetWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype
|
8863
|
+
return getTypeOfListOrSetWithContext(node, (0, typeUtils_1.makeInferenceContext)(subtype));
|
8665
8864
|
});
|
8666
8865
|
if (subtypeResult && assignType(subtype, subtypeResult.type)) {
|
8667
8866
|
// If this is the first result we're seeing or it's the first result
|
@@ -8676,7 +8875,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8676
8875
|
}
|
8677
8876
|
let expectedTypeDiagAddendum;
|
8678
8877
|
if (effectiveExpectedType) {
|
8679
|
-
const result = getTypeOfListOrSetWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType
|
8878
|
+
const result = getTypeOfListOrSetWithContext(node, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType));
|
8680
8879
|
if (result && !result.typeErrors) {
|
8681
8880
|
return result;
|
8682
8881
|
}
|
@@ -8718,7 +8917,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8718
8917
|
}
|
8719
8918
|
else {
|
8720
8919
|
entryTypeResult = getTypeOfExpression(entry,
|
8721
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedEntryType
|
8920
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedEntryType));
|
8722
8921
|
}
|
8723
8922
|
entryTypes.push(entryTypeResult.type);
|
8724
8923
|
if (entryTypeResult.isIncomplete) {
|
@@ -8736,7 +8935,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8736
8935
|
});
|
8737
8936
|
const isTypeInvariant = (0, types_1.isClassInstance)(inferenceContext.expectedType) &&
|
8738
8937
|
types_1.ClassType.isBuiltIn(inferenceContext.expectedType, builtInClassName);
|
8739
|
-
const specializedEntryType =
|
8938
|
+
const specializedEntryType = inferTypeArgFromExpectedEntryType((0, typeUtils_1.makeInferenceContext)(expectedEntryType), entryTypes, !isTypeInvariant);
|
8740
8939
|
if (!specializedEntryType) {
|
8741
8940
|
return { type: types_1.UnknownType.create(), isIncomplete, typeErrors: true, expectedTypeDiagAddendum };
|
8742
8941
|
}
|
@@ -8819,17 +9018,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8819
9018
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, message + diag.getString(), entry);
|
8820
9019
|
}
|
8821
9020
|
}
|
8822
|
-
function
|
8823
|
-
var _a;
|
9021
|
+
function inferTypeArgFromExpectedEntryType(inferenceContext, entryTypes, isNarrowable) {
|
8824
9022
|
// If the expected type is Any, the resulting type becomes Any.
|
8825
9023
|
if ((0, types_1.isAnyOrUnknown)(inferenceContext.expectedType)) {
|
8826
9024
|
return inferenceContext.expectedType;
|
8827
9025
|
}
|
8828
|
-
const typeVarContext =
|
9026
|
+
const typeVarContext = new typeVarContext_1.TypeVarContext();
|
9027
|
+
const expectedType = inferenceContext.expectedType;
|
8829
9028
|
let isCompatible = true;
|
8830
9029
|
entryTypes.forEach((entryType) => {
|
8831
|
-
if (isCompatible &&
|
8832
|
-
!assignType(inferenceContext.expectedType, entryType, /* diag */ undefined, typeVarContext)) {
|
9030
|
+
if (isCompatible && !assignType(expectedType, entryType, /* diag */ undefined, typeVarContext)) {
|
8833
9031
|
isCompatible = false;
|
8834
9032
|
}
|
8835
9033
|
});
|
@@ -8842,10 +9040,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8842
9040
|
? combinedTypes
|
8843
9041
|
: stripLiteralValue(combinedTypes);
|
8844
9042
|
}
|
8845
|
-
|
8846
|
-
return (0, typeUtils_1.applySolvedTypeVars)(inferenceContext.expectedType, typeVarContext);
|
8847
|
-
}
|
8848
|
-
return inferenceContext.expectedType;
|
9043
|
+
return (0, typeUtils_1.applySolvedTypeVars)(inferenceContext.expectedType, typeVarContext, { applyInScopePlaceholders: true });
|
8849
9044
|
}
|
8850
9045
|
function getTypeOfYield(node) {
|
8851
9046
|
let expectedYieldType;
|
@@ -8988,18 +9183,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8988
9183
|
type: types_1.UnknownType.create(),
|
8989
9184
|
});
|
8990
9185
|
}
|
8991
|
-
|
9186
|
+
const expectedReturnType = expectedFunctionType
|
8992
9187
|
? getFunctionEffectiveReturnType(expectedFunctionType)
|
8993
9188
|
: undefined;
|
8994
|
-
if (expectedReturnType && (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.typeVarContext)) {
|
8995
|
-
expectedReturnType = (0, typeUtils_1.applySolvedTypeVars)(expectedReturnType, inferenceContext.typeVarContext);
|
8996
|
-
}
|
8997
9189
|
// If we're speculatively evaluating the lambda, create another speculative
|
8998
9190
|
// evaluation scope for the return expression and do not allow retention
|
8999
9191
|
// of the cached types.
|
9000
9192
|
const inferLambdaReturnType = () => {
|
9001
9193
|
const returnTypeResult = getTypeOfExpression(node.expression,
|
9002
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedReturnType
|
9194
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedReturnType));
|
9003
9195
|
functionType.inferredReturnType = returnTypeResult.type;
|
9004
9196
|
if (returnTypeResult.isIncomplete) {
|
9005
9197
|
isIncomplete = true;
|
@@ -9946,7 +10138,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9946
10138
|
names: boundTypeVars.map((t) => `${t.details.name}`).join(', '),
|
9947
10139
|
}), errorNode);
|
9948
10140
|
}
|
9949
|
-
return types_1.TypeBase.cloneForTypeAlias(type, name.value,
|
10141
|
+
return types_1.TypeBase.cloneForTypeAlias(type, name.value, ParseTreeUtils.getClassFullName(name, fileInfo.moduleName, name.value), typeAliasScopeId, typeParameters.length > 0 ? typeParameters : undefined);
|
9950
10142
|
}
|
9951
10143
|
function createSpecialBuiltInClass(node, assignedName, aliasMapEntry) {
|
9952
10144
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
@@ -10190,7 +10382,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10190
10382
|
if (assignType(declaredType, srcType)) {
|
10191
10383
|
// Narrow the resulting type if possible.
|
10192
10384
|
if (!(0, types_1.isAnyOrUnknown)(srcType)) {
|
10193
|
-
srcType = narrowTypeBasedOnAssignment(declaredType, srcType);
|
10385
|
+
srcType = narrowTypeBasedOnAssignment(node, declaredType, srcType);
|
10194
10386
|
}
|
10195
10387
|
}
|
10196
10388
|
}
|
@@ -10260,52 +10452,60 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10260
10452
|
// variables since they use normal variable assignment syntax.
|
10261
10453
|
function getTypeOfTypeAlias(node) {
|
10262
10454
|
var _a;
|
10263
|
-
|
10455
|
+
return getTypeOfTypeAliasCommon(node, node.name, node.expression, (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.parameters, () => {
|
10456
|
+
let typeParameters = [];
|
10457
|
+
if (node.typeParameters) {
|
10458
|
+
typeParameters = evaluateTypeParameterList(node.typeParameters);
|
10459
|
+
}
|
10460
|
+
return typeParameters;
|
10461
|
+
});
|
10462
|
+
}
|
10463
|
+
// This function is common to the handling of "type" statements and explicit
|
10464
|
+
// calls to the TypeAliasType constructor.
|
10465
|
+
function getTypeOfTypeAliasCommon(declNode, nameNode, valueNode, typeParamNodes, getTypeParamCallback) {
|
10466
|
+
const cachedType = readTypeCache(nameNode, 0 /* None */);
|
10264
10467
|
if (cachedType) {
|
10265
10468
|
return cachedType;
|
10266
10469
|
}
|
10267
10470
|
// Synthesize a type variable that represents the type alias while we're
|
10268
10471
|
// evaluating it. This allows us to handle recursive definitions.
|
10269
|
-
const typeAliasTypeVar = types_1.TypeVarType.createInstantiable(`__type_alias_${
|
10472
|
+
const typeAliasTypeVar = types_1.TypeVarType.createInstantiable(`__type_alias_${nameNode.value}`);
|
10270
10473
|
typeAliasTypeVar.details.isSynthesized = true;
|
10271
|
-
typeAliasTypeVar.details.recursiveTypeAliasName =
|
10272
|
-
const scopeId = ParseTreeUtils.getScopeIdForNode(
|
10474
|
+
typeAliasTypeVar.details.recursiveTypeAliasName = nameNode.value;
|
10475
|
+
const scopeId = ParseTreeUtils.getScopeIdForNode(nameNode);
|
10273
10476
|
typeAliasTypeVar.details.recursiveTypeAliasScopeId = scopeId;
|
10274
10477
|
typeAliasTypeVar.scopeId = scopeId;
|
10275
10478
|
// Write the type to the type cache. It will be replaced below.
|
10276
|
-
writeTypeCache(
|
10479
|
+
writeTypeCache(nameNode, { type: typeAliasTypeVar }, /* flags */ undefined);
|
10277
10480
|
// Set a partial type to handle recursive (self-referential) type aliases.
|
10278
|
-
const scope = ScopeUtils.getScopeForNode(
|
10279
|
-
const typeAliasSymbol = scope === null || scope === void 0 ? void 0 : scope.lookUpSymbolRecursive(
|
10280
|
-
const typeAliasDecl = AnalyzerNodeInfo.getDeclaration(
|
10481
|
+
const scope = ScopeUtils.getScopeForNode(declNode);
|
10482
|
+
const typeAliasSymbol = scope === null || scope === void 0 ? void 0 : scope.lookUpSymbolRecursive(nameNode.value);
|
10483
|
+
const typeAliasDecl = AnalyzerNodeInfo.getDeclaration(declNode);
|
10281
10484
|
if (typeAliasDecl && typeAliasSymbol) {
|
10282
10485
|
setSymbolResolutionPartialType(typeAliasSymbol.symbol, typeAliasDecl, typeAliasTypeVar);
|
10283
10486
|
}
|
10284
|
-
|
10285
|
-
|
10286
|
-
|
10287
|
-
|
10288
|
-
}
|
10289
|
-
if (!isLegalTypeAliasExpressionForm(node.expression)) {
|
10290
|
-
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.expression);
|
10487
|
+
const typeParameters = getTypeParamCallback();
|
10488
|
+
typeAliasTypeVar.details.recursiveTypeParameters = typeParameters;
|
10489
|
+
if (!isLegalTypeAliasExpressionForm(valueNode)) {
|
10490
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(valueNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), valueNode);
|
10291
10491
|
}
|
10292
|
-
const aliasTypeResult = getTypeOfExpressionExpectingType(
|
10492
|
+
const aliasTypeResult = getTypeOfExpressionExpectingType(valueNode, { allowForwardReference: true });
|
10293
10493
|
let isIncomplete = false;
|
10294
10494
|
let aliasType = aliasTypeResult.type;
|
10295
10495
|
if (aliasTypeResult.isIncomplete) {
|
10296
10496
|
isIncomplete = true;
|
10297
10497
|
}
|
10298
|
-
aliasType = transformTypeForTypeAlias(aliasType,
|
10498
|
+
aliasType = transformTypeForTypeAlias(aliasType, nameNode, valueNode, typeParameters, typeParamNodes);
|
10299
10499
|
if ((0, typeUtils_1.isTypeAliasRecursive)(typeAliasTypeVar, aliasType)) {
|
10300
|
-
addDiagnostic(AnalyzerNodeInfo.getFileInfo(
|
10301
|
-
name:
|
10302
|
-
}),
|
10500
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(valueNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIsRecursiveDirect().format({
|
10501
|
+
name: nameNode.value,
|
10502
|
+
}), valueNode);
|
10303
10503
|
aliasType = types_1.UnknownType.create();
|
10304
10504
|
}
|
10305
10505
|
// Set the resulting type to the boundType of the original type alias
|
10306
10506
|
// to support recursive type aliases.
|
10307
10507
|
typeAliasTypeVar.details.boundType = aliasType;
|
10308
|
-
writeTypeCache(
|
10508
|
+
writeTypeCache(nameNode, { type: aliasType, isIncomplete }, 0 /* None */);
|
10309
10509
|
return aliasType;
|
10310
10510
|
}
|
10311
10511
|
function evaluateTypesForAugmentedAssignment(node) {
|
@@ -11005,7 +11205,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11005
11205
|
decoratorCallType.details.builtInName === 'dataclass_transform') {
|
11006
11206
|
originalClassType.details.classDataClassTransform = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
|
11007
11207
|
}
|
11008
|
-
else if (decoratorCallType.details.
|
11208
|
+
else if (decoratorCallType.details.builtInName === 'deprecated') {
|
11009
11209
|
originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
11010
11210
|
return inputClassType;
|
11011
11211
|
}
|
@@ -11830,7 +12030,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11830
12030
|
undecoratedType.details.decoratorDataClassBehaviors = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
|
11831
12031
|
return inputFunctionType;
|
11832
12032
|
}
|
11833
|
-
if (decoratorCallType.details.
|
12033
|
+
if (decoratorCallType.details.builtInName === 'deprecated') {
|
11834
12034
|
undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
11835
12035
|
return inputFunctionType;
|
11836
12036
|
}
|
@@ -13448,8 +13648,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13448
13648
|
}
|
13449
13649
|
function getTypeOfExpressionExpectingType(node, options) {
|
13450
13650
|
let flags = 128 /* ExpectingType */ | 8 /* EvaluateStringLiteralAsType */ | 131072 /* DisallowClassVar */;
|
13651
|
+
if (!(options === null || options === void 0 ? void 0 : options.allowTypeVarsWithoutScopeId)) {
|
13652
|
+
flags |= 4096 /* DisallowTypeVarsWithoutScopeId */;
|
13653
|
+
}
|
13451
13654
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
13452
|
-
if (fileInfo.isStubFile) {
|
13655
|
+
if (fileInfo.isStubFile || (options === null || options === void 0 ? void 0 : options.allowForwardReference)) {
|
13453
13656
|
flags |= 4 /* AllowForwardReferences */;
|
13454
13657
|
}
|
13455
13658
|
else {
|
@@ -16840,7 +17043,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16840
17043
|
}
|
16841
17044
|
// Apply any solved source TypeVars to the dest TypeVar solutions. This
|
16842
17045
|
// allows for higher-order functions to accept generic callbacks.
|
16843
|
-
|
17046
|
+
(0, typeUtils_1.applySourceContextTypeVars)(destTypeVarContext, srcTypeVarContext);
|
16844
17047
|
return canAssign;
|
16845
17048
|
}
|
16846
17049
|
// If the declaredType contains type arguments that are "Any" and
|
@@ -16848,7 +17051,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16848
17051
|
// replace that type argument in the assigned type. This function assumes
|
16849
17052
|
// that the caller has already verified that the assignedType is assignable
|
16850
17053
|
// to the declaredType.
|
16851
|
-
function replaceTypeArgsWithAny(declaredType, assignedType, recursionCount = 0) {
|
17054
|
+
function replaceTypeArgsWithAny(node, declaredType, assignedType, recursionCount = 0) {
|
16852
17055
|
if (recursionCount > types_1.maxTypeRecursionCount) {
|
16853
17056
|
return undefined;
|
16854
17057
|
}
|
@@ -16863,7 +17066,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16863
17066
|
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(assignedType));
|
16864
17067
|
(0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluatorInterface, types_1.ClassType.cloneForSpecialization(assignedType,
|
16865
17068
|
/* typeArguments */ undefined,
|
16866
|
-
/* isTypeArgumentExplicit */ false), declaredType, typeVarContext,
|
17069
|
+
/* isTypeArgumentExplicit */ false), declaredType, typeVarContext, ParseTreeUtils.getTypeVarScopesForNode(node));
|
16867
17070
|
let replacedTypeArg = false;
|
16868
17071
|
const newTypeArgs = assignedType.typeArguments.map((typeArg, index) => {
|
16869
17072
|
const typeParam = assignedType.details.typeParameters[index];
|
@@ -16875,7 +17078,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16875
17078
|
}
|
16876
17079
|
else if ((0, types_1.isClassInstance)(expectedTypeArgType) && (0, types_1.isClassInstance)(typeArg)) {
|
16877
17080
|
// Recursively replace Any in the type argument.
|
16878
|
-
const recursiveReplacement = replaceTypeArgsWithAny(expectedTypeArgType, typeArg, recursionCount);
|
17081
|
+
const recursiveReplacement = replaceTypeArgsWithAny(node, expectedTypeArgType, typeArg, recursionCount);
|
16879
17082
|
if (recursiveReplacement) {
|
16880
17083
|
replacedTypeArg = true;
|
16881
17084
|
return recursiveReplacement;
|
@@ -16892,7 +17095,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16892
17095
|
}
|
16893
17096
|
// When a value is assigned to a variable with a declared type,
|
16894
17097
|
// we may be able to narrow the type based on the assignment.
|
16895
|
-
function narrowTypeBasedOnAssignment(declaredType, assignedType) {
|
17098
|
+
function narrowTypeBasedOnAssignment(node, declaredType, assignedType) {
|
16896
17099
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(assignedType, (assignedSubtype) => {
|
16897
17100
|
const narrowedSubtype = (0, typeUtils_1.mapSubtypes)(declaredType, (declaredSubtype) => {
|
16898
17101
|
// We can't narrow "Any".
|
@@ -16903,13 +17106,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16903
17106
|
// If the source is generic and has unspecified type arguments,
|
16904
17107
|
// see if we can determine then based on the declared type.
|
16905
17108
|
if ((0, types_1.isInstantiableClass)(declaredSubtype) && (0, types_1.isInstantiableClass)(assignedSubtype)) {
|
16906
|
-
const result = replaceTypeArgsWithAny(declaredSubtype, assignedSubtype);
|
17109
|
+
const result = replaceTypeArgsWithAny(node, declaredSubtype, assignedSubtype);
|
16907
17110
|
if (result) {
|
16908
17111
|
assignedSubtype = result;
|
16909
17112
|
}
|
16910
17113
|
}
|
16911
17114
|
else if ((0, types_1.isClassInstance)(declaredSubtype) && (0, types_1.isClassInstance)(assignedSubtype)) {
|
16912
|
-
const result = replaceTypeArgsWithAny(types_1.ClassType.cloneAsInstantiable(declaredSubtype), types_1.ClassType.cloneAsInstantiable(assignedSubtype));
|
17115
|
+
const result = replaceTypeArgsWithAny(node, types_1.ClassType.cloneAsInstantiable(declaredSubtype), types_1.ClassType.cloneAsInstantiable(assignedSubtype));
|
16913
17116
|
if (result) {
|
16914
17117
|
assignedSubtype = types_1.ClassType.cloneAsInstance(result);
|
16915
17118
|
}
|
@@ -17374,6 +17577,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17374
17577
|
return types_1.FunctionType.clone(memberType, /* stripFirstParam */ true);
|
17375
17578
|
}
|
17376
17579
|
if (types_1.FunctionType.isInstanceMethod(memberType)) {
|
17580
|
+
// If the baseType is a metaclass, don't specialize the function.
|
17581
|
+
if ((0, typeUtils_1.isInstantiableMetaclass)(baseType)) {
|
17582
|
+
return memberType;
|
17583
|
+
}
|
17377
17584
|
const baseObj = (0, types_1.isClassInstance)(baseType)
|
17378
17585
|
? baseType
|
17379
17586
|
: types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeClassType)(baseType));
|
@@ -17614,6 +17821,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17614
17821
|
function printControlFlowGraph(flowNode, reference, callName, logger) {
|
17615
17822
|
return codeFlowEngine.printControlFlowGraph(flowNode, reference, callName, logger);
|
17616
17823
|
}
|
17824
|
+
function printTypeVarContext(typeVarContext) {
|
17825
|
+
const contexts = typeVarContext.getSignatureContexts();
|
17826
|
+
contexts.forEach((context, index) => {
|
17827
|
+
if (contexts.length > 1) {
|
17828
|
+
console.log(`Signature context ${index + 1}:`);
|
17829
|
+
}
|
17830
|
+
context.getTypeVars().forEach((typeVarEntry) => {
|
17831
|
+
const type = context.getTypeVarType(typeVarEntry.typeVar);
|
17832
|
+
console.log(` ${typeVarEntry.typeVar.details.name}: ${type ? printType(type) : '<none>'}`);
|
17833
|
+
});
|
17834
|
+
});
|
17835
|
+
}
|
17617
17836
|
const evaluatorInterface = {
|
17618
17837
|
runWithCancellationToken,
|
17619
17838
|
getType,
|
@@ -17707,6 +17926,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17707
17926
|
setTypeForNode,
|
17708
17927
|
checkForCancellation,
|
17709
17928
|
printControlFlowGraph,
|
17929
|
+
printTypeVarContext,
|
17710
17930
|
};
|
17711
17931
|
const codeFlowEngine = (0, codeFlowEngine_1.getCodeFlowEngine)(evaluatorInterface, speculativeTypeTracker);
|
17712
17932
|
return evaluatorInterface;
|