@zzzen/pyright-internal 1.2.0-dev.20240602 → 1.2.0-dev.20240616
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/binder.d.ts +3 -1
- package/dist/analyzer/binder.js +23 -11
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +50 -33
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +27 -38
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructorTransform.js +1 -1
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.js +2 -7
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.d.ts +0 -1
- package/dist/analyzer/dataClasses.js +52 -78
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/decorators.d.ts +2 -1
- package/dist/analyzer/decorators.js +34 -39
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/docStringUtils.d.ts +0 -1
- package/dist/analyzer/docStringUtils.js +1 -6
- package/dist/analyzer/docStringUtils.js.map +1 -1
- package/dist/analyzer/enums.d.ts +1 -1
- package/dist/analyzer/enums.js +20 -17
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/importResolver.js +11 -9
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/namedTuples.js +3 -3
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +6 -7
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/parameterUtils.js +1 -13
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.d.ts +1 -1
- package/dist/analyzer/patternMatching.js +4 -4
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/properties.js +4 -4
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +16 -5
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/service.js +27 -48
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.d.ts +2 -1
- package/dist/analyzer/sourceFile.js +7 -5
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/typeDocStringUtils.d.ts +1 -0
- package/dist/analyzer/typeDocStringUtils.js +8 -1
- package/dist/analyzer/typeDocStringUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +746 -667
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +2 -3
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +3 -2
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.js +25 -16
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +3 -5
- package/dist/analyzer/typeUtils.js +96 -144
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.js +4 -0
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typedDicts.js +6 -6
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +51 -54
- package/dist/analyzer/types.js +221 -260
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +1 -1
- package/dist/backgroundAnalysisBase.js +3 -3
- package/dist/commands/dumpFileDebugInfoCommand.js +17 -25
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/cancellationUtils.d.ts +1 -0
- package/dist/common/cancellationUtils.js +15 -1
- package/dist/common/cancellationUtils.js.map +1 -1
- package/dist/common/configOptions.d.ts +2 -1
- package/dist/common/configOptions.js +13 -4
- package/dist/common/configOptions.js.map +1 -1
- package/dist/common/docStringService.d.ts +15 -0
- package/dist/common/docStringService.js +38 -0
- package/dist/common/docStringService.js.map +1 -0
- package/dist/common/envVarUtils.js +3 -1
- package/dist/common/envVarUtils.js.map +1 -1
- package/dist/common/extensibility.d.ts +1 -7
- package/dist/common/extensibility.js.map +1 -1
- package/dist/common/languageServerInterface.d.ts +1 -2
- package/dist/common/languageServerInterface.js.map +1 -1
- package/dist/common/realFileSystem.d.ts +3 -3
- package/dist/common/realFileSystem.js +6 -6
- package/dist/common/realFileSystem.js.map +1 -1
- package/dist/common/serviceKeys.d.ts +2 -0
- package/dist/common/serviceKeys.js +1 -0
- package/dist/common/serviceKeys.js.map +1 -1
- package/dist/common/serviceProvider.js +3 -0
- package/dist/common/serviceProvider.js.map +1 -1
- package/dist/common/serviceProviderExtensions.d.ts +2 -0
- package/dist/common/serviceProviderExtensions.js +8 -0
- package/dist/common/serviceProviderExtensions.js.map +1 -1
- package/dist/common/stringUtils.d.ts +1 -0
- package/dist/common/stringUtils.js +5 -1
- package/dist/common/stringUtils.js.map +1 -1
- package/dist/common/uri/uriUtils.d.ts +1 -1
- package/dist/common/uri/uriUtils.js +1 -1
- package/dist/common/uri/uriUtils.js.map +1 -1
- package/dist/languageServerBase.d.ts +1 -1
- package/dist/languageServerBase.js +3 -3
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/completionProvider.d.ts +1 -0
- package/dist/languageService/completionProvider.js +16 -7
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/completionProviderUtils.d.ts +2 -1
- package/dist/languageService/completionProviderUtils.js +6 -4
- package/dist/languageService/completionProviderUtils.js.map +1 -1
- package/dist/languageService/definitionProvider.d.ts +2 -1
- package/dist/languageService/definitionProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.d.ts +2 -1
- package/dist/languageService/hoverProvider.js +7 -5
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/signatureHelpProvider.d.ts +3 -1
- package/dist/languageService/signatureHelpProvider.js +8 -6
- package/dist/languageService/signatureHelpProvider.js.map +1 -1
- package/dist/languageService/workspaceSymbolProvider.js +1 -1
- package/dist/localization/localize.d.ts +6 -0
- package/dist/localization/localize.js +3 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.cs.json +4 -3
- package/dist/localization/package.nls.de.json +4 -3
- package/dist/localization/package.nls.en-us.json +3 -0
- package/dist/localization/package.nls.es.json +3 -2
- package/dist/localization/package.nls.fr.json +4 -3
- package/dist/localization/package.nls.it.json +4 -3
- package/dist/localization/package.nls.ja.json +4 -3
- package/dist/localization/package.nls.ko.json +5 -4
- package/dist/localization/package.nls.pl.json +4 -3
- package/dist/localization/package.nls.pt-br.json +4 -3
- package/dist/localization/package.nls.qps-ploc.json +4 -3
- package/dist/localization/package.nls.ru.json +4 -3
- package/dist/localization/package.nls.tr.json +4 -3
- package/dist/localization/package.nls.zh-cn.json +4 -3
- package/dist/localization/package.nls.zh-tw.json +4 -3
- package/dist/pyright.js +3 -0
- package/dist/pyright.js.map +1 -1
- package/dist/tests/checker.test.js +8 -0
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/config.test.js +3 -3
- package/dist/tests/config.test.js.map +1 -1
- package/dist/tests/diagnosticOverrides.test.js +7 -6
- package/dist/tests/diagnosticOverrides.test.js.map +1 -1
- package/dist/tests/docStringConversion.test.d.ts +2 -1
- package/dist/tests/docStringConversion.test.js +300 -293
- package/dist/tests/docStringConversion.test.js.map +1 -1
- package/dist/tests/fourslash/rename.externallyHidden.fourslash.js +2 -2
- package/dist/tests/fourslash/rename.externallyHidden.fourslash.js.map +1 -1
- package/dist/tests/fourslash/rename.externallyHidden.params.fourslash.js +1 -1
- package/dist/tests/fourslash/rename.externallyHidden.params.fourslash.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.js +2 -1
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/pathUtils.test.js +6 -0
- package/dist/tests/pathUtils.test.js.map +1 -1
- package/dist/tests/signatureHelp.test.js +2 -1
- package/dist/tests/signatureHelp.test.js.map +1 -1
- package/dist/tests/testState.test.js +1 -1
- package/dist/tests/testState.test.js.map +1 -1
- package/dist/tests/testUtils.js +1 -1
- package/dist/tests/testUtils.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +4 -0
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +8 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +7 -3
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +15 -9
- package/dist/tests/typeEvaluator7.test.js.map +1 -1
- package/dist/tests/typePrinter.test.js +2 -2
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/dist/tests/uri.test.js +6 -0
- package/dist/tests/uri.test.js.map +1 -1
- package/dist/workspaceFactory.d.ts +1 -1
- package/package.json +1 -1
- package/dist/tests/docStringUtils.test.d.ts +0 -1
- package/dist/tests/docStringUtils.test.js +0 -74
- package/dist/tests/docStringUtils.test.js.map +0 -1
@@ -466,7 +466,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
466
466
|
functionClass = getBuiltInType(node, 'function');
|
467
467
|
unionTypeClass = getTypesType(node, 'UnionType');
|
468
468
|
if (unionTypeClass && (0, types_1.isClass)(unionTypeClass)) {
|
469
|
-
unionTypeClass.details.flags |=
|
469
|
+
unionTypeClass.details.flags |= 8388608 /* ClassTypeFlags.SpecialFormClass */;
|
470
470
|
}
|
471
471
|
// Initialize and cache "Collection" to break a cyclical dependency
|
472
472
|
// that occurs when resolving tuple below.
|
@@ -487,7 +487,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
487
487
|
}
|
488
488
|
// Wire up the `Any` class to the special-form version of our internal AnyType.
|
489
489
|
if (objectClass && (0, types_1.isInstantiableClass)(objectClass) && typeClass && (0, types_1.isInstantiableClass)(typeClass)) {
|
490
|
-
const anyClass = types_1.ClassType.createInstantiable('Any', 'typing.Any', 'typing', uri_1.Uri.empty(), 1 /* ClassTypeFlags.BuiltInClass */ |
|
490
|
+
const anyClass = types_1.ClassType.createInstantiable('Any', 'typing.Any', 'typing', uri_1.Uri.empty(), 1 /* ClassTypeFlags.BuiltInClass */ | 8388608 /* ClassTypeFlags.SpecialFormClass */,
|
491
491
|
/* typeSourceId */ -1,
|
492
492
|
/* declaredMetaclass */ undefined,
|
493
493
|
/* effectiveMetaclass */ typeClass);
|
@@ -679,7 +679,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
679
679
|
}
|
680
680
|
case 3 /* ParseNodeType.Assignment */: {
|
681
681
|
typeResult = getTypeOfExpression(node.rightExpression, flags, inferenceContext, signatureTracker);
|
682
|
-
assignTypeToExpression(node.leftExpression, typeResult
|
682
|
+
assignTypeToExpression(node.leftExpression, typeResult, node.rightExpression,
|
683
683
|
/* ignoreEmptyContainers */ true,
|
684
684
|
/* allowAssignmentToFinalVar */ true);
|
685
685
|
break;
|
@@ -689,8 +689,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
689
689
|
addError(localize_1.LocMessage.walrusNotAllowed(), node);
|
690
690
|
}
|
691
691
|
typeResult = getTypeOfExpression(node.rightExpression, flags, inferenceContext, signatureTracker);
|
692
|
-
assignTypeToExpression(node.name, typeResult
|
693
|
-
/* ignoreEmptyContainers */ true);
|
692
|
+
assignTypeToExpression(node.name, typeResult, node.rightExpression, /* ignoreEmptyContainers */ true);
|
694
693
|
break;
|
695
694
|
}
|
696
695
|
case 60 /* ParseNodeType.Yield */: {
|
@@ -741,6 +740,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
741
740
|
if (expectingInstantiable) {
|
742
741
|
validateTypeIsInstantiable(typeResult, flags, node);
|
743
742
|
}
|
743
|
+
// If this is a PEP 695 type alias, remove the special form so the type
|
744
|
+
// printer prints it as its aliased type rather than TypeAliasType.
|
745
|
+
if ((flags & 256 /* EvaluatorFlags.ExpectingTypeAnnotation */) !== 0) {
|
746
|
+
if (typeResult.type.specialForm && types_1.ClassType.isBuiltIn(typeResult.type.specialForm, 'TypeAliasType')) {
|
747
|
+
typeResult.type = types_1.TypeBase.cloneAsSpecialForm(typeResult.type, undefined);
|
748
|
+
}
|
749
|
+
}
|
744
750
|
return typeResult;
|
745
751
|
}
|
746
752
|
// Reports the case where a function or class has been decorated with
|
@@ -1271,9 +1277,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
1271
1277
|
const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
|
1272
1278
|
if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
1273
1279
|
if (concreteSubtype.literalValue !== undefined) {
|
1280
|
+
let isLiteralFalsy;
|
1281
|
+
if (concreteSubtype.literalValue instanceof types_1.EnumLiteral) {
|
1282
|
+
isLiteralFalsy = !canBeTruthy(concreteSubtype);
|
1283
|
+
}
|
1284
|
+
else {
|
1285
|
+
isLiteralFalsy = !concreteSubtype.literalValue;
|
1286
|
+
}
|
1274
1287
|
// If the object is already definitely falsy, it's fine to
|
1275
1288
|
// include, otherwise it should be removed.
|
1276
|
-
return
|
1289
|
+
return isLiteralFalsy ? subtype : undefined;
|
1277
1290
|
}
|
1278
1291
|
// If the object is a bool, make it "false", since
|
1279
1292
|
// "true" is a truthy value.
|
@@ -1307,9 +1320,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
1307
1320
|
const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
|
1308
1321
|
if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
1309
1322
|
if (concreteSubtype.literalValue !== undefined) {
|
1323
|
+
let isLiteralTruthy;
|
1324
|
+
if (concreteSubtype.literalValue instanceof types_1.EnumLiteral) {
|
1325
|
+
isLiteralTruthy = !canBeFalsy(concreteSubtype);
|
1326
|
+
}
|
1327
|
+
else {
|
1328
|
+
isLiteralTruthy = !!concreteSubtype.literalValue;
|
1329
|
+
}
|
1310
1330
|
// If the object is already definitely truthy, it's fine to
|
1311
1331
|
// include, otherwise it should be removed.
|
1312
|
-
return
|
1332
|
+
return isLiteralTruthy ? subtype : undefined;
|
1313
1333
|
}
|
1314
1334
|
// If the object is a bool, make it "true", since
|
1315
1335
|
// "false" is a falsy value.
|
@@ -1447,7 +1467,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
1447
1467
|
}
|
1448
1468
|
// If this is a type[Any] or type[Unknown], allow any other members.
|
1449
1469
|
if ((0, types_1.isClassInstance)(objectType) && types_1.ClassType.isBuiltIn(objectType, 'type') && objectType.includeSubclasses) {
|
1450
|
-
if ((flags & 8 /* MemberAccessFlags.SkipTypeBaseClass */) === 0) {
|
1470
|
+
if ((flags & (8 /* MemberAccessFlags.SkipTypeBaseClass */ | 512 /* MemberAccessFlags.SkipAttributeAccessOverride */)) === 0) {
|
1451
1471
|
const typeArg = objectType.typeArguments && objectType.typeArguments.length >= 1
|
1452
1472
|
? objectType.typeArguments[0]
|
1453
1473
|
: types_1.UnknownType.create();
|
@@ -2022,7 +2042,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2022
2042
|
errorRange = { start: 0, length: 0 };
|
2023
2043
|
}
|
2024
2044
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
2025
|
-
addDiagnosticForTextRange(fileInfo,
|
2045
|
+
addDiagnosticForTextRange(fileInfo, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.codeTooComplexToAnalyze(), errorRange);
|
2026
2046
|
return true;
|
2027
2047
|
}
|
2028
2048
|
return false;
|
@@ -2163,7 +2183,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2163
2183
|
}
|
2164
2184
|
return diagnostic;
|
2165
2185
|
}
|
2166
|
-
function addDiagnosticForTextRange(fileInfo,
|
2186
|
+
function addDiagnosticForTextRange(fileInfo, rule, message, range) {
|
2187
|
+
const diagLevel = fileInfo.diagnosticRuleSet[rule];
|
2167
2188
|
if (diagLevel === 'none') {
|
2168
2189
|
return undefined;
|
2169
2190
|
}
|
@@ -2184,7 +2205,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2184
2205
|
}
|
2185
2206
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeExpectedClass().format({ type: printType(type) }) + diag.getString(), node);
|
2186
2207
|
}
|
2187
|
-
function assignTypeToNameNode(nameNode,
|
2208
|
+
function assignTypeToNameNode(nameNode, typeResult, ignoreEmptyContainers, srcExpression, allowAssignmentToFinalVar = false, expectedTypeDiagAddendum) {
|
2188
2209
|
var _a, _b, _c;
|
2189
2210
|
const nameValue = nameNode.value;
|
2190
2211
|
const symbolWithScope = lookUpSymbolRecursive(nameNode, nameValue, /* honorCodeFlow */ false);
|
@@ -2211,18 +2232,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2211
2232
|
}
|
2212
2233
|
}
|
2213
2234
|
// We found an existing declared type. Make sure the type is assignable.
|
2214
|
-
let destType = type;
|
2235
|
+
let destType = typeResult.type;
|
2215
2236
|
const isTypeAlias = !!declaredType && (0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias');
|
2216
2237
|
if (declaredType && !isTypeAlias) {
|
2217
2238
|
let diagAddendum = new diagnostic_1.DiagnosticAddendum();
|
2218
|
-
if (!assignType(declaredType, type, diagAddendum)) {
|
2239
|
+
if (!assignType(declaredType, typeResult.type, diagAddendum)) {
|
2219
2240
|
// If there was an expected type mismatch, use that diagnostic
|
2220
2241
|
// addendum because it will be more informative.
|
2221
2242
|
if (expectedTypeDiagAddendum) {
|
2222
2243
|
diagAddendum = expectedTypeDiagAddendum;
|
2223
2244
|
}
|
2224
|
-
if (!
|
2225
|
-
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssignmentType, localize_1.LocMessage.typeAssignmentMismatch().format(printSrcDestTypes(type, declaredType)) +
|
2245
|
+
if (!typeResult.isIncomplete) {
|
2246
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssignmentType, localize_1.LocMessage.typeAssignmentMismatch().format(printSrcDestTypes(typeResult.type, declaredType)) +
|
2226
2247
|
diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode, (_c = (_b = diagAddendum.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : srcExpression) !== null && _c !== void 0 ? _c : nameNode);
|
2227
2248
|
}
|
2228
2249
|
// Replace the assigned type with the (unnarrowed) declared type.
|
@@ -2230,7 +2251,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2230
2251
|
}
|
2231
2252
|
else {
|
2232
2253
|
// Constrain the resulting type to match the declared type.
|
2233
|
-
destType = narrowTypeBasedOnAssignment(nameNode, declaredType, type
|
2254
|
+
destType = narrowTypeBasedOnAssignment(nameNode, declaredType, typeResult).type;
|
2234
2255
|
}
|
2235
2256
|
}
|
2236
2257
|
else {
|
@@ -2263,12 +2284,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2263
2284
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.finalReassigned().format({ name: nameValue }), nameNode);
|
2264
2285
|
}
|
2265
2286
|
}
|
2266
|
-
if (!
|
2287
|
+
if (!typeResult.isIncomplete) {
|
2267
2288
|
reportPossibleUnknownAssignment(fileInfo.diagnosticRuleSet.reportUnknownVariableType, diagnosticRules_1.DiagnosticRule.reportUnknownVariableType, nameNode, destType, nameNode, ignoreEmptyContainers);
|
2268
2289
|
}
|
2269
|
-
writeTypeCache(nameNode, { type: destType, isIncomplete:
|
2290
|
+
writeTypeCache(nameNode, { type: destType, isIncomplete: typeResult.isIncomplete }, 0 /* EvaluatorFlags.None */);
|
2270
2291
|
}
|
2271
|
-
function assignTypeToMemberAccessNode(target,
|
2292
|
+
function assignTypeToMemberAccessNode(target, typeResult, srcExpr, expectedTypeDiagAddendum) {
|
2272
2293
|
var _a;
|
2273
2294
|
const baseTypeResult = getTypeOfExpression(target.leftExpression, 2 /* EvaluatorFlags.MemberAccessBaseDefaults */);
|
2274
2295
|
const baseType = makeTopLevelTypeVarsConcrete(baseTypeResult.type);
|
@@ -2281,14 +2302,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2281
2302
|
if (classTypeResults && (0, types_1.isInstantiableClass)(classTypeResults.classType)) {
|
2282
2303
|
if ((0, types_1.isClassInstance)(baseType)) {
|
2283
2304
|
if (types_1.ClassType.isSameGenericClass(baseType, classTypeResults.classType)) {
|
2284
|
-
assignTypeToMemberVariable(target,
|
2285
|
-
/* isInstanceMember */ true, srcExpr);
|
2305
|
+
assignTypeToMemberVariable(target, typeResult, /* isInstanceMember */ true, srcExpr);
|
2286
2306
|
}
|
2287
2307
|
}
|
2288
2308
|
else if ((0, types_1.isInstantiableClass)(baseType)) {
|
2289
2309
|
if (types_1.ClassType.isSameGenericClass(baseType, classTypeResults.classType)) {
|
2290
|
-
assignTypeToMemberVariable(target,
|
2291
|
-
/* isInstanceMember */ false, srcExpr);
|
2310
|
+
assignTypeToMemberVariable(target, typeResult, /* isInstanceMember */ false, srcExpr);
|
2292
2311
|
}
|
2293
2312
|
}
|
2294
2313
|
// Assignments to instance or class variables through "self" or "cls" is not
|
@@ -2309,7 +2328,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2309
2328
|
}
|
2310
2329
|
const setTypeResult = getTypeOfMemberAccessWithBaseType(target, baseTypeResult, {
|
2311
2330
|
method: 'set',
|
2312
|
-
setType:
|
2331
|
+
setType: typeResult,
|
2313
2332
|
setErrorNode: srcExpr,
|
2314
2333
|
setExpectedTypeDiag: expectedTypeDiagAddendum,
|
2315
2334
|
}, 0 /* EvaluatorFlags.None */);
|
@@ -2317,14 +2336,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2317
2336
|
setAsymmetricDescriptorAssignment(target);
|
2318
2337
|
}
|
2319
2338
|
const resultToCache = {
|
2320
|
-
type: (_a = setTypeResult.narrowedTypeForSet) !== null && _a !== void 0 ? _a : type,
|
2321
|
-
isIncomplete:
|
2339
|
+
type: (_a = setTypeResult.narrowedTypeForSet) !== null && _a !== void 0 ? _a : typeResult.type,
|
2340
|
+
isIncomplete: typeResult.isIncomplete,
|
2322
2341
|
memberAccessDeprecationInfo: setTypeResult.memberAccessDeprecationInfo,
|
2323
2342
|
};
|
2324
2343
|
writeTypeCache(target.memberName, resultToCache, 0 /* EvaluatorFlags.None */);
|
2325
2344
|
writeTypeCache(target, resultToCache, 0 /* EvaluatorFlags.None */);
|
2326
2345
|
}
|
2327
|
-
function assignTypeToMemberVariable(node,
|
2346
|
+
function assignTypeToMemberVariable(node, typeResult, isInstanceMember, srcExprNode) {
|
2328
2347
|
var _a;
|
2329
2348
|
const memberName = node.memberName.value;
|
2330
2349
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
@@ -2394,20 +2413,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2394
2413
|
// The class variable is accessed in this case.
|
2395
2414
|
setSymbolAccessed(fileInfo, memberInfo.symbol, node.memberName);
|
2396
2415
|
const memberType = getTypeOfMember(memberInfo);
|
2397
|
-
|
2416
|
+
typeResult = { ...typeResult, type: (0, types_1.combineTypes)([typeResult.type, memberType]) };
|
2398
2417
|
}
|
2399
2418
|
}
|
2400
2419
|
}
|
2401
2420
|
}
|
2402
2421
|
// Look up the member info again, now that we've potentially updated it.
|
2403
2422
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(classTypeInfo.classType, memberName, 64 /* MemberAccessFlags.DeclaredTypesOnly */);
|
2404
|
-
if (!memberInfo && srcExprNode && !
|
2405
|
-
reportPossibleUnknownAssignment(fileInfo.diagnosticRuleSet.reportUnknownMemberType, diagnosticRules_1.DiagnosticRule.reportUnknownMemberType, node.memberName,
|
2423
|
+
if (!memberInfo && srcExprNode && !typeResult.isIncomplete) {
|
2424
|
+
reportPossibleUnknownAssignment(fileInfo.diagnosticRuleSet.reportUnknownMemberType, diagnosticRules_1.DiagnosticRule.reportUnknownMemberType, node.memberName, typeResult.type, node,
|
2406
2425
|
/* ignoreEmptyContainers */ true);
|
2407
2426
|
}
|
2408
2427
|
}
|
2409
2428
|
}
|
2410
|
-
function assignTypeToTupleOrListNode(target,
|
2429
|
+
function assignTypeToTupleOrListNode(target, typeResult, srcExpr) {
|
2411
2430
|
const targetExpressions = target.nodeType === 34 /* ParseNodeType.List */ ? target.entries : target.expressions;
|
2412
2431
|
// Initialize the array of target types, one for each target.
|
2413
2432
|
const targetTypes = new Array(targetExpressions.length);
|
@@ -2418,9 +2437,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2418
2437
|
// Do any of the targets use an unpack operator? If so, it will consume all of the
|
2419
2438
|
// entries at that location.
|
2420
2439
|
const unpackIndex = targetExpressions.findIndex((expr) => expr.nodeType === 56 /* ParseNodeType.Unpack */);
|
2421
|
-
|
2440
|
+
typeResult = { ...typeResult, type: makeTopLevelTypeVarsConcrete(typeResult.type) };
|
2422
2441
|
const diagAddendum = new diagnostic_1.DiagnosticAddendum();
|
2423
|
-
(0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
|
2442
|
+
(0, typeUtils_1.doForEachSubtype)(typeResult.type, (subtype) => {
|
2424
2443
|
var _a, _b;
|
2425
2444
|
// Is this subtype a tuple?
|
2426
2445
|
const tupleType = (0, typeUtils_1.getSpecializedTupleType)(subtype);
|
@@ -2475,7 +2494,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2475
2494
|
else {
|
2476
2495
|
// The assigned expression isn't a tuple, so it had better
|
2477
2496
|
// be some iterable type.
|
2478
|
-
const iterableType = (_b = (_a = getTypeOfIterator({ type: subtype, isIncomplete:
|
2497
|
+
const iterableType = (_b = (_a = getTypeOfIterator({ type: subtype, isIncomplete: typeResult.isIncomplete },
|
2498
|
+
/* isAsync */ false, srcExpr)) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : types_1.UnknownType.create();
|
2479
2499
|
for (let index = 0; index < targetExpressions.length; index++) {
|
2480
2500
|
targetTypes[index].push((0, typeUtils_1.addConditionToType)(iterableType, (0, typeUtils_1.getTypeCondition)(subtype)));
|
2481
2501
|
}
|
@@ -2485,7 +2505,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2485
2505
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssignmentType, (target.nodeType === 34 /* ParseNodeType.List */
|
2486
2506
|
? localize_1.LocMessage.listAssignmentMismatch()
|
2487
2507
|
: localize_1.LocMessage.tupleAssignmentMismatch()).format({
|
2488
|
-
type: printType(type),
|
2508
|
+
type: printType(typeResult.type),
|
2489
2509
|
}) + diagAddendum.getString(), target);
|
2490
2510
|
}
|
2491
2511
|
// Assign the resulting types to the individual names in the tuple
|
@@ -2493,9 +2513,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2493
2513
|
targetExpressions.forEach((expr, index) => {
|
2494
2514
|
const typeList = targetTypes[index];
|
2495
2515
|
const targetType = typeList.length === 0 ? types_1.UnknownType.create() : (0, types_1.combineTypes)(typeList);
|
2496
|
-
assignTypeToExpression(expr, targetType,
|
2516
|
+
assignTypeToExpression(expr, { type: targetType, isIncomplete: typeResult.isIncomplete }, srcExpr,
|
2517
|
+
/* ignoreEmptyContainers */ true);
|
2497
2518
|
});
|
2498
|
-
writeTypeCache(target,
|
2519
|
+
writeTypeCache(target, typeResult, 0 /* EvaluatorFlags.None */);
|
2499
2520
|
}
|
2500
2521
|
// If the type includes promotion types, expand these to their constituent types.
|
2501
2522
|
function expandPromotionTypes(node, type, excludeBytes = false) {
|
@@ -2546,11 +2567,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2546
2567
|
}
|
2547
2568
|
// If this is a function that contains only a ParamSpec (no additional
|
2548
2569
|
// parameters), convert it to a concrete type of (*args: Any, **kwargs: Any).
|
2549
|
-
if (makeParamSpecsConcrete &&
|
2550
|
-
(0,
|
2551
|
-
|
2552
|
-
|
2553
|
-
|
2570
|
+
if (makeParamSpecsConcrete && (0, types_1.isFunction)(subtype)) {
|
2571
|
+
const convertedType = (0, typeUtils_1.convertParamSpecValueToType)(subtype);
|
2572
|
+
if ((0, types_1.isParamSpec)(convertedType)) {
|
2573
|
+
return types_1.FunctionType.applyParamSpecValue(subtype, (0, typeUtils_1.getUnknownTypeForCallable)());
|
2574
|
+
}
|
2554
2575
|
}
|
2555
2576
|
if ((0, types_1.isTypeVar)(subtype) && subtype.details.isVariadic) {
|
2556
2577
|
// If it's in a union, convert to type or object.
|
@@ -2719,9 +2740,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2719
2740
|
});
|
2720
2741
|
}
|
2721
2742
|
}
|
2722
|
-
function assignTypeToExpression(target,
|
2743
|
+
function assignTypeToExpression(target, typeResult, srcExpr, ignoreEmptyContainers = false, allowAssignmentToFinalVar = false, expectedTypeDiagAddendum) {
|
2723
2744
|
// Is the source expression a TypeVar() call?
|
2724
|
-
if ((0, types_1.isTypeVar)(type)) {
|
2745
|
+
if ((0, types_1.isTypeVar)(typeResult.type)) {
|
2725
2746
|
if (srcExpr && srcExpr.nodeType === 9 /* ParseNodeType.Call */) {
|
2726
2747
|
const callType = getTypeOfExpression(srcExpr.leftExpression, 2 /* EvaluatorFlags.CallBaseDefaults */).type;
|
2727
2748
|
if ((0, types_1.isInstantiableClass)(callType) &&
|
@@ -2729,13 +2750,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2729
2750
|
types_1.ClassType.isBuiltIn(callType, 'TypeVarTuple') ||
|
2730
2751
|
types_1.ClassType.isBuiltIn(callType, 'ParamSpec'))) {
|
2731
2752
|
const typeVarTarget = target.nodeType === 54 /* ParseNodeType.TypeAnnotation */ ? target.valueExpression : target;
|
2732
|
-
if (typeVarTarget.nodeType !== 38 /* ParseNodeType.Name */ ||
|
2733
|
-
|
2753
|
+
if (typeVarTarget.nodeType !== 38 /* ParseNodeType.Name */ ||
|
2754
|
+
typeVarTarget.value !== typeResult.type.details.name) {
|
2755
|
+
addError(typeResult.type.details.isParamSpec
|
2734
2756
|
? localize_1.LocMessage.paramSpecAssignedName().format({
|
2735
|
-
name: types_1.TypeVarType.getReadableName(type),
|
2757
|
+
name: types_1.TypeVarType.getReadableName(typeResult.type),
|
2736
2758
|
})
|
2737
2759
|
: localize_1.LocMessage.typeVarAssignedName().format({
|
2738
|
-
name: types_1.TypeVarType.getReadableName(type),
|
2760
|
+
name: types_1.TypeVarType.getReadableName(typeResult.type),
|
2739
2761
|
}), typeVarTarget);
|
2740
2762
|
}
|
2741
2763
|
}
|
@@ -2744,30 +2766,32 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2744
2766
|
// If the type was partially unbound, an error will have already been logged.
|
2745
2767
|
// Remove the unbound before assigning to the target expression so the unbound
|
2746
2768
|
// error doesn't propagate.
|
2747
|
-
type
|
2769
|
+
if ((0, types_1.findSubtype)(typeResult.type, (subtype) => (0, types_1.isUnbound)(subtype))) {
|
2770
|
+
typeResult = { ...typeResult, type: (0, types_1.removeUnbound)(typeResult.type) };
|
2771
|
+
}
|
2748
2772
|
switch (target.nodeType) {
|
2749
2773
|
case 38 /* ParseNodeType.Name */: {
|
2750
|
-
assignTypeToNameNode(target,
|
2774
|
+
assignTypeToNameNode(target, typeResult, ignoreEmptyContainers, srcExpr, allowAssignmentToFinalVar, expectedTypeDiagAddendum);
|
2751
2775
|
break;
|
2752
2776
|
}
|
2753
2777
|
case 35 /* ParseNodeType.MemberAccess */: {
|
2754
|
-
assignTypeToMemberAccessNode(target,
|
2778
|
+
assignTypeToMemberAccessNode(target, typeResult, srcExpr, expectedTypeDiagAddendum);
|
2755
2779
|
break;
|
2756
2780
|
}
|
2757
2781
|
case 27 /* ParseNodeType.Index */: {
|
2758
2782
|
const baseTypeResult = getTypeOfExpression(target.baseExpression, 2 /* EvaluatorFlags.IndexBaseDefaults */);
|
2759
2783
|
getTypeOfIndexWithBaseType(target, baseTypeResult, {
|
2760
2784
|
method: 'set',
|
2761
|
-
setType:
|
2785
|
+
setType: typeResult,
|
2762
2786
|
setErrorNode: srcExpr,
|
2763
2787
|
setExpectedTypeDiag: expectedTypeDiagAddendum,
|
2764
2788
|
}, 0 /* EvaluatorFlags.None */);
|
2765
|
-
writeTypeCache(target,
|
2789
|
+
writeTypeCache(target, typeResult, 0 /* EvaluatorFlags.None */);
|
2766
2790
|
break;
|
2767
2791
|
}
|
2768
2792
|
case 34 /* ParseNodeType.List */:
|
2769
2793
|
case 52 /* ParseNodeType.Tuple */: {
|
2770
|
-
assignTypeToTupleOrListNode(target,
|
2794
|
+
assignTypeToTupleOrListNode(target, typeResult, srcExpr);
|
2771
2795
|
break;
|
2772
2796
|
}
|
2773
2797
|
case 54 /* ParseNodeType.TypeAnnotation */: {
|
@@ -2782,23 +2806,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2782
2806
|
if (!isBareFinalOrClassVar) {
|
2783
2807
|
const isTypeAliasAnnotation = (0, types_1.isClassInstance)(annotationType) && types_1.ClassType.isBuiltIn(annotationType, 'TypeAlias');
|
2784
2808
|
if (!isTypeAliasAnnotation) {
|
2785
|
-
if (assignType(annotationType, type)) {
|
2809
|
+
if (assignType(annotationType, typeResult.type)) {
|
2786
2810
|
// Don't attempt to narrow based on the annotated type if the type
|
2787
2811
|
// is a enum because the annotated type in an enum doesn't reflect
|
2788
2812
|
// the type of the symbol.
|
2789
|
-
if (!(0, types_1.isClassInstance)(type) || !types_1.ClassType.isEnumClass(type)) {
|
2790
|
-
|
2813
|
+
if (!(0, types_1.isClassInstance)(typeResult.type) || !types_1.ClassType.isEnumClass(typeResult.type)) {
|
2814
|
+
typeResult = narrowTypeBasedOnAssignment(target, annotationType, typeResult);
|
2791
2815
|
}
|
2792
2816
|
}
|
2793
2817
|
}
|
2794
2818
|
}
|
2795
|
-
assignTypeToExpression(target.valueExpression,
|
2819
|
+
assignTypeToExpression(target.valueExpression, typeResult, srcExpr, ignoreEmptyContainers, allowAssignmentToFinalVar, expectedTypeDiagAddendum);
|
2796
2820
|
break;
|
2797
2821
|
}
|
2798
2822
|
case 56 /* ParseNodeType.Unpack */: {
|
2799
2823
|
if (target.expression.nodeType === 38 /* ParseNodeType.Name */) {
|
2800
|
-
assignTypeToNameNode(target.expression,
|
2801
|
-
|
2824
|
+
assignTypeToNameNode(target.expression, {
|
2825
|
+
type: getBuiltInObject(target.expression, 'list', [typeResult.type]),
|
2826
|
+
isIncomplete: typeResult.isIncomplete,
|
2827
|
+
}, ignoreEmptyContainers, srcExpr);
|
2802
2828
|
}
|
2803
2829
|
break;
|
2804
2830
|
}
|
@@ -4014,7 +4040,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4014
4040
|
// descriptor-based accesses.
|
4015
4041
|
narrowedTypeForSet = isDescriptorApplied
|
4016
4042
|
? usage.setType.type
|
4017
|
-
: narrowTypeBasedOnAssignment(errorNode, type, usage.setType.type
|
4043
|
+
: narrowTypeBasedOnAssignment(errorNode, type, usage.setType).type;
|
4018
4044
|
}
|
4019
4045
|
// Verify that the assigned type is compatible.
|
4020
4046
|
if (!assignType(type, usage.setType.type, diag === null || diag === void 0 ? void 0 : diag.createAddendum())) {
|
@@ -4030,7 +4056,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4030
4056
|
isDescriptorError = true;
|
4031
4057
|
}
|
4032
4058
|
if ((0, types_1.isInstantiableClass)(memberInfo.classType) &&
|
4033
|
-
types_1.ClassType.
|
4059
|
+
types_1.ClassType.isDataClassFrozen(memberInfo.classType) &&
|
4034
4060
|
isAccessedThroughObject) {
|
4035
4061
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.dataClassFrozen().format({
|
4036
4062
|
name: printType(types_1.ClassType.cloneAsInstance(memberInfo.classType)),
|
@@ -4301,7 +4327,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4301
4327
|
// If there's no declared return type on the getter, assume it's symmetric.
|
4302
4328
|
if (setterType.details.parameters.length >= 3 && getterType.details.declaredReturnType) {
|
4303
4329
|
const setterValueType = types_1.FunctionType.getEffectiveParameterType(setterType, 2);
|
4304
|
-
const getterReturnType = (_a = types_1.FunctionType.
|
4330
|
+
const getterReturnType = (_a = types_1.FunctionType.getEffectiveReturnType(getterType)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
|
4305
4331
|
if (!(0, types_1.isTypeSame)(setterValueType, getterReturnType)) {
|
4306
4332
|
isAsymmetric = true;
|
4307
4333
|
}
|
@@ -4333,7 +4359,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4333
4359
|
// If there's no declared return type on the getter, assume it's symmetric.
|
4334
4360
|
if (setterType.details.parameters.length >= 3 && getterType.details.declaredReturnType) {
|
4335
4361
|
const setterValueType = types_1.FunctionType.getEffectiveParameterType(setterType, 2);
|
4336
|
-
const getterReturnType = (_a = types_1.FunctionType.
|
4362
|
+
const getterReturnType = (_a = types_1.FunctionType.getEffectiveReturnType(getterType)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
|
4337
4363
|
if (!(0, types_1.isTypeSame)(setterValueType, getterReturnType)) {
|
4338
4364
|
isAsymmetric = true;
|
4339
4365
|
}
|
@@ -4699,11 +4725,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4699
4725
|
if (index === concatTypeArgs.length - 1) {
|
4700
4726
|
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
4701
4727
|
if ((0, types_1.isParamSpec)(typeArg)) {
|
4702
|
-
|
4728
|
+
types_1.FunctionType.addParamSpecVariadics(functionType, typeArg);
|
4703
4729
|
}
|
4704
4730
|
else if ((0, typeUtils_1.isEllipsisType)(typeArg)) {
|
4705
4731
|
types_1.FunctionType.addDefaultParameters(functionType);
|
4706
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.
|
4732
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
4707
4733
|
}
|
4708
4734
|
}
|
4709
4735
|
else {
|
@@ -4720,7 +4746,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4720
4746
|
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */);
|
4721
4747
|
}
|
4722
4748
|
else if ((0, typeUtils_1.isEllipsisType)(typeArgType)) {
|
4723
|
-
const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */ | 32768 /* FunctionTypeFlags.
|
4749
|
+
const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */ | 32768 /* FunctionTypeFlags.GradualCallableForm */);
|
4724
4750
|
types_1.FunctionType.addDefaultParameters(functionType);
|
4725
4751
|
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext);
|
4726
4752
|
}
|
@@ -5628,7 +5654,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5628
5654
|
// the base type of this call is not the same as one of the tracked signatures.
|
5629
5655
|
// This is important for nested generic calls (e.g. "foo(foo(x))").
|
5630
5656
|
if (signatureTracker) {
|
5631
|
-
baseTypeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(baseTypeResult.type, signatureTracker, node.
|
5657
|
+
baseTypeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(baseTypeResult.type, signatureTracker, node.start);
|
5632
5658
|
}
|
5633
5659
|
if (!(0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
|
5634
5660
|
if (node.leftExpression.nodeType === 38 /* ParseNodeType.Name */ && node.leftExpression.value === 'super') {
|
@@ -5822,24 +5848,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5822
5848
|
const type = typeResult.type;
|
5823
5849
|
const exprString = ParseTreeUtils.printExpression(arg0Value);
|
5824
5850
|
const typeString = printType(type, { expandTypeAlias: true });
|
5825
|
-
if (
|
5826
|
-
if (expectedText !==
|
5827
|
-
|
5828
|
-
|
5829
|
-
|
5830
|
-
|
5851
|
+
if (!typeResult.isIncomplete) {
|
5852
|
+
if (expectedText !== undefined) {
|
5853
|
+
if (expectedText !== typeString) {
|
5854
|
+
addError(localize_1.LocMessage.revealTypeExpectedTextMismatch().format({
|
5855
|
+
expected: expectedText,
|
5856
|
+
received: typeString,
|
5857
|
+
}), expectedTextNode !== null && expectedTextNode !== void 0 ? expectedTextNode : arg0Value);
|
5858
|
+
}
|
5831
5859
|
}
|
5832
|
-
|
5833
|
-
|
5834
|
-
|
5835
|
-
|
5836
|
-
|
5837
|
-
|
5838
|
-
|
5839
|
-
}
|
5860
|
+
if (expectedRevealType) {
|
5861
|
+
if (!(0, types_1.isTypeSame)(expectedRevealType, type, { ignorePseudoGeneric: true })) {
|
5862
|
+
const expectedRevealTypeText = printType(expectedRevealType);
|
5863
|
+
addError(localize_1.LocMessage.revealTypeExpectedTypeMismatch().format({
|
5864
|
+
expected: expectedRevealTypeText,
|
5865
|
+
received: typeString,
|
5866
|
+
}), expectedRevealTypeNode !== null && expectedRevealTypeNode !== void 0 ? expectedRevealTypeNode : arg0Value);
|
5867
|
+
}
|
5840
5868
|
}
|
5869
|
+
addInformation(localize_1.LocAddendum.typeOfSymbol().format({ name: exprString, type: typeString }), node.arguments[0]);
|
5841
5870
|
}
|
5842
|
-
addInformation(localize_1.LocAddendum.typeOfSymbol().format({ name: exprString, type: typeString }), node.arguments[0]);
|
5843
5871
|
return { type, isIncomplete: typeResult.isIncomplete };
|
5844
5872
|
}
|
5845
5873
|
function getTypeOfRevealLocals(node) {
|
@@ -6943,6 +6971,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
6943
6971
|
if ((0, types_1.isTypeVar)(unexpandedCallType)) {
|
6944
6972
|
returnType = (0, typeUtils_1.convertToInstance)(unexpandedCallType);
|
6945
6973
|
}
|
6974
|
+
// If we instantiated the "deprecated" class, attach the deprecation
|
6975
|
+
// message to the instance.
|
6976
|
+
if (errorNode.nodeType === 9 /* ParseNodeType.Call */ &&
|
6977
|
+
returnType &&
|
6978
|
+
(0, types_1.isClassInstance)(returnType) &&
|
6979
|
+
types_1.ClassType.isBuiltIn(returnType, 'deprecated')) {
|
6980
|
+
returnType = types_1.ClassType.cloneForDeprecatedInstance(returnType, (0, decorators_1.getDeprecatedMessageFromCall)(errorNode));
|
6981
|
+
}
|
6946
6982
|
// If we instantiated a type, transform it into a class.
|
6947
6983
|
// This can happen if someone directly instantiates a metaclass
|
6948
6984
|
// deriving from type.
|
@@ -7074,6 +7110,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7074
7110
|
let reportedArgError = false;
|
7075
7111
|
let isTypeIncomplete = !!typeResult.isIncomplete;
|
7076
7112
|
let isVariadicTypeVarFullyMatched = false;
|
7113
|
+
const paramSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(typeResult.type);
|
7077
7114
|
// Expand any unpacked tuples in the arg list.
|
7078
7115
|
argList = expandArgList(argList, signatureTracker);
|
7079
7116
|
// Build a map of parameters by name.
|
@@ -7099,6 +7136,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7099
7136
|
let paramSpecArgList;
|
7100
7137
|
let paramSpecTarget;
|
7101
7138
|
let hasParamSpecArgsKwargs = false;
|
7139
|
+
// Determine how many positional args are being passed before
|
7140
|
+
// we see a keyword arg.
|
7141
|
+
let positionalArgCount = argList.findIndex((arg) => arg.argumentCategory === 2 /* ArgumentCategory.UnpackedDictionary */ || arg.name !== undefined);
|
7142
|
+
if (positionalArgCount < 0) {
|
7143
|
+
positionalArgCount = argList.length;
|
7144
|
+
}
|
7102
7145
|
if (varArgListParamIndex !== undefined && varArgDictParamIndex !== undefined) {
|
7103
7146
|
(0, debug_1.assert)(paramDetails.params[varArgListParamIndex], 'varArgListParamIndex params entry is undefined');
|
7104
7147
|
const varArgListParam = paramDetails.params[varArgListParamIndex].param;
|
@@ -7120,16 +7163,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7120
7163
|
}
|
7121
7164
|
else {
|
7122
7165
|
positionalOnlyLimitIndex = varArgListParamIndex;
|
7166
|
+
positionalArgCount = varArgListParamIndex;
|
7167
|
+
positionParamLimitIndex = varArgListParamIndex;
|
7123
7168
|
}
|
7124
7169
|
}
|
7125
7170
|
}
|
7126
|
-
else if (
|
7127
|
-
|
7128
|
-
if ((_c = (0, typeUtils_1.getTypeVarScopeIds)(typeResult.type)) === null || _c === void 0 ? void 0 : _c.some((id) => id === paramSpecScopeId)) {
|
7171
|
+
else if (paramSpec) {
|
7172
|
+
if ((_c = (0, typeUtils_1.getTypeVarScopeIds)(typeResult.type)) === null || _c === void 0 ? void 0 : _c.some((id) => id === paramSpec.scopeId)) {
|
7129
7173
|
hasParamSpecArgsKwargs = true;
|
7130
7174
|
paramSpecArgList = [];
|
7131
|
-
paramSpecTarget =
|
7132
|
-
/* access */ undefined);
|
7175
|
+
paramSpecTarget = paramSpec;
|
7133
7176
|
}
|
7134
7177
|
}
|
7135
7178
|
// If there are keyword arguments present after a *args argument,
|
@@ -7159,12 +7202,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7159
7202
|
if (positionParamLimitIndex < 0) {
|
7160
7203
|
positionParamLimitIndex = paramDetails.params.length;
|
7161
7204
|
}
|
7162
|
-
// Determine how many positional args are being passed before
|
7163
|
-
// we see a keyword arg.
|
7164
|
-
let positionalArgCount = argList.findIndex((arg) => arg.argumentCategory === 2 /* ArgumentCategory.UnpackedDictionary */ || arg.name !== undefined);
|
7165
|
-
if (positionalArgCount < 0) {
|
7166
|
-
positionalArgCount = argList.length;
|
7167
|
-
}
|
7168
7205
|
let validateArgTypeParams = [];
|
7169
7206
|
let activeParam;
|
7170
7207
|
function trySetActive(arg, param) {
|
@@ -7541,13 +7578,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7541
7578
|
reportedArgError = true;
|
7542
7579
|
}
|
7543
7580
|
}
|
7544
|
-
else if (
|
7545
|
-
(0, parameterUtils_1.isParamSpecKwargsArgument)(typeResult.type.details.paramSpec, argType)) {
|
7581
|
+
else if (paramSpec && (0, parameterUtils_1.isParamSpecKwargsArgument)(paramSpec, argType)) {
|
7546
7582
|
unpackedDictionaryArgType = types_1.AnyType.create();
|
7547
7583
|
if (!paramSpecArgList) {
|
7548
7584
|
validateArgTypeParams.push({
|
7549
7585
|
paramCategory: 2 /* ParameterCategory.KwargsDict */,
|
7550
|
-
paramType:
|
7586
|
+
paramType: paramSpec,
|
7551
7587
|
requiresTypeVarMatching: false,
|
7552
7588
|
argument: argList[argIndex],
|
7553
7589
|
argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
|
@@ -7685,17 +7721,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7685
7721
|
else if (argList[argIndex].argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
|
7686
7722
|
// Handle the case where a *args: P.args (or *args: Any) is passed as an
|
7687
7723
|
// argument to a function that accepts a ParamSpec.
|
7688
|
-
if (
|
7724
|
+
if (paramSpec) {
|
7689
7725
|
const argTypeResult = getTypeOfArgument(argList[argIndex],
|
7690
7726
|
/* inferenceContext */ undefined, signatureTracker);
|
7691
7727
|
const argType = argTypeResult.type;
|
7692
7728
|
if (argTypeResult.isIncomplete) {
|
7693
7729
|
isTypeIncomplete = true;
|
7694
7730
|
}
|
7695
|
-
if ((0, parameterUtils_1.isParamSpecArgsArgument)(
|
7731
|
+
if ((0, parameterUtils_1.isParamSpecArgsArgument)(paramSpec, argType)) {
|
7696
7732
|
validateArgTypeParams.push({
|
7697
7733
|
paramCategory: 1 /* ParameterCategory.ArgsList */,
|
7698
|
-
paramType:
|
7734
|
+
paramType: paramSpec,
|
7699
7735
|
requiresTypeVarMatching: false,
|
7700
7736
|
argument: argList[argIndex],
|
7701
7737
|
argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
|
@@ -7904,7 +7940,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7904
7940
|
(0, types_1.isAnyOrUnknown)(inferenceContext.expectedType) ||
|
7905
7941
|
(0, types_1.isNever)(inferenceContext.expectedType) ||
|
7906
7942
|
!type.details.declaredReturnType ||
|
7907
|
-
!(0, typeUtils_1.requiresSpecialization)((_a = types_1.FunctionType.
|
7943
|
+
!(0, typeUtils_1.requiresSpecialization)((_a = types_1.FunctionType.getEffectiveReturnType(type)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create())) {
|
7908
7944
|
return validateFunctionArgumentTypes(errorNode, matchResults, typeVarContext, signatureTracker, skipUnknownArgCheck);
|
7909
7945
|
}
|
7910
7946
|
const effectiveReturnType = getFunctionEffectiveReturnType(type);
|
@@ -7995,6 +8031,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7995
8031
|
let specializedInitSelfType;
|
7996
8032
|
let anyOrUnknownArgument;
|
7997
8033
|
const typeCondition = (0, typeUtils_1.getTypeCondition)(type);
|
8034
|
+
const paramSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(type);
|
7998
8035
|
// Check for an attempt to invoke an unimplemented abstract method.
|
7999
8036
|
if (type.boundToType && !type.boundToType.includeSubclasses && type.details.methodClass) {
|
8000
8037
|
const abstractSymbolInfo = getAbstractSymbolInfo(type.details.methodClass, type.details.name);
|
@@ -8004,36 +8041,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8004
8041
|
}), errorNode.nodeType === 9 /* ParseNodeType.Call */ ? errorNode.leftExpression : errorNode);
|
8005
8042
|
}
|
8006
8043
|
}
|
8007
|
-
|
8008
|
-
|
8009
|
-
|
8010
|
-
|
8011
|
-
|
8012
|
-
|
8013
|
-
|
8014
|
-
|
8015
|
-
|
8016
|
-
|
8017
|
-
|
8018
|
-
|
8019
|
-
|
8020
|
-
|
8021
|
-
|
8022
|
-
|
8023
|
-
(0, types_1.isClassInstance)(type.boundToType) &&
|
8024
|
-
types_1.ClassType.isSameGenericClass(type.strippedFirstParamType, type.boundToType) &&
|
8025
|
-
type.strippedFirstParamType.typeArguments) {
|
8026
|
-
const typeParams = type.strippedFirstParamType.details.typeParameters;
|
8027
|
-
specializedInitSelfType = type.strippedFirstParamType;
|
8028
|
-
type.strippedFirstParamType.typeArguments.forEach((typeArg, index) => {
|
8029
|
-
if (index < typeParams.length) {
|
8030
|
-
const typeParam = typeParams[index];
|
8031
|
-
if (!(0, types_1.isTypeSame)(typeParam, typeArg, { ignorePseudoGeneric: true })) {
|
8032
|
-
typeVarContext.setTypeVarType(typeParams[index], typeArg);
|
8033
|
-
}
|
8044
|
+
// The type annotation for the "self" parameter in an __init__ method to
|
8045
|
+
// can influence the type being constructed.
|
8046
|
+
if (type.details.name === '__init__' &&
|
8047
|
+
type.strippedFirstParamType &&
|
8048
|
+
type.boundToType &&
|
8049
|
+
(0, types_1.isClassInstance)(type.strippedFirstParamType) &&
|
8050
|
+
(0, types_1.isClassInstance)(type.boundToType) &&
|
8051
|
+
types_1.ClassType.isSameGenericClass(type.strippedFirstParamType, type.boundToType) &&
|
8052
|
+
type.strippedFirstParamType.typeArguments) {
|
8053
|
+
const typeParams = type.strippedFirstParamType.details.typeParameters;
|
8054
|
+
specializedInitSelfType = type.strippedFirstParamType;
|
8055
|
+
type.strippedFirstParamType.typeArguments.forEach((typeArg, index) => {
|
8056
|
+
if (index < typeParams.length) {
|
8057
|
+
const typeParam = typeParams[index];
|
8058
|
+
if (!(0, types_1.isTypeSame)(typeParam, typeArg, { ignorePseudoGeneric: true })) {
|
8059
|
+
typeVarContext.setTypeVarType(typeParams[index], typeArg);
|
8034
8060
|
}
|
8035
|
-
}
|
8036
|
-
}
|
8061
|
+
}
|
8062
|
+
});
|
8037
8063
|
}
|
8038
8064
|
// Special-case a few built-in calls that are often used for
|
8039
8065
|
// casting or checking for unknown types.
|
@@ -8124,14 +8150,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8124
8150
|
? (0, typeUtils_1.preserveUnknown)(argResult.argType, anyOrUnknownArgument)
|
8125
8151
|
: argResult.argType;
|
8126
8152
|
}
|
8127
|
-
if (
|
8153
|
+
if (paramSpec) {
|
8128
8154
|
if (argParam.argument.argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
|
8129
|
-
if ((0, parameterUtils_1.isParamSpecArgsArgument)(
|
8155
|
+
if ((0, parameterUtils_1.isParamSpecArgsArgument)(paramSpec, argResult.argType)) {
|
8130
8156
|
sawParamSpecArgs = true;
|
8131
8157
|
}
|
8132
8158
|
}
|
8133
8159
|
if (argParam.argument.argumentCategory === 2 /* ArgumentCategory.UnpackedDictionary */) {
|
8134
|
-
if ((0, parameterUtils_1.isParamSpecKwargsArgument)(
|
8160
|
+
if ((0, parameterUtils_1.isParamSpecKwargsArgument)(paramSpec, argResult.argType)) {
|
8135
8161
|
sawParamSpecKwargs = true;
|
8136
8162
|
}
|
8137
8163
|
}
|
@@ -8147,10 +8173,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8147
8173
|
}
|
8148
8174
|
paramSpecTypeVarContext = paramSpecArgResult.typeVarContexts;
|
8149
8175
|
}
|
8150
|
-
else if (
|
8176
|
+
else if (paramSpec) {
|
8151
8177
|
if (!sawParamSpecArgs || !sawParamSpecKwargs) {
|
8152
8178
|
if (!isTypeIncomplete) {
|
8153
|
-
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.paramSpecArgsMissing().format({ type: printType(
|
8179
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.paramSpecArgsMissing().format({ type: printType(paramSpec) }), errorNode);
|
8154
8180
|
}
|
8155
8181
|
argumentErrors = true;
|
8156
8182
|
argumentMatchScore += 1;
|
@@ -8373,39 +8399,42 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8373
8399
|
});
|
8374
8400
|
return { argumentErrors: true, typeVarContexts: [srcTypeVarContext] };
|
8375
8401
|
}
|
8402
|
+
const functionParamSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(functionType);
|
8403
|
+
const functionWithoutParamSpec = types_1.FunctionType.cloneRemoveParamSpecArgsKwargs(functionType);
|
8376
8404
|
// Handle the recursive case where we're passing (*args: P.args, **kwargs: P.args)
|
8377
8405
|
// a remaining function of type (*P).
|
8378
|
-
if (
|
8379
|
-
|
8380
|
-
(0, types_1.isTypeSame)(
|
8406
|
+
if (functionParamSpec &&
|
8407
|
+
functionWithoutParamSpec.details.parameters.length === 0 &&
|
8408
|
+
(0, types_1.isTypeSame)(functionParamSpec, paramSpec)) {
|
8381
8409
|
// If there are any arguments other than *args: P.args or **kwargs: P.kwargs,
|
8382
8410
|
// report an error.
|
8383
|
-
let
|
8384
|
-
let
|
8411
|
+
let argsCount = 0;
|
8412
|
+
let kwargsCount = 0;
|
8385
8413
|
let argumentErrors = false;
|
8386
8414
|
let argErrorNode;
|
8387
8415
|
for (const arg of argList) {
|
8388
8416
|
const argType = (_a = getTypeOfArgument(arg, /* inferenceContext */ undefined, signatureTracker)) === null || _a === void 0 ? void 0 : _a.type;
|
8389
|
-
|
8390
|
-
|
8391
|
-
|
8417
|
+
if (arg.argumentCategory === 1 /* ArgumentCategory.UnpackedList */) {
|
8418
|
+
if ((0, parameterUtils_1.isParamSpecArgsArgument)(paramSpec, argType)) {
|
8419
|
+
argsCount++;
|
8420
|
+
}
|
8392
8421
|
}
|
8393
|
-
else if (arg.argumentCategory === 2 /* ArgumentCategory.UnpackedDictionary */
|
8394
|
-
|
8395
|
-
|
8396
|
-
|
8422
|
+
else if (arg.argumentCategory === 2 /* ArgumentCategory.UnpackedDictionary */) {
|
8423
|
+
if ((0, parameterUtils_1.isParamSpecKwargsArgument)(paramSpec, argType)) {
|
8424
|
+
kwargsCount++;
|
8425
|
+
}
|
8397
8426
|
}
|
8398
8427
|
else {
|
8399
8428
|
argErrorNode = argErrorNode !== null && argErrorNode !== void 0 ? argErrorNode : arg.valueExpression;
|
8400
8429
|
argumentErrors = true;
|
8401
8430
|
}
|
8402
8431
|
}
|
8403
|
-
if (
|
8432
|
+
if (argsCount !== 1 || kwargsCount !== 1) {
|
8404
8433
|
argumentErrors = true;
|
8405
8434
|
}
|
8406
8435
|
if (argumentErrors) {
|
8407
8436
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.paramSpecArgsMissing().format({
|
8408
|
-
type: printType(
|
8437
|
+
type: printType(functionParamSpec),
|
8409
8438
|
}), argErrorNode !== null && argErrorNode !== void 0 ? argErrorNode : errorNode);
|
8410
8439
|
}
|
8411
8440
|
return { argumentErrors, typeVarContexts: [srcTypeVarContext] };
|
@@ -8438,7 +8467,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8438
8467
|
// the solved TypeVars if the typeVarContext has more than one signature.
|
8439
8468
|
// This will expand the ParamSpec into an overload, which will cause problems.
|
8440
8469
|
const skipApplySolvedTypeVars = (0, types_1.isFunction)(argParam.paramType) &&
|
8441
|
-
|
8470
|
+
types_1.FunctionType.getParamSpecFromArgsKwargs(argParam.paramType) &&
|
8442
8471
|
typeVarContext.getSignatureContexts().length > 1;
|
8443
8472
|
if (!skipApplySolvedTypeVars) {
|
8444
8473
|
expectedType = (0, typeUtils_1.applySolvedTypeVars)(expectedType, typeVarContext, {
|
@@ -8957,7 +8986,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8957
8986
|
const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */);
|
8958
8987
|
if (node.nodeType === 21 /* ParseNodeType.Ellipsis */) {
|
8959
8988
|
types_1.FunctionType.addDefaultParameters(functionType);
|
8960
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.
|
8989
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
8961
8990
|
return functionType;
|
8962
8991
|
}
|
8963
8992
|
if (node.nodeType === 34 /* ParseNodeType.List */) {
|
@@ -8991,7 +9020,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
8991
9020
|
return undefined;
|
8992
9021
|
}
|
8993
9022
|
if ((0, types_1.isParamSpec)(typeResult.type)) {
|
8994
|
-
|
9023
|
+
types_1.FunctionType.addParamSpecVariadics(functionType, typeResult.type);
|
8995
9024
|
return functionType;
|
8996
9025
|
}
|
8997
9026
|
if ((0, types_1.isClassInstance)(typeResult.type) &&
|
@@ -9166,7 +9195,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9166
9195
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeLiteral(), argList[1].node || errorNode);
|
9167
9196
|
}
|
9168
9197
|
let classFlags = baseClass.details.flags & ~(1 /* ClassTypeFlags.BuiltInClass */ | 2 /* ClassTypeFlags.SpecialBuiltIn */);
|
9169
|
-
classFlags |=
|
9198
|
+
classFlags |= 256 /* ClassTypeFlags.Final */ | 2097152 /* ClassTypeFlags.NewTypeClass */ | 4194304 /* ClassTypeFlags.ValidTypeAliasClass */;
|
9170
9199
|
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, classFlags, ParseTreeUtils.getTypeSourceId(errorNode),
|
9171
9200
|
/* declaredMetaclass */ undefined, baseClass.details.effectiveMetaclass);
|
9172
9201
|
classType.details.baseClasses.push(isBaseClassAny ? types_1.AnyType.create() : baseClass);
|
@@ -9198,7 +9227,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9198
9227
|
});
|
9199
9228
|
types_1.FunctionType.addDefaultParameters(newType);
|
9200
9229
|
newType.details.declaredReturnType = types_1.ClassType.cloneAsInstance(classType);
|
9201
|
-
newType.details.constructorTypeVarScopeId = classType
|
9230
|
+
newType.details.constructorTypeVarScopeId = (0, typeUtils_1.getTypeVarScopeId)(classType);
|
9202
9231
|
types_1.ClassType.getSymbolTable(classType).set('__new__', symbol_1.Symbol.createWithType(4 /* SymbolFlags.ClassMember */, newType));
|
9203
9232
|
}
|
9204
9233
|
return classType;
|
@@ -9220,7 +9249,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9220
9249
|
if (!(0, types_1.isClassInstance)(arg1Type) || !(0, typeUtils_1.isTupleClass)(arg1Type) || arg1Type.tupleTypeArguments === undefined) {
|
9221
9250
|
return undefined;
|
9222
9251
|
}
|
9223
|
-
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri,
|
9252
|
+
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, 4194304 /* ClassTypeFlags.ValidTypeAliasClass */, ParseTreeUtils.getTypeSourceId(errorNode), metaclass, arg1Type.details.effectiveMetaclass);
|
9224
9253
|
arg1Type.tupleTypeArguments.forEach((typeArg) => {
|
9225
9254
|
const specializedType = makeTopLevelTypeVarsConcrete(typeArg.type);
|
9226
9255
|
if ((0, typeUtils_1.isEffectivelyInstantiable)(specializedType)) {
|
@@ -9944,7 +9973,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9944
9973
|
if (enclosingFunction) {
|
9945
9974
|
const functionTypeInfo = getTypeOfFunction(enclosingFunction);
|
9946
9975
|
if (functionTypeInfo) {
|
9947
|
-
const returnType = types_1.FunctionType.
|
9976
|
+
const returnType = types_1.FunctionType.getEffectiveReturnType(functionTypeInfo.functionType);
|
9948
9977
|
if (returnType) {
|
9949
9978
|
expectedYieldType = (0, typeUtils_1.getGeneratorYieldType)(returnType, !!enclosingFunction.isAsync);
|
9950
9979
|
const generatorTypeArgs = (0, typeUtils_1.getGeneratorTypeArgs)(returnType);
|
@@ -10033,128 +10062,134 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10033
10062
|
}
|
10034
10063
|
let functionType = types_1.FunctionType.createInstance('', '', '', 131072 /* FunctionTypeFlags.PartiallyEvaluated */);
|
10035
10064
|
functionType.details.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(node);
|
10036
|
-
|
10037
|
-
|
10038
|
-
|
10039
|
-
|
10040
|
-
|
10041
|
-
|
10042
|
-
|
10043
|
-
|
10044
|
-
|
10045
|
-
|
10046
|
-
|
10047
|
-
|
10048
|
-
if (
|
10049
|
-
|
10050
|
-
|
10051
|
-
|
10052
|
-
|
10053
|
-
|
10054
|
-
|
10065
|
+
return (0, cancellationUtils_1.invalidateTypeCacheIfCanceled)(() => {
|
10066
|
+
// Pre-cache the incomplete function type in case the evaluation of the
|
10067
|
+
// lambda depends on itself.
|
10068
|
+
writeTypeCache(node, { type: functionType, isIncomplete: true }, 0 /* EvaluatorFlags.None */);
|
10069
|
+
// We assume for simplicity that the parameter signature of the lambda is
|
10070
|
+
// the same as the expected type. If this isn't the case, we'll use
|
10071
|
+
// object for any lambda parameters that don't match. We could make this
|
10072
|
+
// more sophisticated in the future, but it becomes very complex to handle
|
10073
|
+
// all of the permutations.
|
10074
|
+
let sawParamMismatch = false;
|
10075
|
+
node.parameters.forEach((param, index) => {
|
10076
|
+
let paramType;
|
10077
|
+
if (expectedParamDetails && !sawParamMismatch) {
|
10078
|
+
if (index < expectedParamDetails.params.length) {
|
10079
|
+
const expectedParam = expectedParamDetails.params[index];
|
10080
|
+
// If the parameter category matches and both of the parameters are
|
10081
|
+
// either separators (/ or *) or not separators, copy the type
|
10082
|
+
// from the expected parameter.
|
10083
|
+
if (expectedParam.param.category === param.category &&
|
10084
|
+
!param.name === !expectedParam.param.name) {
|
10085
|
+
paramType = expectedParam.type;
|
10086
|
+
}
|
10087
|
+
else {
|
10088
|
+
sawParamMismatch = true;
|
10089
|
+
}
|
10055
10090
|
}
|
10056
|
-
else {
|
10057
|
-
|
10091
|
+
else if (param.defaultValue) {
|
10092
|
+
// If the lambda param has a default value but there is no associated
|
10093
|
+
// parameter in the expected type, assume that the default value is
|
10094
|
+
// being used to explicitly capture a value from an outer scope. Infer
|
10095
|
+
// its type from the default value expression.
|
10096
|
+
paramType = getTypeOfExpression(param.defaultValue, undefined, inferenceContext).type;
|
10058
10097
|
}
|
10059
10098
|
}
|
10060
10099
|
else if (param.defaultValue) {
|
10061
|
-
// If
|
10062
|
-
//
|
10063
|
-
|
10064
|
-
|
10065
|
-
|
10066
|
-
|
10067
|
-
|
10068
|
-
|
10069
|
-
|
10070
|
-
|
10071
|
-
|
10072
|
-
|
10073
|
-
|
10074
|
-
|
10075
|
-
|
10076
|
-
|
10077
|
-
|
10078
|
-
|
10079
|
-
|
10080
|
-
|
10081
|
-
|
10082
|
-
|
10083
|
-
|
10084
|
-
|
10085
|
-
|
10086
|
-
|
10087
|
-
|
10100
|
+
// If there is no inference context but we have a default value,
|
10101
|
+
// use the default value to infer the parameter's type.
|
10102
|
+
paramType = inferParameterTypeFromDefaultValue(param.defaultValue);
|
10103
|
+
}
|
10104
|
+
if (param.name) {
|
10105
|
+
writeTypeCache(param.name, { type: transformVariadicParamType(node, param.category, paramType !== null && paramType !== void 0 ? paramType : types_1.UnknownType.create()) }, 0 /* EvaluatorFlags.None */);
|
10106
|
+
}
|
10107
|
+
if (param.defaultValue) {
|
10108
|
+
// Evaluate the default value if it's present.
|
10109
|
+
getTypeOfExpression(param.defaultValue, 1 /* EvaluatorFlags.ConvertEllipsisToAny */);
|
10110
|
+
}
|
10111
|
+
// Determine whether we need to insert an implied position-only parameter.
|
10112
|
+
// This is needed when a function's parameters are named using the old-style
|
10113
|
+
// way of specifying position-only parameters.
|
10114
|
+
if (index >= 0) {
|
10115
|
+
let isImplicitPositionOnlyParam = false;
|
10116
|
+
if (param.category === 0 /* ParameterCategory.Simple */ && param.name) {
|
10117
|
+
if ((0, symbolNameUtils_1.isPrivateName)(param.name.value)) {
|
10118
|
+
isImplicitPositionOnlyParam = true;
|
10119
|
+
}
|
10120
|
+
}
|
10121
|
+
else {
|
10122
|
+
paramsArePositionOnly = false;
|
10123
|
+
}
|
10124
|
+
if (paramsArePositionOnly &&
|
10125
|
+
!isImplicitPositionOnlyParam &&
|
10126
|
+
functionType.details.parameters.length > 0) {
|
10127
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
10128
|
+
}
|
10129
|
+
if (!isImplicitPositionOnlyParam) {
|
10130
|
+
paramsArePositionOnly = false;
|
10088
10131
|
}
|
10089
10132
|
}
|
10090
|
-
|
10091
|
-
|
10092
|
-
|
10093
|
-
|
10094
|
-
|
10095
|
-
|
10096
|
-
types_1.
|
10133
|
+
const functionParam = {
|
10134
|
+
category: param.category,
|
10135
|
+
name: param.name ? param.name.value : undefined,
|
10136
|
+
hasDefault: !!param.defaultValue,
|
10137
|
+
defaultValueExpression: param.defaultValue,
|
10138
|
+
hasDeclaredType: true,
|
10139
|
+
type: paramType !== null && paramType !== void 0 ? paramType : types_1.UnknownType.create(),
|
10140
|
+
};
|
10141
|
+
types_1.FunctionType.addParameter(functionType, functionParam);
|
10142
|
+
});
|
10143
|
+
if (paramsArePositionOnly && functionType.details.parameters.length > 0) {
|
10144
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
10145
|
+
}
|
10146
|
+
let typeErrors = false;
|
10147
|
+
// If we're speculatively evaluating the lambda, create another speculative
|
10148
|
+
// evaluation scope for the return expression and do not allow retention
|
10149
|
+
// of the cached types.
|
10150
|
+
// We need to set allowCacheRetention to false because we don't want to
|
10151
|
+
// cache the type of the lambda return expression because it depends on
|
10152
|
+
// the parameter types that we set above, and the speculative type cache
|
10153
|
+
// doesn't know about that context.
|
10154
|
+
useSpeculativeMode(forceSpeculative || isSpeculativeModeInUse(node) || (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete)
|
10155
|
+
? node.expression
|
10156
|
+
: undefined, () => {
|
10157
|
+
const returnTypeResult = getTypeOfExpression(node.expression,
|
10158
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedReturnType));
|
10159
|
+
functionType.inferredReturnType = returnTypeResult.type;
|
10160
|
+
if (returnTypeResult.isIncomplete) {
|
10161
|
+
isIncomplete = true;
|
10097
10162
|
}
|
10098
|
-
if (
|
10099
|
-
|
10163
|
+
if (returnTypeResult.typeErrors) {
|
10164
|
+
typeErrors = true;
|
10100
10165
|
}
|
10101
|
-
|
10102
|
-
|
10103
|
-
|
10104
|
-
|
10105
|
-
|
10106
|
-
|
10107
|
-
|
10108
|
-
|
10109
|
-
|
10110
|
-
|
10111
|
-
|
10112
|
-
if (paramsArePositionOnly && functionType.details.parameters.length > 0) {
|
10113
|
-
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
10114
|
-
}
|
10115
|
-
let typeErrors = false;
|
10116
|
-
// If we're speculatively evaluating the lambda, create another speculative
|
10117
|
-
// evaluation scope for the return expression and do not allow retention
|
10118
|
-
// of the cached types.
|
10119
|
-
// We need to set allowCacheRetention to false because we don't want to
|
10120
|
-
// cache the type of the lambda return expression because it depends on
|
10121
|
-
// the parameter types that we set above, and the speculative type cache
|
10122
|
-
// doesn't know about that context.
|
10123
|
-
useSpeculativeMode(forceSpeculative || isSpeculativeModeInUse(node) || (inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete)
|
10124
|
-
? node.expression
|
10125
|
-
: undefined, () => {
|
10126
|
-
const returnTypeResult = getTypeOfExpression(node.expression,
|
10127
|
-
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedReturnType));
|
10128
|
-
functionType.inferredReturnType = returnTypeResult.type;
|
10129
|
-
if (returnTypeResult.isIncomplete) {
|
10130
|
-
isIncomplete = true;
|
10131
|
-
}
|
10132
|
-
if (returnTypeResult.typeErrors) {
|
10133
|
-
typeErrors = true;
|
10134
|
-
}
|
10135
|
-
else if (expectedReturnType) {
|
10136
|
-
// If the expectedReturnType is generic, see if the actual return type
|
10137
|
-
// provides types for some or all type variables.
|
10138
|
-
if ((0, typeUtils_1.requiresSpecialization)(expectedReturnType)) {
|
10139
|
-
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(functionType));
|
10140
|
-
if (assignType(expectedReturnType, returnTypeResult.type, /* diag */ undefined, typeVarContext)) {
|
10141
|
-
functionType = (0, typeUtils_1.applySolvedTypeVars)(functionType, typeVarContext, {
|
10142
|
-
applyInScopePlaceholders: true,
|
10143
|
-
});
|
10166
|
+
else if (expectedReturnType) {
|
10167
|
+
// If the expectedReturnType is generic, see if the actual return type
|
10168
|
+
// provides types for some or all type variables.
|
10169
|
+
if ((0, typeUtils_1.requiresSpecialization)(expectedReturnType)) {
|
10170
|
+
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(functionType));
|
10171
|
+
if (assignType(expectedReturnType, returnTypeResult.type,
|
10172
|
+
/* diag */ undefined, typeVarContext)) {
|
10173
|
+
functionType = (0, typeUtils_1.applySolvedTypeVars)(functionType, typeVarContext, {
|
10174
|
+
applyInScopePlaceholders: true,
|
10175
|
+
});
|
10176
|
+
}
|
10144
10177
|
}
|
10145
10178
|
}
|
10179
|
+
}, {
|
10180
|
+
dependentType: inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType,
|
10181
|
+
allowDiagnostics: !forceSpeculative &&
|
10182
|
+
!isDiagnosticSuppressedForNode(node) &&
|
10183
|
+
!(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete),
|
10184
|
+
});
|
10185
|
+
// Mark the function type as no longer being evaluated.
|
10186
|
+
functionType.details.flags &= ~131072 /* FunctionTypeFlags.PartiallyEvaluated */;
|
10187
|
+
// Is the resulting function compatible with the expected type?
|
10188
|
+
if (expectedType && !assignType(expectedType, functionType)) {
|
10189
|
+
typeErrors = true;
|
10146
10190
|
}
|
10147
|
-
|
10148
|
-
dependentType: inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType,
|
10149
|
-
allowDiagnostics: !forceSpeculative && !isDiagnosticSuppressedForNode(node) && !(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete),
|
10191
|
+
return { type: functionType, isIncomplete, typeErrors };
|
10150
10192
|
});
|
10151
|
-
// Mark the function type as no longer being evaluated.
|
10152
|
-
functionType.details.flags &= ~131072 /* FunctionTypeFlags.PartiallyEvaluated */;
|
10153
|
-
// Is the resulting function compatible with the expected type?
|
10154
|
-
if (expectedType && !assignType(expectedType, functionType)) {
|
10155
|
-
typeErrors = true;
|
10156
|
-
}
|
10157
|
-
return { type: functionType, isIncomplete, typeErrors };
|
10158
10193
|
}
|
10159
10194
|
function getTypeOfComprehension(node, flags, inferenceContext) {
|
10160
10195
|
let isIncomplete = false;
|
@@ -10229,16 +10264,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10229
10264
|
const iterableType = stripLiteralValue(iterableTypeResult.type);
|
10230
10265
|
const itemTypeResult = (_a = getTypeOfIterator({ type: iterableType, isIncomplete: iterableTypeResult.isIncomplete }, !!node.isAsync, node.iterableExpression)) !== null && _a !== void 0 ? _a : { type: types_1.UnknownType.create(), isIncomplete: iterableTypeResult.isIncomplete };
|
10231
10266
|
const targetExpr = node.targetExpression;
|
10232
|
-
assignTypeToExpression(targetExpr, itemTypeResult
|
10267
|
+
assignTypeToExpression(targetExpr, itemTypeResult, node.iterableExpression);
|
10233
10268
|
}
|
10234
10269
|
else {
|
10235
10270
|
(0, debug_1.assert)(node.nodeType === 13 /* ParseNodeType.ComprehensionIf */);
|
10236
10271
|
// Evaluate the test expression to validate it and mark symbols
|
10237
|
-
// as referenced.
|
10238
|
-
//
|
10239
|
-
|
10240
|
-
|
10241
|
-
|
10272
|
+
// as referenced. This doesn't affect the type of the evaluated
|
10273
|
+
// comprehension, but it is important for evaluating intermediate
|
10274
|
+
// expressions such as assignment expressions that can affect other
|
10275
|
+
// subexpressions.
|
10276
|
+
getTypeOfExpression(node.testExpression);
|
10242
10277
|
}
|
10243
10278
|
return isIncomplete;
|
10244
10279
|
}
|
@@ -10370,6 +10405,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10370
10405
|
// The second argument, if present, should specify the return type.
|
10371
10406
|
function createCallableType(classType, typeArgs, errorNode) {
|
10372
10407
|
const functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
|
10408
|
+
let paramSpec;
|
10373
10409
|
functionType.specialForm = classType;
|
10374
10410
|
functionType.details.declaredReturnType = types_1.UnknownType.create();
|
10375
10411
|
functionType.details.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(errorNode);
|
@@ -10424,10 +10460,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10424
10460
|
}
|
10425
10461
|
else if ((0, typeUtils_1.isEllipsisType)(typeArgs[0].type)) {
|
10426
10462
|
types_1.FunctionType.addDefaultParameters(functionType);
|
10427
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.
|
10463
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
10428
10464
|
}
|
10429
10465
|
else if ((0, types_1.isParamSpec)(typeArgs[0].type)) {
|
10430
|
-
|
10466
|
+
paramSpec = typeArgs[0].type;
|
10431
10467
|
}
|
10432
10468
|
else {
|
10433
10469
|
if ((0, types_1.isInstantiableClass)(typeArgs[0].type) && types_1.ClassType.isBuiltIn(typeArgs[0].type, 'Concatenate')) {
|
@@ -10437,11 +10473,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10437
10473
|
if (index === concatTypeArgs.length - 1) {
|
10438
10474
|
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
10439
10475
|
if ((0, types_1.isParamSpec)(typeArg)) {
|
10440
|
-
|
10476
|
+
paramSpec = typeArg;
|
10441
10477
|
}
|
10442
10478
|
else if ((0, typeUtils_1.isEllipsisType)(typeArg)) {
|
10443
10479
|
types_1.FunctionType.addDefaultParameters(functionType);
|
10444
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.
|
10480
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
10445
10481
|
}
|
10446
10482
|
}
|
10447
10483
|
else {
|
@@ -10477,7 +10513,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10477
10513
|
}
|
10478
10514
|
else {
|
10479
10515
|
types_1.FunctionType.addDefaultParameters(functionType, /* useUnknown */ true);
|
10480
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.
|
10516
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
10517
|
+
}
|
10518
|
+
if (paramSpec) {
|
10519
|
+
types_1.FunctionType.addParamSpecVariadics(functionType, paramSpec);
|
10481
10520
|
}
|
10482
10521
|
return functionType;
|
10483
10522
|
}
|
@@ -10866,8 +10905,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10866
10905
|
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
|
10867
10906
|
}
|
10868
10907
|
function createAnnotatedType(classType, errorNode, typeArgs) {
|
10869
|
-
if (typeArgs
|
10870
|
-
|
10908
|
+
if (typeArgs) {
|
10909
|
+
if (typeArgs.length < 2) {
|
10910
|
+
addError(localize_1.LocMessage.annotatedTypeArgMissing(), errorNode);
|
10911
|
+
}
|
10912
|
+
else {
|
10913
|
+
validateAnnotatedMetadata(errorNode, typeArgs[0].type, typeArgs.slice(1));
|
10914
|
+
}
|
10871
10915
|
}
|
10872
10916
|
if (!typeArgs || typeArgs.length === 0) {
|
10873
10917
|
return { type: types_1.AnyType.create() };
|
@@ -10882,6 +10926,46 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10882
10926
|
isNotRequired: typeArgs[0].isNotRequired,
|
10883
10927
|
};
|
10884
10928
|
}
|
10929
|
+
// Enforces metadata consistency as specified in PEP 746.
|
10930
|
+
function validateAnnotatedMetadata(errorNode, annotatedType, metaArgs) {
|
10931
|
+
var _a;
|
10932
|
+
// This is an experimental feature because PEP 746 hasn't been accepted.
|
10933
|
+
if (!AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.enableExperimentalFeatures) {
|
10934
|
+
return;
|
10935
|
+
}
|
10936
|
+
for (const metaArg of metaArgs) {
|
10937
|
+
if ((0, types_1.isClass)(metaArg.type)) {
|
10938
|
+
const supportsTypeMethod = (_a = getTypeOfBoundMember(
|
10939
|
+
/* errorNode */ undefined, metaArg.type, '__supports_type__')) === null || _a === void 0 ? void 0 : _a.type;
|
10940
|
+
if (!supportsTypeMethod) {
|
10941
|
+
continue;
|
10942
|
+
}
|
10943
|
+
// "Call" the __supports_type__ method to determine if the type is supported.
|
10944
|
+
const callResult = useSpeculativeMode(errorNode, () => validateCallArguments(errorNode, [
|
10945
|
+
{
|
10946
|
+
argumentCategory: 0 /* ArgumentCategory.Simple */,
|
10947
|
+
typeResult: { type: (0, typeUtils_1.convertToInstance)(annotatedType) },
|
10948
|
+
},
|
10949
|
+
], { type: supportsTypeMethod },
|
10950
|
+
/* typeVarContext */ undefined,
|
10951
|
+
/* skipUnknownArgCheck */ true,
|
10952
|
+
/* inferenceContext */ undefined,
|
10953
|
+
/* signatureTracker */ undefined));
|
10954
|
+
if (callResult.isTypeIncomplete || !callResult.returnType) {
|
10955
|
+
continue;
|
10956
|
+
}
|
10957
|
+
// If there are no errors and the return type is potentially truthy,
|
10958
|
+
// we know that the type is supported by this metadata object.
|
10959
|
+
if (!callResult.argumentErrors && canBeTruthy(callResult.returnType)) {
|
10960
|
+
continue;
|
10961
|
+
}
|
10962
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.annotatedMetadataInconsistent().format({
|
10963
|
+
metadataType: printType(metaArg.type),
|
10964
|
+
type: printType((0, typeUtils_1.convertToInstance)(annotatedType)),
|
10965
|
+
}), metaArg.node);
|
10966
|
+
}
|
10967
|
+
}
|
10968
|
+
}
|
10885
10969
|
// Creates one of several "special" types that are defined in typing.pyi
|
10886
10970
|
// but not declared in their entirety. This includes the likes of "Tuple",
|
10887
10971
|
// "Dict", etc.
|
@@ -11113,10 +11197,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11113
11197
|
// Determine if there are any generic type parameters associated
|
11114
11198
|
// with this type alias.
|
11115
11199
|
typeParameters = [];
|
11116
|
-
(0, typeUtils_1.
|
11117
|
-
(0, debug_1.assert)(typeParameters !== undefined);
|
11118
|
-
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParameters, (0, typeUtils_1.getTypeVarArgumentsRecursive)(subtype));
|
11119
|
-
});
|
11200
|
+
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParameters, (0, typeUtils_1.getTypeVarArgumentsRecursive)(type));
|
11120
11201
|
// Don't include any synthesized type variables.
|
11121
11202
|
typeParameters = typeParameters.filter((typeVar) => !typeVar.details.isSynthesized);
|
11122
11203
|
}
|
@@ -11185,7 +11266,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11185
11266
|
/* declaredMetaclass */ undefined,
|
11186
11267
|
/* effectiveMetaclass */ undefined);
|
11187
11268
|
if (aliasMapEntry.isSpecialForm) {
|
11188
|
-
specialClassType.details.flags |=
|
11269
|
+
specialClassType.details.flags |= 8388608 /* ClassTypeFlags.SpecialFormClass */;
|
11189
11270
|
}
|
11190
11271
|
// Synthesize a single type parameter with the specified variance if
|
11191
11272
|
// specified in the alias map entry.
|
@@ -11198,7 +11279,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11198
11279
|
const specialBuiltInClassDeclaration = ((_a = AnalyzerNodeInfo.getDeclaration(node)) !== null && _a !== void 0 ? _a : (node.parent ? AnalyzerNodeInfo.getDeclaration(node.parent) : undefined));
|
11199
11280
|
specialClassType.details.declaration = specialBuiltInClassDeclaration;
|
11200
11281
|
if (fileInfo.isTypingExtensionsStubFile) {
|
11201
|
-
specialClassType.details.flags |=
|
11282
|
+
specialClassType.details.flags |= 4096 /* ClassTypeFlags.TypingExtensionClass */;
|
11202
11283
|
}
|
11203
11284
|
const baseClassName = aliasMapEntry.alias || 'object';
|
11204
11285
|
let baseClass;
|
@@ -11218,7 +11299,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11218
11299
|
// cause the TypedDict to be marked as abstract.
|
11219
11300
|
if ((0, types_1.isInstantiableClass)(baseClass) && types_1.ClassType.isBuiltIn(baseClass, '_TypedDict')) {
|
11220
11301
|
baseClass = types_1.ClassType.cloneWithNewFlags(baseClass, baseClass.details.flags &
|
11221
|
-
~(
|
11302
|
+
~(64 /* ClassTypeFlags.SupportsAbstractMethods */ | 1048576 /* ClassTypeFlags.TypeCheckOnly */));
|
11222
11303
|
}
|
11223
11304
|
}
|
11224
11305
|
}
|
@@ -11461,7 +11542,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11461
11542
|
}
|
11462
11543
|
}
|
11463
11544
|
}
|
11464
|
-
assignTypeToExpression(node.leftExpression, rightHandType, isIncomplete, node.rightExpression,
|
11545
|
+
assignTypeToExpression(node.leftExpression, { type: rightHandType, isIncomplete }, node.rightExpression,
|
11465
11546
|
/* ignoreEmptyContainers */ true,
|
11466
11547
|
/* allowAssignmentToFinalVar */ true, expectedTypeDiagAddendum);
|
11467
11548
|
writeTypeCache(node, { type: rightHandType, isIncomplete }, 0 /* EvaluatorFlags.None */);
|
@@ -11593,17 +11674,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11593
11674
|
fileInfo.isTypeshedStubFile) {
|
11594
11675
|
classFlags |= 1 /* ClassTypeFlags.BuiltInClass */;
|
11595
11676
|
if (fileInfo.isTypingExtensionsStubFile) {
|
11596
|
-
classFlags |=
|
11677
|
+
classFlags |= 4096 /* ClassTypeFlags.TypingExtensionClass */;
|
11597
11678
|
}
|
11598
11679
|
if (node.name.value === 'property') {
|
11599
|
-
classFlags |=
|
11680
|
+
classFlags |= 128 /* ClassTypeFlags.PropertyClass */;
|
11600
11681
|
}
|
11601
11682
|
if (node.name.value === 'tuple') {
|
11602
|
-
classFlags |=
|
11683
|
+
classFlags |= 32768 /* ClassTypeFlags.TupleClass */;
|
11603
11684
|
}
|
11604
11685
|
}
|
11605
11686
|
if (fileInfo.isStubFile) {
|
11606
|
-
classFlags |=
|
11687
|
+
classFlags |= 262144 /* ClassTypeFlags.DefinedInStub */;
|
11607
11688
|
}
|
11608
11689
|
const classType = types_1.ClassType.createInstantiable(node.name.value, ParseTreeUtils.getClassFullName(node, fileInfo.moduleName, node.name.value), fileInfo.moduleName, fileInfo.fileUri, classFlags,
|
11609
11690
|
/* typeSourceId */ 0,
|
@@ -11626,9 +11707,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11626
11707
|
if (classDecl && classSymbol) {
|
11627
11708
|
setSymbolResolutionPartialType(classSymbol, classDecl, classType);
|
11628
11709
|
}
|
11629
|
-
classType.details.flags |=
|
11710
|
+
classType.details.flags |= 8192 /* ClassTypeFlags.PartiallyEvaluated */;
|
11630
11711
|
classType.details.declaration = classDecl;
|
11631
|
-
|
11712
|
+
return (0, cancellationUtils_1.invalidateTypeCacheIfCanceled)(() => {
|
11632
11713
|
writeTypeCache(node, { type: classType }, /* flags */ undefined);
|
11633
11714
|
writeTypeCache(node.name, { type: classType }, /* flags */ undefined);
|
11634
11715
|
// Keep a list of unique type parameters that are used in the
|
@@ -11712,29 +11793,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11712
11793
|
fileInfo.executionEnvironment.pythonVersion.isLessThan(pythonVersion_1.pythonVersion3_7)) {
|
11713
11794
|
addError(localize_1.LocMessage.protocolIllegal(), arg.valueExpression);
|
11714
11795
|
}
|
11715
|
-
classType.details.flags |=
|
11796
|
+
classType.details.flags |= 512 /* ClassTypeFlags.ProtocolClass */;
|
11716
11797
|
}
|
11717
11798
|
if (types_1.ClassType.isBuiltIn(argType, 'property')) {
|
11718
|
-
classType.details.flags |=
|
11799
|
+
classType.details.flags |= 128 /* ClassTypeFlags.PropertyClass */;
|
11719
11800
|
}
|
11720
11801
|
// If the class directly derives from NamedTuple (in Python 3.6 or
|
11721
11802
|
// newer), it's considered a (read-only) dataclass.
|
11722
11803
|
if (fileInfo.executionEnvironment.pythonVersion.isGreaterOrEqualTo(pythonVersion_1.pythonVersion3_6)) {
|
11723
11804
|
if (types_1.ClassType.isBuiltIn(argType, 'NamedTuple')) {
|
11724
11805
|
isNamedTupleSubclass = true;
|
11725
|
-
classType.details.flags |=
|
11726
|
-
4 /* ClassTypeFlags.DataClass */ |
|
11727
|
-
32 /* ClassTypeFlags.SkipSynthesizedDataClassEq */ |
|
11728
|
-
33554432 /* ClassTypeFlags.ReadOnlyInstanceVariables */;
|
11806
|
+
classType.details.flags |= 524288 /* ClassTypeFlags.ReadOnlyInstanceVariables */;
|
11729
11807
|
}
|
11730
11808
|
}
|
11731
11809
|
// If the class directly derives from TypedDict or from a class that is
|
11732
11810
|
// a TypedDict, it is considered a TypedDict.
|
11733
11811
|
if (types_1.ClassType.isBuiltIn(argType, 'TypedDict') || types_1.ClassType.isTypedDictClass(argType)) {
|
11734
|
-
classType.details.flags |=
|
11812
|
+
classType.details.flags |= 4 /* ClassTypeFlags.TypedDictClass */;
|
11735
11813
|
// Propagate the "effectively closed" flag from base classes.
|
11736
11814
|
if (types_1.ClassType.isTypedDictEffectivelyClosed(argType)) {
|
11737
|
-
classType.details.flags |=
|
11815
|
+
classType.details.flags |= 16 /* ClassTypeFlags.TypedDictEffectivelyClosed */;
|
11738
11816
|
}
|
11739
11817
|
}
|
11740
11818
|
// Validate that the class isn't deriving from itself, creating a
|
@@ -11765,7 +11843,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11765
11843
|
classType.details.baseClasses.push(argType);
|
11766
11844
|
if ((0, types_1.isInstantiableClass)(argType)) {
|
11767
11845
|
if (types_1.ClassType.isEnumClass(argType)) {
|
11768
|
-
classType.details.flags |=
|
11846
|
+
classType.details.flags |= 65536 /* ClassTypeFlags.EnumClass */;
|
11769
11847
|
}
|
11770
11848
|
// Determine if the class is abstract. Protocol classes support abstract methods
|
11771
11849
|
// because they are constructed by the _ProtocolMeta metaclass, which derives
|
@@ -11774,10 +11852,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11774
11852
|
// because of dependencies on some of these built-in protocol classes.
|
11775
11853
|
if (types_1.ClassType.supportsAbstractMethods(argType) ||
|
11776
11854
|
(types_1.ClassType.isProtocolClass(argType) && !types_1.ClassType.isBuiltIn(argType))) {
|
11777
|
-
classType.details.flags |=
|
11855
|
+
classType.details.flags |= 64 /* ClassTypeFlags.SupportsAbstractMethods */;
|
11778
11856
|
}
|
11779
11857
|
if (types_1.ClassType.isPropertyClass(argType)) {
|
11780
|
-
classType.details.flags |=
|
11858
|
+
classType.details.flags |= 128 /* ClassTypeFlags.PropertyClass */;
|
11781
11859
|
}
|
11782
11860
|
if (types_1.ClassType.isFinal(argType)) {
|
11783
11861
|
const className = printObjectTypeForClass(argType);
|
@@ -11827,13 +11905,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11827
11905
|
addError(localize_1.LocMessage.typedDictBoolParam().format({ name: arg.name.value }), arg.valueExpression);
|
11828
11906
|
}
|
11829
11907
|
else if (arg.name.value === 'total' && !constArgValue) {
|
11830
|
-
classType.details.flags |=
|
11908
|
+
classType.details.flags |= 32 /* ClassTypeFlags.CanOmitDictValues */;
|
11831
11909
|
}
|
11832
11910
|
else if (arg.name.value === 'closed' && constArgValue) {
|
11833
11911
|
// This is an experimental feature because PEP 728 hasn't been accepted yet.
|
11834
11912
|
if (AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.enableExperimentalFeatures) {
|
11835
11913
|
classType.details.flags |=
|
11836
|
-
|
11914
|
+
8 /* ClassTypeFlags.TypedDictMarkedClosed */ | 16 /* ClassTypeFlags.TypedDictEffectivelyClosed */;
|
11837
11915
|
}
|
11838
11916
|
}
|
11839
11917
|
}
|
@@ -11966,7 +12044,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11966
12044
|
param.category === 0 /* ParameterCategory.Simple */ &&
|
11967
12045
|
!param.defaultValue);
|
11968
12046
|
if (genericParams.length > 0) {
|
11969
|
-
classType.details.flags |=
|
12047
|
+
classType.details.flags |= 1024 /* ClassTypeFlags.PseudoGenericClass */;
|
11970
12048
|
// Create a type parameter for each simple, named parameter
|
11971
12049
|
// in the __init__ method.
|
11972
12050
|
classType.details.typeParameters = genericParams.map((param) => {
|
@@ -11987,7 +12065,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11987
12065
|
if (classType.details.typeParameters.length === 0 && !types_1.ClassType.isBuiltIn(classType, 'type')) {
|
11988
12066
|
if (classType.details.baseClasses.some((baseClass) => (0, types_1.isInstantiableClass)(baseClass) && types_1.ClassType.hasCustomClassGetItem(baseClass)) ||
|
11989
12067
|
classType.details.fields.has('__class_getitem__')) {
|
11990
|
-
classType.details.flags |=
|
12068
|
+
classType.details.flags |= 16384 /* ClassTypeFlags.HasCustomClassGetItem */;
|
11991
12069
|
}
|
11992
12070
|
}
|
11993
12071
|
// Determine the effective metaclass.
|
@@ -12000,17 +12078,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12000
12078
|
classType.details.declaredMetaclass = metaclassType;
|
12001
12079
|
if ((0, types_1.isInstantiableClass)(metaclassType)) {
|
12002
12080
|
if ((0, enums_1.isEnumMetaclass)(metaclassType)) {
|
12003
|
-
classType.details.flags |=
|
12081
|
+
classType.details.flags |= 65536 /* ClassTypeFlags.EnumClass */;
|
12004
12082
|
}
|
12005
12083
|
if (types_1.ClassType.isBuiltIn(metaclassType, 'ABCMeta')) {
|
12006
|
-
classType.details.flags |=
|
12084
|
+
classType.details.flags |= 64 /* ClassTypeFlags.SupportsAbstractMethods */;
|
12007
12085
|
}
|
12008
12086
|
}
|
12009
12087
|
}
|
12010
12088
|
}
|
12011
12089
|
const effectiveMetaclass = computeEffectiveMetaclass(classType, node.name);
|
12012
12090
|
// Clear the "partially constructed" flag.
|
12013
|
-
classType.details.flags &= ~
|
12091
|
+
classType.details.flags &= ~8192 /* ClassTypeFlags.PartiallyEvaluated */;
|
12014
12092
|
// Now determine the decorated type of the class.
|
12015
12093
|
let decoratedType = classType;
|
12016
12094
|
let foundUnknown = false;
|
@@ -12047,7 +12125,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12047
12125
|
}
|
12048
12126
|
}
|
12049
12127
|
if (dataClassBehaviors) {
|
12050
|
-
(0, dataClasses_1.applyDataClassDefaultBehaviors)(classType, dataClassBehaviors);
|
12051
12128
|
(0, dataClasses_1.applyDataClassClassBehaviorOverrides)(evaluatorInterface, node.name, classType, initSubclassArgs, dataClassBehaviors);
|
12052
12129
|
}
|
12053
12130
|
// Run any deferred class completions that depend on this class.
|
@@ -12075,8 +12152,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12075
12152
|
(0, typedDicts_1.synthesizeTypedDictClassMethods)(evaluatorInterface, node, classType);
|
12076
12153
|
}
|
12077
12154
|
// Synthesize dataclass methods.
|
12078
|
-
if (types_1.ClassType.isDataClass(classType)) {
|
12079
|
-
const skipSynthesizedInit = types_1.ClassType.
|
12155
|
+
if (types_1.ClassType.isDataClass(classType) || isNamedTupleSubclass) {
|
12156
|
+
const skipSynthesizedInit = types_1.ClassType.isDataClassSkipGenerateInit(classType);
|
12080
12157
|
let hasExistingInitMethod = skipSynthesizedInit;
|
12081
12158
|
// See if there's already a non-synthesized __init__ method.
|
12082
12159
|
// We shouldn't override it.
|
@@ -12142,15 +12219,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12142
12219
|
// Update the decorated class type.
|
12143
12220
|
writeTypeCache(node, { type: decoratedType }, 0 /* EvaluatorFlags.None */);
|
12144
12221
|
return { classType, decoratedType };
|
12145
|
-
}
|
12146
|
-
catch (e) {
|
12147
|
-
if (cancellationUtils_1.OperationCanceledException.is(e)) {
|
12148
|
-
// If the work was canceled before the class types were updated, the
|
12149
|
-
// class type in the type cache is in an invalid, partially-constructed state.
|
12150
|
-
e.isTypeCacheInvalid = true;
|
12151
|
-
}
|
12152
|
-
throw e;
|
12153
|
-
}
|
12222
|
+
});
|
12154
12223
|
}
|
12155
12224
|
// Determines whether the type parameters has a default that refers to another
|
12156
12225
|
// type parameter. If so, validates that it is in the list of "live" type
|
@@ -12566,7 +12635,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12566
12635
|
// Evaluates the type of a "def" statement without applying an async
|
12567
12636
|
// modifier or any decorators.
|
12568
12637
|
function getTypeOfFunctionPredecorated(node) {
|
12569
|
-
var _a
|
12638
|
+
var _a;
|
12570
12639
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
12571
12640
|
// Is this type already cached?
|
12572
12641
|
const cachedFunctionType = readTypeCache(node.name, 0 /* EvaluatorFlags.None */);
|
@@ -12621,299 +12690,303 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12621
12690
|
if (functionDecl && functionSymbol) {
|
12622
12691
|
setSymbolResolutionPartialType(functionSymbol.symbol, functionDecl, functionType);
|
12623
12692
|
}
|
12624
|
-
|
12625
|
-
|
12626
|
-
|
12627
|
-
|
12628
|
-
|
12629
|
-
|
12630
|
-
|
12631
|
-
|
12632
|
-
|
12633
|
-
|
12634
|
-
|
12635
|
-
firstCommentAnnotationIndex =
|
12636
|
-
|
12637
|
-
|
12638
|
-
|
12639
|
-
|
12640
|
-
|
12641
|
-
|
12642
|
-
|
12643
|
-
|
12644
|
-
|
12645
|
-
|
12646
|
-
|
12647
|
-
|
12648
|
-
|
12649
|
-
|
12650
|
-
|
12651
|
-
|
12652
|
-
|
12653
|
-
|
12654
|
-
|
12655
|
-
|
12656
|
-
|
12657
|
-
|
12658
|
-
|
12659
|
-
|
12660
|
-
|
12661
|
-
functionType.details.typeParameters = typeParametersSeen;
|
12662
|
-
}
|
12663
|
-
let paramsArePositionOnly = true;
|
12664
|
-
const isFirstParamClsOrSelf = containingClassType &&
|
12665
|
-
(types_1.FunctionType.isClassMethod(functionType) ||
|
12666
|
-
types_1.FunctionType.isInstanceMethod(functionType) ||
|
12667
|
-
types_1.FunctionType.isConstructorMethod(functionType));
|
12668
|
-
const firstNonClsSelfParamIndex = isFirstParamClsOrSelf ? 1 : 0;
|
12669
|
-
node.parameters.forEach((param, index) => {
|
12670
|
-
let paramType;
|
12671
|
-
let annotatedType;
|
12672
|
-
let paramTypeNode;
|
12673
|
-
if (param.name) {
|
12674
|
-
if (index === 0 && isFirstParamClsOrSelf) {
|
12675
|
-
// Mark "self/cls" as accessed.
|
12676
|
-
markParamAccessed(param);
|
12677
|
-
}
|
12678
|
-
else if (types_1.FunctionType.isAbstractMethod(functionType)) {
|
12679
|
-
// Mark all parameters in abstract methods as accessed.
|
12680
|
-
markParamAccessed(param);
|
12681
|
-
}
|
12682
|
-
else if (containingClassType && types_1.ClassType.isProtocolClass(containingClassType)) {
|
12683
|
-
// Mark all parameters in protocol methods as accessed.
|
12684
|
-
markParamAccessed(param);
|
12685
|
-
}
|
12686
|
-
}
|
12687
|
-
if (param.typeAnnotation) {
|
12688
|
-
paramTypeNode = param.typeAnnotation;
|
12689
|
-
}
|
12690
|
-
else if (param.typeAnnotationComment) {
|
12691
|
-
paramTypeNode = param.typeAnnotationComment;
|
12693
|
+
return (0, cancellationUtils_1.invalidateTypeCacheIfCanceled)(() => {
|
12694
|
+
var _a, _b;
|
12695
|
+
writeTypeCache(node.name, { type: functionType }, /* flags */ undefined);
|
12696
|
+
// Is this an "__init__" method within a pseudo-generic class? If so,
|
12697
|
+
// we'll add generic types to the constructor's parameters.
|
12698
|
+
const addGenericParamTypes = containingClassType &&
|
12699
|
+
types_1.ClassType.isPseudoGenericClass(containingClassType) &&
|
12700
|
+
node.name.value === '__init__';
|
12701
|
+
const paramTypes = [];
|
12702
|
+
// Determine if the first parameter should be skipped for comment-based
|
12703
|
+
// function annotations.
|
12704
|
+
let firstCommentAnnotationIndex = 0;
|
12705
|
+
if (containingClassType && (functionType.details.flags & 4 /* FunctionTypeFlags.StaticMethod */) === 0) {
|
12706
|
+
firstCommentAnnotationIndex = 1;
|
12707
|
+
}
|
12708
|
+
// If there is a function annotation comment, validate that it has the correct
|
12709
|
+
// number of parameter annotations.
|
12710
|
+
if (node.functionAnnotationComment && !node.functionAnnotationComment.isParamListEllipsis) {
|
12711
|
+
const expected = node.parameters.length - firstCommentAnnotationIndex;
|
12712
|
+
const received = node.functionAnnotationComment.paramTypeAnnotations.length;
|
12713
|
+
// For methods with "self" or "cls" parameters, the annotation list
|
12714
|
+
// can either include or exclude the annotation for the first parameter.
|
12715
|
+
if (firstCommentAnnotationIndex > 0 && received === node.parameters.length) {
|
12716
|
+
firstCommentAnnotationIndex = 0;
|
12717
|
+
}
|
12718
|
+
else if (received !== expected) {
|
12719
|
+
addError(localize_1.LocMessage.annotatedParamCountMismatch().format({
|
12720
|
+
expected,
|
12721
|
+
received,
|
12722
|
+
}), node.functionAnnotationComment);
|
12723
|
+
}
|
12724
|
+
}
|
12725
|
+
// If this function uses PEP 695 syntax for type parameters,
|
12726
|
+
// accumulate the list of type parameters upfront.
|
12727
|
+
const typeParametersSeen = [];
|
12728
|
+
if (node.typeParameters) {
|
12729
|
+
functionType.details.typeParameters = evaluateTypeParameterList(node.typeParameters).map((typeParam) => (0, typeUtils_1.convertToInstance)(typeParam));
|
12692
12730
|
}
|
12693
|
-
else
|
12694
|
-
|
12695
|
-
if (adjustedIndex >= 0 && adjustedIndex < node.functionAnnotationComment.paramTypeAnnotations.length) {
|
12696
|
-
paramTypeNode = node.functionAnnotationComment.paramTypeAnnotations[adjustedIndex];
|
12697
|
-
}
|
12731
|
+
else {
|
12732
|
+
functionType.details.typeParameters = typeParametersSeen;
|
12698
12733
|
}
|
12699
|
-
|
12700
|
-
|
12701
|
-
|
12734
|
+
let paramsArePositionOnly = true;
|
12735
|
+
const isFirstParamClsOrSelf = containingClassType &&
|
12736
|
+
(types_1.FunctionType.isClassMethod(functionType) ||
|
12737
|
+
types_1.FunctionType.isInstanceMethod(functionType) ||
|
12738
|
+
types_1.FunctionType.isConstructorMethod(functionType));
|
12739
|
+
const firstNonClsSelfParamIndex = isFirstParamClsOrSelf ? 1 : 0;
|
12740
|
+
node.parameters.forEach((param, index) => {
|
12741
|
+
let paramType;
|
12742
|
+
let annotatedType;
|
12743
|
+
let paramTypeNode;
|
12744
|
+
if (param.name) {
|
12745
|
+
if (index === 0 && isFirstParamClsOrSelf) {
|
12746
|
+
// Mark "self/cls" as accessed.
|
12747
|
+
markParamAccessed(param);
|
12748
|
+
}
|
12749
|
+
else if (types_1.FunctionType.isAbstractMethod(functionType)) {
|
12750
|
+
// Mark all parameters in abstract methods as accessed.
|
12751
|
+
markParamAccessed(param);
|
12752
|
+
}
|
12753
|
+
else if (containingClassType && types_1.ClassType.isProtocolClass(containingClassType)) {
|
12754
|
+
// Mark all parameters in protocol methods as accessed.
|
12755
|
+
markParamAccessed(param);
|
12756
|
+
}
|
12702
12757
|
}
|
12703
|
-
|
12704
|
-
|
12758
|
+
if (param.typeAnnotation) {
|
12759
|
+
paramTypeNode = param.typeAnnotation;
|
12705
12760
|
}
|
12706
|
-
if (
|
12707
|
-
|
12761
|
+
else if (param.typeAnnotationComment) {
|
12762
|
+
paramTypeNode = param.typeAnnotationComment;
|
12708
12763
|
}
|
12709
|
-
if (
|
12710
|
-
|
12711
|
-
|
12712
|
-
|
12713
|
-
|
12714
|
-
|
12764
|
+
else if (node.functionAnnotationComment && !node.functionAnnotationComment.isParamListEllipsis) {
|
12765
|
+
const adjustedIndex = index - firstCommentAnnotationIndex;
|
12766
|
+
if (adjustedIndex >= 0 &&
|
12767
|
+
adjustedIndex < node.functionAnnotationComment.paramTypeAnnotations.length) {
|
12768
|
+
paramTypeNode = node.functionAnnotationComment.paramTypeAnnotations[adjustedIndex];
|
12769
|
+
}
|
12715
12770
|
}
|
12716
|
-
|
12717
|
-
|
12718
|
-
|
12719
|
-
|
12720
|
-
|
12771
|
+
if (paramTypeNode) {
|
12772
|
+
if ((functionInfo.flags & 524288 /* FunctionTypeFlags.NoTypeCheck */) !== 0) {
|
12773
|
+
annotatedType = types_1.UnknownType.create();
|
12774
|
+
}
|
12775
|
+
else {
|
12776
|
+
annotatedType = getTypeOfParameterAnnotation(paramTypeNode, param.category);
|
12777
|
+
}
|
12778
|
+
if (annotatedType) {
|
12779
|
+
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParametersSeen, (0, typeUtils_1.getTypeVarArgumentsRecursive)(annotatedType), functionType.details.typeVarScopeId);
|
12780
|
+
}
|
12781
|
+
if ((0, types_1.isVariadicTypeVar)(annotatedType) && !annotatedType.isVariadicUnpacked) {
|
12782
|
+
addError(localize_1.LocMessage.unpackedTypeVarTupleExpected().format({
|
12783
|
+
name1: annotatedType.details.name,
|
12784
|
+
name2: annotatedType.details.name,
|
12785
|
+
}), paramTypeNode);
|
12786
|
+
annotatedType = types_1.UnknownType.create();
|
12787
|
+
}
|
12721
12788
|
}
|
12722
|
-
|
12723
|
-
|
12724
|
-
|
12725
|
-
|
12726
|
-
|
12789
|
+
if (!annotatedType && addGenericParamTypes) {
|
12790
|
+
if (index > 0 && param.category === 0 /* ParameterCategory.Simple */ && param.name && !param.defaultValue) {
|
12791
|
+
const typeParamName = getPseudoGenericTypeVarName(param.name.value);
|
12792
|
+
annotatedType = containingClassType.details.typeParameters.find((param) => param.details.name === typeParamName);
|
12793
|
+
}
|
12727
12794
|
}
|
12728
|
-
|
12729
|
-
|
12730
|
-
|
12731
|
-
|
12732
|
-
|
12733
|
-
// an "Any" value.
|
12734
|
-
let treatEllipsisAsAny = fileInfo.isStubFile || ParseTreeUtils.isSuiteEmpty(node.suite);
|
12735
|
-
if (containingClassType && types_1.ClassType.isProtocolClass(containingClassType)) {
|
12736
|
-
treatEllipsisAsAny = true;
|
12795
|
+
if (annotatedType) {
|
12796
|
+
const adjustedAnnotatedType = adjustParameterAnnotatedType(param, annotatedType);
|
12797
|
+
if (adjustedAnnotatedType !== annotatedType) {
|
12798
|
+
annotatedType = adjustedAnnotatedType;
|
12799
|
+
}
|
12737
12800
|
}
|
12738
|
-
|
12739
|
-
|
12801
|
+
let defaultValueType;
|
12802
|
+
if (param.defaultValue) {
|
12803
|
+
// If this is a stub file, a protocol, an overload, or a class
|
12804
|
+
// whose body is a placeholder implementation, treat a "...", as
|
12805
|
+
// an "Any" value.
|
12806
|
+
let treatEllipsisAsAny = fileInfo.isStubFile || ParseTreeUtils.isSuiteEmpty(node.suite);
|
12807
|
+
if (containingClassType && types_1.ClassType.isProtocolClass(containingClassType)) {
|
12808
|
+
treatEllipsisAsAny = true;
|
12809
|
+
}
|
12810
|
+
if (types_1.FunctionType.isOverloaded(functionType) || types_1.FunctionType.isAbstractMethod(functionType)) {
|
12811
|
+
treatEllipsisAsAny = true;
|
12812
|
+
}
|
12813
|
+
defaultValueType = getTypeOfExpression(param.defaultValue, treatEllipsisAsAny ? 1 /* EvaluatorFlags.ConvertEllipsisToAny */ : 0 /* EvaluatorFlags.None */, (0, typeUtils_1.makeInferenceContext)(annotatedType)).type;
|
12740
12814
|
}
|
12741
|
-
|
12742
|
-
|
12743
|
-
|
12744
|
-
|
12745
|
-
|
12746
|
-
|
12747
|
-
|
12748
|
-
|
12749
|
-
|
12750
|
-
|
12751
|
-
|
12815
|
+
if (annotatedType) {
|
12816
|
+
// If there was both a type annotation and a default value, verify
|
12817
|
+
// that the default value matches the annotation.
|
12818
|
+
if (param.defaultValue && defaultValueType) {
|
12819
|
+
const diagAddendum = new diagnostic_1.DiagnosticAddendum();
|
12820
|
+
const typeVarContext = new typeVarContext_1.TypeVarContext(functionType.details.typeVarScopeId);
|
12821
|
+
if (containingClassType && containingClassType.details.typeVarScopeId !== undefined) {
|
12822
|
+
if (node.name.value === '__init__' || node.name.value === '__new__') {
|
12823
|
+
typeVarContext.addSolveForScope(containingClassType.details.typeVarScopeId);
|
12824
|
+
}
|
12825
|
+
}
|
12826
|
+
if (!assignType(annotatedType, defaultValueType, diagAddendum, typeVarContext)) {
|
12827
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportArgumentType, localize_1.LocMessage.paramAssignmentMismatch().format({
|
12828
|
+
sourceType: printType(defaultValueType),
|
12829
|
+
paramType: printType(annotatedType),
|
12830
|
+
}) + diagAddendum.getString(), param.defaultValue);
|
12752
12831
|
}
|
12753
12832
|
}
|
12754
|
-
|
12755
|
-
|
12756
|
-
|
12757
|
-
|
12758
|
-
|
12759
|
-
|
12760
|
-
|
12761
|
-
|
12762
|
-
|
12763
|
-
|
12764
|
-
|
12765
|
-
|
12766
|
-
|
12767
|
-
|
12768
|
-
|
12769
|
-
|
12770
|
-
|
12771
|
-
isImplicitPositionOnlyParam = true;
|
12772
|
-
// If the parameter name indicates an implicit position-only parameter
|
12773
|
-
// but we have already seen non-position-only parameters, report an error.
|
12774
|
-
if (!paramsArePositionOnly &&
|
12775
|
-
functionType.details.parameters.every((p) => p.category === 0 /* ParameterCategory.Simple */)) {
|
12776
|
-
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.positionOnlyAfterNon(), param.name);
|
12833
|
+
paramType = annotatedType;
|
12834
|
+
}
|
12835
|
+
// Determine whether we need to insert an implied position-only parameter.
|
12836
|
+
// This is needed when a function's parameters are named using the old-style
|
12837
|
+
// way of specifying position-only parameters.
|
12838
|
+
if (index >= firstNonClsSelfParamIndex) {
|
12839
|
+
let isImplicitPositionOnlyParam = false;
|
12840
|
+
if (param.category === 0 /* ParameterCategory.Simple */ && param.name) {
|
12841
|
+
if ((0, symbolNameUtils_1.isPrivateName)(param.name.value) &&
|
12842
|
+
!node.parameters.some((p) => p.category === 0 /* ParameterCategory.Simple */ && !p.name)) {
|
12843
|
+
isImplicitPositionOnlyParam = true;
|
12844
|
+
// If the parameter name indicates an implicit position-only parameter
|
12845
|
+
// but we have already seen non-position-only parameters, report an error.
|
12846
|
+
if (!paramsArePositionOnly &&
|
12847
|
+
functionType.details.parameters.every((p) => p.category === 0 /* ParameterCategory.Simple */)) {
|
12848
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.positionOnlyAfterNon(), param.name);
|
12849
|
+
}
|
12777
12850
|
}
|
12778
12851
|
}
|
12852
|
+
else {
|
12853
|
+
paramsArePositionOnly = false;
|
12854
|
+
}
|
12855
|
+
if (paramsArePositionOnly &&
|
12856
|
+
!isImplicitPositionOnlyParam &&
|
12857
|
+
functionType.details.parameters.length > firstNonClsSelfParamIndex) {
|
12858
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
12859
|
+
}
|
12860
|
+
if (!isImplicitPositionOnlyParam) {
|
12861
|
+
paramsArePositionOnly = false;
|
12862
|
+
}
|
12863
|
+
}
|
12864
|
+
// If there was no annotation for the parameter, infer its type if possible.
|
12865
|
+
let isTypeInferred = false;
|
12866
|
+
if (!paramTypeNode) {
|
12867
|
+
isTypeInferred = true;
|
12868
|
+
const inferredType = inferParameterType(node, functionType.details.flags, index, containingClassType);
|
12869
|
+
if (inferredType) {
|
12870
|
+
paramType = inferredType;
|
12871
|
+
}
|
12872
|
+
}
|
12873
|
+
const functionParam = {
|
12874
|
+
category: param.category,
|
12875
|
+
name: param.name ? param.name.value : undefined,
|
12876
|
+
hasDefault: !!param.defaultValue,
|
12877
|
+
defaultValueExpression: param.defaultValue,
|
12878
|
+
defaultType: defaultValueType,
|
12879
|
+
type: paramType !== null && paramType !== void 0 ? paramType : types_1.UnknownType.create(),
|
12880
|
+
typeAnnotation: paramTypeNode,
|
12881
|
+
hasDeclaredType: !!paramTypeNode,
|
12882
|
+
isTypeInferred,
|
12883
|
+
};
|
12884
|
+
types_1.FunctionType.addParameter(functionType, functionParam);
|
12885
|
+
if (functionParam.hasDeclaredType) {
|
12886
|
+
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParametersSeen, (0, typeUtils_1.getTypeVarArgumentsRecursive)(functionParam.type), functionType.details.typeVarScopeId);
|
12779
12887
|
}
|
12780
|
-
|
12781
|
-
|
12888
|
+
if (param.name) {
|
12889
|
+
const variadicParamType = transformVariadicParamType(node, param.category, functionParam.type);
|
12890
|
+
paramTypes.push(variadicParamType);
|
12782
12891
|
}
|
12783
|
-
|
12784
|
-
|
12785
|
-
functionType.details.parameters.length > firstNonClsSelfParamIndex) {
|
12786
|
-
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
12892
|
+
else {
|
12893
|
+
paramTypes.push(functionParam.type);
|
12787
12894
|
}
|
12788
|
-
|
12789
|
-
|
12790
|
-
|
12791
|
-
}
|
12792
|
-
// If there was no annotation for the parameter, infer its type if possible.
|
12793
|
-
let isTypeInferred = false;
|
12794
|
-
if (!paramTypeNode) {
|
12795
|
-
isTypeInferred = true;
|
12796
|
-
const inferredType = inferParameterType(node, functionType.details.flags, index, containingClassType);
|
12797
|
-
if (inferredType) {
|
12798
|
-
paramType = inferredType;
|
12799
|
-
}
|
12800
|
-
}
|
12801
|
-
const functionParam = {
|
12802
|
-
category: param.category,
|
12803
|
-
name: param.name ? param.name.value : undefined,
|
12804
|
-
hasDefault: !!param.defaultValue,
|
12805
|
-
defaultValueExpression: param.defaultValue,
|
12806
|
-
defaultType: defaultValueType,
|
12807
|
-
type: paramType !== null && paramType !== void 0 ? paramType : types_1.UnknownType.create(),
|
12808
|
-
typeAnnotation: paramTypeNode,
|
12809
|
-
hasDeclaredType: !!paramTypeNode,
|
12810
|
-
isTypeInferred,
|
12811
|
-
};
|
12812
|
-
types_1.FunctionType.addParameter(functionType, functionParam);
|
12813
|
-
if (functionParam.hasDeclaredType) {
|
12814
|
-
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParametersSeen, (0, typeUtils_1.getTypeVarArgumentsRecursive)(functionParam.type), functionType.details.typeVarScopeId);
|
12815
|
-
}
|
12816
|
-
if (param.name) {
|
12817
|
-
const variadicParamType = transformVariadicParamType(node, param.category, functionParam.type);
|
12818
|
-
paramTypes.push(variadicParamType);
|
12819
|
-
}
|
12820
|
-
else {
|
12821
|
-
paramTypes.push(functionParam.type);
|
12895
|
+
});
|
12896
|
+
if (paramsArePositionOnly && functionType.details.parameters.length > firstNonClsSelfParamIndex) {
|
12897
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
12822
12898
|
}
|
12823
|
-
|
12824
|
-
|
12825
|
-
|
12826
|
-
|
12827
|
-
|
12828
|
-
|
12829
|
-
|
12830
|
-
|
12831
|
-
if ((0, types_1.isUnknown)(paramType)) {
|
12832
|
-
functionType.details.flags |= 16384 /* FunctionTypeFlags.UnannotatedParams */;
|
12899
|
+
// Update the types for the nodes associated with the parameters.
|
12900
|
+
paramTypes.forEach((paramType, index) => {
|
12901
|
+
const paramNameNode = node.parameters[index].name;
|
12902
|
+
if (paramNameNode) {
|
12903
|
+
if ((0, types_1.isUnknown)(paramType)) {
|
12904
|
+
functionType.details.flags |= 16384 /* FunctionTypeFlags.UnannotatedParams */;
|
12905
|
+
}
|
12906
|
+
writeTypeCache(paramNameNode, { type: paramType }, 0 /* EvaluatorFlags.None */);
|
12833
12907
|
}
|
12834
|
-
writeTypeCache(paramNameNode, { type: paramType }, 0 /* EvaluatorFlags.None */);
|
12835
|
-
}
|
12836
|
-
});
|
12837
|
-
// If the function ends in P.args and P.kwargs parameters, make it exempt from
|
12838
|
-
// args/kwargs compatibility checks. This is important for protocol comparisons.
|
12839
|
-
if (paramTypes.length >= 2) {
|
12840
|
-
const paramType1 = paramTypes[paramTypes.length - 2];
|
12841
|
-
const paramType2 = paramTypes[paramTypes.length - 1];
|
12842
|
-
if ((0, types_1.isParamSpec)(paramType1) &&
|
12843
|
-
paramType1.paramSpecAccess === 'args' &&
|
12844
|
-
(0, types_1.isParamSpec)(paramType2) &&
|
12845
|
-
paramType2.paramSpecAccess === 'kwargs') {
|
12846
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.SkipArgsKwargsCompatibilityCheck */;
|
12847
|
-
}
|
12848
|
-
}
|
12849
|
-
// If the function contains an *args and a **kwargs parameter and both
|
12850
|
-
// are annotated as Any or are unannotated, make it exempt from
|
12851
|
-
// args/kwargs compatibility checks.
|
12852
|
-
const variadicsWithAnyType = functionType.details.parameters.filter((param) => param.category !== 0 /* ParameterCategory.Simple */ && param.name && (0, types_1.isAnyOrUnknown)(param.type));
|
12853
|
-
if (variadicsWithAnyType.length >= 2) {
|
12854
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.SkipArgsKwargsCompatibilityCheck */;
|
12855
|
-
}
|
12856
|
-
// If there was a defined return type, analyze that first so when we
|
12857
|
-
// walk the contents of the function, return statements can be
|
12858
|
-
// validated against this type.
|
12859
|
-
const returnTypeAnnotationNode = (_b = node.returnTypeAnnotation) !== null && _b !== void 0 ? _b : (_c = node.functionAnnotationComment) === null || _c === void 0 ? void 0 : _c.returnTypeAnnotation;
|
12860
|
-
if (returnTypeAnnotationNode) {
|
12861
|
-
// Temporarily set the return type to unknown in case of recursion.
|
12862
|
-
functionType.details.declaredReturnType = types_1.UnknownType.create();
|
12863
|
-
const returnType = getTypeOfAnnotation(returnTypeAnnotationNode, {
|
12864
|
-
associateTypeVarsWithScope: true,
|
12865
12908
|
});
|
12866
|
-
|
12867
|
-
|
12868
|
-
|
12869
|
-
|
12870
|
-
|
12871
|
-
|
12872
|
-
|
12873
|
-
|
12874
|
-
|
12875
|
-
|
12876
|
-
|
12877
|
-
|
12878
|
-
|
12879
|
-
|
12909
|
+
// If the function ends in P.args and P.kwargs parameters, make it exempt from
|
12910
|
+
// args/kwargs compatibility checks. This is important for protocol comparisons.
|
12911
|
+
if (paramTypes.length >= 2) {
|
12912
|
+
const paramType1 = paramTypes[paramTypes.length - 2];
|
12913
|
+
const paramType2 = paramTypes[paramTypes.length - 1];
|
12914
|
+
if ((0, types_1.isParamSpec)(paramType1) &&
|
12915
|
+
paramType1.paramSpecAccess === 'args' &&
|
12916
|
+
(0, types_1.isParamSpec)(paramType2) &&
|
12917
|
+
paramType2.paramSpecAccess === 'kwargs') {
|
12918
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
12919
|
+
}
|
12920
|
+
}
|
12921
|
+
// If the function contains an *args and a **kwargs parameter and both
|
12922
|
+
// are annotated as Any or are unannotated, make it exempt from
|
12923
|
+
// args/kwargs compatibility checks.
|
12924
|
+
const variadicsWithAnyType = functionType.details.parameters.filter((param) => param.category !== 0 /* ParameterCategory.Simple */ && param.name && (0, types_1.isAnyOrUnknown)(param.type));
|
12925
|
+
if (variadicsWithAnyType.length >= 2) {
|
12926
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
12927
|
+
}
|
12928
|
+
// If there was a defined return type, analyze that first so when we
|
12929
|
+
// walk the contents of the function, return statements can be
|
12930
|
+
// validated against this type.
|
12931
|
+
const returnTypeAnnotationNode = (_a = node.returnTypeAnnotation) !== null && _a !== void 0 ? _a : (_b = node.functionAnnotationComment) === null || _b === void 0 ? void 0 : _b.returnTypeAnnotation;
|
12932
|
+
if (returnTypeAnnotationNode) {
|
12933
|
+
// Temporarily set the return type to unknown in case of recursion.
|
12934
|
+
functionType.details.declaredReturnType = types_1.UnknownType.create();
|
12935
|
+
const returnType = getTypeOfAnnotation(returnTypeAnnotationNode, {
|
12936
|
+
associateTypeVarsWithScope: true,
|
12937
|
+
});
|
12938
|
+
functionType.details.declaredReturnType = returnType;
|
12939
|
+
}
|
12940
|
+
else {
|
12941
|
+
// If there was no return type annotation and this is a type stub,
|
12942
|
+
// we have no opportunity to infer the return type, so we'll indicate
|
12943
|
+
// that it's unknown.
|
12944
|
+
if (fileInfo.isStubFile) {
|
12945
|
+
// Special-case the __init__ method, which is commonly left without
|
12946
|
+
// an annotated return type, but we can assume it returns None.
|
12947
|
+
if (node.name.value === '__init__') {
|
12948
|
+
functionType.details.declaredReturnType = getNoneType();
|
12949
|
+
}
|
12950
|
+
else {
|
12951
|
+
functionType.details.declaredReturnType = types_1.UnknownType.create();
|
12952
|
+
}
|
12880
12953
|
}
|
12881
12954
|
}
|
12882
|
-
|
12883
|
-
|
12884
|
-
|
12885
|
-
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParametersSeen, (0, typeUtils_1.getTypeVarArgumentsRecursive)(functionType.details.declaredReturnType), functionType.details.typeVarScopeId);
|
12886
|
-
}
|
12887
|
-
// If the return type is explicitly annotated as a generator, mark the
|
12888
|
-
// function as a generator even though it may not contain a "yield" statement.
|
12889
|
-
// This is important for generator functions declared in stub files, abstract
|
12890
|
-
// methods or protocol definitions.
|
12891
|
-
if (fileInfo.isStubFile || ParseTreeUtils.isSuiteEmpty(node.suite)) {
|
12892
|
-
if (functionType.details.declaredReturnType &&
|
12893
|
-
(0, types_1.isClassInstance)(functionType.details.declaredReturnType) &&
|
12894
|
-
types_1.ClassType.isBuiltIn(functionType.details.declaredReturnType, [
|
12895
|
-
'Generator',
|
12896
|
-
'AsyncGenerator',
|
12897
|
-
'AwaitableGenerator',
|
12898
|
-
])) {
|
12899
|
-
functionType.details.flags |= 16 /* FunctionTypeFlags.Generator */;
|
12955
|
+
// Accumulate any type parameters used in the return type.
|
12956
|
+
if (functionType.details.declaredReturnType && returnTypeAnnotationNode) {
|
12957
|
+
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParametersSeen, (0, typeUtils_1.getTypeVarArgumentsRecursive)(functionType.details.declaredReturnType), functionType.details.typeVarScopeId);
|
12900
12958
|
}
|
12901
|
-
|
12902
|
-
|
12903
|
-
|
12904
|
-
|
12905
|
-
|
12906
|
-
|
12907
|
-
|
12908
|
-
|
12959
|
+
// If the return type is explicitly annotated as a generator, mark the
|
12960
|
+
// function as a generator even though it may not contain a "yield" statement.
|
12961
|
+
// This is important for generator functions declared in stub files, abstract
|
12962
|
+
// methods or protocol definitions.
|
12963
|
+
if (fileInfo.isStubFile || ParseTreeUtils.isSuiteEmpty(node.suite)) {
|
12964
|
+
if (functionType.details.declaredReturnType &&
|
12965
|
+
(0, types_1.isClassInstance)(functionType.details.declaredReturnType) &&
|
12966
|
+
types_1.ClassType.isBuiltIn(functionType.details.declaredReturnType, [
|
12967
|
+
'Generator',
|
12968
|
+
'AsyncGenerator',
|
12969
|
+
'AwaitableGenerator',
|
12970
|
+
])) {
|
12971
|
+
functionType.details.flags |= 16 /* FunctionTypeFlags.Generator */;
|
12972
|
+
}
|
12909
12973
|
}
|
12910
|
-
|
12974
|
+
// Validate the default types for all type parameters.
|
12975
|
+
functionType.details.typeParameters.forEach((typeParam, index) => {
|
12976
|
+
var _a;
|
12977
|
+
let bestErrorNode = node.name;
|
12978
|
+
if (node.typeParameters && index < node.typeParameters.parameters.length) {
|
12979
|
+
const typeParamNode = node.typeParameters.parameters[index];
|
12980
|
+
bestErrorNode = (_a = typeParamNode.defaultExpression) !== null && _a !== void 0 ? _a : typeParamNode.name;
|
12981
|
+
}
|
12982
|
+
validateTypeParameterDefault(bestErrorNode, typeParam, functionType.details.typeParameters.slice(0, index), functionType.details.typeVarScopeId);
|
12983
|
+
});
|
12984
|
+
// Clear the "partially evaluated" flag to indicate that the functionType
|
12985
|
+
// is fully evaluated.
|
12986
|
+
functionType.details.flags &= ~131072 /* FunctionTypeFlags.PartiallyEvaluated */;
|
12987
|
+
writeTypeCache(node.name, { type: functionType }, 0 /* EvaluatorFlags.None */);
|
12988
|
+
return functionType;
|
12911
12989
|
});
|
12912
|
-
// Clear the "partially evaluated" flag to indicate that the functionType
|
12913
|
-
// is fully evaluated.
|
12914
|
-
functionType.details.flags &= ~131072 /* FunctionTypeFlags.PartiallyEvaluated */;
|
12915
|
-
writeTypeCache(node.name, { type: functionType }, 0 /* EvaluatorFlags.None */);
|
12916
|
-
return functionType;
|
12917
12990
|
}
|
12918
12991
|
function markParamAccessed(param) {
|
12919
12992
|
if (param.name) {
|
@@ -13226,8 +13299,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13226
13299
|
const iteratorTypeResult = getTypeOfExpression(yieldNode.expression);
|
13227
13300
|
if ((0, types_1.isClassInstance)(iteratorTypeResult.type) &&
|
13228
13301
|
types_1.ClassType.isBuiltIn(iteratorTypeResult.type, 'Coroutine')) {
|
13302
|
+
const yieldType = iteratorTypeResult.type.typeArguments &&
|
13303
|
+
iteratorTypeResult.type.typeArguments.length > 0
|
13304
|
+
? iteratorTypeResult.type.typeArguments[0]
|
13305
|
+
: types_1.UnknownType.create();
|
13229
13306
|
// Handle old-style (pre-await) Coroutines.
|
13230
|
-
inferredYieldTypes.push();
|
13307
|
+
inferredYieldTypes.push(yieldType);
|
13231
13308
|
useAwaitableGenerator = true;
|
13232
13309
|
}
|
13233
13310
|
else {
|
@@ -13253,9 +13330,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13253
13330
|
}
|
13254
13331
|
});
|
13255
13332
|
}
|
13256
|
-
if (inferredYieldTypes.length === 0) {
|
13257
|
-
inferredYieldTypes.push(getNoneType());
|
13258
|
-
}
|
13259
13333
|
const inferredYieldType = (0, types_1.combineTypes)(inferredYieldTypes);
|
13260
13334
|
// Inferred yield types need to be wrapped in a Generator or
|
13261
13335
|
// AwaitableGenerator to produce the final result.
|
@@ -13322,7 +13396,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13322
13396
|
}
|
13323
13397
|
const iteratorTypeResult = getTypeOfExpression(node.iterableExpression);
|
13324
13398
|
const iteratedType = (_b = (_a = getTypeOfIterator(iteratorTypeResult, !!node.isAsync, node.iterableExpression)) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : types_1.UnknownType.create();
|
13325
|
-
assignTypeToExpression(node.targetExpression, iteratedType,
|
13399
|
+
assignTypeToExpression(node.targetExpression, { type: iteratedType, isIncomplete: iteratorTypeResult.isIncomplete }, node.targetExpression);
|
13326
13400
|
writeTypeCache(node, { type: iteratedType, isIncomplete: !!iteratorTypeResult.isIncomplete }, 0 /* EvaluatorFlags.None */);
|
13327
13401
|
}
|
13328
13402
|
function evaluateTypesForExceptStatement(node) {
|
@@ -13371,7 +13445,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13371
13445
|
targetType = getBuiltInObject(node, 'BaseExceptionGroup', [targetType]);
|
13372
13446
|
}
|
13373
13447
|
if (node.name) {
|
13374
|
-
assignTypeToExpression(node.name,
|
13448
|
+
assignTypeToExpression(node.name, { type: targetType }, node.name);
|
13375
13449
|
}
|
13376
13450
|
writeTypeCache(node, { type: targetType }, 0 /* EvaluatorFlags.None */);
|
13377
13451
|
}
|
@@ -13433,7 +13507,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13433
13507
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeNotUsableWith().format({ type: printType(subtype), method: exitMethodName }), node.expression);
|
13434
13508
|
});
|
13435
13509
|
if (node.target) {
|
13436
|
-
assignTypeToExpression(node.target, scopedType,
|
13510
|
+
assignTypeToExpression(node.target, { type: scopedType, isIncomplete: exprTypeResult.isIncomplete }, node.target);
|
13437
13511
|
}
|
13438
13512
|
writeTypeCache(node, { type: scopedType, isIncomplete: !!exprTypeResult.isIncomplete }, 0 /* EvaluatorFlags.None */);
|
13439
13513
|
}
|
@@ -13466,7 +13540,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13466
13540
|
symbolType = cachedModuleType;
|
13467
13541
|
}
|
13468
13542
|
}
|
13469
|
-
assignTypeToNameNode(symbolNameNode,
|
13543
|
+
assignTypeToNameNode(symbolNameNode, { type: symbolType }, /* ignoreEmptyContainers */ false);
|
13470
13544
|
writeTypeCache(node, { type: symbolType }, 0 /* EvaluatorFlags.None */);
|
13471
13545
|
}
|
13472
13546
|
function evaluateTypesForImportFromAs(node) {
|
@@ -13532,7 +13606,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13532
13606
|
symbolType = types_1.UnknownType.create();
|
13533
13607
|
}
|
13534
13608
|
}
|
13535
|
-
assignTypeToNameNode(aliasNode,
|
13609
|
+
assignTypeToNameNode(aliasNode, { type: symbolType }, /* ignoreEmptyContainers */ false);
|
13536
13610
|
writeTypeCache(node, { type: symbolType }, 0 /* EvaluatorFlags.None */);
|
13537
13611
|
}
|
13538
13612
|
function evaluateTypesForMatchStatement(node) {
|
@@ -13630,9 +13704,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13630
13704
|
symbolType = cachedModuleType;
|
13631
13705
|
}
|
13632
13706
|
}
|
13633
|
-
assignTypeToNameNode(symbolNameNode, symbolType,
|
13634
|
-
/* isIncomplete */ false,
|
13635
|
-
/* ignoreEmptyContainers */ false);
|
13707
|
+
assignTypeToNameNode(symbolNameNode, { type: symbolType }, /* ignoreEmptyContainers */ false);
|
13636
13708
|
writeTypeCache(node, { type: symbolType }, 0 /* EvaluatorFlags.None */);
|
13637
13709
|
}
|
13638
13710
|
}
|
@@ -14052,7 +14124,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14052
14124
|
return;
|
14053
14125
|
}
|
14054
14126
|
case 4 /* ParseNodeType.AssignmentExpression */: {
|
14055
|
-
|
14127
|
+
evaluateTypesForExpressionInContext(curNode);
|
14056
14128
|
return;
|
14057
14129
|
}
|
14058
14130
|
case 5 /* ParseNodeType.AugmentedAssignment */: {
|
@@ -14469,7 +14541,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14469
14541
|
const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */);
|
14470
14542
|
if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
|
14471
14543
|
types_1.FunctionType.addDefaultParameters(functionType);
|
14472
|
-
functionType.details.flags |= 32768 /* FunctionTypeFlags.
|
14544
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
14473
14545
|
typeArgTypes.push(functionType);
|
14474
14546
|
typeVarContext.setTypeVarType(typeParam, (0, typeUtils_1.convertTypeToParamSpecValue)(functionType));
|
14475
14547
|
return;
|
@@ -14497,12 +14569,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14497
14569
|
concatTypeArgs.forEach((typeArg, index) => {
|
14498
14570
|
if (index === concatTypeArgs.length - 1) {
|
14499
14571
|
if ((0, types_1.isParamSpec)(typeArg)) {
|
14500
|
-
|
14572
|
+
types_1.FunctionType.addParamSpecVariadics(functionType, typeArg);
|
14501
14573
|
}
|
14502
14574
|
else if ((0, typeUtils_1.isEllipsisType)(typeArg)) {
|
14503
14575
|
types_1.FunctionType.addDefaultParameters(functionType);
|
14504
|
-
functionType.details.flags |=
|
14505
|
-
32768 /* FunctionTypeFlags.SkipArgsKwargsCompatibilityCheck */;
|
14576
|
+
functionType.details.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
14506
14577
|
}
|
14507
14578
|
}
|
14508
14579
|
else {
|
@@ -15865,7 +15936,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15865
15936
|
// the return type. If a list of args is provided, the inference logic may take
|
15866
15937
|
// into account argument types to infer the return type.
|
15867
15938
|
function getFunctionEffectiveReturnType(type, callSiteInfo, inferTypeIfNeeded = true) {
|
15868
|
-
const specializedReturnType = types_1.FunctionType.
|
15939
|
+
const specializedReturnType = types_1.FunctionType.getEffectiveReturnType(type, /* includeInferred */ false);
|
15869
15940
|
if (specializedReturnType && !(0, types_1.isUnknown)(specializedReturnType)) {
|
15870
15941
|
const liveTypeVarScopes = (callSiteInfo === null || callSiteInfo === void 0 ? void 0 : callSiteInfo.errorNode)
|
15871
15942
|
? ParseTreeUtils.getTypeVarScopesForNode(callSiteInfo === null || callSiteInfo === void 0 ? void 0 : callSiteInfo.errorNode)
|
@@ -16136,20 +16207,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16136
16207
|
// and cache the return type if necessary. This needs to be done
|
16137
16208
|
// prior to specializing.
|
16138
16209
|
inferReturnTypeIfNecessary(typeResult.type);
|
16139
|
-
|
16140
|
-
|
16141
|
-
|
16142
|
-
(0,
|
16143
|
-
|
16144
|
-
|
16145
|
-
|
16146
|
-
|
16147
|
-
|
16148
|
-
|
16149
|
-
|
16150
|
-
}
|
16151
|
-
});
|
16152
|
-
if (isGenericNonCallable && errorNode) {
|
16210
|
+
// Check for ambiguous accesses to attributes with generic types?
|
16211
|
+
if (errorNode &&
|
16212
|
+
selfClass &&
|
16213
|
+
(0, types_1.isClass)(selfClass) &&
|
16214
|
+
member.isInstanceMember &&
|
16215
|
+
(0, types_1.isClass)(member.unspecializedClassType) &&
|
16216
|
+
(flags & 1024 /* MemberAccessFlags.DisallowGenericInstanceVariableAccess */) !== 0 &&
|
16217
|
+
(0, typeUtils_1.requiresSpecialization)(typeResult.type, { ignoreSelf: true, ignoreImplicitTypeArgs: true })) {
|
16218
|
+
const specializedType = (0, typeUtils_1.partiallySpecializeType)(typeResult.type, member.unspecializedClassType, (0, typeUtils_1.selfSpecializeClass)(selfClass, /* overrideTypeArgs */ true));
|
16219
|
+
if ((0, types_1.findSubtype)(specializedType, (subtype) => !(0, types_1.isFunction)(subtype) &&
|
16220
|
+
!(0, types_1.isOverloadedFunction)(subtype) &&
|
16221
|
+
(0, typeUtils_1.requiresSpecialization)(subtype, { ignoreSelf: true, ignoreImplicitTypeArgs: true }))) {
|
16153
16222
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.genericInstanceVariableAccess(), errorNode);
|
16154
16223
|
}
|
16155
16224
|
}
|
@@ -16331,7 +16400,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16331
16400
|
let flags = 0 /* AssignTypeFlags.Default */;
|
16332
16401
|
if ((primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* DeclarationType.Variable */ &&
|
16333
16402
|
!isFinalVariableDeclaration(primaryDecl) &&
|
16334
|
-
!types_1.ClassType.
|
16403
|
+
!types_1.ClassType.isDataClassFrozen(destType)) {
|
16335
16404
|
// Class and instance variables that are mutable need to
|
16336
16405
|
// enforce invariance. We will exempt variables that are
|
16337
16406
|
// private or protected, since these are presumably
|
@@ -16963,7 +17032,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16963
17032
|
// it's Any.
|
16964
17033
|
if ((0, types_1.isParamSpec)(srcType) &&
|
16965
17034
|
(0, types_1.isFunction)(destType) &&
|
16966
|
-
types_1.FunctionType.
|
17035
|
+
types_1.FunctionType.isGradualCallableForm(destType) &&
|
16967
17036
|
destType.details.parameters.length <= 2) {
|
16968
17037
|
return true;
|
16969
17038
|
}
|
@@ -17245,7 +17314,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17245
17314
|
/* selfType */ undefined,
|
17246
17315
|
/* diag */ undefined, recursionCount);
|
17247
17316
|
if (boundMethod) {
|
17248
|
-
concreteSrcType =
|
17317
|
+
concreteSrcType = boundMethod;
|
17249
17318
|
}
|
17250
17319
|
}
|
17251
17320
|
// If it's a class, use the constructor for type compatibility checking.
|
@@ -17789,13 +17858,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17789
17858
|
}
|
17790
17859
|
}
|
17791
17860
|
}
|
17792
|
-
|
17793
|
-
/* selfType */ undefined,
|
17794
|
-
/* diag */ undefined, recursionCount);
|
17795
|
-
if (boundMethod) {
|
17796
|
-
return (0, typeUtils_1.removeParamSpecVariadicsFromSignature)(boundMethod);
|
17797
|
-
}
|
17798
|
-
return undefined;
|
17861
|
+
return getBoundMagicMethod(objType, '__call__', /* selfType */ undefined, /* diag */ undefined, recursionCount);
|
17799
17862
|
}
|
17800
17863
|
function assignFunctionParameter(destType, srcType, paramIndex, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
17801
17864
|
// Handle the special case where the dest type is a synthesized
|
@@ -17927,17 +17990,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17927
17990
|
}
|
17928
17991
|
}
|
17929
17992
|
function assignFunction(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
17930
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
17993
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
17931
17994
|
let canAssign = true;
|
17932
17995
|
const checkReturnType = (flags & 64 /* AssignTypeFlags.SkipFunctionReturnTypeCheck */) === 0;
|
17933
17996
|
const reverseMatching = (flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) !== 0;
|
17934
17997
|
flags &= ~64 /* AssignTypeFlags.SkipFunctionReturnTypeCheck */;
|
17935
|
-
|
17936
|
-
|
17998
|
+
const destParamSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(destType);
|
17999
|
+
if (destParamSpec) {
|
18000
|
+
destType = types_1.FunctionType.cloneRemoveParamSpecArgsKwargs(destType);
|
18001
|
+
}
|
18002
|
+
const srcParamSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(srcType);
|
18003
|
+
if (srcParamSpec) {
|
18004
|
+
srcType = types_1.FunctionType.cloneRemoveParamSpecArgsKwargs(srcType);
|
18005
|
+
}
|
17937
18006
|
const destParamDetails = (0, parameterUtils_1.getParameterListDetails)(destType);
|
17938
18007
|
const srcParamDetails = (0, parameterUtils_1.getParameterListDetails)(srcType);
|
17939
18008
|
adjustSourceParamDetailsForDestVariadic(reverseMatching ? destParamDetails : srcParamDetails, reverseMatching ? srcParamDetails : destParamDetails);
|
17940
|
-
const targetIncludesParamSpec = reverseMatching ? !!
|
18009
|
+
const targetIncludesParamSpec = reverseMatching ? !!srcParamSpec : !!destParamSpec;
|
17941
18010
|
const destPositionalCount = (_a = destParamDetails.firstKeywordOnlyIndex) !== null && _a !== void 0 ? _a : destParamDetails.params.length;
|
17942
18011
|
const srcPositionalCount = (_b = srcParamDetails.firstKeywordOnlyIndex) !== null && _b !== void 0 ? _b : srcParamDetails.params.length;
|
17943
18012
|
const positionalsToMatch = Math.min(destPositionalCount, srcPositionalCount);
|
@@ -18025,7 +18094,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18025
18094
|
canAssign = false;
|
18026
18095
|
}
|
18027
18096
|
}
|
18028
|
-
if (!types_1.FunctionType.
|
18097
|
+
if (!types_1.FunctionType.isGradualCallableForm(destType) &&
|
18029
18098
|
destParamDetails.firstPositionOrKeywordIndex < srcParamDetails.positionOnlyParamCount &&
|
18030
18099
|
!targetIncludesParamSpec) {
|
18031
18100
|
diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.LocAddendum.argsPositionOnly().format({
|
@@ -18061,7 +18130,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18061
18130
|
}
|
18062
18131
|
continue;
|
18063
18132
|
}
|
18064
|
-
// If the source parameter is also
|
18133
|
+
// If the source parameter is also addressable by keyword, it is OK
|
18065
18134
|
// that there is no matching positional parameter in the dest.
|
18066
18135
|
if (srcParam.kind === parameterUtils_1.ParameterKind.Standard) {
|
18067
18136
|
continue;
|
@@ -18128,7 +18197,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18128
18197
|
// their types are compatible.
|
18129
18198
|
if (srcParamDetails.argsIndex !== undefined &&
|
18130
18199
|
destParamDetails.argsIndex !== undefined &&
|
18131
|
-
!types_1.FunctionType.
|
18200
|
+
!types_1.FunctionType.isGradualCallableForm(destType)) {
|
18132
18201
|
let destArgsType = destParamDetails.params[destParamDetails.argsIndex].type;
|
18133
18202
|
let srcArgsType = srcParamDetails.params[srcParamDetails.argsIndex].type;
|
18134
18203
|
if (!(0, types_1.isUnpacked)(destArgsType)) {
|
@@ -18143,9 +18212,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18143
18212
|
}
|
18144
18213
|
// If the dest has an "*args" but the source doesn't, report the incompatibility.
|
18145
18214
|
// The converse situation is OK.
|
18146
|
-
if (!types_1.FunctionType.
|
18215
|
+
if (!types_1.FunctionType.isGradualCallableForm(destType) &&
|
18147
18216
|
srcParamDetails.argsIndex === undefined &&
|
18148
|
-
|
18217
|
+
srcParamSpec === undefined &&
|
18149
18218
|
destParamDetails.argsIndex !== undefined &&
|
18150
18219
|
!destParamDetails.hasUnpackedVariadicTypeVar) {
|
18151
18220
|
diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.LocAddendum.argsParamMissing().format({
|
@@ -18260,9 +18329,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18260
18329
|
}
|
18261
18330
|
// If the dest has a "**kwargs" but the source doesn't, report the incompatibility.
|
18262
18331
|
// The converse situation is OK.
|
18263
|
-
if (!types_1.FunctionType.
|
18332
|
+
if (!types_1.FunctionType.isGradualCallableForm(destType) &&
|
18264
18333
|
srcParamDetails.kwargsIndex === undefined &&
|
18265
|
-
|
18334
|
+
srcParamSpec === undefined &&
|
18266
18335
|
destParamDetails.kwargsIndex !== undefined) {
|
18267
18336
|
diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.LocAddendum.kwargsParamMissing().format({
|
18268
18337
|
paramName: destParamDetails.params[destParamDetails.kwargsIndex].param.name,
|
@@ -18272,8 +18341,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18272
18341
|
}
|
18273
18342
|
// If the source and the dest are using the same ParamSpec, any additional
|
18274
18343
|
// concatenated parameters must match.
|
18275
|
-
if (targetIncludesParamSpec &&
|
18276
|
-
((_j = srcType.details.paramSpec) === null || _j === void 0 ? void 0 : _j.nameWithScope) === ((_k = destType.details.paramSpec) === null || _k === void 0 ? void 0 : _k.nameWithScope)) {
|
18344
|
+
if (targetIncludesParamSpec && (srcParamSpec === null || srcParamSpec === void 0 ? void 0 : srcParamSpec.nameWithScope) === (destParamSpec === null || destParamSpec === void 0 ? void 0 : destParamSpec.nameWithScope)) {
|
18277
18345
|
if (srcParamDetails.params.length !== destParamDetails.params.length) {
|
18278
18346
|
canAssign = false;
|
18279
18347
|
}
|
@@ -18288,9 +18356,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18288
18356
|
});
|
18289
18357
|
// Are we assigning to a function with a ParamSpec?
|
18290
18358
|
if (targetIncludesParamSpec) {
|
18291
|
-
const effectiveDestType = reverseMatching ? srcType : destType;
|
18292
18359
|
const effectiveSrcType = reverseMatching ? destType : srcType;
|
18293
|
-
|
18360
|
+
const effectiveDestType = reverseMatching ? srcType : destType;
|
18361
|
+
const effectiveSrcParamSpec = reverseMatching ? destParamSpec : srcParamSpec;
|
18362
|
+
const effectiveDestParamSpec = reverseMatching ? srcParamSpec : destParamSpec;
|
18363
|
+
if (effectiveDestParamSpec) {
|
18294
18364
|
const requiredMatchParamCount = effectiveDestType.details.parameters.filter((p) => {
|
18295
18365
|
if (!p.name) {
|
18296
18366
|
return false;
|
@@ -18327,13 +18397,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18327
18397
|
});
|
18328
18398
|
}
|
18329
18399
|
});
|
18330
|
-
const srcParamSpec = effectiveSrcType.details.paramSpec;
|
18331
|
-
const destParamSpec = effectiveDestType.details.paramSpec;
|
18332
18400
|
// If there are remaining parameters and the source and dest do not contain
|
18333
18401
|
// the same ParamSpec, synthesize a function for the remaining parameters.
|
18334
18402
|
if (remainingParams.length > 0 ||
|
18335
|
-
!
|
18336
|
-
!(0, types_1.isTypeSame)(
|
18403
|
+
!effectiveSrcParamSpec ||
|
18404
|
+
!(0, types_1.isTypeSame)(effectiveSrcParamSpec, effectiveDestParamSpec, { ignoreTypeFlags: true })) {
|
18337
18405
|
const remainingFunction = types_1.FunctionType.createInstance('', '', '', effectiveSrcType.details.flags | 64 /* FunctionTypeFlags.SynthesizedMethod */, effectiveSrcType.details.docString);
|
18338
18406
|
remainingFunction.details.deprecatedMessage = effectiveSrcType.details.deprecatedMessage;
|
18339
18407
|
remainingFunction.details.typeVarScopeId = effectiveSrcType.details.typeVarScopeId;
|
@@ -18343,16 +18411,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18343
18411
|
remainingParams.forEach((param) => {
|
18344
18412
|
types_1.FunctionType.addParameter(remainingFunction, param);
|
18345
18413
|
});
|
18346
|
-
|
18414
|
+
if (effectiveSrcParamSpec) {
|
18415
|
+
types_1.FunctionType.addParamSpecVariadics(remainingFunction, (0, typeUtils_1.convertToInstance)(effectiveSrcParamSpec));
|
18416
|
+
}
|
18347
18417
|
types_1.FunctionType.addHigherOrderTypeVarScopeIds(remainingFunction, effectiveSrcType.details.higherOrderTypeVarScopeIds);
|
18348
|
-
if (!assignType(
|
18418
|
+
if (!assignType(effectiveDestParamSpec, remainingFunction,
|
18349
18419
|
/* diag */ undefined, destTypeVarContext, srcTypeVarContext, flags)) {
|
18350
18420
|
// If we couldn't assign the function to the ParamSpec, see if we can
|
18351
18421
|
// assign only the ParamSpec. This is possible if there were no
|
18352
18422
|
// remaining parameters.
|
18353
18423
|
if (remainingParams.length > 0 ||
|
18354
|
-
!
|
18355
|
-
!assignType((0, typeUtils_1.convertToInstance)(
|
18424
|
+
!effectiveSrcParamSpec ||
|
18425
|
+
!assignType((0, typeUtils_1.convertToInstance)(effectiveDestParamSpec), (0, typeUtils_1.convertToInstance)(effectiveSrcParamSpec),
|
18356
18426
|
/* diag */ undefined, destTypeVarContext, srcTypeVarContext, flags)) {
|
18357
18427
|
canAssign = false;
|
18358
18428
|
}
|
@@ -18473,13 +18543,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18473
18543
|
}
|
18474
18544
|
// When a value is assigned to a variable with a declared type,
|
18475
18545
|
// we may be able to narrow the type based on the assignment.
|
18476
|
-
function narrowTypeBasedOnAssignment(node, declaredType,
|
18477
|
-
// TODO: The rules for narrowing types on assignment are defined in
|
18546
|
+
function narrowTypeBasedOnAssignment(node, declaredType, assignedTypeResult) {
|
18547
|
+
// TODO: The rules for narrowing types on assignment are not defined in
|
18478
18548
|
// the typing spec. Pyright's current logic is currently not even internally
|
18479
18549
|
// consistent and probably not sound from a type theory perspective. It
|
18480
18550
|
// should be completely reworked once there has been a public discussion
|
18481
18551
|
// about the correct behavior.
|
18482
|
-
|
18552
|
+
// If the result is incomplete, do not attempt to narrow the type.
|
18553
|
+
if (assignedTypeResult.isIncomplete) {
|
18554
|
+
return assignedTypeResult;
|
18555
|
+
}
|
18556
|
+
const narrowedType = (0, typeUtils_1.mapSubtypes)(assignedTypeResult.type, (assignedSubtype) => {
|
18483
18557
|
// Handle the special case where the assigned type is a literal type.
|
18484
18558
|
// Some types include very large unions of literal types, and we don't
|
18485
18559
|
// want to use an n^2 loop to compare them.
|
@@ -18541,12 +18615,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18541
18615
|
// with the declared type. In strict mode, this will retain the "unknown type"
|
18542
18616
|
// diagnostics while still providing reasonable completion suggestions.
|
18543
18617
|
if ((0, typeUtils_1.isIncompleteUnknown)(narrowedType)) {
|
18544
|
-
return narrowedType;
|
18618
|
+
return { type: narrowedType };
|
18545
18619
|
}
|
18546
18620
|
else if ((0, types_1.isUnknown)(narrowedType)) {
|
18547
|
-
return (0, types_1.combineTypes)([narrowedType, declaredType]);
|
18621
|
+
return { type: (0, types_1.combineTypes)([narrowedType, declaredType]) };
|
18548
18622
|
}
|
18549
|
-
return narrowedType;
|
18623
|
+
return { type: narrowedType };
|
18550
18624
|
}
|
18551
18625
|
function validateOverrideMethod(baseMethod, overrideMethod, baseClass, diag, enforceParamNames = true) {
|
18552
18626
|
// If we're overriding a non-method with a method, report it as an error.
|
@@ -18676,8 +18750,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18676
18750
|
const baseParamDetails = (0, parameterUtils_1.getParameterListDetails)(baseMethod);
|
18677
18751
|
const overrideParamDetails = (0, parameterUtils_1.getParameterListDetails)(overrideMethod);
|
18678
18752
|
let canOverride = true;
|
18679
|
-
if (!types_1.FunctionType.
|
18680
|
-
!types_1.FunctionType.shouldSkipArgsKwargsCompatibilityCheck(overrideMethod)) {
|
18753
|
+
if (!types_1.FunctionType.isGradualCallableForm(baseMethod) && !types_1.FunctionType.isGradualCallableForm(overrideMethod)) {
|
18681
18754
|
// Verify that we're not overriding a static, class or instance method with
|
18682
18755
|
// an incompatible type.
|
18683
18756
|
if (types_1.FunctionType.isStaticMethod(baseMethod)) {
|
@@ -19137,7 +19210,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19137
19210
|
memberTypeFirstParam.hasDeclaredType) {
|
19138
19211
|
if (subDiag) {
|
19139
19212
|
subDiag.addMessage(localize_1.LocMessage.bindTypeMismatch().format({
|
19140
|
-
type: printType(
|
19213
|
+
type: printType(firstParamType),
|
19141
19214
|
methodName: memberType.details.name || '<anonymous>',
|
19142
19215
|
paramName: memberTypeFirstParam.name,
|
19143
19216
|
}));
|
@@ -19151,7 +19224,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19151
19224
|
// evaluating (and caching) the inferred return type if there is no defined return type.
|
19152
19225
|
getFunctionEffectiveReturnType(memberType);
|
19153
19226
|
const specializedFunction = (0, typeUtils_1.applySolvedTypeVars)(memberType, typeVarContext);
|
19154
|
-
|
19227
|
+
// If this is a constructor method, provide the base type's TypeVar scope ID
|
19228
|
+
// so any TypeVars in this type can be solved.
|
19229
|
+
let baseTypeTypeVarScopeId;
|
19230
|
+
if (types_1.FunctionType.isConstructorMethod(specializedFunction) || specializedFunction.details.name === '__init__') {
|
19231
|
+
baseTypeTypeVarScopeId = (0, typeUtils_1.getTypeVarScopeId)(baseType);
|
19232
|
+
}
|
19233
|
+
return types_1.FunctionType.clone(specializedFunction, stripFirstParam, baseType, baseTypeTypeVarScopeId);
|
19155
19234
|
}
|
19156
19235
|
function isFinalVariable(symbol) {
|
19157
19236
|
return symbol.getDeclarations().some((decl) => isFinalVariableDeclaration(decl));
|