@zzzen/pyright-internal 1.2.0-dev.20230219 → 1.2.0-dev.20230305
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/backgroundAnalysisProgram.d.ts +1 -1
- package/dist/analyzer/backgroundAnalysisProgram.js +3 -3
- package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
- package/dist/analyzer/binder.d.ts +0 -1
- package/dist/analyzer/binder.js +18 -93
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +50 -30
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +5 -5
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +0 -10
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/declaration.d.ts +3 -4
- package/dist/analyzer/declaration.js +6 -1
- package/dist/analyzer/declaration.js.map +1 -1
- package/dist/analyzer/declarationUtils.d.ts +1 -5
- package/dist/analyzer/declarationUtils.js +2 -47
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/docStringConversion.js +9 -3
- package/dist/analyzer/docStringConversion.js.map +1 -1
- package/dist/analyzer/importResolver.d.ts +3 -2
- package/dist/analyzer/importResolver.js +28 -19
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.d.ts +4 -2
- package/dist/analyzer/parseTreeUtils.js +77 -2
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.d.ts +1 -0
- package/dist/analyzer/patternMatching.js +47 -2
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +5 -4
- package/dist/analyzer/program.js +182 -43
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +8 -3
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +8 -5
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/pythonPathUtils.js +27 -13
- package/dist/analyzer/pythonPathUtils.js.map +1 -1
- package/dist/analyzer/service.d.ts +7 -3
- package/dist/analyzer/service.js +24 -7
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.js +3 -0
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/symbol.js +1 -4
- package/dist/analyzer/symbol.js.map +1 -1
- package/dist/analyzer/symbolUtils.d.ts +0 -3
- package/dist/analyzer/symbolUtils.js +1 -14
- package/dist/analyzer/symbolUtils.js.map +1 -1
- package/dist/analyzer/typeDocStringUtils.js +5 -3
- package/dist/analyzer/typeDocStringUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +586 -294
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +10 -2
- package/dist/analyzer/typeGuards.js +2 -2
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +1 -0
- package/dist/analyzer/typeUtils.js +14 -1
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.js +26 -17
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +2 -1
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +2 -3
- package/dist/backgroundAnalysisBase.js +12 -13
- package/dist/backgroundAnalysisBase.js.map +1 -1
- package/dist/commands/commandController.js +1 -0
- package/dist/commands/commandController.js.map +1 -1
- package/dist/commands/quickActionCommand.js +2 -2
- package/dist/commands/quickActionCommand.js.map +1 -1
- package/dist/common/cancellationUtils.d.ts +18 -2
- package/dist/common/cancellationUtils.js +80 -3
- package/dist/common/cancellationUtils.js.map +1 -1
- package/dist/common/collectionUtils.d.ts +1 -0
- package/dist/common/collectionUtils.js +8 -1
- package/dist/common/collectionUtils.js.map +1 -1
- package/dist/common/extensibility.d.ts +4 -3
- package/dist/common/extensibility.js.map +1 -1
- package/dist/common/fileBasedCancellationUtils.d.ts +0 -1
- package/dist/common/fileBasedCancellationUtils.js +7 -81
- package/dist/common/fileBasedCancellationUtils.js.map +1 -1
- package/dist/common/positionUtils.d.ts +2 -0
- package/dist/common/positionUtils.js +15 -1
- package/dist/common/positionUtils.js.map +1 -1
- package/dist/common/{textEditUtils.d.ts → textEditTracker.d.ts} +2 -6
- package/dist/common/{textEditUtils.js → textEditTracker.js} +6 -49
- package/dist/common/textEditTracker.js.map +1 -0
- package/dist/common/workspaceEditUtils.d.ts +14 -8
- package/dist/common/workspaceEditUtils.js +115 -59
- package/dist/common/workspaceEditUtils.js.map +1 -1
- package/dist/languageServerBase.js +9 -3
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +4 -2
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/codeActionProvider.js +1 -1
- package/dist/languageService/codeActionProvider.js.map +1 -1
- package/dist/languageService/completionProvider.js +49 -43
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/completionProviderUtils.d.ts +1 -1
- package/dist/languageService/completionProviderUtils.js +3 -3
- package/dist/languageService/completionProviderUtils.js.map +1 -1
- package/dist/languageService/definitionProvider.js +2 -1
- package/dist/languageService/definitionProvider.js.map +1 -1
- package/dist/languageService/documentSymbolProvider.js +2 -1
- package/dist/languageService/documentSymbolProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.js +14 -3
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/importAdder.d.ts +2 -2
- package/dist/languageService/importAdder.js +5 -3
- package/dist/languageService/importAdder.js.map +1 -1
- package/dist/languageService/indentationUtils.d.ts +6 -2
- package/dist/languageService/indentationUtils.js +33 -13
- package/dist/languageService/indentationUtils.js.map +1 -1
- package/dist/languageService/renameModuleProvider.d.ts +10 -2
- package/dist/languageService/renameModuleProvider.js +87 -14
- package/dist/languageService/renameModuleProvider.js.map +1 -1
- package/dist/localization/localize.d.ts +12 -2
- package/dist/localization/localize.js +5 -1
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +6 -2
- package/dist/parser/parser.js +18 -18
- package/dist/parser/parser.js.map +1 -1
- package/dist/pyright.js +1 -1
- package/dist/pyright.js.map +1 -1
- package/dist/tests/docStringConversion.test.js +23 -0
- package/dist/tests/docStringConversion.test.js.map +1 -1
- package/dist/tests/fourslash/completions.errorNodes.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/completions.errorNodes.fourslash.js +24 -0
- package/dist/tests/fourslash/completions.errorNodes.fourslash.js.map +1 -0
- package/dist/tests/fourslash/hover.docstring.parameter.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/hover.docstring.parameter.fourslash.js +57 -0
- package/dist/tests/fourslash/hover.docstring.parameter.fourslash.js.map +1 -0
- package/dist/tests/fourslash/hover.slots.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/hover.slots.fourslash.js +16 -0
- package/dist/tests/fourslash/hover.slots.fourslash.js.map +1 -0
- package/dist/tests/harness/fourslash/testState.js +9 -2
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/importAdder.test.js +16 -3
- package/dist/tests/importAdder.test.js.map +1 -1
- package/dist/tests/indentationUtils.reindent.test.js +22 -1
- package/dist/tests/indentationUtils.reindent.test.js.map +1 -1
- package/dist/tests/moveSymbol.importAdder.test.js +183 -24
- package/dist/tests/moveSymbol.importAdder.test.js.map +1 -1
- package/dist/tests/moveSymbol.insertion.test.js +137 -2
- package/dist/tests/moveSymbol.insertion.test.js.map +1 -1
- package/dist/tests/moveSymbol.misc.test.js +109 -4
- package/dist/tests/moveSymbol.misc.test.js.map +1 -1
- package/dist/tests/renameModuleTestUtils.d.ts +1 -1
- package/dist/tests/renameModuleTestUtils.js +9 -5
- package/dist/tests/renameModuleTestUtils.js.map +1 -1
- package/dist/tests/testStateUtils.js +2 -2
- package/dist/tests/testStateUtils.js.map +1 -1
- package/dist/tests/textEditUtil.test.js +2 -2
- package/dist/tests/textEditUtil.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +8 -0
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +13 -1
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +29 -4
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +1 -1
- package/dist/tests/typeEvaluator5.test.js +6 -0
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/workspaceEditUtils.test.js +7 -7
- package/dist/tests/workspaceEditUtils.test.js.map +1 -1
- package/package.json +3 -3
- package/dist/common/textEditUtils.js.map +0 -1
@@ -298,8 +298,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
298
298
|
}
|
299
299
|
}
|
300
300
|
function isTypeCached(node) {
|
301
|
-
const
|
302
|
-
|
301
|
+
const cacheEntry = readTypeCacheEntry(node);
|
302
|
+
if (!cacheEntry) {
|
303
|
+
return false;
|
304
|
+
}
|
305
|
+
return (!cacheEntry.typeResult.isIncomplete || cacheEntry.incompleteGenerationCount === incompleteGenerationCount);
|
303
306
|
}
|
304
307
|
function readTypeCache(node, flags) {
|
305
308
|
var _a, _b;
|
@@ -689,7 +692,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
689
692
|
typeResult.type = types_1.UnknownType.create();
|
690
693
|
}
|
691
694
|
}
|
692
|
-
if (!
|
695
|
+
if (!(0, typeUtils_1.isEffectivelyInstantiable)(typeResult.type)) {
|
693
696
|
const isEmptyVariadic = (0, types_1.isClassInstance)(typeResult.type) &&
|
694
697
|
types_1.ClassType.isTupleClass(typeResult.type) &&
|
695
698
|
((_a = typeResult.type.tupleTypeArguments) === null || _a === void 0 ? void 0 : _a.length) === 0;
|
@@ -997,6 +1000,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
997
1000
|
if (options === null || options === void 0 ? void 0 : options.notParsedByInterpreter) {
|
998
1001
|
evaluatorFlags |= 524288 /* NotParsedByInterpreter */;
|
999
1002
|
}
|
1003
|
+
if (options === null || options === void 0 ? void 0 : options.allowRequired) {
|
1004
|
+
evaluatorFlags |= 1048576 /* AllowRequired */;
|
1005
|
+
}
|
1000
1006
|
if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo)) {
|
1001
1007
|
evaluatorFlags |= 4 /* AllowForwardReferences */;
|
1002
1008
|
}
|
@@ -1493,7 +1499,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1493
1499
|
/* honorCodeFlow */ false);
|
1494
1500
|
if (symbolWithScope) {
|
1495
1501
|
const symbol = symbolWithScope.symbol;
|
1496
|
-
return symbol.getDeclarations().find((decl) =>
|
1502
|
+
return symbol.getDeclarations().find((decl) => isExplicitTypeAliasDeclaration(decl)) !== undefined;
|
1497
1503
|
}
|
1498
1504
|
}
|
1499
1505
|
}
|
@@ -1501,6 +1507,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1501
1507
|
}
|
1502
1508
|
// Determines whether the specified expression is a symbol with a declared type.
|
1503
1509
|
function getDeclaredTypeForExpression(expression, usage) {
|
1510
|
+
var _a, _b;
|
1504
1511
|
let symbol;
|
1505
1512
|
let classOrObjectBase;
|
1506
1513
|
let memberAccessClass;
|
@@ -1513,7 +1520,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1513
1520
|
// Handle the case where the symbol is a class-level variable
|
1514
1521
|
// where the type isn't declared in this class but is in
|
1515
1522
|
// a parent class.
|
1516
|
-
if (getDeclaredTypeOfSymbol(symbol, expression) ===
|
1523
|
+
if (!((_a = getDeclaredTypeOfSymbol(symbol, expression)) === null || _a === void 0 ? void 0 : _a.type) &&
|
1517
1524
|
symbolWithScope.scope.type === 2 /* Class */) {
|
1518
1525
|
const enclosingClass = ParseTreeUtils.getEnclosingClassOrFunction(expression);
|
1519
1526
|
if (enclosingClass && enclosingClass.nodeType === 10 /* Class */) {
|
@@ -1587,7 +1594,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1587
1594
|
}
|
1588
1595
|
}
|
1589
1596
|
if (symbol) {
|
1590
|
-
let declaredType = getDeclaredTypeOfSymbol(symbol);
|
1597
|
+
let declaredType = (_b = getDeclaredTypeOfSymbol(symbol)) === null || _b === void 0 ? void 0 : _b.type;
|
1591
1598
|
if (declaredType) {
|
1592
1599
|
// If it's a descriptor, we need to get the setter type.
|
1593
1600
|
if ((0, types_1.isClassInstance)(declaredType)) {
|
@@ -2027,7 +2034,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2027
2034
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
2028
2035
|
if ((0, types_1.isUnion)(type)) {
|
2029
2036
|
(0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
|
2030
|
-
if (!
|
2037
|
+
if (!(0, typeUtils_1.isEffectivelyInstantiable)(subtype)) {
|
2031
2038
|
diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typeNotClass().format({ type: printType(subtype) }));
|
2032
2039
|
}
|
2033
2040
|
});
|
@@ -2035,7 +2042,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2035
2042
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeExpectedClass().format({ type: printType(type) }) + diag.getString(), node);
|
2036
2043
|
}
|
2037
2044
|
function assignTypeToNameNode(nameNode, type, isTypeIncomplete, ignoreEmptyContainers, srcExpression, allowAssignmentToFinalVar = false, expectedTypeDiagAddendum) {
|
2038
|
-
var _a, _b;
|
2045
|
+
var _a, _b, _c;
|
2039
2046
|
const nameValue = nameNode.value;
|
2040
2047
|
const symbolWithScope = lookUpSymbolRecursive(nameNode, nameValue, /* honorCodeFlow */ false);
|
2041
2048
|
if (!symbolWithScope) {
|
@@ -2044,7 +2051,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2044
2051
|
return;
|
2045
2052
|
}
|
2046
2053
|
const declarations = symbolWithScope.symbol.getDeclarations();
|
2047
|
-
let declaredType = getDeclaredTypeOfSymbol(symbolWithScope.symbol);
|
2054
|
+
let declaredType = (_a = getDeclaredTypeOfSymbol(symbolWithScope.symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
2048
2055
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(nameNode);
|
2049
2056
|
// If this is a class scope and there is no type declared for this class variable,
|
2050
2057
|
// see if a parent class has a type declared.
|
@@ -2062,7 +2069,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2062
2069
|
}
|
2063
2070
|
// We found an existing declared type. Make sure the type is assignable.
|
2064
2071
|
let destType = type;
|
2065
|
-
|
2072
|
+
const isTypeAlias = !!declaredType && (0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias');
|
2073
|
+
if (declaredType && !isTypeAlias) {
|
2066
2074
|
let diagAddendum = new diagnostic_1.DiagnosticAddendum();
|
2067
2075
|
if (!assignType(declaredType, type, diagAddendum)) {
|
2068
2076
|
// If there was an expected type mismatch, use that diagnostic
|
@@ -2073,7 +2081,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2073
2081
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAssignmentMismatch().format({
|
2074
2082
|
sourceType: printType(type),
|
2075
2083
|
destType: printType(declaredType),
|
2076
|
-
}) + diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode, (
|
2084
|
+
}) + 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);
|
2077
2085
|
// Replace the assigned type with the (unnarrowed) declared type.
|
2078
2086
|
destType = declaredType;
|
2079
2087
|
}
|
@@ -2091,7 +2099,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2091
2099
|
if ((scope === null || scope === void 0 ? void 0 : scope.type) === 2 /* Class */) {
|
2092
2100
|
if (types_1.TypeBase.isInstance(destType) &&
|
2093
2101
|
!(0, symbolNameUtils_1.isConstantName)(nameValue) &&
|
2094
|
-
!
|
2102
|
+
!isFinalVariable(symbolWithScope.symbol)) {
|
2095
2103
|
destType = stripLiteralValue(destType);
|
2096
2104
|
}
|
2097
2105
|
}
|
@@ -2105,7 +2113,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2105
2113
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportConstantRedefinition, diagnosticRules_1.DiagnosticRule.reportConstantRedefinition, localize_1.Localizer.Diagnostic.constantRedefinition().format({ name: nameValue }), nameNode);
|
2106
2114
|
}
|
2107
2115
|
}
|
2108
|
-
else if (varDecl
|
2116
|
+
else if (isFinalVariableDeclaration(varDecl) && !allowAssignmentToFinalVar) {
|
2109
2117
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.finalReassigned().format({ name: nameValue }), nameNode);
|
2110
2118
|
}
|
2111
2119
|
}
|
@@ -2160,6 +2168,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2160
2168
|
writeTypeCache(target, { type, isIncomplete: isTypeIncomplete }, 0 /* None */);
|
2161
2169
|
}
|
2162
2170
|
function assignTypeToMemberVariable(node, srcType, isTypeIncomplete, isInstanceMember, srcExprNode) {
|
2171
|
+
var _a;
|
2163
2172
|
const memberName = node.memberName.value;
|
2164
2173
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
2165
2174
|
const classDef = ParseTreeUtils.getEnclosingClass(node);
|
@@ -2218,7 +2227,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2218
2227
|
}
|
2219
2228
|
else {
|
2220
2229
|
// Is the target a property?
|
2221
|
-
const declaredType = getDeclaredTypeOfSymbol(memberInfo.symbol);
|
2230
|
+
const declaredType = (_a = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
2222
2231
|
if (declaredType && !(0, typeUtils_1.isProperty)(declaredType)) {
|
2223
2232
|
// Handle the case where there is a class variable defined with the same
|
2224
2233
|
// name, but there's also now an instance variable introduced. Combine the
|
@@ -3117,6 +3126,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3117
3126
|
// type arguments. If so, it fills in these type arguments with Unknown
|
3118
3127
|
// and optionally reports an error.
|
3119
3128
|
function reportMissingTypeArguments(node, type, flags) {
|
3129
|
+
var _a;
|
3120
3130
|
if ((flags & 2 /* DoNotSpecialize */) !== 0) {
|
3121
3131
|
return type;
|
3122
3132
|
}
|
@@ -3130,7 +3140,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3130
3140
|
}
|
3131
3141
|
}
|
3132
3142
|
if (!type.typeArguments) {
|
3133
|
-
type = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node);
|
3143
|
+
type = (_a = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _a === void 0 ? void 0 : _a.type;
|
3134
3144
|
}
|
3135
3145
|
}
|
3136
3146
|
// Is this a generic type alias that needs to be specialized?
|
@@ -3348,7 +3358,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3348
3358
|
return typeResult;
|
3349
3359
|
}
|
3350
3360
|
function getTypeOfMemberAccessWithBaseType(node, baseTypeResult, usage, flags) {
|
3351
|
-
var _a, _b;
|
3361
|
+
var _a, _b, _c;
|
3352
3362
|
let baseType = baseTypeResult.type;
|
3353
3363
|
const memberName = node.memberName.value;
|
3354
3364
|
let diag = new diagnostic_1.DiagnosticAddendum();
|
@@ -3356,6 +3366,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3356
3366
|
let type;
|
3357
3367
|
let isIncomplete = !!baseTypeResult.isIncomplete;
|
3358
3368
|
let isAsymmetricDescriptor;
|
3369
|
+
const isRequired = false;
|
3370
|
+
const isNotRequired = false;
|
3359
3371
|
// If the base type was incomplete and unbound, don't proceed
|
3360
3372
|
// because false positive errors will be generated.
|
3361
3373
|
if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseTypeResult.type)) {
|
@@ -3514,7 +3526,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3514
3526
|
}
|
3515
3527
|
if (!type) {
|
3516
3528
|
if (!isIncomplete) {
|
3517
|
-
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.moduleUnknownMember().format({
|
3529
|
+
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.moduleUnknownMember().format({
|
3530
|
+
memberName,
|
3531
|
+
moduleName: baseType.moduleName,
|
3532
|
+
}), node.memberName);
|
3518
3533
|
}
|
3519
3534
|
type = evaluatorOptions.evaluateUnknownImportsAsAny ? types_1.AnyType.create() : types_1.UnknownType.create();
|
3520
3535
|
}
|
@@ -3628,7 +3643,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3628
3643
|
// Should we specialize the class?
|
3629
3644
|
if ((flags & 2 /* DoNotSpecialize */) === 0) {
|
3630
3645
|
if ((0, types_1.isInstantiableClass)(type) && !type.typeArguments) {
|
3631
|
-
type = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node);
|
3646
|
+
type = (_b = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _b === void 0 ? void 0 : _b.type;
|
3632
3647
|
}
|
3633
3648
|
}
|
3634
3649
|
if (usage.method === 'get') {
|
@@ -3639,7 +3654,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3639
3654
|
// It can also come up in cases like "isinstance(x, (list, dict))".
|
3640
3655
|
if ((0, types_1.isInstantiableClass)(type)) {
|
3641
3656
|
const argNode = ParseTreeUtils.getParentNodeOfType(node, 1 /* Argument */);
|
3642
|
-
if (argNode && ((
|
3657
|
+
if (argNode && ((_c = argNode === null || argNode === void 0 ? void 0 : argNode.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 9 /* Call */) {
|
3643
3658
|
skipPartialUnknownCheck = true;
|
3644
3659
|
}
|
3645
3660
|
}
|
@@ -3648,10 +3663,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3648
3663
|
/* ignoreEmptyContainers */ false);
|
3649
3664
|
}
|
3650
3665
|
}
|
3651
|
-
return { type, isIncomplete, isAsymmetricDescriptor };
|
3666
|
+
return { type, isIncomplete, isAsymmetricDescriptor, isRequired, isNotRequired };
|
3652
3667
|
}
|
3653
3668
|
function getTypeOfClassMemberName(errorNode, classType, isAccessedThroughObject, memberName, usage, diag, flags, bindToType) {
|
3654
|
-
var _a, _b;
|
3669
|
+
var _a, _b, _c;
|
3655
3670
|
let classLookupFlags = 0 /* Default */;
|
3656
3671
|
if (flags & 1 /* AccessClassMembersOnly */) {
|
3657
3672
|
classLookupFlags |= 8 /* SkipInstanceVariables */;
|
@@ -3692,7 +3707,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3692
3707
|
if (containingClassType &&
|
3693
3708
|
(0, types_1.isInstantiableClass)(containingClassType) &&
|
3694
3709
|
types_1.ClassType.isSameGenericClass(containingClassType, classType)) {
|
3695
|
-
type = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) !== null &&
|
3710
|
+
type = (_c = (_b = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : types_1.UnknownType.create();
|
3696
3711
|
if (type && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
|
3697
3712
|
type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType);
|
3698
3713
|
}
|
@@ -3703,8 +3718,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3703
3718
|
// Determine whether to replace Self variables with a specific
|
3704
3719
|
// class. Avoid doing this if there's a "bindToType" specified
|
3705
3720
|
// because that case is used for super() calls where we want
|
3706
|
-
// to leave the Self type generic (not specialized).
|
3707
|
-
|
3721
|
+
// to leave the Self type generic (not specialized). We'll also
|
3722
|
+
// skip this for __new__ methods because they are not bound
|
3723
|
+
// to the class but rather assume the type of the cls argument.
|
3724
|
+
const selfClass = bindToType || memberName === '__new__' ? undefined : classType;
|
3708
3725
|
const typeResult = getTypeOfMemberInternal(memberInfo, selfClass);
|
3709
3726
|
if (typeResult) {
|
3710
3727
|
type = typeResult.type;
|
@@ -4030,7 +4047,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4030
4047
|
}
|
4031
4048
|
}
|
4032
4049
|
// Check for an attempt to overwrite a final member variable.
|
4033
|
-
const finalVarTypeDecl = memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.symbol.getDeclarations().find((decl) =>
|
4050
|
+
const finalVarTypeDecl = memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.symbol.getDeclarations().find((decl) => isFinalVariableDeclaration(decl));
|
4034
4051
|
if (finalVarTypeDecl && !ParseTreeUtils.isNodeContainedWithin(errorNode, finalVarTypeDecl.node)) {
|
4035
4052
|
// If a Final instance variable is declared in the class body but is
|
4036
4053
|
// being assigned within an __init__ method, it's allowed.
|
@@ -4251,20 +4268,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4251
4268
|
if (baseTypeResult.isIncomplete) {
|
4252
4269
|
indexTypeResult.isIncomplete = true;
|
4253
4270
|
}
|
4254
|
-
// Handle "Required" and "NotRequired" specially.
|
4255
|
-
if ((flags & 1048576 /* AllowRequired */) !== 0) {
|
4256
|
-
if ((0, types_1.isInstantiableClass)(baseTypeResult.type)) {
|
4257
|
-
if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'Required')) {
|
4258
|
-
indexTypeResult.isRequired = true;
|
4259
|
-
}
|
4260
|
-
else if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'NotRequired')) {
|
4261
|
-
indexTypeResult.isNotRequired = true;
|
4262
|
-
}
|
4263
|
-
}
|
4264
|
-
}
|
4265
4271
|
return indexTypeResult;
|
4266
4272
|
}
|
4267
|
-
function adjustTypeArgumentsForVariadicTypeVar(typeArgs, typeParameters,
|
4273
|
+
function adjustTypeArgumentsForVariadicTypeVar(typeArgs, typeParameters, errorNode) {
|
4268
4274
|
const variadicIndex = typeParameters.findIndex((param) => (0, types_1.isVariadicTypeVar)(param));
|
4269
4275
|
// Do we need to adjust the type arguments to map to a variadic type
|
4270
4276
|
// param at the end of the list?
|
@@ -4336,7 +4342,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4336
4342
|
// Handles index expressions that are providing type arguments for a
|
4337
4343
|
// generic type alias.
|
4338
4344
|
function createSpecializedTypeAlias(node, baseType, flags) {
|
4339
|
-
var _a, _b;
|
4345
|
+
var _a, _b, _c;
|
4340
4346
|
if (!((_a = baseType.typeAliasInfo) === null || _a === void 0 ? void 0 : _a.typeParameters) ||
|
4341
4347
|
(baseType.typeAliasInfo.typeParameters.length === 0 && baseType.typeAliasInfo.typeArguments)) {
|
4342
4348
|
return undefined;
|
@@ -4350,7 +4356,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4350
4356
|
return undefined;
|
4351
4357
|
}
|
4352
4358
|
const typeParameters = baseType.typeAliasInfo.typeParameters;
|
4353
|
-
let typeArgs = adjustTypeArgumentsForVariadicTypeVar(getTypeArgs(node, flags), typeParameters,
|
4359
|
+
let typeArgs = adjustTypeArgumentsForVariadicTypeVar(getTypeArgs(node, flags), typeParameters, node);
|
4354
4360
|
// PEP 612 says that if the class has only one type parameter consisting
|
4355
4361
|
// of a ParamSpec, the list of arguments does not need to be enclosed in
|
4356
4362
|
// a list. We'll handle that case specially here. Presumably this applies to
|
@@ -4366,13 +4372,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4366
4372
|
];
|
4367
4373
|
}
|
4368
4374
|
}
|
4369
|
-
if (
|
4370
|
-
|
4371
|
-
|
4372
|
-
|
4373
|
-
|
4374
|
-
|
4375
|
-
|
4375
|
+
if (!typeParameters.some((typeVar) => typeVar.details.isVariadic && !typeVar.isVariadicInUnion)) {
|
4376
|
+
let minTypeArgCount = typeParameters.length;
|
4377
|
+
const firstNonDefaultParam = typeParameters.findIndex((param) => !!param.details.defaultType);
|
4378
|
+
if (firstNonDefaultParam >= 0) {
|
4379
|
+
minTypeArgCount = firstNonDefaultParam;
|
4380
|
+
}
|
4381
|
+
if (typeArgs.length > typeParameters.length) {
|
4382
|
+
addError(localize_1.Localizer.Diagnostic.typeArgsTooMany().format({
|
4383
|
+
name: printType(baseType),
|
4384
|
+
expected: typeParameters.length,
|
4385
|
+
received: typeArgs.length,
|
4386
|
+
}), typeArgs[typeParameters.length].node);
|
4387
|
+
}
|
4388
|
+
else if (typeArgs.length < minTypeArgCount) {
|
4389
|
+
addError(localize_1.Localizer.Diagnostic.typeArgsTooFew().format({
|
4390
|
+
name: printType(baseType),
|
4391
|
+
expected: typeParameters.length,
|
4392
|
+
received: typeArgs.length,
|
4393
|
+
}), node.items[node.items.length - 1]);
|
4394
|
+
}
|
4376
4395
|
}
|
4377
4396
|
// Handle the mypy_extensions.FlexibleAlias type specially.
|
4378
4397
|
if ((0, types_1.isInstantiableClass)(baseType) &&
|
@@ -4455,15 +4474,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4455
4474
|
else {
|
4456
4475
|
typeArgType = types_1.UnknownType.create();
|
4457
4476
|
}
|
4477
|
+
if ((flags & 16384 /* EnforceTypeVarVarianceConsistency */) !== 0) {
|
4478
|
+
if ((0, types_1.isTypeVar)(typeArgType)) {
|
4479
|
+
const usageVariances = inferTypeParameterVarianceForTypeAlias(baseType);
|
4480
|
+
if (usageVariances && index < usageVariances.length) {
|
4481
|
+
const usageVariance = usageVariances[index];
|
4482
|
+
if (!(0, typeUtils_1.isVarianceOfTypeArgumentCompatible)(typeArgType, usageVariance)) {
|
4483
|
+
const messageDiag = diag.createAddendum();
|
4484
|
+
messageDiag.addMessage(localize_1.Localizer.DiagnosticAddendum.varianceMismatchForTypeAlias().format({
|
4485
|
+
typeVarName: printType(typeArgType),
|
4486
|
+
typeAliasParam: printType(typeParameters[index]),
|
4487
|
+
}));
|
4488
|
+
messageDiag.addTextRange(typeArgs[index].node);
|
4489
|
+
}
|
4490
|
+
}
|
4491
|
+
}
|
4492
|
+
}
|
4458
4493
|
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, typeArgType, diag, typeVarContext, 128 /* RetainLiteralsForTypeVar */);
|
4459
4494
|
}
|
4460
4495
|
});
|
4461
4496
|
if (!diag.isEmpty()) {
|
4462
|
-
addError(localize_1.Localizer.Diagnostic.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node);
|
4497
|
+
addError(localize_1.Localizer.Diagnostic.typeNotSpecializable().format({ type: printType(baseType) }) + diag.getString(), node, (_b = diag.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : node);
|
4463
4498
|
}
|
4464
4499
|
const primarySignatureContext = typeVarContext.getPrimarySignature();
|
4465
4500
|
const aliasTypeArgs = [];
|
4466
|
-
(
|
4501
|
+
(_c = baseType.typeAliasInfo.typeParameters) === null || _c === void 0 ? void 0 : _c.forEach((typeParam) => {
|
4467
4502
|
let typeVarType;
|
4468
4503
|
if ((0, types_1.isParamSpec)(typeParam)) {
|
4469
4504
|
const paramSpecType = primarySignatureContext.getParamSpecType(typeParam);
|
@@ -4489,9 +4524,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4489
4524
|
return { type };
|
4490
4525
|
}
|
4491
4526
|
let isIncomplete = baseTypeResult.isIncomplete;
|
4527
|
+
let isRequired = false;
|
4528
|
+
let isNotRequired = false;
|
4492
4529
|
const type = mapSubtypesExpandTypeVars(baseTypeResult.type,
|
4493
4530
|
/* conditionFilter */ undefined, (concreteSubtype, unexpandedSubtype) => {
|
4494
|
-
var _a
|
4531
|
+
var _a;
|
4495
4532
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
4496
4533
|
return concreteSubtype;
|
4497
4534
|
}
|
@@ -4558,7 +4595,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4558
4595
|
const isClassVarAnnotation = (0, types_1.isInstantiableClass)(concreteSubtype) && types_1.ClassType.isBuiltIn(concreteSubtype, 'ClassVar');
|
4559
4596
|
let typeArgs = getTypeArgs(node, flags, isAnnotatedClass, hasCustomClassGetItem || !isGenericClass, isFinalAnnotation, isClassVarAnnotation);
|
4560
4597
|
if (!isAnnotatedClass) {
|
4561
|
-
typeArgs = adjustTypeArgumentsForVariadicTypeVar(typeArgs, concreteSubtype.details.typeParameters,
|
4598
|
+
typeArgs = adjustTypeArgumentsForVariadicTypeVar(typeArgs, concreteSubtype.details.typeParameters, node);
|
4562
4599
|
}
|
4563
4600
|
// If this is a custom __class_getitem__, there's no need to specialize the class.
|
4564
4601
|
// Just return it as is.
|
@@ -4571,7 +4608,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4571
4608
|
}), node.baseExpression);
|
4572
4609
|
return concreteSubtype;
|
4573
4610
|
}
|
4574
|
-
|
4611
|
+
const result = createSpecializedClassType(concreteSubtype, typeArgs, flags, node);
|
4612
|
+
if (result.isRequired) {
|
4613
|
+
isRequired = true;
|
4614
|
+
}
|
4615
|
+
else if (result.isNotRequired) {
|
4616
|
+
isNotRequired = true;
|
4617
|
+
}
|
4618
|
+
return result.type;
|
4575
4619
|
}
|
4576
4620
|
if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
4577
4621
|
const typeResult = getTypeOfIndexedObjectOrClass(node, concreteSubtype, usage);
|
@@ -4602,7 +4646,81 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4602
4646
|
}
|
4603
4647
|
});
|
4604
4648
|
}
|
4605
|
-
return { type, isIncomplete };
|
4649
|
+
return { type, isIncomplete, isRequired, isNotRequired };
|
4650
|
+
}
|
4651
|
+
// Determines the effective variance of the type parameters for a generic
|
4652
|
+
// type alias. Normally, variance is not important for type aliases, but
|
4653
|
+
// it can be important in cases where the type alias is used to specify
|
4654
|
+
// a base class in a class definition.
|
4655
|
+
function inferTypeParameterVarianceForTypeAlias(type) {
|
4656
|
+
// If this isn't a generic type alias, there's nothing to do.
|
4657
|
+
if (!type.typeAliasInfo || !type.typeAliasInfo.typeParameters) {
|
4658
|
+
return undefined;
|
4659
|
+
}
|
4660
|
+
// Is the usage variance info already cached?
|
4661
|
+
if (type.typeAliasInfo.usageVariance) {
|
4662
|
+
return type.typeAliasInfo.usageVariance;
|
4663
|
+
}
|
4664
|
+
const typeParams = type.typeAliasInfo.typeParameters;
|
4665
|
+
// Start with all of the usage variances unknown.
|
4666
|
+
const usageVariances = typeParams.map(() => 1 /* Unknown */);
|
4667
|
+
// Prepopulate the cached value for the type alias to handle
|
4668
|
+
// recursive type aliases.
|
4669
|
+
type.typeAliasInfo.usageVariance = usageVariances;
|
4670
|
+
// Traverse the type alias type definition and adjust the usage
|
4671
|
+
// variances accordingly.
|
4672
|
+
updateUsageVariancesRecursive(type, typeParams, usageVariances);
|
4673
|
+
return usageVariances;
|
4674
|
+
}
|
4675
|
+
// Looks at uses of the type parameters within the type and adjusts the
|
4676
|
+
// variances accordingly. For example, if the type is `Mapping[T1, T2]`,
|
4677
|
+
// then T1 will be set to invariant and T2 will be set to covariant.
|
4678
|
+
function updateUsageVariancesRecursive(type, typeAliasTypeParams, usageVariances, recursionCount = 0) {
|
4679
|
+
if (recursionCount > types_1.maxTypeRecursionCount) {
|
4680
|
+
return;
|
4681
|
+
}
|
4682
|
+
recursionCount++;
|
4683
|
+
// Define a helper function that performs the actual usage variant update.
|
4684
|
+
function updateUsageVarianceForType(type, variance) {
|
4685
|
+
(0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
|
4686
|
+
const typeParamIndex = typeAliasTypeParams.findIndex((param) => (0, types_1.isTypeSame)(param, subtype));
|
4687
|
+
if (typeParamIndex >= 0) {
|
4688
|
+
usageVariances[typeParamIndex] = (0, typeUtils_1.combineVariances)(usageVariances[typeParamIndex], variance);
|
4689
|
+
}
|
4690
|
+
else {
|
4691
|
+
updateUsageVariancesRecursive(subtype, typeAliasTypeParams, usageVariances, recursionCount);
|
4692
|
+
}
|
4693
|
+
});
|
4694
|
+
}
|
4695
|
+
(0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
|
4696
|
+
if (subtype.category === 5 /* Function */) {
|
4697
|
+
if (subtype.specializedTypes) {
|
4698
|
+
subtype.specializedTypes.parameterTypes.forEach((paramType) => {
|
4699
|
+
updateUsageVarianceForType(paramType, 4 /* Contravariant */);
|
4700
|
+
});
|
4701
|
+
const returnType = subtype.specializedTypes.returnType;
|
4702
|
+
if (returnType) {
|
4703
|
+
updateUsageVarianceForType(returnType, 3 /* Covariant */);
|
4704
|
+
}
|
4705
|
+
}
|
4706
|
+
}
|
4707
|
+
else if (subtype.category === 7 /* Class */) {
|
4708
|
+
if (subtype.typeArguments) {
|
4709
|
+
// If the class includes type parameters that uses auto variance,
|
4710
|
+
// compute the calculated variance.
|
4711
|
+
inferTypeParameterVarianceForClass(subtype);
|
4712
|
+
// Is the class specialized using any type arguments that correspond to
|
4713
|
+
// the type alias' type parameters?
|
4714
|
+
subtype.typeArguments.forEach((typeArg, classParamIndex) => {
|
4715
|
+
var _a;
|
4716
|
+
if (classParamIndex < subtype.details.typeParameters.length) {
|
4717
|
+
const classTypeParam = subtype.details.typeParameters[classParamIndex];
|
4718
|
+
updateUsageVarianceForType(typeArg, (_a = classTypeParam.computedVariance) !== null && _a !== void 0 ? _a : classTypeParam.details.declaredVariance);
|
4719
|
+
}
|
4720
|
+
});
|
4721
|
+
}
|
4722
|
+
}
|
4723
|
+
});
|
4606
4724
|
}
|
4607
4725
|
function makeTupleObject(entryTypes, isUnspecifiedLength = false) {
|
4608
4726
|
if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
|
@@ -4837,8 +4955,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4837
4955
|
adjFlags &= ~(2 /* DoNotSpecialize */ |
|
4838
4956
|
32 /* DisallowParamSpec */ |
|
4839
4957
|
64 /* DisallowTypeVarTuple */ |
|
4840
|
-
1048576 /* AllowRequired */
|
4841
|
-
16384 /* EnforceTypeVarVarianceConsistency */);
|
4958
|
+
1048576 /* AllowRequired */);
|
4842
4959
|
if (!isAnnotatedClass) {
|
4843
4960
|
adjFlags |= 131072 /* DisallowClassVar */ | 16 /* DisallowFinal */;
|
4844
4961
|
}
|
@@ -5195,7 +5312,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5195
5312
|
return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
|
5196
5313
|
}
|
5197
5314
|
const assertedType = (0, typeUtils_1.convertToInstance)(getTypeOfArgumentExpectingType(node.arguments[1]).type);
|
5198
|
-
if (!(0, types_1.isTypeSame)(assertedType, arg0TypeResult.type, { treatAnySameAsUnknown: true })) {
|
5315
|
+
if (!(0, types_1.isTypeSame)(assertedType, arg0TypeResult.type, { treatAnySameAsUnknown: true, ignorePseudoGeneric: true })) {
|
5199
5316
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.assertTypeTypeMismatch().format({
|
5200
5317
|
expected: printType(assertedType),
|
5201
5318
|
received: printType(arg0TypeResult.type),
|
@@ -5253,7 +5370,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5253
5370
|
}
|
5254
5371
|
}
|
5255
5372
|
if (expectedRevealType) {
|
5256
|
-
if (!(0, types_1.isTypeSame)(expectedRevealType, type)) {
|
5373
|
+
if (!(0, types_1.isTypeSame)(expectedRevealType, type, { ignorePseudoGeneric: true })) {
|
5257
5374
|
const expectedRevealTypeText = printType(expectedRevealType);
|
5258
5375
|
addError(localize_1.Localizer.Diagnostic.revealTypeExpectedTypeMismatch().format({
|
5259
5376
|
expected: expectedRevealTypeText,
|
@@ -5411,9 +5528,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5411
5528
|
type: resultIsInstance
|
5412
5529
|
? types_1.ClassType.cloneAsInstance(lookupResults.classType)
|
5413
5530
|
: lookupResults.classType,
|
5414
|
-
bindToType:
|
5415
|
-
? types_1.
|
5416
|
-
:
|
5531
|
+
bindToType: bindToType
|
5532
|
+
? types_1.TypeBase.cloneForCondition((0, typeUtils_1.synthesizeTypeVarForSelfCls)(bindToType, !resultIsInstance), bindToType.condition)
|
5533
|
+
: undefined,
|
5417
5534
|
};
|
5418
5535
|
}
|
5419
5536
|
}
|
@@ -5479,6 +5596,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5479
5596
|
: typeVarContext;
|
5480
5597
|
const effectiveTypeVarContext = (_a = typeVarContextToClone === null || typeVarContextToClone === void 0 ? void 0 : typeVarContextToClone.clone()) !== null && _a !== void 0 ? _a : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(overload));
|
5481
5598
|
effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(overload));
|
5599
|
+
if (overload.details.constructorTypeVarScopeId) {
|
5600
|
+
effectiveTypeVarContext.addSolveForScope(overload.details.constructorTypeVarScopeId);
|
5601
|
+
}
|
5482
5602
|
effectiveTypeVarContext.unlock();
|
5483
5603
|
// Use speculative mode so we don't output any diagnostics or
|
5484
5604
|
// record any final types in the type cache.
|
@@ -5686,6 +5806,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5686
5806
|
});
|
5687
5807
|
const effectiveTypeVarContext = typeVarContext !== null && typeVarContext !== void 0 ? typeVarContext : new typeVarContext_1.TypeVarContext();
|
5688
5808
|
effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(lastMatch.overload));
|
5809
|
+
if (lastMatch.overload.details.constructorTypeVarScopeId) {
|
5810
|
+
effectiveTypeVarContext.addSolveForScope(lastMatch.overload.details.constructorTypeVarScopeId);
|
5811
|
+
}
|
5689
5812
|
effectiveTypeVarContext.unlock();
|
5690
5813
|
return validateFunctionArgumentTypesWithExpectedType(errorNode, lastMatch, effectiveTypeVarContext, skipUnknownArgCheck, inferenceContext);
|
5691
5814
|
};
|
@@ -6496,7 +6619,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6496
6619
|
// validation is left to the caller.
|
6497
6620
|
// This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
|
6498
6621
|
function matchFunctionArgumentsToParameters(errorNode, argList, typeResult, overloadIndex) {
|
6499
|
-
var _a, _b, _c, _d, _e;
|
6622
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
6500
6623
|
const paramDetails = (0, typeUtils_1.getParameterListDetails)(typeResult.type);
|
6501
6624
|
let argIndex = 0;
|
6502
6625
|
let matchedUnpackedListOfUnknownLength = false;
|
@@ -6619,11 +6742,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6619
6742
|
tooManyPositionals = true;
|
6620
6743
|
}
|
6621
6744
|
if (tooManyPositionals) {
|
6622
|
-
|
6623
|
-
|
6624
|
-
|
6625
|
-
|
6626
|
-
|
6745
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
6746
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
|
6747
|
+
? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
|
6748
|
+
: localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
|
6749
|
+
expected: positionParamLimitIndex,
|
6750
|
+
}), (_b = argList[argIndex].valueExpression) !== null && _b !== void 0 ? _b : errorNode);
|
6751
|
+
}
|
6627
6752
|
reportedArgError = true;
|
6628
6753
|
}
|
6629
6754
|
}
|
@@ -6647,11 +6772,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6647
6772
|
// all positional parameters specified in the Concatenate must be
|
6648
6773
|
// filled explicitly.
|
6649
6774
|
if (typeResult.type.details.paramSpec && paramIndex < positionParamLimitIndex) {
|
6650
|
-
|
6651
|
-
|
6652
|
-
|
6653
|
-
|
6654
|
-
|
6775
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
6776
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
|
6777
|
+
? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
|
6778
|
+
: localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
|
6779
|
+
expected: positionParamLimitIndex,
|
6780
|
+
}), (_c = argList[argIndex].valueExpression) !== null && _c !== void 0 ? _c : errorNode);
|
6781
|
+
}
|
6655
6782
|
reportedArgError = true;
|
6656
6783
|
}
|
6657
6784
|
const argType = argTypeResult.type;
|
@@ -6710,7 +6837,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6710
6837
|
// It's not allowed to use unpacked arguments with a variadic *args
|
6711
6838
|
// parameter unless the argument is a variadic arg as well.
|
6712
6839
|
if (isParamVariadic && !isArgCompatibleWithVariadic) {
|
6713
|
-
|
6840
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
6841
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedArgWithVariadicParam(), argList[argIndex].valueExpression || errorNode);
|
6842
|
+
}
|
6714
6843
|
reportedArgError = true;
|
6715
6844
|
}
|
6716
6845
|
else {
|
@@ -6723,7 +6852,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6723
6852
|
paramType,
|
6724
6853
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
6725
6854
|
argument: funcArg,
|
6726
|
-
errorNode: argList[argIndex].valueExpression
|
6855
|
+
errorNode: (_f = argList[argIndex].valueExpression) !== null && _f !== void 0 ? _f : errorNode,
|
6727
6856
|
paramName,
|
6728
6857
|
isParamNameSynthesized: paramDetails.params[paramIndex].param.isNameSynthesized,
|
6729
6858
|
mapsToVarArgList: isParamVariadic && remainingArgCount > remainingParamCount,
|
@@ -6766,12 +6895,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6766
6895
|
: 0 /* Simple */;
|
6767
6896
|
if (remainingArgCount <= remainingParamCount) {
|
6768
6897
|
if (remainingArgCount < remainingParamCount) {
|
6769
|
-
|
6770
|
-
|
6771
|
-
|
6772
|
-
|
6773
|
-
|
6774
|
-
|
6898
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
6899
|
+
// Have we run out of arguments and still have parameters left to fill?
|
6900
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, remainingArgCount === 1
|
6901
|
+
? localize_1.Localizer.Diagnostic.argMorePositionalExpectedOne()
|
6902
|
+
: localize_1.Localizer.Diagnostic.argMorePositionalExpectedCount().format({
|
6903
|
+
expected: remainingArgCount,
|
6904
|
+
}), argList[argIndex].valueExpression || errorNode);
|
6905
|
+
}
|
6775
6906
|
reportedArgError = true;
|
6776
6907
|
}
|
6777
6908
|
paramIndex++;
|
@@ -6846,13 +6977,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6846
6977
|
argsRemainingCount--;
|
6847
6978
|
}
|
6848
6979
|
if (argsRemainingCount > 0) {
|
6849
|
-
|
6850
|
-
|
6851
|
-
|
6852
|
-
|
6853
|
-
|
6854
|
-
|
6855
|
-
|
6980
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
6981
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, argsRemainingCount === 1
|
6982
|
+
? localize_1.Localizer.Diagnostic.argMorePositionalExpectedOne()
|
6983
|
+
: localize_1.Localizer.Diagnostic.argMorePositionalExpectedCount().format({
|
6984
|
+
expected: argsRemainingCount,
|
6985
|
+
}), argList.length > positionalArgCount
|
6986
|
+
? argList[positionalArgCount].valueExpression || errorNode
|
6987
|
+
: errorNode);
|
6988
|
+
}
|
6856
6989
|
reportedArgError = true;
|
6857
6990
|
}
|
6858
6991
|
}
|
@@ -6926,7 +7059,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6926
7059
|
}
|
6927
7060
|
});
|
6928
7061
|
if (!diag.isEmpty()) {
|
6929
|
-
|
7062
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
7063
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedTypedDictArgument() + diag.getString(), argList[argIndex].valueExpression || errorNode);
|
7064
|
+
}
|
6930
7065
|
reportedArgError = true;
|
6931
7066
|
}
|
6932
7067
|
}
|
@@ -6972,7 +7107,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6972
7107
|
}
|
6973
7108
|
}
|
6974
7109
|
if (!isValidMappingType) {
|
6975
|
-
|
7110
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
7111
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet
|
7112
|
+
.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.unpackedDictArgumentNotMapping(), argList[argIndex].valueExpression || errorNode);
|
7113
|
+
}
|
6976
7114
|
reportedArgError = true;
|
6977
7115
|
}
|
6978
7116
|
}
|
@@ -7004,7 +7142,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7004
7142
|
paramType,
|
7005
7143
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
7006
7144
|
argument: argList[argIndex],
|
7007
|
-
errorNode: argList[argIndex].valueExpression
|
7145
|
+
errorNode: (_g = argList[argIndex].valueExpression) !== null && _g !== void 0 ? _g : errorNode,
|
7008
7146
|
paramName: paramNameValue,
|
7009
7147
|
});
|
7010
7148
|
trySetActive(argList[argIndex], paramDetails.params[paramInfoIndex].param);
|
@@ -7021,7 +7159,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7021
7159
|
paramType,
|
7022
7160
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
7023
7161
|
argument: argList[argIndex],
|
7024
|
-
errorNode: argList[argIndex].valueExpression
|
7162
|
+
errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
|
7025
7163
|
paramName: paramNameValue,
|
7026
7164
|
});
|
7027
7165
|
// Remember that this parameter has already received a value.
|
@@ -7040,12 +7178,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7040
7178
|
}
|
7041
7179
|
}
|
7042
7180
|
else if (argList[argIndex].argumentCategory === 0 /* Simple */) {
|
7043
|
-
|
7044
|
-
|
7045
|
-
|
7046
|
-
|
7047
|
-
|
7048
|
-
|
7181
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
7182
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
7183
|
+
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, positionParamLimitIndex === 1
|
7184
|
+
? localize_1.Localizer.Diagnostic.argPositionalExpectedOne()
|
7185
|
+
: localize_1.Localizer.Diagnostic.argPositionalExpectedCount().format({
|
7186
|
+
expected: positionParamLimitIndex,
|
7187
|
+
}), argList[argIndex].valueExpression || errorNode);
|
7188
|
+
}
|
7049
7189
|
reportedArgError = true;
|
7050
7190
|
}
|
7051
7191
|
else if (argList[argIndex].argumentCategory === 1 /* UnpackedList */) {
|
@@ -7063,7 +7203,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7063
7203
|
paramType: typeResult.type.details.paramSpec,
|
7064
7204
|
requiresTypeVarMatching: false,
|
7065
7205
|
argument: argList[argIndex],
|
7066
|
-
errorNode: argList[argIndex].valueExpression
|
7206
|
+
errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
|
7067
7207
|
});
|
7068
7208
|
}
|
7069
7209
|
}
|
@@ -7113,10 +7253,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7113
7253
|
return !entry || entry.argsReceived < entry.argsNeeded;
|
7114
7254
|
});
|
7115
7255
|
if (unassignedParams.length > 0) {
|
7116
|
-
|
7117
|
-
|
7118
|
-
|
7119
|
-
|
7256
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
7257
|
+
const missingParamNames = unassignedParams.map((p) => `"${p}"`).join(', ');
|
7258
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, unassignedParams.length === 1
|
7259
|
+
? localize_1.Localizer.Diagnostic.argMissingForParam().format({ name: missingParamNames })
|
7260
|
+
: localize_1.Localizer.Diagnostic.argMissingForParams().format({ names: missingParamNames }), errorNode);
|
7261
|
+
}
|
7120
7262
|
reportedArgError = true;
|
7121
7263
|
}
|
7122
7264
|
// Add any implicit (default) arguments that are needed for resolving
|
@@ -7142,7 +7284,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7142
7284
|
argumentCategory: 0 /* Simple */,
|
7143
7285
|
typeResult: { type: defaultArgType },
|
7144
7286
|
},
|
7145
|
-
errorNode
|
7287
|
+
errorNode,
|
7146
7288
|
paramName: param.name,
|
7147
7289
|
isParamNameSynthesized: param.isNameSynthesized,
|
7148
7290
|
});
|
@@ -7180,7 +7322,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7180
7322
|
if (containsVariadicTypeVar &&
|
7181
7323
|
argParam.argument.argumentCategory !== 1 /* UnpackedList */ &&
|
7182
7324
|
!argParam.mapsToVarArgList) {
|
7183
|
-
|
7325
|
+
if (!isDiagnosticSuppressedForNode(errorNode)) {
|
7326
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet
|
7327
|
+
.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarTupleMustBeUnpacked(), (_a = argParam.argument.valueExpression) !== null && _a !== void 0 ? _a : errorNode);
|
7328
|
+
}
|
7184
7329
|
reportedArgError = true;
|
7185
7330
|
}
|
7186
7331
|
return {
|
@@ -8271,9 +8416,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8271
8416
|
/* declaredMetaclass */ undefined, arg1Type.details.effectiveMetaclass);
|
8272
8417
|
arg1Type.tupleTypeArguments.forEach((typeArg) => {
|
8273
8418
|
const specializedType = makeTopLevelTypeVarsConcrete(typeArg.type);
|
8274
|
-
if ((0,
|
8275
|
-
(0, types_1.isAnyOrUnknown)(specializedType) ||
|
8276
|
-
((0, types_1.isClassInstance)(specializedType) && types_1.ClassType.isBuiltIn(specializedType, 'type'))) {
|
8419
|
+
if ((0, typeUtils_1.isEffectivelyInstantiable)(specializedType)) {
|
8277
8420
|
classType.details.baseClasses.push(specializedType);
|
8278
8421
|
}
|
8279
8422
|
else {
|
@@ -8545,9 +8688,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8545
8688
|
}
|
8546
8689
|
}
|
8547
8690
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
8548
|
-
// Don't use literal math if either of the
|
8549
|
-
//
|
8550
|
-
// so the literal values may change each time.
|
8691
|
+
// Don't use literal math if either of the operation is within a loop
|
8692
|
+
// because the literal values may change each time.
|
8551
8693
|
const isLiteralMathAllowed = !ParseTreeUtils.isWithinLoop(node);
|
8552
8694
|
// Don't special-case tuple __add__ if the left type is a union. This
|
8553
8695
|
// can result in an infinite loop if we keep creating new tuple types
|
@@ -8654,11 +8796,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8654
8796
|
// If the LHS class didn't support the magic method for augmented
|
8655
8797
|
// assignment, fall back on the normal binary expression evaluator.
|
8656
8798
|
const binaryOperator = operatorMap[node.operator][1];
|
8657
|
-
// Don't use literal math if either of the
|
8658
|
-
//
|
8659
|
-
|
8660
|
-
const isLiteralMathAllowed = !leftTypeResult.isIncomplete &&
|
8661
|
-
!rightTypeResult.isIncomplete &&
|
8799
|
+
// Don't use literal math if either of the operation is within a loop
|
8800
|
+
// because the literal values may change each time.
|
8801
|
+
const isLiteralMathAllowed = !ParseTreeUtils.isWithinLoop(node) &&
|
8662
8802
|
(0, typeUtils_1.getUnionSubtypeCount)(leftType) * (0, typeUtils_1.getUnionSubtypeCount)(rightType) <
|
8663
8803
|
maxLiteralMathSubtypeCount;
|
8664
8804
|
// Don't special-case tuple __add__ if the left type is a union. This
|
@@ -9207,6 +9347,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9207
9347
|
if (!keyTypeResult.isIncomplete && !keyTypeResult.typeErrors) {
|
9208
9348
|
verifySetEntryOrDictKeyIsHashable(entryNode.keyExpression, keyType, /* isDictKey */ true);
|
9209
9349
|
}
|
9350
|
+
if (expectedDiagAddendum && keyTypeResult.expectedTypeDiagAddendum) {
|
9351
|
+
expectedDiagAddendum.addAddendum(keyTypeResult.expectedTypeDiagAddendum);
|
9352
|
+
}
|
9210
9353
|
let valueTypeResult;
|
9211
9354
|
if (expectedTypedDictEntries &&
|
9212
9355
|
(0, types_1.isClassInstance)(keyType) &&
|
@@ -9236,7 +9379,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9236
9379
|
addUnknown = false;
|
9237
9380
|
}
|
9238
9381
|
else if (entryNode.nodeType === 16 /* DictionaryExpandEntry */) {
|
9239
|
-
|
9382
|
+
// Verify that the type supports the `keys` and `__getitem__` methods.
|
9383
|
+
// This protocol is defined in the _typeshed stub. If we can't find
|
9384
|
+
// it there, fall back on typing.Mapping.
|
9385
|
+
let mappingType = getTypeshedType(node, 'SupportsKeysAndGetItem');
|
9386
|
+
if (!mappingType) {
|
9387
|
+
mappingType = getTypingType(node, 'Mapping');
|
9388
|
+
}
|
9389
|
+
let expectedType;
|
9390
|
+
if (expectedKeyType && expectedValueType) {
|
9391
|
+
if (mappingType && (0, types_1.isInstantiableClass)(mappingType)) {
|
9392
|
+
expectedType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(mappingType, [expectedKeyType, expectedValueType],
|
9393
|
+
/* isTypeArgumentExplicit */ true));
|
9394
|
+
}
|
9395
|
+
}
|
9396
|
+
const unexpandedTypeResult = getTypeOfExpression(entryNode.expandExpression,
|
9397
|
+
/* flags */ undefined, (0, typeUtils_1.makeInferenceContext)(expectedType));
|
9240
9398
|
if (unexpandedTypeResult.isIncomplete) {
|
9241
9399
|
isIncomplete = true;
|
9242
9400
|
}
|
@@ -9260,20 +9418,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9260
9418
|
}
|
9261
9419
|
}
|
9262
9420
|
else {
|
9263
|
-
// Verify that the type supports the `keys` and `__getitem__` methods.
|
9264
|
-
// This protocol is defined in the _typeshed stub. If we can't find
|
9265
|
-
// it there, fall back on typing.Mapping.
|
9266
|
-
let mappingType = getTypeshedType(node, 'SupportsKeysAndGetItem');
|
9267
|
-
if (!mappingType) {
|
9268
|
-
mappingType = getTypingType(node, 'Mapping');
|
9269
|
-
}
|
9270
9421
|
if (mappingType && (0, types_1.isInstantiableClass)(mappingType)) {
|
9271
9422
|
const mappingTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(mappingType));
|
9272
9423
|
// Self-specialize the class.
|
9273
9424
|
mappingType = types_1.ClassType.cloneForSpecialization(mappingType, mappingType.details.typeParameters,
|
9274
9425
|
/* isTypeArgumentExplicit */ true);
|
9275
9426
|
if (assignType(types_1.ClassType.cloneAsInstance(mappingType), unexpandedType,
|
9276
|
-
/* diag */ undefined, mappingTypeVarContext
|
9427
|
+
/* diag */ undefined, mappingTypeVarContext,
|
9428
|
+
/* srcTypeVarContext */ undefined, 128 /* RetainLiteralsForTypeVar */)) {
|
9277
9429
|
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(mappingType, mappingTypeVarContext);
|
9278
9430
|
const typeArgs = specializedMapping.typeArguments;
|
9279
9431
|
if (typeArgs && typeArgs.length >= 2) {
|
@@ -9538,7 +9690,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9538
9690
|
const typesToCombine = [];
|
9539
9691
|
let isIncomplete = false;
|
9540
9692
|
let typeErrors = false;
|
9541
|
-
|
9693
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
9694
|
+
const constExprValue = (0, staticExpressions_1.evaluateStaticBoolExpression)(node.testExpression, fileInfo.executionEnvironment, fileInfo.definedConstants);
|
9695
|
+
if (constExprValue !== false && isNodeReachable(node.ifExpression)) {
|
9542
9696
|
const ifType = getTypeOfExpression(node.ifExpression, flags, inferenceContext);
|
9543
9697
|
typesToCombine.push(ifType.type);
|
9544
9698
|
if (ifType.isIncomplete) {
|
@@ -9548,7 +9702,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9548
9702
|
typeErrors = true;
|
9549
9703
|
}
|
9550
9704
|
}
|
9551
|
-
if (isNodeReachable(node.elseExpression)) {
|
9705
|
+
if (constExprValue !== true && isNodeReachable(node.elseExpression)) {
|
9552
9706
|
const elseType = getTypeOfExpression(node.elseExpression, flags, inferenceContext);
|
9553
9707
|
typesToCombine.push(elseType.type);
|
9554
9708
|
if (elseType.isIncomplete) {
|
@@ -10091,7 +10245,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10091
10245
|
if (!validateTypeArg(typeArgs[0])) {
|
10092
10246
|
typeArg0Type = types_1.UnknownType.create();
|
10093
10247
|
}
|
10094
|
-
else if (!
|
10248
|
+
else if (!(0, typeUtils_1.isEffectivelyInstantiable)(typeArg0Type)) {
|
10095
10249
|
addExpectedClassDiagnostic(typeArg0Type, typeArgs[0].node);
|
10096
10250
|
typeArg0Type = types_1.UnknownType.create();
|
10097
10251
|
}
|
@@ -10291,16 +10445,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10291
10445
|
return (0, typeUtils_1.synthesizeTypeVarForSelfCls)(enclosingClassTypeResult.classType, /* isClsParam */ true);
|
10292
10446
|
}
|
10293
10447
|
function createRequiredType(classType, errorNode, isRequired, typeArgs, flags) {
|
10294
|
-
var _a;
|
10295
10448
|
// If no type arguments are provided, the resulting type
|
10296
10449
|
// depends on whether we're evaluating a type annotation or
|
10297
10450
|
// we're in some other context.
|
10298
10451
|
if (!typeArgs && (flags & 256 /* ExpectingTypeAnnotation */) === 0) {
|
10299
|
-
return classType;
|
10452
|
+
return { type: classType };
|
10300
10453
|
}
|
10301
10454
|
if (!typeArgs || typeArgs.length !== 1) {
|
10302
10455
|
addError(isRequired ? localize_1.Localizer.Diagnostic.requiredArgCount() : localize_1.Localizer.Diagnostic.notRequiredArgCount(), errorNode);
|
10303
|
-
return classType;
|
10456
|
+
return { type: classType };
|
10304
10457
|
}
|
10305
10458
|
const typeArgType = typeArgs[0].type;
|
10306
10459
|
// Make sure this is used only in a dataclass.
|
@@ -10311,21 +10464,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10311
10464
|
(0, types_1.isInstantiableClass)(classTypeInfo.classType) &&
|
10312
10465
|
types_1.ClassType.isTypedDictClass(classTypeInfo.classType)) {
|
10313
10466
|
// The only legal usage is when used in a type annotation statement.
|
10314
|
-
if ((
|
10315
|
-
errorNode.parent.typeAnnotation === errorNode) {
|
10467
|
+
if (ParseTreeUtils.isNodeContainedWithinNodeType(errorNode, 54 /* TypeAnnotation */)) {
|
10316
10468
|
isUsageLegal = true;
|
10317
10469
|
}
|
10318
10470
|
}
|
10319
10471
|
if ((flags & 1048576 /* AllowRequired */) !== 0) {
|
10320
10472
|
isUsageLegal = true;
|
10321
10473
|
}
|
10474
|
+
// Nested Required/NotRequired are not allowed.
|
10475
|
+
if (typeArgs[0].isRequired || typeArgs[0].isNotRequired) {
|
10476
|
+
isUsageLegal = false;
|
10477
|
+
}
|
10322
10478
|
if (!isUsageLegal) {
|
10323
10479
|
addError(isRequired
|
10324
10480
|
? localize_1.Localizer.Diagnostic.requiredNotInTypedDict()
|
10325
10481
|
: localize_1.Localizer.Diagnostic.notRequiredNotInTypedDict(), errorNode);
|
10326
|
-
return types_1.ClassType.cloneForSpecialization(classType, [(0, typeUtils_1.convertToInstance)(typeArgType)], !!typeArgs);
|
10482
|
+
return { type: types_1.ClassType.cloneForSpecialization(classType, [(0, typeUtils_1.convertToInstance)(typeArgType)], !!typeArgs) };
|
10327
10483
|
}
|
10328
|
-
return typeArgType;
|
10484
|
+
return { type: typeArgType, isRequired, isNotRequired: !isRequired };
|
10329
10485
|
}
|
10330
10486
|
function createUnpackType(classType, errorNode, typeArgs, flags) {
|
10331
10487
|
// If no type arguments are provided, the resulting type
|
@@ -10402,9 +10558,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10402
10558
|
addError(localize_1.Localizer.Diagnostic.annotatedTypeArgMissing(), errorNode);
|
10403
10559
|
}
|
10404
10560
|
if (!typeArgs || typeArgs.length === 0) {
|
10405
|
-
return types_1.AnyType.create();
|
10561
|
+
return { type: types_1.AnyType.create() };
|
10406
10562
|
}
|
10407
|
-
return
|
10563
|
+
return {
|
10564
|
+
type: types_1.TypeBase.cloneForAnnotated(typeArgs[0].type),
|
10565
|
+
isRequired: typeArgs[0].isRequired,
|
10566
|
+
isNotRequired: typeArgs[0].isNotRequired,
|
10567
|
+
};
|
10408
10568
|
}
|
10409
10569
|
// Creates one of several "special" types that are defined in typing.pyi
|
10410
10570
|
// but not declared in their entirety. This includes the likes of "Tuple",
|
@@ -10533,7 +10693,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10533
10693
|
if (!validateTypeArg(typeArg, { allowVariadicTypeVar: true, allowUnpackedTuples: true })) {
|
10534
10694
|
typeArgType = types_1.UnknownType.create();
|
10535
10695
|
}
|
10536
|
-
else if (!
|
10696
|
+
else if (!(0, typeUtils_1.isEffectivelyInstantiable)(typeArgType)) {
|
10537
10697
|
addExpectedClassDiagnostic(typeArgType, typeArg.node);
|
10538
10698
|
typeArgType = types_1.UnknownType.create();
|
10539
10699
|
}
|
@@ -10797,7 +10957,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10797
10957
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
10798
10958
|
// If the entire statement has already been evaluated, don't
|
10799
10959
|
// re-evaluate it.
|
10800
|
-
if (
|
10960
|
+
if (isTypeCached(node)) {
|
10801
10961
|
return;
|
10802
10962
|
}
|
10803
10963
|
let flags = 0 /* None */;
|
@@ -10844,7 +11004,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10844
11004
|
let isSpeculativeTypeAlias = false;
|
10845
11005
|
if (isDeclaredTypeAlias(node.leftExpression)) {
|
10846
11006
|
typeAliasNameNode = node.leftExpression.valueExpression;
|
10847
|
-
if (!
|
11007
|
+
if (!isLegalTypeAliasExpressionForm(node.rightExpression)) {
|
10848
11008
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.rightExpression);
|
10849
11009
|
}
|
10850
11010
|
}
|
@@ -10941,7 +11101,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10941
11101
|
}
|
10942
11102
|
function isPossibleTypeAliasOrTypedDict(decl) {
|
10943
11103
|
var _a;
|
10944
|
-
if (
|
11104
|
+
if (isPossibleTypeAliasDeclaration(decl)) {
|
10945
11105
|
return true;
|
10946
11106
|
}
|
10947
11107
|
if (decl.type === 1 /* Variable */ &&
|
@@ -10997,7 +11157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10997
11157
|
typeParameters = evaluateTypeParameterList(node.typeParameters);
|
10998
11158
|
typeAliasTypeVar.details.recursiveTypeParameters = typeParameters;
|
10999
11159
|
}
|
11000
|
-
if (!
|
11160
|
+
if (!isLegalTypeAliasExpressionForm(node.expression)) {
|
11001
11161
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.expression);
|
11002
11162
|
}
|
11003
11163
|
const aliasTypeResult = getTypeOfExpressionExpectingType(node.expression);
|
@@ -11025,7 +11185,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11025
11185
|
return aliasType;
|
11026
11186
|
}
|
11027
11187
|
function evaluateTypesForAugmentedAssignment(node) {
|
11028
|
-
if (
|
11188
|
+
if (isTypeCached(node)) {
|
11029
11189
|
return;
|
11030
11190
|
}
|
11031
11191
|
const destTypeResult = getTypeOfAugmentedAssignment(node, /* inferenceContext */ undefined);
|
@@ -11136,7 +11296,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11136
11296
|
argType = (0, types_1.removeUnbound)(argType);
|
11137
11297
|
}
|
11138
11298
|
if (!(0, types_1.isAnyOrUnknown)(argType) && !(0, types_1.isUnbound)(argType)) {
|
11139
|
-
if (
|
11299
|
+
if ((0, types_1.isClass)(argType) && types_1.TypeBase.isInstance(argType) && types_1.ClassType.isBuiltIn(argType, 'type')) {
|
11300
|
+
argType =
|
11301
|
+
argType.typeArguments && argType.typeArguments.length > 0
|
11302
|
+
? argType.typeArguments[0]
|
11303
|
+
: types_1.UnknownType.create();
|
11304
|
+
}
|
11305
|
+
else if (!(0, types_1.isInstantiableClass)(argType)) {
|
11140
11306
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.baseClassInvalid(), arg);
|
11141
11307
|
argType = types_1.UnknownType.create();
|
11142
11308
|
}
|
@@ -11566,7 +11732,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11566
11732
|
// Presumptively mark the variance inference as complete. This
|
11567
11733
|
// prevents potential recursion.
|
11568
11734
|
classType.details.requiresVarianceInference = false;
|
11569
|
-
// Presumptively mark the computed variance to "
|
11735
|
+
// Presumptively mark the computed variance to "unknown". We'll
|
11570
11736
|
// replace this below once the variance has been inferred.
|
11571
11737
|
classType.details.typeParameters.forEach((param) => {
|
11572
11738
|
if (param.details.declaredVariance === 0 /* Auto */) {
|
@@ -11603,13 +11769,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11603
11769
|
/* isTypeArgumentExplicit */ true);
|
11604
11770
|
const destType = types_1.ClassType.cloneForSpecialization(updatedClassType, destTypeArgs,
|
11605
11771
|
/* isTypeArgumentExplicit */ true);
|
11606
|
-
const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType);
|
11772
|
+
const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType, /* ignoreBaseClassVariance */ false);
|
11607
11773
|
let inferredVariance;
|
11608
11774
|
if (isDestSubtypeOfSrc) {
|
11609
11775
|
inferredVariance = 3 /* Covariant */;
|
11610
11776
|
}
|
11611
11777
|
else {
|
11612
|
-
const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType);
|
11778
|
+
const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType, /* ignoreBaseClassVariance */ false);
|
11613
11779
|
if (isSrcSubtypeOfDest) {
|
11614
11780
|
inferredVariance = 4 /* Contravariant */;
|
11615
11781
|
}
|
@@ -11626,9 +11792,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11626
11792
|
function evaluateTypeParameterList(node) {
|
11627
11793
|
const paramTypes = [];
|
11628
11794
|
node.parameters.forEach((param) => {
|
11795
|
+
var _a;
|
11629
11796
|
const paramSymbol = AnalyzerNodeInfo.getTypeParameterSymbol(param.name);
|
11630
11797
|
(0, debug_1.assert)(paramSymbol);
|
11631
|
-
const typeOfParam = getDeclaredTypeOfSymbol(paramSymbol, param.name);
|
11798
|
+
const typeOfParam = (_a = getDeclaredTypeOfSymbol(paramSymbol, param.name)) === null || _a === void 0 ? void 0 : _a.type;
|
11632
11799
|
if (!typeOfParam || !(0, types_1.isTypeVar)(typeOfParam)) {
|
11633
11800
|
return;
|
11634
11801
|
}
|
@@ -11937,10 +12104,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11937
12104
|
if (functionDecl === null || functionDecl === void 0 ? void 0 : functionDecl.isGenerator) {
|
11938
12105
|
functionFlags |= 16 /* Generator */;
|
11939
12106
|
}
|
11940
|
-
// Special-case magic method __class_getitem__, which is implicitly a class method.
|
11941
|
-
if (containingClassNode && node.name.value === '__class_getitem__') {
|
11942
|
-
functionFlags |= 2 /* ClassMethod */;
|
11943
|
-
}
|
11944
12107
|
if (fileInfo.isStubFile) {
|
11945
12108
|
functionFlags |= 2048 /* StubDefinition */;
|
11946
12109
|
}
|
@@ -12463,15 +12626,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12463
12626
|
function getFunctionFlagsFromDecorators(node, isInClass) {
|
12464
12627
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
12465
12628
|
let flags = 0 /* None */;
|
12466
|
-
|
12467
|
-
|
12468
|
-
|
12469
|
-
|
12470
|
-
|
12471
|
-
|
12472
|
-
|
12473
|
-
|
12474
|
-
|
12629
|
+
if (isInClass) {
|
12630
|
+
// The "__new__" magic method is not an instance method.
|
12631
|
+
// It acts as a static method instead.
|
12632
|
+
if (node.name.value === '__new__') {
|
12633
|
+
flags |= 1 /* ConstructorMethod */;
|
12634
|
+
}
|
12635
|
+
// Several magic methods are treated as class methods implicitly
|
12636
|
+
// by the runtime. Check for these here.
|
12637
|
+
const implicitClassMethods = ['__init_subclass__', '__class_getitem__'];
|
12638
|
+
if (implicitClassMethods.some((name) => node.name.value === name)) {
|
12639
|
+
flags |= 2 /* ClassMethod */;
|
12640
|
+
}
|
12475
12641
|
}
|
12476
12642
|
for (const decoratorNode of node.decorators) {
|
12477
12643
|
// Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators.
|
@@ -12928,7 +13094,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12928
13094
|
}
|
12929
13095
|
function evaluateTypesForForStatement(node) {
|
12930
13096
|
var _a, _b;
|
12931
|
-
if (
|
13097
|
+
if (isTypeCached(node)) {
|
12932
13098
|
return;
|
12933
13099
|
}
|
12934
13100
|
const iteratorTypeResult = getTypeOfExpression(node.iterableExpression);
|
@@ -12939,7 +13105,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12939
13105
|
function evaluateTypesForExceptStatement(node) {
|
12940
13106
|
// This should be called only if the except node has a target exception.
|
12941
13107
|
(0, debug_1.assert)(node.typeExpression !== undefined);
|
12942
|
-
if (
|
13108
|
+
if (isTypeCached(node)) {
|
12943
13109
|
return;
|
12944
13110
|
}
|
12945
13111
|
const exceptionTypeResult = getTypeOfExpression(node.typeExpression);
|
@@ -12987,7 +13153,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12987
13153
|
writeTypeCache(node, { type: targetType }, 0 /* None */);
|
12988
13154
|
}
|
12989
13155
|
function evaluateTypesForWithStatement(node) {
|
12990
|
-
if (
|
13156
|
+
if (isTypeCached(node)) {
|
12991
13157
|
return;
|
12992
13158
|
}
|
12993
13159
|
const exprTypeResult = getTypeOfExpression(node.expression);
|
@@ -13053,7 +13219,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13053
13219
|
}
|
13054
13220
|
function evaluateTypesForImportAs(node) {
|
13055
13221
|
var _a;
|
13056
|
-
if (
|
13222
|
+
if (isTypeCached(node)) {
|
13057
13223
|
return;
|
13058
13224
|
}
|
13059
13225
|
let symbolNameNode;
|
@@ -13085,7 +13251,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13085
13251
|
}
|
13086
13252
|
function evaluateTypesForImportFromAs(node) {
|
13087
13253
|
var _a;
|
13088
|
-
if (
|
13254
|
+
if (isTypeCached(node)) {
|
13089
13255
|
return;
|
13090
13256
|
}
|
13091
13257
|
const aliasNode = node.alias || node.name;
|
@@ -13141,7 +13307,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13141
13307
|
writeTypeCache(node, { type: symbolType }, 0 /* None */);
|
13142
13308
|
}
|
13143
13309
|
function evaluateTypesForMatchStatement(node) {
|
13144
|
-
if (
|
13310
|
+
if (isTypeCached(node)) {
|
13145
13311
|
return;
|
13146
13312
|
}
|
13147
13313
|
const subjectTypeResult = getTypeOfExpression(node.subjectExpression);
|
@@ -13156,19 +13322,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13156
13322
|
writeTypeCache(node, { type: subjectType, isIncomplete: !!subjectTypeResult.isIncomplete }, 0 /* None */);
|
13157
13323
|
}
|
13158
13324
|
function evaluateTypesForCaseStatement(node) {
|
13159
|
-
if (
|
13325
|
+
if (isTypeCached(node)) {
|
13160
13326
|
return;
|
13161
13327
|
}
|
13162
13328
|
if (!node.parent || node.parent.nodeType !== 63 /* Match */) {
|
13163
13329
|
(0, debug_1.fail)('Expected parent of case statement to be match statement');
|
13164
13330
|
return;
|
13165
13331
|
}
|
13332
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
13166
13333
|
const subjectTypeResult = getTypeOfExpression(node.parent.subjectExpression);
|
13167
13334
|
let subjectType = subjectTypeResult.type;
|
13168
13335
|
// Apply negative narrowing for each of the cases prior to the current one
|
13169
13336
|
// except for those that have a guard expression.
|
13170
13337
|
for (const caseStatement of node.parent.cases) {
|
13171
13338
|
if (caseStatement === node) {
|
13339
|
+
if (fileInfo.diagnosticRuleSet.reportUnnecessaryComparison !== 'none') {
|
13340
|
+
(0, patternMatching_1.checkForUnusedPattern)(evaluatorInterface, node.pattern, subjectType);
|
13341
|
+
}
|
13172
13342
|
break;
|
13173
13343
|
}
|
13174
13344
|
if (!caseStatement.guardExpression) {
|
@@ -13190,7 +13360,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13190
13360
|
writeTypeCache(node, { type: subjectType, isIncomplete: !!subjectTypeResult.isIncomplete }, 0 /* None */);
|
13191
13361
|
}
|
13192
13362
|
function evaluateTypesForImportFrom(node) {
|
13193
|
-
if (
|
13363
|
+
if (isTypeCached(node)) {
|
13194
13364
|
return;
|
13195
13365
|
}
|
13196
13366
|
// Use the first element of the name parts as the symbol.
|
@@ -13764,22 +13934,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13764
13934
|
const aliasedName = classType.aliasName || classType.details.name;
|
13765
13935
|
switch (aliasedName) {
|
13766
13936
|
case 'Callable': {
|
13767
|
-
return createCallableType(typeArgs, errorNode);
|
13937
|
+
return { type: createCallableType(typeArgs, errorNode) };
|
13768
13938
|
}
|
13769
13939
|
case 'Never': {
|
13770
13940
|
if (typeArgs && typeArgs.length > 0) {
|
13771
13941
|
addError(localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({ name: 'Never' }), typeArgs[0].node);
|
13772
13942
|
}
|
13773
|
-
return types_1.NeverType.createNever();
|
13943
|
+
return { type: types_1.NeverType.createNever() };
|
13774
13944
|
}
|
13775
13945
|
case 'NoReturn': {
|
13776
13946
|
if (typeArgs && typeArgs.length > 0) {
|
13777
13947
|
addError(localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
|
13778
13948
|
}
|
13779
|
-
return types_1.NeverType.createNoReturn();
|
13949
|
+
return { type: types_1.NeverType.createNoReturn() };
|
13780
13950
|
}
|
13781
13951
|
case 'Optional': {
|
13782
|
-
return createOptionalType(classType, errorNode, typeArgs, flags);
|
13952
|
+
return { type: createOptionalType(classType, errorNode, typeArgs, flags) };
|
13783
13953
|
}
|
13784
13954
|
case 'Type': {
|
13785
13955
|
// PEP 484 says that Type[Any] should be considered
|
@@ -13788,56 +13958,58 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13788
13958
|
(0, types_1.isAnyOrUnknown)(typeArgs[0].type) &&
|
13789
13959
|
typeClassType &&
|
13790
13960
|
(0, types_1.isInstantiableClass)(typeClassType)) {
|
13791
|
-
return typeClassType;
|
13961
|
+
return { type: typeClassType };
|
13792
13962
|
}
|
13793
13963
|
let typeType = createSpecialType(classType, typeArgs, 1);
|
13794
13964
|
if ((0, types_1.isInstantiableClass)(typeType)) {
|
13795
13965
|
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
13796
13966
|
}
|
13797
|
-
return typeType;
|
13967
|
+
return { type: typeType };
|
13798
13968
|
}
|
13799
13969
|
case 'ClassVar': {
|
13800
|
-
return createClassVarType(classType, errorNode, typeArgs, flags);
|
13970
|
+
return { type: createClassVarType(classType, errorNode, typeArgs, flags) };
|
13801
13971
|
}
|
13802
13972
|
case 'Protocol': {
|
13803
|
-
return
|
13804
|
-
|
13805
|
-
|
13973
|
+
return {
|
13974
|
+
type: createSpecialType(classType, typeArgs,
|
13975
|
+
/* paramLimit */ undefined,
|
13976
|
+
/* allowParamSpec */ true),
|
13977
|
+
};
|
13806
13978
|
}
|
13807
13979
|
case 'Tuple': {
|
13808
|
-
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined);
|
13980
|
+
return { type: createSpecialType(classType, typeArgs, /* paramLimit */ undefined) };
|
13809
13981
|
}
|
13810
13982
|
case 'Union': {
|
13811
|
-
return createUnionType(classType, errorNode, typeArgs, flags);
|
13983
|
+
return { type: createUnionType(classType, errorNode, typeArgs, flags) };
|
13812
13984
|
}
|
13813
13985
|
case 'Generic': {
|
13814
|
-
return createGenericType(classType, errorNode, typeArgs, flags);
|
13986
|
+
return { type: createGenericType(classType, errorNode, typeArgs, flags) };
|
13815
13987
|
}
|
13816
13988
|
case 'Final': {
|
13817
|
-
return createFinalType(classType, errorNode, typeArgs, flags);
|
13989
|
+
return { type: createFinalType(classType, errorNode, typeArgs, flags) };
|
13818
13990
|
}
|
13819
13991
|
case 'Annotated': {
|
13820
13992
|
return createAnnotatedType(errorNode, typeArgs);
|
13821
13993
|
}
|
13822
13994
|
case 'Concatenate': {
|
13823
|
-
return createConcatenateType(errorNode, classType, typeArgs);
|
13995
|
+
return { type: createConcatenateType(errorNode, classType, typeArgs) };
|
13824
13996
|
}
|
13825
13997
|
case 'TypeGuard':
|
13826
13998
|
case 'StrictTypeGuard': {
|
13827
|
-
return createTypeGuardType(errorNode, classType, typeArgs, flags);
|
13999
|
+
return { type: createTypeGuardType(errorNode, classType, typeArgs, flags) };
|
13828
14000
|
}
|
13829
14001
|
case 'Unpack': {
|
13830
|
-
return createUnpackType(classType, errorNode, typeArgs, flags);
|
14002
|
+
return { type: createUnpackType(classType, errorNode, typeArgs, flags) };
|
13831
14003
|
}
|
13832
14004
|
case 'Required':
|
13833
14005
|
case 'NotRequired': {
|
13834
14006
|
return createRequiredType(classType, errorNode, aliasedName === 'Required', typeArgs, flags);
|
13835
14007
|
}
|
13836
14008
|
case 'Self': {
|
13837
|
-
return createSelfType(classType, errorNode, typeArgs);
|
14009
|
+
return { type: createSelfType(classType, errorNode, typeArgs) };
|
13838
14010
|
}
|
13839
14011
|
case 'LiteralString': {
|
13840
|
-
return createSpecialType(classType, typeArgs, 0);
|
14012
|
+
return { type: createSpecialType(classType, typeArgs, 0) };
|
13841
14013
|
}
|
13842
14014
|
}
|
13843
14015
|
}
|
@@ -13852,7 +14024,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13852
14024
|
// PEP 484 says that type[Any] should be considered
|
13853
14025
|
// equivalent to type.
|
13854
14026
|
if (typeArgs.length === 1 && (0, types_1.isAnyOrUnknown)(typeArgs[0].type)) {
|
13855
|
-
return classType;
|
14027
|
+
return { type: classType };
|
13856
14028
|
}
|
13857
14029
|
const typeClass = getTypingType(errorNode, 'Type');
|
13858
14030
|
if (typeClass && (0, types_1.isInstantiableClass)(typeClass)) {
|
@@ -13862,16 +14034,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13862
14034
|
if ((0, types_1.isInstantiableClass)(typeType)) {
|
13863
14035
|
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
13864
14036
|
}
|
13865
|
-
return typeType;
|
14037
|
+
return { type: typeType };
|
13866
14038
|
}
|
13867
14039
|
}
|
13868
14040
|
// Handle "tuple" specially, since it needs to act like "Tuple"
|
13869
14041
|
// in Python 3.9 and newer.
|
13870
14042
|
if ((0, typeUtils_1.isTupleClass)(classType)) {
|
13871
|
-
return
|
13872
|
-
|
13873
|
-
|
13874
|
-
|
14043
|
+
return {
|
14044
|
+
type: createSpecialType(classType, typeArgs,
|
14045
|
+
/* paramLimit */ undefined,
|
14046
|
+
/* allowParamSpec */ undefined,
|
14047
|
+
/* isCallable */ true),
|
14048
|
+
};
|
13875
14049
|
}
|
13876
14050
|
}
|
13877
14051
|
let typeArgCount = typeArgs ? typeArgs.length : 0;
|
@@ -13880,7 +14054,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13880
14054
|
// If there are no type parameters or args, the class is already specialized.
|
13881
14055
|
// No need to do any more work.
|
13882
14056
|
if (typeParameters.length === 0 && typeArgCount === 0) {
|
13883
|
-
return classType;
|
14057
|
+
return { type: classType };
|
13884
14058
|
}
|
13885
14059
|
const variadicTypeParamIndex = typeParameters.findIndex((param) => (0, types_1.isVariadicTypeVar)(param));
|
13886
14060
|
if (typeArgs) {
|
@@ -14043,7 +14217,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14043
14217
|
const destType = typeParameters[index];
|
14044
14218
|
const declaredVariance = destType.details.declaredVariance;
|
14045
14219
|
if (!(0, typeUtils_1.isVarianceOfTypeArgumentCompatible)(adjustedTypeArgType, declaredVariance)) {
|
14046
|
-
diag.addMessage(localize_1.Localizer.DiagnosticAddendum.
|
14220
|
+
diag.addMessage(localize_1.Localizer.DiagnosticAddendum.varianceMismatchForClass().format({
|
14047
14221
|
typeVarName: printType(adjustedTypeArgType),
|
14048
14222
|
className: classType.details.name,
|
14049
14223
|
}));
|
@@ -14073,7 +14247,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14073
14247
|
typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
|
14074
14248
|
}
|
14075
14249
|
const specializedClass = types_1.ClassType.cloneForSpecialization(classType, typeArgTypes, typeArgs !== undefined);
|
14076
|
-
return specializedClass;
|
14250
|
+
return { type: specializedClass };
|
14077
14251
|
}
|
14078
14252
|
function getTypeOfArgument(arg) {
|
14079
14253
|
if (arg.typeResult) {
|
@@ -14494,59 +14668,63 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14494
14668
|
return declarations;
|
14495
14669
|
}
|
14496
14670
|
function getTypeForDeclaration(declaration) {
|
14497
|
-
var _a, _b;
|
14671
|
+
var _a, _b, _c;
|
14498
14672
|
switch (declaration.type) {
|
14499
14673
|
case 0 /* Intrinsic */: {
|
14500
14674
|
if (declaration.intrinsicType === 'Any') {
|
14501
|
-
return types_1.AnyType.create();
|
14675
|
+
return { type: types_1.AnyType.create() };
|
14502
14676
|
}
|
14503
14677
|
if (declaration.intrinsicType === 'class') {
|
14504
14678
|
const classNode = ParseTreeUtils.getEnclosingClass(declaration.node);
|
14505
14679
|
const classTypeInfo = getTypeOfClass(classNode);
|
14506
|
-
return classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.classType;
|
14680
|
+
return { type: classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.classType };
|
14507
14681
|
}
|
14508
14682
|
const strType = getBuiltInObject(declaration.node, 'str');
|
14509
14683
|
const intType = getBuiltInObject(declaration.node, 'int');
|
14510
14684
|
if ((0, types_1.isClassInstance)(intType) && (0, types_1.isClassInstance)(strType)) {
|
14511
14685
|
if (declaration.intrinsicType === 'str') {
|
14512
|
-
return strType;
|
14686
|
+
return { type: strType };
|
14513
14687
|
}
|
14514
14688
|
if (declaration.intrinsicType === 'str | None') {
|
14515
|
-
return (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]);
|
14689
|
+
return { type: (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]) };
|
14516
14690
|
}
|
14517
14691
|
if (declaration.intrinsicType === 'int') {
|
14518
|
-
return intType;
|
14692
|
+
return { type: intType };
|
14519
14693
|
}
|
14520
14694
|
if (declaration.intrinsicType === 'Iterable[str]') {
|
14521
14695
|
const iterableType = getBuiltInType(declaration.node, 'Iterable');
|
14522
14696
|
if ((0, types_1.isInstantiableClass)(iterableType)) {
|
14523
|
-
return
|
14524
|
-
|
14697
|
+
return {
|
14698
|
+
type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(iterableType, [strType],
|
14699
|
+
/* isTypeArgumentExplicit */ true)),
|
14700
|
+
};
|
14525
14701
|
}
|
14526
14702
|
}
|
14527
14703
|
if (declaration.intrinsicType === 'Dict[str, Any]') {
|
14528
14704
|
const dictType = getBuiltInType(declaration.node, 'dict');
|
14529
14705
|
if ((0, types_1.isInstantiableClass)(dictType)) {
|
14530
|
-
return
|
14531
|
-
|
14706
|
+
return {
|
14707
|
+
type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(dictType, [strType, types_1.AnyType.create()],
|
14708
|
+
/* isTypeArgumentExplicit */ true)),
|
14709
|
+
};
|
14532
14710
|
}
|
14533
14711
|
}
|
14534
14712
|
}
|
14535
|
-
return types_1.UnknownType.create();
|
14713
|
+
return { type: types_1.UnknownType.create() };
|
14536
14714
|
}
|
14537
14715
|
case 6 /* Class */: {
|
14538
14716
|
const classTypeInfo = getTypeOfClass(declaration.node);
|
14539
|
-
return classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.decoratedType;
|
14717
|
+
return { type: classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.decoratedType };
|
14540
14718
|
}
|
14541
14719
|
case 7 /* SpecialBuiltInClass */: {
|
14542
|
-
return getTypeOfAnnotation(declaration.node.typeAnnotation);
|
14720
|
+
return { type: getTypeOfAnnotation(declaration.node.typeAnnotation) };
|
14543
14721
|
}
|
14544
14722
|
case 5 /* Function */: {
|
14545
14723
|
const functionTypeInfo = getTypeOfFunction(declaration.node);
|
14546
|
-
return functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.decoratedType;
|
14724
|
+
return { type: functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.decoratedType };
|
14547
14725
|
}
|
14548
14726
|
case 4 /* TypeAlias */: {
|
14549
|
-
return getTypeOfTypeAlias(declaration.node);
|
14727
|
+
return { type: getTypeOfTypeAlias(declaration.node) };
|
14550
14728
|
}
|
14551
14729
|
case 2 /* Parameter */: {
|
14552
14730
|
let typeAnnotationNode = declaration.node.typeAnnotation || declaration.node.typeAnnotationComment;
|
@@ -14565,19 +14743,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14565
14743
|
}
|
14566
14744
|
if (typeAnnotationNode) {
|
14567
14745
|
const declaredType = getTypeOfParameterAnnotation(typeAnnotationNode, declaration.node.category);
|
14568
|
-
return
|
14746
|
+
return {
|
14747
|
+
type: transformVariadicParamType(declaration.node, declaration.node.category, adjustParameterAnnotatedType(declaration.node, declaredType)),
|
14748
|
+
};
|
14569
14749
|
}
|
14570
|
-
return undefined;
|
14750
|
+
return { type: undefined };
|
14571
14751
|
}
|
14572
14752
|
case 3 /* TypeParameter */: {
|
14573
|
-
return getTypeOfTypeParameter(declaration.node);
|
14753
|
+
return { type: getTypeOfTypeParameter(declaration.node) };
|
14574
14754
|
}
|
14575
14755
|
case 1 /* Variable */: {
|
14576
14756
|
const typeAnnotationNode = declaration.typeAnnotationNode;
|
14577
14757
|
if (typeAnnotationNode) {
|
14578
|
-
const typeAliasNode = isDeclaredTypeAlias(typeAnnotationNode)
|
14579
|
-
? ParseTreeUtils.getTypeAnnotationNode(typeAnnotationNode)
|
14580
|
-
: undefined;
|
14581
14758
|
let declaredType;
|
14582
14759
|
if (declaration.isRuntimeTypeExpression) {
|
14583
14760
|
declaredType = (0, typeUtils_1.convertToInstance)(getTypeOfExpressionExpectingType(typeAnnotationNode, {
|
@@ -14594,24 +14771,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14594
14771
|
isVariableAnnotation: true,
|
14595
14772
|
allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(declNode),
|
14596
14773
|
allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(declNode),
|
14774
|
+
allowRequired: ParseTreeUtils.isRequiredAllowedForAssignmentTarget(declNode),
|
14597
14775
|
});
|
14598
14776
|
}
|
14599
14777
|
if (declaredType) {
|
14600
14778
|
// Apply enum transform if appropriate.
|
14601
14779
|
if (declaration.node.nodeType === 38 /* Name */) {
|
14602
14780
|
declaredType =
|
14603
|
-
(0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType)
|
14781
|
+
(_c = (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType)) !== null && _c !== void 0 ? _c : declaredType;
|
14604
14782
|
}
|
14605
|
-
if (
|
14606
|
-
|
14783
|
+
if ((0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias')) {
|
14784
|
+
return { type: undefined, isTypeAlias: true };
|
14607
14785
|
}
|
14608
|
-
return declaredType;
|
14786
|
+
return { type: declaredType };
|
14609
14787
|
}
|
14610
14788
|
}
|
14611
|
-
return undefined;
|
14789
|
+
return { type: undefined };
|
14612
14790
|
}
|
14613
14791
|
case 8 /* Alias */: {
|
14614
|
-
return undefined;
|
14792
|
+
return { type: undefined };
|
14615
14793
|
}
|
14616
14794
|
}
|
14617
14795
|
}
|
@@ -14780,8 +14958,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14780
14958
|
}
|
14781
14959
|
}
|
14782
14960
|
const declaredType = getTypeForDeclaration(resolvedDecl);
|
14783
|
-
if (declaredType) {
|
14784
|
-
return declaredType;
|
14961
|
+
if (declaredType.type) {
|
14962
|
+
return declaredType.type;
|
14785
14963
|
}
|
14786
14964
|
// If this is part of a "py.typed" package, don't fall back on type inference
|
14787
14965
|
// unless it's marked Final, is a constant, or is a declared type alias.
|
@@ -14801,7 +14979,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14801
14979
|
isUnambiguousType = true;
|
14802
14980
|
}
|
14803
14981
|
}
|
14804
|
-
if (resolvedDecl
|
14982
|
+
if (isFinalVariableDeclaration(resolvedDecl) || resolvedDecl.isConstant) {
|
14805
14983
|
isUnambiguousType = true;
|
14806
14984
|
}
|
14807
14985
|
}
|
@@ -14814,7 +14992,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14814
14992
|
})) === null || _a === void 0 ? void 0 : _a.type;
|
14815
14993
|
}
|
14816
14994
|
if (resolvedDecl.type === 1 /* Variable */ && resolvedDecl.inferredTypeSource) {
|
14817
|
-
const isTypeAlias =
|
14995
|
+
const isTypeAlias = isExplicitTypeAliasDeclaration(resolvedDecl) || isPossibleTypeAliasOrTypedDict(resolvedDecl);
|
14818
14996
|
// If this is a type alias, evaluate types for the entire assignment
|
14819
14997
|
// statement rather than just the RHS of the assignment.
|
14820
14998
|
const typeSource = isTypeAlias && resolvedDecl.inferredTypeSource.parent
|
@@ -14936,27 +15114,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14936
15114
|
}
|
14937
15115
|
function getEffectiveTypeOfSymbolForUsage(symbol, usageNode, useLastDecl = false) {
|
14938
15116
|
var _a, _b;
|
14939
|
-
// If there's a declared type, it takes precedence over inferred types.
|
14940
|
-
if (symbol.hasTypedDeclarations()) {
|
14941
|
-
const declaredType = getDeclaredTypeOfSymbol(symbol, usageNode);
|
14942
|
-
const typedDecls = symbol.getTypedDeclarations();
|
14943
|
-
let isIncomplete = false;
|
14944
|
-
if (declaredType) {
|
14945
|
-
if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
|
14946
|
-
isIncomplete = true;
|
14947
|
-
}
|
14948
|
-
else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
|
14949
|
-
isIncomplete = true;
|
14950
|
-
}
|
14951
|
-
}
|
14952
|
-
return {
|
14953
|
-
type: declaredType !== null && declaredType !== void 0 ? declaredType : types_1.UnknownType.create(),
|
14954
|
-
isIncomplete,
|
14955
|
-
includesVariableDecl: typedDecls.some((decl) => decl.type === 1 /* Variable */),
|
14956
|
-
includesIllegalTypeAliasDecl: !typedDecls.every((decl) => (0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)),
|
14957
|
-
isRecursiveDefinition: !declaredType,
|
14958
|
-
};
|
14959
|
-
}
|
14960
15117
|
// Look in the cache to see if we've computed this already.
|
14961
15118
|
let cacheEntries = effectiveTypeCache.get(symbol.id);
|
14962
15119
|
const usageNodeId = usageNode ? usageNode.id : undefined;
|
@@ -14969,10 +15126,37 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14969
15126
|
}
|
14970
15127
|
}
|
14971
15128
|
}
|
15129
|
+
let declaredTypeInfo;
|
15130
|
+
// If there's a declared type, it takes precedence over inferred types.
|
15131
|
+
if (symbol.hasTypedDeclarations()) {
|
15132
|
+
declaredTypeInfo = getDeclaredTypeOfSymbol(symbol, usageNode);
|
15133
|
+
const declaredType = declaredTypeInfo === null || declaredTypeInfo === void 0 ? void 0 : declaredTypeInfo.type;
|
15134
|
+
const hasMetadata = !!declaredTypeInfo.isTypeAlias;
|
15135
|
+
if (declaredType || !hasMetadata) {
|
15136
|
+
let isIncomplete = false;
|
15137
|
+
if (declaredType) {
|
15138
|
+
if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
|
15139
|
+
isIncomplete = true;
|
15140
|
+
}
|
15141
|
+
else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
|
15142
|
+
isIncomplete = true;
|
15143
|
+
}
|
15144
|
+
}
|
15145
|
+
const typedDecls = symbol.getTypedDeclarations();
|
15146
|
+
const result = {
|
15147
|
+
type: declaredType !== null && declaredType !== void 0 ? declaredType : types_1.UnknownType.create(),
|
15148
|
+
isIncomplete,
|
15149
|
+
includesVariableDecl: typedDecls.some((decl) => decl.type === 1 /* Variable */),
|
15150
|
+
includesIllegalTypeAliasDecl: !typedDecls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15151
|
+
isRecursiveDefinition: !declaredType,
|
15152
|
+
};
|
15153
|
+
addToEffectiveTypeCache(result);
|
15154
|
+
return result;
|
15155
|
+
}
|
15156
|
+
}
|
14972
15157
|
// Infer the type.
|
14973
15158
|
const typesToCombine = [];
|
14974
15159
|
const decls = symbol.getDeclarations();
|
14975
|
-
const isFinalVar = (0, symbolUtils_1.isFinalVariable)(symbol);
|
14976
15160
|
let isIncomplete = false;
|
14977
15161
|
let sawPendingEvaluation = false;
|
14978
15162
|
let includesVariableDecl = false;
|
@@ -14980,13 +15164,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14980
15164
|
let declIndexToConsider;
|
14981
15165
|
// Limit the number of declarations to explore.
|
14982
15166
|
if (decls.length > maxDeclarationsToUseForInference) {
|
14983
|
-
|
15167
|
+
const result = {
|
14984
15168
|
type: types_1.UnknownType.create(),
|
14985
15169
|
isIncomplete: false,
|
14986
15170
|
includesVariableDecl: false,
|
14987
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) =>
|
15171
|
+
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
14988
15172
|
isRecursiveDefinition: false,
|
14989
15173
|
};
|
15174
|
+
addToEffectiveTypeCache(result);
|
15175
|
+
return result;
|
14990
15176
|
}
|
14991
15177
|
// If the caller has requested that we use only the last decl, we
|
14992
15178
|
// will use only the last one, but we'll ignore decls that are in
|
@@ -15036,7 +15222,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15036
15222
|
const resolvedDecl = (_a = resolveAliasDeclaration(decl,
|
15037
15223
|
/* resolveLocalNames */ true,
|
15038
15224
|
/* allowExternallyHiddenAccess */ AnalyzerNodeInfo.getFileInfo(decl.node).isStubFile)) !== null && _a !== void 0 ? _a : decl;
|
15039
|
-
const isExplicitTypeAlias =
|
15225
|
+
const isExplicitTypeAlias = isExplicitTypeAliasDeclaration(decl);
|
15040
15226
|
const isTypeAlias = isExplicitTypeAlias || isPossibleTypeAliasOrTypedDict(decl);
|
15041
15227
|
if (isExplicitTypeAlias) {
|
15042
15228
|
sawExplicitTypeAlias = true;
|
@@ -15047,14 +15233,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15047
15233
|
resolvedDecl.type === 1 /* Variable */ &&
|
15048
15234
|
((_c = (_b = resolvedDecl.inferredTypeSource) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 3 /* Assignment */) {
|
15049
15235
|
evaluateTypesForAssignmentStatement(resolvedDecl.inferredTypeSource.parent);
|
15050
|
-
if (resolvedDecl.typeAliasAnnotation) {
|
15051
|
-
// Mark "TypeAlias" declaration as accessed.
|
15052
|
-
getTypeOfAnnotation(resolvedDecl.typeAliasAnnotation, {
|
15053
|
-
isVariableAnnotation: true,
|
15054
|
-
allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(resolvedDecl.node),
|
15055
|
-
allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(resolvedDecl.node),
|
15056
|
-
});
|
15057
|
-
}
|
15058
15236
|
}
|
15059
15237
|
if (pushSymbolResolution(symbol, decl)) {
|
15060
15238
|
try {
|
@@ -15070,7 +15248,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15070
15248
|
if (!fileInfo.isTypingStubFile) {
|
15071
15249
|
includesVariableDecl = true;
|
15072
15250
|
}
|
15073
|
-
let isConstant =
|
15251
|
+
let isConstant = false;
|
15252
|
+
if (resolvedDecl.type === 1 /* Variable */) {
|
15253
|
+
if (resolvedDecl.isConstant || isFinalVariableDeclaration(resolvedDecl)) {
|
15254
|
+
isConstant = true;
|
15255
|
+
}
|
15256
|
+
}
|
15074
15257
|
// Treat enum values declared within an enum class as though they are const even
|
15075
15258
|
// though they may not be named as such.
|
15076
15259
|
if ((0, types_1.isClassInstance)(type) &&
|
@@ -15080,7 +15263,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15080
15263
|
}
|
15081
15264
|
// If the symbol is constant, we can retain the literal
|
15082
15265
|
// value. Otherwise, strip literal values to widen the type.
|
15083
|
-
if (types_1.TypeBase.isInstance(type) && !isExplicitTypeAlias && !isConstant
|
15266
|
+
if (types_1.TypeBase.isInstance(type) && !isExplicitTypeAlias && !isConstant) {
|
15084
15267
|
type = stripLiteralValue(type);
|
15085
15268
|
}
|
15086
15269
|
}
|
@@ -15124,17 +15307,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15124
15307
|
type: (0, types_1.combineTypes)(typesToCombine),
|
15125
15308
|
isIncomplete,
|
15126
15309
|
includesVariableDecl,
|
15127
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) =>
|
15310
|
+
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15128
15311
|
isRecursiveDefinition: false,
|
15129
15312
|
evaluationAttempts,
|
15130
15313
|
};
|
15131
15314
|
if (!includesSpeculativeResult) {
|
15132
|
-
|
15133
|
-
if (!cacheEntries) {
|
15134
|
-
cacheEntries = new Map();
|
15135
|
-
effectiveTypeCache.set(symbol.id, cacheEntries);
|
15136
|
-
}
|
15137
|
-
cacheEntries.set(effectiveTypeCacheKey, result);
|
15315
|
+
addToEffectiveTypeCache(result);
|
15138
15316
|
}
|
15139
15317
|
return result;
|
15140
15318
|
}
|
@@ -15142,19 +15320,33 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15142
15320
|
type: types_1.UnboundType.create(),
|
15143
15321
|
isIncomplete,
|
15144
15322
|
includesVariableDecl,
|
15145
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) =>
|
15323
|
+
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15146
15324
|
isRecursiveDefinition: false,
|
15147
15325
|
};
|
15326
|
+
function addToEffectiveTypeCache(result) {
|
15327
|
+
// Add the entry to the cache so we don't need to compute it next time.
|
15328
|
+
if (!cacheEntries) {
|
15329
|
+
cacheEntries = new Map();
|
15330
|
+
effectiveTypeCache.set(symbol.id, cacheEntries);
|
15331
|
+
}
|
15332
|
+
cacheEntries.set(effectiveTypeCacheKey, result);
|
15333
|
+
}
|
15148
15334
|
}
|
15335
|
+
// If a declaration has an explicit type (e.g. a variable with an annotation),
|
15336
|
+
// this function evaluates the type and returns it. If the symbol has no
|
15337
|
+
// explicit declared type, its type will need to be inferred instead. In some
|
15338
|
+
// cases, non-type information (such as Final or ClassVar attributes) may be
|
15339
|
+
// provided, but type inference is still required. In such cases, the attributes
|
15340
|
+
// are returned as flags.
|
15149
15341
|
function getDeclaredTypeOfSymbol(symbol, usageNode) {
|
15150
15342
|
const synthesizedType = symbol.getSynthesizedType();
|
15151
15343
|
if (synthesizedType) {
|
15152
|
-
return synthesizedType;
|
15344
|
+
return { type: synthesizedType };
|
15153
15345
|
}
|
15154
15346
|
let typedDecls = symbol.getTypedDeclarations();
|
15155
15347
|
if (typedDecls.length === 0) {
|
15156
15348
|
// There was no declaration with a defined type.
|
15157
|
-
return undefined;
|
15349
|
+
return { type: undefined };
|
15158
15350
|
}
|
15159
15351
|
// If there is more than one typed decl, filter out any that are not
|
15160
15352
|
// reachable from the usage node (if specified). This can happen in
|
@@ -15175,7 +15367,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15175
15367
|
return true;
|
15176
15368
|
});
|
15177
15369
|
if (filteredTypedDecls.length === 0) {
|
15178
|
-
return types_1.UnboundType.create();
|
15370
|
+
return { type: types_1.UnboundType.create() };
|
15179
15371
|
}
|
15180
15372
|
typedDecls = filteredTypedDecls;
|
15181
15373
|
}
|
@@ -15191,12 +15383,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15191
15383
|
// for recursive symbol resolution, return it as the resolved type.
|
15192
15384
|
const partialType = getSymbolResolutionPartialType(symbol, decl);
|
15193
15385
|
if (partialType) {
|
15194
|
-
return partialType;
|
15386
|
+
return { type: partialType };
|
15195
15387
|
}
|
15196
15388
|
if (getIndexOfSymbolResolution(symbol, decl) < 0) {
|
15197
15389
|
if (pushSymbolResolution(symbol, decl)) {
|
15198
15390
|
try {
|
15199
|
-
const
|
15391
|
+
const declaredTypeInfo = getTypeForDeclaration(decl);
|
15200
15392
|
// If there was recursion detected, don't use this declaration.
|
15201
15393
|
// The exception is it's a class declaration because getTypeOfClass
|
15202
15394
|
// handles recursion by populating a partially-created class type
|
@@ -15204,7 +15396,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15204
15396
|
// circular dependency between the "type" and "object" classes in
|
15205
15397
|
// builtins.pyi (since "object" is a "type" and "type" is an "object").
|
15206
15398
|
if (popSymbolResolution(symbol) || decl.type === 6 /* Class */) {
|
15207
|
-
return
|
15399
|
+
return declaredTypeInfo;
|
15208
15400
|
}
|
15209
15401
|
}
|
15210
15402
|
catch (e) {
|
@@ -15216,7 +15408,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15216
15408
|
}
|
15217
15409
|
declIndex--;
|
15218
15410
|
}
|
15219
|
-
return undefined;
|
15411
|
+
return { type: undefined };
|
15220
15412
|
}
|
15221
15413
|
function inferReturnTypeIfNecessary(type) {
|
15222
15414
|
if ((0, types_1.isFunction)(type)) {
|
@@ -15577,18 +15769,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15577
15769
|
return false;
|
15578
15770
|
}
|
15579
15771
|
// This function is used to validate or infer the variance of type
|
15580
|
-
// parameters within a class.
|
15581
|
-
|
15772
|
+
// parameters within a class. If ignoreBaseClassVariance is set to false,
|
15773
|
+
// the type parameters for the base class are honored. This is useful for
|
15774
|
+
// variance inference (PEP 695). For validation of protocol variance, we
|
15775
|
+
// want to ignore the variance for all base classes in the class hierarchy.
|
15776
|
+
function assignClassToSelf(destType, srcType, ignoreBaseClassVariance = true, recursionCount = 0) {
|
15582
15777
|
(0, debug_1.assert)(types_1.ClassType.isSameGenericClass(destType, srcType));
|
15583
15778
|
(0, debug_1.assert)(destType.details.typeParameters.length > 0);
|
15584
15779
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
15585
15780
|
const typeVarContext = new typeVarContext_1.TypeVarContext();
|
15586
15781
|
let isAssignable = true;
|
15587
15782
|
destType.details.fields.forEach((symbol, name) => {
|
15783
|
+
var _a;
|
15588
15784
|
if (isAssignable && symbol.isClassMember() && !symbol.isIgnoredForProtocolMatch()) {
|
15589
15785
|
const memberInfo = (0, typeUtils_1.lookUpClassMember)(srcType, name);
|
15590
15786
|
(0, debug_1.assert)(memberInfo !== undefined);
|
15591
|
-
let destMemberType = getDeclaredTypeOfSymbol(symbol);
|
15787
|
+
let destMemberType = (_a = getDeclaredTypeOfSymbol(symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
15592
15788
|
if (destMemberType) {
|
15593
15789
|
const srcMemberType = getTypeOfMember(memberInfo);
|
15594
15790
|
destMemberType = (0, typeUtils_1.partiallySpecializeType)(destMemberType, destType);
|
@@ -15606,7 +15802,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15606
15802
|
const primaryDecl = symbol.getDeclarations()[0];
|
15607
15803
|
// Class and instance variables that are mutable need to
|
15608
15804
|
// enforce invariance.
|
15609
|
-
const flags = (primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* Variable */ && !primaryDecl
|
15805
|
+
const flags = (primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* Variable */ && !isFinalVariableDeclaration(primaryDecl)
|
15610
15806
|
? 1 /* EnforceInvariance */
|
15611
15807
|
: 0 /* Default */;
|
15612
15808
|
if (!assignType(destMemberType, srcMemberType, diag, typeVarContext,
|
@@ -15626,7 +15822,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15626
15822
|
baseClass.details.typeParameters.length > 0) {
|
15627
15823
|
const specializedDestBaseClass = (0, typeUtils_1.specializeForBaseClass)(destType, baseClass);
|
15628
15824
|
const specializedSrcBaseClass = (0, typeUtils_1.specializeForBaseClass)(srcType, baseClass);
|
15629
|
-
if (!
|
15825
|
+
if (!ignoreBaseClassVariance) {
|
15826
|
+
specializedDestBaseClass.details.typeParameters.forEach((param, index) => {
|
15827
|
+
if (!param.details.isParamSpec &&
|
15828
|
+
!param.details.isVariadic &&
|
15829
|
+
!param.details.isSynthesized &&
|
15830
|
+
specializedSrcBaseClass.typeArguments &&
|
15831
|
+
index < specializedSrcBaseClass.typeArguments.length &&
|
15832
|
+
specializedDestBaseClass.typeArguments &&
|
15833
|
+
index < specializedDestBaseClass.typeArguments.length) {
|
15834
|
+
const paramVariance = param.details.declaredVariance;
|
15835
|
+
if ((0, types_1.isTypeVar)(specializedSrcBaseClass.typeArguments[index])) {
|
15836
|
+
if (paramVariance === 2 /* Invariant */ || paramVariance === 4 /* Contravariant */) {
|
15837
|
+
isAssignable = false;
|
15838
|
+
}
|
15839
|
+
}
|
15840
|
+
else if ((0, types_1.isTypeVar)(specializedDestBaseClass.typeArguments[index])) {
|
15841
|
+
if (paramVariance === 2 /* Invariant */ || paramVariance === 3 /* Covariant */) {
|
15842
|
+
isAssignable = false;
|
15843
|
+
}
|
15844
|
+
}
|
15845
|
+
}
|
15846
|
+
});
|
15847
|
+
}
|
15848
|
+
if (isAssignable &&
|
15849
|
+
!assignClassToSelf(specializedDestBaseClass, specializedSrcBaseClass, ignoreBaseClassVariance, recursionCount)) {
|
15630
15850
|
isAssignable = false;
|
15631
15851
|
}
|
15632
15852
|
}
|
@@ -15723,12 +15943,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15723
15943
|
// specified inheritance chain, taking into account its type arguments.
|
15724
15944
|
function assignClassWithTypeArgs(destType, srcType, inheritanceChain, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
15725
15945
|
let curSrcType = srcType;
|
15726
|
-
let
|
15946
|
+
let curDestTypeVarContext = destTypeVarContext;
|
15727
15947
|
let effectiveFlags = flags;
|
15728
15948
|
inferTypeParameterVarianceForClass(destType);
|
15729
15949
|
effectiveFlags |= 8 /* SkipSolveTypeVars */;
|
15730
15950
|
if (!destTypeVarContext) {
|
15731
|
-
|
15951
|
+
curDestTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(destType));
|
15732
15952
|
effectiveFlags &= ~8 /* SkipSolveTypeVars */;
|
15733
15953
|
}
|
15734
15954
|
else {
|
@@ -15739,6 +15959,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15739
15959
|
}
|
15740
15960
|
for (let ancestorIndex = inheritanceChain.length - 1; ancestorIndex >= 0; ancestorIndex--) {
|
15741
15961
|
const ancestorType = inheritanceChain[ancestorIndex];
|
15962
|
+
const curSrcTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(curSrcType));
|
15742
15963
|
// If we've hit an "unknown", all bets are off, and we need to assume
|
15743
15964
|
// that the type is assignable.
|
15744
15965
|
if ((0, types_1.isUnknown)(ancestorType)) {
|
@@ -15756,7 +15977,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15756
15977
|
// Handle built-in types that support arbitrary numbers
|
15757
15978
|
// of type parameters like Tuple.
|
15758
15979
|
if (ancestorIndex === 0 && destType.tupleTypeArguments && curSrcType.tupleTypeArguments) {
|
15759
|
-
return assignTupleTypeArgs(destType, curSrcType, diag,
|
15980
|
+
return assignTupleTypeArgs(destType, curSrcType, diag, curDestTypeVarContext, flags, recursionCount);
|
15760
15981
|
}
|
15761
15982
|
// If there are no type parameters on this class, we're done.
|
15762
15983
|
const ancestorTypeParams = types_1.ClassType.getTypeParameters(ancestorType);
|
@@ -15768,12 +15989,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15768
15989
|
return true;
|
15769
15990
|
}
|
15770
15991
|
// Validate that the type arguments match.
|
15771
|
-
if (!verifyTypeArgumentsAssignable(ancestorType, curSrcType, diag,
|
15772
|
-
/* srcTypeVarContext */ undefined, effectiveFlags, recursionCount)) {
|
15992
|
+
if (!verifyTypeArgumentsAssignable(ancestorType, curSrcType, diag, curDestTypeVarContext, curSrcTypeVarContext, effectiveFlags, recursionCount)) {
|
15773
15993
|
return false;
|
15774
15994
|
}
|
15775
15995
|
// Allocate a new type var map for the next time through the loop.
|
15776
|
-
|
15996
|
+
curDestTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(ancestorType));
|
15777
15997
|
effectiveFlags &= ~8 /* SkipSolveTypeVars */;
|
15778
15998
|
}
|
15779
15999
|
if (destType.typeArguments) {
|
@@ -15799,12 +16019,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15799
16019
|
return true;
|
15800
16020
|
}
|
15801
16021
|
function getGetterTypeFromProperty(propertyClass, inferTypeIfNeeded) {
|
16022
|
+
var _a;
|
15802
16023
|
if (!types_1.ClassType.isPropertyClass(propertyClass)) {
|
15803
16024
|
return undefined;
|
15804
16025
|
}
|
15805
16026
|
const fgetSymbol = propertyClass.details.fields.get('fget');
|
15806
16027
|
if (fgetSymbol) {
|
15807
|
-
const fgetType = getDeclaredTypeOfSymbol(fgetSymbol);
|
16028
|
+
const fgetType = (_a = getDeclaredTypeOfSymbol(fgetSymbol)) === null || _a === void 0 ? void 0 : _a.type;
|
15808
16029
|
if (fgetType && (0, types_1.isFunction)(fgetType)) {
|
15809
16030
|
return getFunctionEffectiveReturnType(fgetType, /* args */ undefined, inferTypeIfNeeded);
|
15810
16031
|
}
|
@@ -16136,8 +16357,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16136
16357
|
if ((0, types_1.isUnion)(destType)) {
|
16137
16358
|
return assignToUnionType(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, originalFlags, recursionCount);
|
16138
16359
|
}
|
16139
|
-
if ((0, types_1.isNoneInstance)(destType)
|
16140
|
-
|
16360
|
+
if ((0, types_1.isNoneInstance)(destType)) {
|
16361
|
+
if ((0, types_1.isNoneInstance)(srcType)) {
|
16362
|
+
return true;
|
16363
|
+
}
|
16364
|
+
if ((0, types_1.isClassInstance)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'NoneType')) {
|
16365
|
+
return true;
|
16366
|
+
}
|
16141
16367
|
}
|
16142
16368
|
if ((0, types_1.isNoneTypeClass)(destType)) {
|
16143
16369
|
if ((0, types_1.isNoneTypeClass)(srcType)) {
|
@@ -16519,6 +16745,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16519
16745
|
});
|
16520
16746
|
}
|
16521
16747
|
else if (remainingSrcSubtypes.length === 0) {
|
16748
|
+
if ((flags & 1024 /* PopulatingExpectedType */) !== 0) {
|
16749
|
+
// If we're populating an expected type, try not to leave
|
16750
|
+
// any TypeVars unsolved. Assign the full type to the remaining
|
16751
|
+
// dest TypeVars.
|
16752
|
+
remainingDestSubtypes.forEach((destSubtype) => {
|
16753
|
+
assignType(destSubtype, srcType,
|
16754
|
+
/* diag */ undefined, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
|
16755
|
+
});
|
16756
|
+
}
|
16522
16757
|
// If we've assigned all of the source subtypes but one or more dest
|
16523
16758
|
// TypeVars have gone unmatched, treat this as success.
|
16524
16759
|
return true;
|
@@ -17521,14 +17756,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17521
17756
|
// When a value is assigned to a variable with a declared type,
|
17522
17757
|
// we may be able to narrow the type based on the assignment.
|
17523
17758
|
function narrowTypeBasedOnAssignment(declaredType, assignedType) {
|
17524
|
-
const diag = new diagnostic_1.DiagnosticAddendum();
|
17525
17759
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(assignedType, (assignedSubtype) => {
|
17526
17760
|
const narrowedSubtype = (0, typeUtils_1.mapSubtypes)(declaredType, (declaredSubtype) => {
|
17527
17761
|
// We can't narrow "Any".
|
17528
17762
|
if ((0, types_1.isAnyOrUnknown)(declaredType)) {
|
17529
17763
|
return declaredType;
|
17530
17764
|
}
|
17531
|
-
if (assignType(declaredSubtype, assignedSubtype
|
17765
|
+
if (assignType(declaredSubtype, assignedSubtype)) {
|
17532
17766
|
// If the source is generic and has unspecified type arguments,
|
17533
17767
|
// see if we can determine then based on the declared type.
|
17534
17768
|
if ((0, types_1.isInstantiableClass)(declaredSubtype) && (0, types_1.isInstantiableClass)(assignedSubtype)) {
|
@@ -18012,7 +18246,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18012
18246
|
}
|
18013
18247
|
else if ((0, types_1.isOverloadedFunction)(memberType)) {
|
18014
18248
|
const newOverloadType = types_1.OverloadedFunctionType.create([]);
|
18015
|
-
|
18249
|
+
// Don't bother binding the implementation.
|
18250
|
+
types_1.OverloadedFunctionType.getOverloads(memberType).forEach((overload) => {
|
18016
18251
|
const boundMethod = bindFunctionToClassOrObject(baseType, overload, memberClass,
|
18017
18252
|
/* errorNode */ undefined, recursionCount, treatConstructorAsClassMember, firstParamType);
|
18018
18253
|
if (boundMethod) {
|
@@ -18049,7 +18284,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18049
18284
|
const memberTypeFirstParamType = types_1.FunctionType.getEffectiveParameterType(memberType, 0);
|
18050
18285
|
// Fill out the typeVarContext for the "self" or "cls" parameter.
|
18051
18286
|
typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(memberType));
|
18052
|
-
const diag = new diagnostic_1.DiagnosticAddendum();
|
18287
|
+
const diag = errorNode ? new diagnostic_1.DiagnosticAddendum() : undefined;
|
18053
18288
|
if ((0, types_1.isTypeVar)(memberTypeFirstParamType) &&
|
18054
18289
|
memberTypeFirstParamType.details.boundType &&
|
18055
18290
|
(0, types_1.isClassInstance)(memberTypeFirstParamType.details.boundType) &&
|
@@ -18075,7 +18310,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18075
18310
|
type: printType(baseType),
|
18076
18311
|
methodName: methodName,
|
18077
18312
|
paramName: memberTypeFirstParam.name,
|
18078
|
-
}) + diag.getString(), errorNode);
|
18313
|
+
}) + (diag === null || diag === void 0 ? void 0 : diag.getString()), errorNode);
|
18079
18314
|
}
|
18080
18315
|
else {
|
18081
18316
|
// If there was no errorNode, we couldn't report the error,
|
@@ -18092,6 +18327,60 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18092
18327
|
const specializedFunction = (0, typeUtils_1.applySolvedTypeVars)(memberType, typeVarContext);
|
18093
18328
|
return types_1.FunctionType.clone(specializedFunction, stripFirstParam, baseType, (0, typeUtils_1.getTypeVarScopeId)(baseType));
|
18094
18329
|
}
|
18330
|
+
function isFinalVariable(symbol) {
|
18331
|
+
return symbol.getDeclarations().some((decl) => isFinalVariableDeclaration(decl));
|
18332
|
+
}
|
18333
|
+
function isFinalVariableDeclaration(decl) {
|
18334
|
+
return decl.type === 1 /* Variable */ && !!decl.isFinal;
|
18335
|
+
}
|
18336
|
+
function isExplicitTypeAliasDeclaration(decl) {
|
18337
|
+
if (decl.type !== 1 /* Variable */ || !decl.typeAnnotationNode) {
|
18338
|
+
return false;
|
18339
|
+
}
|
18340
|
+
if (decl.typeAnnotationNode.nodeType !== 38 /* Name */ &&
|
18341
|
+
decl.typeAnnotationNode.nodeType !== 35 /* MemberAccess */ &&
|
18342
|
+
decl.typeAnnotationNode.nodeType !== 48 /* StringList */) {
|
18343
|
+
return false;
|
18344
|
+
}
|
18345
|
+
const type = getTypeOfAnnotation(decl.typeAnnotationNode, { isVariableAnnotation: true, allowClassVar: true });
|
18346
|
+
return (0, types_1.isClassInstance)(type) && types_1.ClassType.isBuiltIn(type, 'TypeAlias');
|
18347
|
+
}
|
18348
|
+
function isPossibleTypeAliasDeclaration(decl) {
|
18349
|
+
var _a;
|
18350
|
+
if (decl.type !== 1 /* Variable */ || !decl.typeAliasName || decl.typeAnnotationNode) {
|
18351
|
+
return false;
|
18352
|
+
}
|
18353
|
+
if (((_a = decl.node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 3 /* Assignment */) {
|
18354
|
+
return false;
|
18355
|
+
}
|
18356
|
+
// Perform a sanity check on the RHS expression. Some expression
|
18357
|
+
// forms should never be considered legitimate for type aliases.
|
18358
|
+
return isLegalTypeAliasExpressionForm(decl.node.parent.rightExpression);
|
18359
|
+
}
|
18360
|
+
function isLegalTypeAliasExpressionForm(node) {
|
18361
|
+
switch (node.nodeType) {
|
18362
|
+
case 0 /* Error */:
|
18363
|
+
case 55 /* UnaryOperation */:
|
18364
|
+
case 4 /* AssignmentExpression */:
|
18365
|
+
case 54 /* TypeAnnotation */:
|
18366
|
+
case 6 /* Await */:
|
18367
|
+
case 51 /* Ternary */:
|
18368
|
+
case 56 /* Unpack */:
|
18369
|
+
case 52 /* Tuple */:
|
18370
|
+
case 9 /* Call */:
|
18371
|
+
case 32 /* ListComprehension */:
|
18372
|
+
case 46 /* Slice */:
|
18373
|
+
case 60 /* Yield */:
|
18374
|
+
case 61 /* YieldFrom */:
|
18375
|
+
case 30 /* Lambda */:
|
18376
|
+
case 40 /* Number */:
|
18377
|
+
case 15 /* Dictionary */:
|
18378
|
+
case 31 /* List */:
|
18379
|
+
case 45 /* Set */:
|
18380
|
+
return false;
|
18381
|
+
}
|
18382
|
+
return true;
|
18383
|
+
}
|
18095
18384
|
function printObjectTypeForClass(type) {
|
18096
18385
|
return TypePrinter.printObjectTypeForClass(type, evaluatorOptions.printTypeFlags, getFunctionEffectiveReturnType);
|
18097
18386
|
}
|
@@ -18234,6 +18523,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18234
18523
|
verifyTypeArgumentsAssignable,
|
18235
18524
|
inferReturnTypeIfNecessary,
|
18236
18525
|
inferTypeParameterVarianceForClass,
|
18526
|
+
isFinalVariable,
|
18527
|
+
isFinalVariableDeclaration,
|
18528
|
+
isExplicitTypeAliasDeclaration,
|
18237
18529
|
addError,
|
18238
18530
|
addWarning,
|
18239
18531
|
addInformation,
|