@zzzen/pyright-internal 1.2.0-dev.20230226 → 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/binder.d.ts +0 -1
- package/dist/analyzer/binder.js +9 -92
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +45 -28
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +4 -4
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/declaration.d.ts +2 -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/parseTreeUtils.d.ts +4 -2
- package/dist/analyzer/parseTreeUtils.js +77 -2
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +18 -1
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +1 -0
- package/dist/analyzer/program.js +151 -11
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +3 -2
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +4 -2
- 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.js +6 -1
- package/dist/analyzer/service.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 +503 -234
- 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/common/collectionUtils.d.ts +1 -0
- package/dist/common/collectionUtils.js +8 -1
- package/dist/common/collectionUtils.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/languageServerBase.js +6 -0
- 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/completionProvider.js +46 -40
- 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 +11 -3
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/importAdder.js +4 -1
- package/dist/languageService/importAdder.js.map +1 -1
- package/dist/languageService/indentationUtils.d.ts +1 -1
- package/dist/languageService/indentationUtils.js +22 -12
- package/dist/languageService/indentationUtils.js.map +1 -1
- package/dist/languageService/renameModuleProvider.js +20 -7
- package/dist/languageService/renameModuleProvider.js.map +1 -1
- package/dist/localization/localize.d.ts +9 -2
- package/dist/localization/localize.js +4 -1
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +5 -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.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/importAdder.test.js +13 -0
- package/dist/tests/importAdder.test.js.map +1 -1
- package/dist/tests/indentationUtils.reindent.test.js +21 -0
- package/dist/tests/indentationUtils.reindent.test.js.map +1 -1
- package/dist/tests/moveSymbol.importAdder.test.js +137 -31
- package/dist/tests/moveSymbol.importAdder.test.js.map +1 -1
- package/dist/tests/moveSymbol.insertion.test.js +98 -0
- package/dist/tests/moveSymbol.insertion.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/typeEvaluator1.test.js +4 -0
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +1 -1
- package/dist/tests/typeEvaluator3.test.js +20 -4
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +6 -0
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/package.json +1 -1
@@ -1000,6 +1000,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1000
1000
|
if (options === null || options === void 0 ? void 0 : options.notParsedByInterpreter) {
|
1001
1001
|
evaluatorFlags |= 524288 /* NotParsedByInterpreter */;
|
1002
1002
|
}
|
1003
|
+
if (options === null || options === void 0 ? void 0 : options.allowRequired) {
|
1004
|
+
evaluatorFlags |= 1048576 /* AllowRequired */;
|
1005
|
+
}
|
1003
1006
|
if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo)) {
|
1004
1007
|
evaluatorFlags |= 4 /* AllowForwardReferences */;
|
1005
1008
|
}
|
@@ -1496,7 +1499,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1496
1499
|
/* honorCodeFlow */ false);
|
1497
1500
|
if (symbolWithScope) {
|
1498
1501
|
const symbol = symbolWithScope.symbol;
|
1499
|
-
return symbol.getDeclarations().find((decl) =>
|
1502
|
+
return symbol.getDeclarations().find((decl) => isExplicitTypeAliasDeclaration(decl)) !== undefined;
|
1500
1503
|
}
|
1501
1504
|
}
|
1502
1505
|
}
|
@@ -1504,6 +1507,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1504
1507
|
}
|
1505
1508
|
// Determines whether the specified expression is a symbol with a declared type.
|
1506
1509
|
function getDeclaredTypeForExpression(expression, usage) {
|
1510
|
+
var _a, _b;
|
1507
1511
|
let symbol;
|
1508
1512
|
let classOrObjectBase;
|
1509
1513
|
let memberAccessClass;
|
@@ -1516,7 +1520,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1516
1520
|
// Handle the case where the symbol is a class-level variable
|
1517
1521
|
// where the type isn't declared in this class but is in
|
1518
1522
|
// a parent class.
|
1519
|
-
if (getDeclaredTypeOfSymbol(symbol, expression) ===
|
1523
|
+
if (!((_a = getDeclaredTypeOfSymbol(symbol, expression)) === null || _a === void 0 ? void 0 : _a.type) &&
|
1520
1524
|
symbolWithScope.scope.type === 2 /* Class */) {
|
1521
1525
|
const enclosingClass = ParseTreeUtils.getEnclosingClassOrFunction(expression);
|
1522
1526
|
if (enclosingClass && enclosingClass.nodeType === 10 /* Class */) {
|
@@ -1590,7 +1594,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1590
1594
|
}
|
1591
1595
|
}
|
1592
1596
|
if (symbol) {
|
1593
|
-
let declaredType = getDeclaredTypeOfSymbol(symbol);
|
1597
|
+
let declaredType = (_b = getDeclaredTypeOfSymbol(symbol)) === null || _b === void 0 ? void 0 : _b.type;
|
1594
1598
|
if (declaredType) {
|
1595
1599
|
// If it's a descriptor, we need to get the setter type.
|
1596
1600
|
if ((0, types_1.isClassInstance)(declaredType)) {
|
@@ -2038,7 +2042,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2038
2042
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeExpectedClass().format({ type: printType(type) }) + diag.getString(), node);
|
2039
2043
|
}
|
2040
2044
|
function assignTypeToNameNode(nameNode, type, isTypeIncomplete, ignoreEmptyContainers, srcExpression, allowAssignmentToFinalVar = false, expectedTypeDiagAddendum) {
|
2041
|
-
var _a, _b;
|
2045
|
+
var _a, _b, _c;
|
2042
2046
|
const nameValue = nameNode.value;
|
2043
2047
|
const symbolWithScope = lookUpSymbolRecursive(nameNode, nameValue, /* honorCodeFlow */ false);
|
2044
2048
|
if (!symbolWithScope) {
|
@@ -2047,7 +2051,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2047
2051
|
return;
|
2048
2052
|
}
|
2049
2053
|
const declarations = symbolWithScope.symbol.getDeclarations();
|
2050
|
-
let declaredType = getDeclaredTypeOfSymbol(symbolWithScope.symbol);
|
2054
|
+
let declaredType = (_a = getDeclaredTypeOfSymbol(symbolWithScope.symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
2051
2055
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(nameNode);
|
2052
2056
|
// If this is a class scope and there is no type declared for this class variable,
|
2053
2057
|
// see if a parent class has a type declared.
|
@@ -2065,7 +2069,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2065
2069
|
}
|
2066
2070
|
// We found an existing declared type. Make sure the type is assignable.
|
2067
2071
|
let destType = type;
|
2068
|
-
|
2072
|
+
const isTypeAlias = !!declaredType && (0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias');
|
2073
|
+
if (declaredType && !isTypeAlias) {
|
2069
2074
|
let diagAddendum = new diagnostic_1.DiagnosticAddendum();
|
2070
2075
|
if (!assignType(declaredType, type, diagAddendum)) {
|
2071
2076
|
// If there was an expected type mismatch, use that diagnostic
|
@@ -2076,7 +2081,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2076
2081
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAssignmentMismatch().format({
|
2077
2082
|
sourceType: printType(type),
|
2078
2083
|
destType: printType(declaredType),
|
2079
|
-
}) + 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);
|
2080
2085
|
// Replace the assigned type with the (unnarrowed) declared type.
|
2081
2086
|
destType = declaredType;
|
2082
2087
|
}
|
@@ -2094,7 +2099,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2094
2099
|
if ((scope === null || scope === void 0 ? void 0 : scope.type) === 2 /* Class */) {
|
2095
2100
|
if (types_1.TypeBase.isInstance(destType) &&
|
2096
2101
|
!(0, symbolNameUtils_1.isConstantName)(nameValue) &&
|
2097
|
-
!
|
2102
|
+
!isFinalVariable(symbolWithScope.symbol)) {
|
2098
2103
|
destType = stripLiteralValue(destType);
|
2099
2104
|
}
|
2100
2105
|
}
|
@@ -2108,7 +2113,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2108
2113
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportConstantRedefinition, diagnosticRules_1.DiagnosticRule.reportConstantRedefinition, localize_1.Localizer.Diagnostic.constantRedefinition().format({ name: nameValue }), nameNode);
|
2109
2114
|
}
|
2110
2115
|
}
|
2111
|
-
else if (varDecl
|
2116
|
+
else if (isFinalVariableDeclaration(varDecl) && !allowAssignmentToFinalVar) {
|
2112
2117
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.finalReassigned().format({ name: nameValue }), nameNode);
|
2113
2118
|
}
|
2114
2119
|
}
|
@@ -2163,6 +2168,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2163
2168
|
writeTypeCache(target, { type, isIncomplete: isTypeIncomplete }, 0 /* None */);
|
2164
2169
|
}
|
2165
2170
|
function assignTypeToMemberVariable(node, srcType, isTypeIncomplete, isInstanceMember, srcExprNode) {
|
2171
|
+
var _a;
|
2166
2172
|
const memberName = node.memberName.value;
|
2167
2173
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
2168
2174
|
const classDef = ParseTreeUtils.getEnclosingClass(node);
|
@@ -2221,7 +2227,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2221
2227
|
}
|
2222
2228
|
else {
|
2223
2229
|
// Is the target a property?
|
2224
|
-
const declaredType = getDeclaredTypeOfSymbol(memberInfo.symbol);
|
2230
|
+
const declaredType = (_a = getDeclaredTypeOfSymbol(memberInfo.symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
2225
2231
|
if (declaredType && !(0, typeUtils_1.isProperty)(declaredType)) {
|
2226
2232
|
// Handle the case where there is a class variable defined with the same
|
2227
2233
|
// name, but there's also now an instance variable introduced. Combine the
|
@@ -3120,6 +3126,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3120
3126
|
// type arguments. If so, it fills in these type arguments with Unknown
|
3121
3127
|
// and optionally reports an error.
|
3122
3128
|
function reportMissingTypeArguments(node, type, flags) {
|
3129
|
+
var _a;
|
3123
3130
|
if ((flags & 2 /* DoNotSpecialize */) !== 0) {
|
3124
3131
|
return type;
|
3125
3132
|
}
|
@@ -3133,7 +3140,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3133
3140
|
}
|
3134
3141
|
}
|
3135
3142
|
if (!type.typeArguments) {
|
3136
|
-
type = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node);
|
3143
|
+
type = (_a = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _a === void 0 ? void 0 : _a.type;
|
3137
3144
|
}
|
3138
3145
|
}
|
3139
3146
|
// Is this a generic type alias that needs to be specialized?
|
@@ -3351,7 +3358,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3351
3358
|
return typeResult;
|
3352
3359
|
}
|
3353
3360
|
function getTypeOfMemberAccessWithBaseType(node, baseTypeResult, usage, flags) {
|
3354
|
-
var _a, _b;
|
3361
|
+
var _a, _b, _c;
|
3355
3362
|
let baseType = baseTypeResult.type;
|
3356
3363
|
const memberName = node.memberName.value;
|
3357
3364
|
let diag = new diagnostic_1.DiagnosticAddendum();
|
@@ -3359,6 +3366,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3359
3366
|
let type;
|
3360
3367
|
let isIncomplete = !!baseTypeResult.isIncomplete;
|
3361
3368
|
let isAsymmetricDescriptor;
|
3369
|
+
const isRequired = false;
|
3370
|
+
const isNotRequired = false;
|
3362
3371
|
// If the base type was incomplete and unbound, don't proceed
|
3363
3372
|
// because false positive errors will be generated.
|
3364
3373
|
if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseTypeResult.type)) {
|
@@ -3517,7 +3526,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3517
3526
|
}
|
3518
3527
|
if (!type) {
|
3519
3528
|
if (!isIncomplete) {
|
3520
|
-
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);
|
3521
3533
|
}
|
3522
3534
|
type = evaluatorOptions.evaluateUnknownImportsAsAny ? types_1.AnyType.create() : types_1.UnknownType.create();
|
3523
3535
|
}
|
@@ -3631,7 +3643,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3631
3643
|
// Should we specialize the class?
|
3632
3644
|
if ((flags & 2 /* DoNotSpecialize */) === 0) {
|
3633
3645
|
if ((0, types_1.isInstantiableClass)(type) && !type.typeArguments) {
|
3634
|
-
type = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node);
|
3646
|
+
type = (_b = createSpecializedClassType(type, /* typeArgs */ undefined, flags, node)) === null || _b === void 0 ? void 0 : _b.type;
|
3635
3647
|
}
|
3636
3648
|
}
|
3637
3649
|
if (usage.method === 'get') {
|
@@ -3642,7 +3654,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3642
3654
|
// It can also come up in cases like "isinstance(x, (list, dict))".
|
3643
3655
|
if ((0, types_1.isInstantiableClass)(type)) {
|
3644
3656
|
const argNode = ParseTreeUtils.getParentNodeOfType(node, 1 /* Argument */);
|
3645
|
-
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 */) {
|
3646
3658
|
skipPartialUnknownCheck = true;
|
3647
3659
|
}
|
3648
3660
|
}
|
@@ -3651,10 +3663,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3651
3663
|
/* ignoreEmptyContainers */ false);
|
3652
3664
|
}
|
3653
3665
|
}
|
3654
|
-
return { type, isIncomplete, isAsymmetricDescriptor };
|
3666
|
+
return { type, isIncomplete, isAsymmetricDescriptor, isRequired, isNotRequired };
|
3655
3667
|
}
|
3656
3668
|
function getTypeOfClassMemberName(errorNode, classType, isAccessedThroughObject, memberName, usage, diag, flags, bindToType) {
|
3657
|
-
var _a, _b;
|
3669
|
+
var _a, _b, _c;
|
3658
3670
|
let classLookupFlags = 0 /* Default */;
|
3659
3671
|
if (flags & 1 /* AccessClassMembersOnly */) {
|
3660
3672
|
classLookupFlags |= 8 /* SkipInstanceVariables */;
|
@@ -3695,7 +3707,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3695
3707
|
if (containingClassType &&
|
3696
3708
|
(0, types_1.isInstantiableClass)(containingClassType) &&
|
3697
3709
|
types_1.ClassType.isSameGenericClass(containingClassType, classType)) {
|
3698
|
-
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();
|
3699
3711
|
if (type && (0, types_1.isInstantiableClass)(memberInfo.classType)) {
|
3700
3712
|
type = (0, typeUtils_1.partiallySpecializeType)(type, memberInfo.classType);
|
3701
3713
|
}
|
@@ -4035,7 +4047,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4035
4047
|
}
|
4036
4048
|
}
|
4037
4049
|
// Check for an attempt to overwrite a final member variable.
|
4038
|
-
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));
|
4039
4051
|
if (finalVarTypeDecl && !ParseTreeUtils.isNodeContainedWithin(errorNode, finalVarTypeDecl.node)) {
|
4040
4052
|
// If a Final instance variable is declared in the class body but is
|
4041
4053
|
// being assigned within an __init__ method, it's allowed.
|
@@ -4256,20 +4268,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4256
4268
|
if (baseTypeResult.isIncomplete) {
|
4257
4269
|
indexTypeResult.isIncomplete = true;
|
4258
4270
|
}
|
4259
|
-
// Handle "Required" and "NotRequired" specially.
|
4260
|
-
if ((flags & 1048576 /* AllowRequired */) !== 0) {
|
4261
|
-
if ((0, types_1.isInstantiableClass)(baseTypeResult.type)) {
|
4262
|
-
if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'Required')) {
|
4263
|
-
indexTypeResult.isRequired = true;
|
4264
|
-
}
|
4265
|
-
else if (types_1.ClassType.isBuiltIn(baseTypeResult.type, 'NotRequired')) {
|
4266
|
-
indexTypeResult.isNotRequired = true;
|
4267
|
-
}
|
4268
|
-
}
|
4269
|
-
}
|
4270
4271
|
return indexTypeResult;
|
4271
4272
|
}
|
4272
|
-
function adjustTypeArgumentsForVariadicTypeVar(typeArgs, typeParameters,
|
4273
|
+
function adjustTypeArgumentsForVariadicTypeVar(typeArgs, typeParameters, errorNode) {
|
4273
4274
|
const variadicIndex = typeParameters.findIndex((param) => (0, types_1.isVariadicTypeVar)(param));
|
4274
4275
|
// Do we need to adjust the type arguments to map to a variadic type
|
4275
4276
|
// param at the end of the list?
|
@@ -4341,7 +4342,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4341
4342
|
// Handles index expressions that are providing type arguments for a
|
4342
4343
|
// generic type alias.
|
4343
4344
|
function createSpecializedTypeAlias(node, baseType, flags) {
|
4344
|
-
var _a, _b;
|
4345
|
+
var _a, _b, _c;
|
4345
4346
|
if (!((_a = baseType.typeAliasInfo) === null || _a === void 0 ? void 0 : _a.typeParameters) ||
|
4346
4347
|
(baseType.typeAliasInfo.typeParameters.length === 0 && baseType.typeAliasInfo.typeArguments)) {
|
4347
4348
|
return undefined;
|
@@ -4355,7 +4356,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4355
4356
|
return undefined;
|
4356
4357
|
}
|
4357
4358
|
const typeParameters = baseType.typeAliasInfo.typeParameters;
|
4358
|
-
let typeArgs = adjustTypeArgumentsForVariadicTypeVar(getTypeArgs(node, flags), typeParameters,
|
4359
|
+
let typeArgs = adjustTypeArgumentsForVariadicTypeVar(getTypeArgs(node, flags), typeParameters, node);
|
4359
4360
|
// PEP 612 says that if the class has only one type parameter consisting
|
4360
4361
|
// of a ParamSpec, the list of arguments does not need to be enclosed in
|
4361
4362
|
// a list. We'll handle that case specially here. Presumably this applies to
|
@@ -4371,13 +4372,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4371
4372
|
];
|
4372
4373
|
}
|
4373
4374
|
}
|
4374
|
-
if (
|
4375
|
-
|
4376
|
-
|
4377
|
-
|
4378
|
-
|
4379
|
-
|
4380
|
-
|
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
|
+
}
|
4381
4395
|
}
|
4382
4396
|
// Handle the mypy_extensions.FlexibleAlias type specially.
|
4383
4397
|
if ((0, types_1.isInstantiableClass)(baseType) &&
|
@@ -4460,15 +4474,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4460
4474
|
else {
|
4461
4475
|
typeArgType = types_1.UnknownType.create();
|
4462
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
|
+
}
|
4463
4493
|
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, typeArgType, diag, typeVarContext, 128 /* RetainLiteralsForTypeVar */);
|
4464
4494
|
}
|
4465
4495
|
});
|
4466
4496
|
if (!diag.isEmpty()) {
|
4467
|
-
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);
|
4468
4498
|
}
|
4469
4499
|
const primarySignatureContext = typeVarContext.getPrimarySignature();
|
4470
4500
|
const aliasTypeArgs = [];
|
4471
|
-
(
|
4501
|
+
(_c = baseType.typeAliasInfo.typeParameters) === null || _c === void 0 ? void 0 : _c.forEach((typeParam) => {
|
4472
4502
|
let typeVarType;
|
4473
4503
|
if ((0, types_1.isParamSpec)(typeParam)) {
|
4474
4504
|
const paramSpecType = primarySignatureContext.getParamSpecType(typeParam);
|
@@ -4494,9 +4524,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4494
4524
|
return { type };
|
4495
4525
|
}
|
4496
4526
|
let isIncomplete = baseTypeResult.isIncomplete;
|
4527
|
+
let isRequired = false;
|
4528
|
+
let isNotRequired = false;
|
4497
4529
|
const type = mapSubtypesExpandTypeVars(baseTypeResult.type,
|
4498
4530
|
/* conditionFilter */ undefined, (concreteSubtype, unexpandedSubtype) => {
|
4499
|
-
var _a
|
4531
|
+
var _a;
|
4500
4532
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
4501
4533
|
return concreteSubtype;
|
4502
4534
|
}
|
@@ -4563,7 +4595,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4563
4595
|
const isClassVarAnnotation = (0, types_1.isInstantiableClass)(concreteSubtype) && types_1.ClassType.isBuiltIn(concreteSubtype, 'ClassVar');
|
4564
4596
|
let typeArgs = getTypeArgs(node, flags, isAnnotatedClass, hasCustomClassGetItem || !isGenericClass, isFinalAnnotation, isClassVarAnnotation);
|
4565
4597
|
if (!isAnnotatedClass) {
|
4566
|
-
typeArgs = adjustTypeArgumentsForVariadicTypeVar(typeArgs, concreteSubtype.details.typeParameters,
|
4598
|
+
typeArgs = adjustTypeArgumentsForVariadicTypeVar(typeArgs, concreteSubtype.details.typeParameters, node);
|
4567
4599
|
}
|
4568
4600
|
// If this is a custom __class_getitem__, there's no need to specialize the class.
|
4569
4601
|
// Just return it as is.
|
@@ -4576,7 +4608,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4576
4608
|
}), node.baseExpression);
|
4577
4609
|
return concreteSubtype;
|
4578
4610
|
}
|
4579
|
-
|
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;
|
4580
4619
|
}
|
4581
4620
|
if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
4582
4621
|
const typeResult = getTypeOfIndexedObjectOrClass(node, concreteSubtype, usage);
|
@@ -4607,7 +4646,81 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4607
4646
|
}
|
4608
4647
|
});
|
4609
4648
|
}
|
4610
|
-
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
|
+
});
|
4611
4724
|
}
|
4612
4725
|
function makeTupleObject(entryTypes, isUnspecifiedLength = false) {
|
4613
4726
|
if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
|
@@ -4842,8 +4955,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4842
4955
|
adjFlags &= ~(2 /* DoNotSpecialize */ |
|
4843
4956
|
32 /* DisallowParamSpec */ |
|
4844
4957
|
64 /* DisallowTypeVarTuple */ |
|
4845
|
-
1048576 /* AllowRequired */
|
4846
|
-
16384 /* EnforceTypeVarVarianceConsistency */);
|
4958
|
+
1048576 /* AllowRequired */);
|
4847
4959
|
if (!isAnnotatedClass) {
|
4848
4960
|
adjFlags |= 131072 /* DisallowClassVar */ | 16 /* DisallowFinal */;
|
4849
4961
|
}
|
@@ -5484,6 +5596,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5484
5596
|
: typeVarContext;
|
5485
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));
|
5486
5598
|
effectiveTypeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(overload));
|
5599
|
+
if (overload.details.constructorTypeVarScopeId) {
|
5600
|
+
effectiveTypeVarContext.addSolveForScope(overload.details.constructorTypeVarScopeId);
|
5601
|
+
}
|
5487
5602
|
effectiveTypeVarContext.unlock();
|
5488
5603
|
// Use speculative mode so we don't output any diagnostics or
|
5489
5604
|
// record any final types in the type cache.
|
@@ -6504,7 +6619,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6504
6619
|
// validation is left to the caller.
|
6505
6620
|
// This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
|
6506
6621
|
function matchFunctionArgumentsToParameters(errorNode, argList, typeResult, overloadIndex) {
|
6507
|
-
var _a, _b, _c, _d, _e;
|
6622
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
6508
6623
|
const paramDetails = (0, typeUtils_1.getParameterListDetails)(typeResult.type);
|
6509
6624
|
let argIndex = 0;
|
6510
6625
|
let matchedUnpackedListOfUnknownLength = false;
|
@@ -6627,11 +6742,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6627
6742
|
tooManyPositionals = true;
|
6628
6743
|
}
|
6629
6744
|
if (tooManyPositionals) {
|
6630
|
-
|
6631
|
-
|
6632
|
-
|
6633
|
-
|
6634
|
-
|
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
|
+
}
|
6635
6752
|
reportedArgError = true;
|
6636
6753
|
}
|
6637
6754
|
}
|
@@ -6655,11 +6772,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6655
6772
|
// all positional parameters specified in the Concatenate must be
|
6656
6773
|
// filled explicitly.
|
6657
6774
|
if (typeResult.type.details.paramSpec && paramIndex < positionParamLimitIndex) {
|
6658
|
-
|
6659
|
-
|
6660
|
-
|
6661
|
-
|
6662
|
-
|
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
|
+
}
|
6663
6782
|
reportedArgError = true;
|
6664
6783
|
}
|
6665
6784
|
const argType = argTypeResult.type;
|
@@ -6718,7 +6837,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6718
6837
|
// It's not allowed to use unpacked arguments with a variadic *args
|
6719
6838
|
// parameter unless the argument is a variadic arg as well.
|
6720
6839
|
if (isParamVariadic && !isArgCompatibleWithVariadic) {
|
6721
|
-
|
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
|
+
}
|
6722
6843
|
reportedArgError = true;
|
6723
6844
|
}
|
6724
6845
|
else {
|
@@ -6731,7 +6852,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6731
6852
|
paramType,
|
6732
6853
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
6733
6854
|
argument: funcArg,
|
6734
|
-
errorNode: argList[argIndex].valueExpression
|
6855
|
+
errorNode: (_f = argList[argIndex].valueExpression) !== null && _f !== void 0 ? _f : errorNode,
|
6735
6856
|
paramName,
|
6736
6857
|
isParamNameSynthesized: paramDetails.params[paramIndex].param.isNameSynthesized,
|
6737
6858
|
mapsToVarArgList: isParamVariadic && remainingArgCount > remainingParamCount,
|
@@ -6774,12 +6895,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6774
6895
|
: 0 /* Simple */;
|
6775
6896
|
if (remainingArgCount <= remainingParamCount) {
|
6776
6897
|
if (remainingArgCount < remainingParamCount) {
|
6777
|
-
|
6778
|
-
|
6779
|
-
|
6780
|
-
|
6781
|
-
|
6782
|
-
|
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
|
+
}
|
6783
6906
|
reportedArgError = true;
|
6784
6907
|
}
|
6785
6908
|
paramIndex++;
|
@@ -6854,13 +6977,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6854
6977
|
argsRemainingCount--;
|
6855
6978
|
}
|
6856
6979
|
if (argsRemainingCount > 0) {
|
6857
|
-
|
6858
|
-
|
6859
|
-
|
6860
|
-
|
6861
|
-
|
6862
|
-
|
6863
|
-
|
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
|
+
}
|
6864
6989
|
reportedArgError = true;
|
6865
6990
|
}
|
6866
6991
|
}
|
@@ -6934,7 +7059,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6934
7059
|
}
|
6935
7060
|
});
|
6936
7061
|
if (!diag.isEmpty()) {
|
6937
|
-
|
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
|
+
}
|
6938
7065
|
reportedArgError = true;
|
6939
7066
|
}
|
6940
7067
|
}
|
@@ -6980,7 +7107,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6980
7107
|
}
|
6981
7108
|
}
|
6982
7109
|
if (!isValidMappingType) {
|
6983
|
-
|
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
|
+
}
|
6984
7114
|
reportedArgError = true;
|
6985
7115
|
}
|
6986
7116
|
}
|
@@ -7012,7 +7142,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7012
7142
|
paramType,
|
7013
7143
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
7014
7144
|
argument: argList[argIndex],
|
7015
|
-
errorNode: argList[argIndex].valueExpression
|
7145
|
+
errorNode: (_g = argList[argIndex].valueExpression) !== null && _g !== void 0 ? _g : errorNode,
|
7016
7146
|
paramName: paramNameValue,
|
7017
7147
|
});
|
7018
7148
|
trySetActive(argList[argIndex], paramDetails.params[paramInfoIndex].param);
|
@@ -7029,7 +7159,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7029
7159
|
paramType,
|
7030
7160
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
7031
7161
|
argument: argList[argIndex],
|
7032
|
-
errorNode: argList[argIndex].valueExpression
|
7162
|
+
errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
|
7033
7163
|
paramName: paramNameValue,
|
7034
7164
|
});
|
7035
7165
|
// Remember that this parameter has already received a value.
|
@@ -7048,12 +7178,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7048
7178
|
}
|
7049
7179
|
}
|
7050
7180
|
else if (argList[argIndex].argumentCategory === 0 /* Simple */) {
|
7051
|
-
|
7052
|
-
|
7053
|
-
|
7054
|
-
|
7055
|
-
|
7056
|
-
|
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
|
+
}
|
7057
7189
|
reportedArgError = true;
|
7058
7190
|
}
|
7059
7191
|
else if (argList[argIndex].argumentCategory === 1 /* UnpackedList */) {
|
@@ -7071,7 +7203,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7071
7203
|
paramType: typeResult.type.details.paramSpec,
|
7072
7204
|
requiresTypeVarMatching: false,
|
7073
7205
|
argument: argList[argIndex],
|
7074
|
-
errorNode: argList[argIndex].valueExpression
|
7206
|
+
errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
|
7075
7207
|
});
|
7076
7208
|
}
|
7077
7209
|
}
|
@@ -7121,10 +7253,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7121
7253
|
return !entry || entry.argsReceived < entry.argsNeeded;
|
7122
7254
|
});
|
7123
7255
|
if (unassignedParams.length > 0) {
|
7124
|
-
|
7125
|
-
|
7126
|
-
|
7127
|
-
|
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
|
+
}
|
7128
7262
|
reportedArgError = true;
|
7129
7263
|
}
|
7130
7264
|
// Add any implicit (default) arguments that are needed for resolving
|
@@ -7150,7 +7284,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7150
7284
|
argumentCategory: 0 /* Simple */,
|
7151
7285
|
typeResult: { type: defaultArgType },
|
7152
7286
|
},
|
7153
|
-
errorNode
|
7287
|
+
errorNode,
|
7154
7288
|
paramName: param.name,
|
7155
7289
|
isParamNameSynthesized: param.isNameSynthesized,
|
7156
7290
|
});
|
@@ -7188,7 +7322,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7188
7322
|
if (containsVariadicTypeVar &&
|
7189
7323
|
argParam.argument.argumentCategory !== 1 /* UnpackedList */ &&
|
7190
7324
|
!argParam.mapsToVarArgList) {
|
7191
|
-
|
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
|
+
}
|
7192
7329
|
reportedArgError = true;
|
7193
7330
|
}
|
7194
7331
|
return {
|
@@ -9210,6 +9347,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9210
9347
|
if (!keyTypeResult.isIncomplete && !keyTypeResult.typeErrors) {
|
9211
9348
|
verifySetEntryOrDictKeyIsHashable(entryNode.keyExpression, keyType, /* isDictKey */ true);
|
9212
9349
|
}
|
9350
|
+
if (expectedDiagAddendum && keyTypeResult.expectedTypeDiagAddendum) {
|
9351
|
+
expectedDiagAddendum.addAddendum(keyTypeResult.expectedTypeDiagAddendum);
|
9352
|
+
}
|
9213
9353
|
let valueTypeResult;
|
9214
9354
|
if (expectedTypedDictEntries &&
|
9215
9355
|
(0, types_1.isClassInstance)(keyType) &&
|
@@ -9239,7 +9379,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9239
9379
|
addUnknown = false;
|
9240
9380
|
}
|
9241
9381
|
else if (entryNode.nodeType === 16 /* DictionaryExpandEntry */) {
|
9242
|
-
|
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));
|
9243
9398
|
if (unexpandedTypeResult.isIncomplete) {
|
9244
9399
|
isIncomplete = true;
|
9245
9400
|
}
|
@@ -9263,20 +9418,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9263
9418
|
}
|
9264
9419
|
}
|
9265
9420
|
else {
|
9266
|
-
// Verify that the type supports the `keys` and `__getitem__` methods.
|
9267
|
-
// This protocol is defined in the _typeshed stub. If we can't find
|
9268
|
-
// it there, fall back on typing.Mapping.
|
9269
|
-
let mappingType = getTypeshedType(node, 'SupportsKeysAndGetItem');
|
9270
|
-
if (!mappingType) {
|
9271
|
-
mappingType = getTypingType(node, 'Mapping');
|
9272
|
-
}
|
9273
9421
|
if (mappingType && (0, types_1.isInstantiableClass)(mappingType)) {
|
9274
9422
|
const mappingTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(mappingType));
|
9275
9423
|
// Self-specialize the class.
|
9276
9424
|
mappingType = types_1.ClassType.cloneForSpecialization(mappingType, mappingType.details.typeParameters,
|
9277
9425
|
/* isTypeArgumentExplicit */ true);
|
9278
9426
|
if (assignType(types_1.ClassType.cloneAsInstance(mappingType), unexpandedType,
|
9279
|
-
/* diag */ undefined, mappingTypeVarContext
|
9427
|
+
/* diag */ undefined, mappingTypeVarContext,
|
9428
|
+
/* srcTypeVarContext */ undefined, 128 /* RetainLiteralsForTypeVar */)) {
|
9280
9429
|
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(mappingType, mappingTypeVarContext);
|
9281
9430
|
const typeArgs = specializedMapping.typeArguments;
|
9282
9431
|
if (typeArgs && typeArgs.length >= 2) {
|
@@ -10296,16 +10445,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10296
10445
|
return (0, typeUtils_1.synthesizeTypeVarForSelfCls)(enclosingClassTypeResult.classType, /* isClsParam */ true);
|
10297
10446
|
}
|
10298
10447
|
function createRequiredType(classType, errorNode, isRequired, typeArgs, flags) {
|
10299
|
-
var _a;
|
10300
10448
|
// If no type arguments are provided, the resulting type
|
10301
10449
|
// depends on whether we're evaluating a type annotation or
|
10302
10450
|
// we're in some other context.
|
10303
10451
|
if (!typeArgs && (flags & 256 /* ExpectingTypeAnnotation */) === 0) {
|
10304
|
-
return classType;
|
10452
|
+
return { type: classType };
|
10305
10453
|
}
|
10306
10454
|
if (!typeArgs || typeArgs.length !== 1) {
|
10307
10455
|
addError(isRequired ? localize_1.Localizer.Diagnostic.requiredArgCount() : localize_1.Localizer.Diagnostic.notRequiredArgCount(), errorNode);
|
10308
|
-
return classType;
|
10456
|
+
return { type: classType };
|
10309
10457
|
}
|
10310
10458
|
const typeArgType = typeArgs[0].type;
|
10311
10459
|
// Make sure this is used only in a dataclass.
|
@@ -10316,21 +10464,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10316
10464
|
(0, types_1.isInstantiableClass)(classTypeInfo.classType) &&
|
10317
10465
|
types_1.ClassType.isTypedDictClass(classTypeInfo.classType)) {
|
10318
10466
|
// The only legal usage is when used in a type annotation statement.
|
10319
|
-
if ((
|
10320
|
-
errorNode.parent.typeAnnotation === errorNode) {
|
10467
|
+
if (ParseTreeUtils.isNodeContainedWithinNodeType(errorNode, 54 /* TypeAnnotation */)) {
|
10321
10468
|
isUsageLegal = true;
|
10322
10469
|
}
|
10323
10470
|
}
|
10324
10471
|
if ((flags & 1048576 /* AllowRequired */) !== 0) {
|
10325
10472
|
isUsageLegal = true;
|
10326
10473
|
}
|
10474
|
+
// Nested Required/NotRequired are not allowed.
|
10475
|
+
if (typeArgs[0].isRequired || typeArgs[0].isNotRequired) {
|
10476
|
+
isUsageLegal = false;
|
10477
|
+
}
|
10327
10478
|
if (!isUsageLegal) {
|
10328
10479
|
addError(isRequired
|
10329
10480
|
? localize_1.Localizer.Diagnostic.requiredNotInTypedDict()
|
10330
10481
|
: localize_1.Localizer.Diagnostic.notRequiredNotInTypedDict(), errorNode);
|
10331
|
-
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) };
|
10332
10483
|
}
|
10333
|
-
return typeArgType;
|
10484
|
+
return { type: typeArgType, isRequired, isNotRequired: !isRequired };
|
10334
10485
|
}
|
10335
10486
|
function createUnpackType(classType, errorNode, typeArgs, flags) {
|
10336
10487
|
// If no type arguments are provided, the resulting type
|
@@ -10407,9 +10558,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10407
10558
|
addError(localize_1.Localizer.Diagnostic.annotatedTypeArgMissing(), errorNode);
|
10408
10559
|
}
|
10409
10560
|
if (!typeArgs || typeArgs.length === 0) {
|
10410
|
-
return types_1.AnyType.create();
|
10561
|
+
return { type: types_1.AnyType.create() };
|
10411
10562
|
}
|
10412
|
-
return
|
10563
|
+
return {
|
10564
|
+
type: types_1.TypeBase.cloneForAnnotated(typeArgs[0].type),
|
10565
|
+
isRequired: typeArgs[0].isRequired,
|
10566
|
+
isNotRequired: typeArgs[0].isNotRequired,
|
10567
|
+
};
|
10413
10568
|
}
|
10414
10569
|
// Creates one of several "special" types that are defined in typing.pyi
|
10415
10570
|
// but not declared in their entirety. This includes the likes of "Tuple",
|
@@ -10849,7 +11004,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10849
11004
|
let isSpeculativeTypeAlias = false;
|
10850
11005
|
if (isDeclaredTypeAlias(node.leftExpression)) {
|
10851
11006
|
typeAliasNameNode = node.leftExpression.valueExpression;
|
10852
|
-
if (!
|
11007
|
+
if (!isLegalTypeAliasExpressionForm(node.rightExpression)) {
|
10853
11008
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.rightExpression);
|
10854
11009
|
}
|
10855
11010
|
}
|
@@ -10946,7 +11101,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10946
11101
|
}
|
10947
11102
|
function isPossibleTypeAliasOrTypedDict(decl) {
|
10948
11103
|
var _a;
|
10949
|
-
if (
|
11104
|
+
if (isPossibleTypeAliasDeclaration(decl)) {
|
10950
11105
|
return true;
|
10951
11106
|
}
|
10952
11107
|
if (decl.type === 1 /* Variable */ &&
|
@@ -11002,7 +11157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11002
11157
|
typeParameters = evaluateTypeParameterList(node.typeParameters);
|
11003
11158
|
typeAliasTypeVar.details.recursiveTypeParameters = typeParameters;
|
11004
11159
|
}
|
11005
|
-
if (!
|
11160
|
+
if (!isLegalTypeAliasExpressionForm(node.expression)) {
|
11006
11161
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasIllegalExpressionForm(), node.expression);
|
11007
11162
|
}
|
11008
11163
|
const aliasTypeResult = getTypeOfExpressionExpectingType(node.expression);
|
@@ -11577,7 +11732,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11577
11732
|
// Presumptively mark the variance inference as complete. This
|
11578
11733
|
// prevents potential recursion.
|
11579
11734
|
classType.details.requiresVarianceInference = false;
|
11580
|
-
// Presumptively mark the computed variance to "
|
11735
|
+
// Presumptively mark the computed variance to "unknown". We'll
|
11581
11736
|
// replace this below once the variance has been inferred.
|
11582
11737
|
classType.details.typeParameters.forEach((param) => {
|
11583
11738
|
if (param.details.declaredVariance === 0 /* Auto */) {
|
@@ -11614,13 +11769,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11614
11769
|
/* isTypeArgumentExplicit */ true);
|
11615
11770
|
const destType = types_1.ClassType.cloneForSpecialization(updatedClassType, destTypeArgs,
|
11616
11771
|
/* isTypeArgumentExplicit */ true);
|
11617
|
-
const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType);
|
11772
|
+
const isDestSubtypeOfSrc = assignClassToSelf(srcType, destType, /* ignoreBaseClassVariance */ false);
|
11618
11773
|
let inferredVariance;
|
11619
11774
|
if (isDestSubtypeOfSrc) {
|
11620
11775
|
inferredVariance = 3 /* Covariant */;
|
11621
11776
|
}
|
11622
11777
|
else {
|
11623
|
-
const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType);
|
11778
|
+
const isSrcSubtypeOfDest = assignClassToSelf(destType, srcType, /* ignoreBaseClassVariance */ false);
|
11624
11779
|
if (isSrcSubtypeOfDest) {
|
11625
11780
|
inferredVariance = 4 /* Contravariant */;
|
11626
11781
|
}
|
@@ -11637,9 +11792,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11637
11792
|
function evaluateTypeParameterList(node) {
|
11638
11793
|
const paramTypes = [];
|
11639
11794
|
node.parameters.forEach((param) => {
|
11795
|
+
var _a;
|
11640
11796
|
const paramSymbol = AnalyzerNodeInfo.getTypeParameterSymbol(param.name);
|
11641
11797
|
(0, debug_1.assert)(paramSymbol);
|
11642
|
-
const typeOfParam = getDeclaredTypeOfSymbol(paramSymbol, param.name);
|
11798
|
+
const typeOfParam = (_a = getDeclaredTypeOfSymbol(paramSymbol, param.name)) === null || _a === void 0 ? void 0 : _a.type;
|
11643
11799
|
if (!typeOfParam || !(0, types_1.isTypeVar)(typeOfParam)) {
|
11644
11800
|
return;
|
11645
11801
|
}
|
@@ -13778,22 +13934,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13778
13934
|
const aliasedName = classType.aliasName || classType.details.name;
|
13779
13935
|
switch (aliasedName) {
|
13780
13936
|
case 'Callable': {
|
13781
|
-
return createCallableType(typeArgs, errorNode);
|
13937
|
+
return { type: createCallableType(typeArgs, errorNode) };
|
13782
13938
|
}
|
13783
13939
|
case 'Never': {
|
13784
13940
|
if (typeArgs && typeArgs.length > 0) {
|
13785
13941
|
addError(localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({ name: 'Never' }), typeArgs[0].node);
|
13786
13942
|
}
|
13787
|
-
return types_1.NeverType.createNever();
|
13943
|
+
return { type: types_1.NeverType.createNever() };
|
13788
13944
|
}
|
13789
13945
|
case 'NoReturn': {
|
13790
13946
|
if (typeArgs && typeArgs.length > 0) {
|
13791
13947
|
addError(localize_1.Localizer.Diagnostic.typeArgsExpectingNone().format({ name: 'NoReturn' }), typeArgs[0].node);
|
13792
13948
|
}
|
13793
|
-
return types_1.NeverType.createNoReturn();
|
13949
|
+
return { type: types_1.NeverType.createNoReturn() };
|
13794
13950
|
}
|
13795
13951
|
case 'Optional': {
|
13796
|
-
return createOptionalType(classType, errorNode, typeArgs, flags);
|
13952
|
+
return { type: createOptionalType(classType, errorNode, typeArgs, flags) };
|
13797
13953
|
}
|
13798
13954
|
case 'Type': {
|
13799
13955
|
// PEP 484 says that Type[Any] should be considered
|
@@ -13802,56 +13958,58 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13802
13958
|
(0, types_1.isAnyOrUnknown)(typeArgs[0].type) &&
|
13803
13959
|
typeClassType &&
|
13804
13960
|
(0, types_1.isInstantiableClass)(typeClassType)) {
|
13805
|
-
return typeClassType;
|
13961
|
+
return { type: typeClassType };
|
13806
13962
|
}
|
13807
13963
|
let typeType = createSpecialType(classType, typeArgs, 1);
|
13808
13964
|
if ((0, types_1.isInstantiableClass)(typeType)) {
|
13809
13965
|
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
13810
13966
|
}
|
13811
|
-
return typeType;
|
13967
|
+
return { type: typeType };
|
13812
13968
|
}
|
13813
13969
|
case 'ClassVar': {
|
13814
|
-
return createClassVarType(classType, errorNode, typeArgs, flags);
|
13970
|
+
return { type: createClassVarType(classType, errorNode, typeArgs, flags) };
|
13815
13971
|
}
|
13816
13972
|
case 'Protocol': {
|
13817
|
-
return
|
13818
|
-
|
13819
|
-
|
13973
|
+
return {
|
13974
|
+
type: createSpecialType(classType, typeArgs,
|
13975
|
+
/* paramLimit */ undefined,
|
13976
|
+
/* allowParamSpec */ true),
|
13977
|
+
};
|
13820
13978
|
}
|
13821
13979
|
case 'Tuple': {
|
13822
|
-
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined);
|
13980
|
+
return { type: createSpecialType(classType, typeArgs, /* paramLimit */ undefined) };
|
13823
13981
|
}
|
13824
13982
|
case 'Union': {
|
13825
|
-
return createUnionType(classType, errorNode, typeArgs, flags);
|
13983
|
+
return { type: createUnionType(classType, errorNode, typeArgs, flags) };
|
13826
13984
|
}
|
13827
13985
|
case 'Generic': {
|
13828
|
-
return createGenericType(classType, errorNode, typeArgs, flags);
|
13986
|
+
return { type: createGenericType(classType, errorNode, typeArgs, flags) };
|
13829
13987
|
}
|
13830
13988
|
case 'Final': {
|
13831
|
-
return createFinalType(classType, errorNode, typeArgs, flags);
|
13989
|
+
return { type: createFinalType(classType, errorNode, typeArgs, flags) };
|
13832
13990
|
}
|
13833
13991
|
case 'Annotated': {
|
13834
13992
|
return createAnnotatedType(errorNode, typeArgs);
|
13835
13993
|
}
|
13836
13994
|
case 'Concatenate': {
|
13837
|
-
return createConcatenateType(errorNode, classType, typeArgs);
|
13995
|
+
return { type: createConcatenateType(errorNode, classType, typeArgs) };
|
13838
13996
|
}
|
13839
13997
|
case 'TypeGuard':
|
13840
13998
|
case 'StrictTypeGuard': {
|
13841
|
-
return createTypeGuardType(errorNode, classType, typeArgs, flags);
|
13999
|
+
return { type: createTypeGuardType(errorNode, classType, typeArgs, flags) };
|
13842
14000
|
}
|
13843
14001
|
case 'Unpack': {
|
13844
|
-
return createUnpackType(classType, errorNode, typeArgs, flags);
|
14002
|
+
return { type: createUnpackType(classType, errorNode, typeArgs, flags) };
|
13845
14003
|
}
|
13846
14004
|
case 'Required':
|
13847
14005
|
case 'NotRequired': {
|
13848
14006
|
return createRequiredType(classType, errorNode, aliasedName === 'Required', typeArgs, flags);
|
13849
14007
|
}
|
13850
14008
|
case 'Self': {
|
13851
|
-
return createSelfType(classType, errorNode, typeArgs);
|
14009
|
+
return { type: createSelfType(classType, errorNode, typeArgs) };
|
13852
14010
|
}
|
13853
14011
|
case 'LiteralString': {
|
13854
|
-
return createSpecialType(classType, typeArgs, 0);
|
14012
|
+
return { type: createSpecialType(classType, typeArgs, 0) };
|
13855
14013
|
}
|
13856
14014
|
}
|
13857
14015
|
}
|
@@ -13866,7 +14024,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13866
14024
|
// PEP 484 says that type[Any] should be considered
|
13867
14025
|
// equivalent to type.
|
13868
14026
|
if (typeArgs.length === 1 && (0, types_1.isAnyOrUnknown)(typeArgs[0].type)) {
|
13869
|
-
return classType;
|
14027
|
+
return { type: classType };
|
13870
14028
|
}
|
13871
14029
|
const typeClass = getTypingType(errorNode, 'Type');
|
13872
14030
|
if (typeClass && (0, types_1.isInstantiableClass)(typeClass)) {
|
@@ -13876,16 +14034,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13876
14034
|
if ((0, types_1.isInstantiableClass)(typeType)) {
|
13877
14035
|
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
13878
14036
|
}
|
13879
|
-
return typeType;
|
14037
|
+
return { type: typeType };
|
13880
14038
|
}
|
13881
14039
|
}
|
13882
14040
|
// Handle "tuple" specially, since it needs to act like "Tuple"
|
13883
14041
|
// in Python 3.9 and newer.
|
13884
14042
|
if ((0, typeUtils_1.isTupleClass)(classType)) {
|
13885
|
-
return
|
13886
|
-
|
13887
|
-
|
13888
|
-
|
14043
|
+
return {
|
14044
|
+
type: createSpecialType(classType, typeArgs,
|
14045
|
+
/* paramLimit */ undefined,
|
14046
|
+
/* allowParamSpec */ undefined,
|
14047
|
+
/* isCallable */ true),
|
14048
|
+
};
|
13889
14049
|
}
|
13890
14050
|
}
|
13891
14051
|
let typeArgCount = typeArgs ? typeArgs.length : 0;
|
@@ -13894,7 +14054,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13894
14054
|
// If there are no type parameters or args, the class is already specialized.
|
13895
14055
|
// No need to do any more work.
|
13896
14056
|
if (typeParameters.length === 0 && typeArgCount === 0) {
|
13897
|
-
return classType;
|
14057
|
+
return { type: classType };
|
13898
14058
|
}
|
13899
14059
|
const variadicTypeParamIndex = typeParameters.findIndex((param) => (0, types_1.isVariadicTypeVar)(param));
|
13900
14060
|
if (typeArgs) {
|
@@ -14057,7 +14217,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14057
14217
|
const destType = typeParameters[index];
|
14058
14218
|
const declaredVariance = destType.details.declaredVariance;
|
14059
14219
|
if (!(0, typeUtils_1.isVarianceOfTypeArgumentCompatible)(adjustedTypeArgType, declaredVariance)) {
|
14060
|
-
diag.addMessage(localize_1.Localizer.DiagnosticAddendum.
|
14220
|
+
diag.addMessage(localize_1.Localizer.DiagnosticAddendum.varianceMismatchForClass().format({
|
14061
14221
|
typeVarName: printType(adjustedTypeArgType),
|
14062
14222
|
className: classType.details.name,
|
14063
14223
|
}));
|
@@ -14087,7 +14247,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14087
14247
|
typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
|
14088
14248
|
}
|
14089
14249
|
const specializedClass = types_1.ClassType.cloneForSpecialization(classType, typeArgTypes, typeArgs !== undefined);
|
14090
|
-
return specializedClass;
|
14250
|
+
return { type: specializedClass };
|
14091
14251
|
}
|
14092
14252
|
function getTypeOfArgument(arg) {
|
14093
14253
|
if (arg.typeResult) {
|
@@ -14508,59 +14668,63 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14508
14668
|
return declarations;
|
14509
14669
|
}
|
14510
14670
|
function getTypeForDeclaration(declaration) {
|
14511
|
-
var _a, _b;
|
14671
|
+
var _a, _b, _c;
|
14512
14672
|
switch (declaration.type) {
|
14513
14673
|
case 0 /* Intrinsic */: {
|
14514
14674
|
if (declaration.intrinsicType === 'Any') {
|
14515
|
-
return types_1.AnyType.create();
|
14675
|
+
return { type: types_1.AnyType.create() };
|
14516
14676
|
}
|
14517
14677
|
if (declaration.intrinsicType === 'class') {
|
14518
14678
|
const classNode = ParseTreeUtils.getEnclosingClass(declaration.node);
|
14519
14679
|
const classTypeInfo = getTypeOfClass(classNode);
|
14520
|
-
return classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.classType;
|
14680
|
+
return { type: classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.classType };
|
14521
14681
|
}
|
14522
14682
|
const strType = getBuiltInObject(declaration.node, 'str');
|
14523
14683
|
const intType = getBuiltInObject(declaration.node, 'int');
|
14524
14684
|
if ((0, types_1.isClassInstance)(intType) && (0, types_1.isClassInstance)(strType)) {
|
14525
14685
|
if (declaration.intrinsicType === 'str') {
|
14526
|
-
return strType;
|
14686
|
+
return { type: strType };
|
14527
14687
|
}
|
14528
14688
|
if (declaration.intrinsicType === 'str | None') {
|
14529
|
-
return (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]);
|
14689
|
+
return { type: (0, types_1.combineTypes)([strType, types_1.NoneType.createInstance()]) };
|
14530
14690
|
}
|
14531
14691
|
if (declaration.intrinsicType === 'int') {
|
14532
|
-
return intType;
|
14692
|
+
return { type: intType };
|
14533
14693
|
}
|
14534
14694
|
if (declaration.intrinsicType === 'Iterable[str]') {
|
14535
14695
|
const iterableType = getBuiltInType(declaration.node, 'Iterable');
|
14536
14696
|
if ((0, types_1.isInstantiableClass)(iterableType)) {
|
14537
|
-
return
|
14538
|
-
|
14697
|
+
return {
|
14698
|
+
type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(iterableType, [strType],
|
14699
|
+
/* isTypeArgumentExplicit */ true)),
|
14700
|
+
};
|
14539
14701
|
}
|
14540
14702
|
}
|
14541
14703
|
if (declaration.intrinsicType === 'Dict[str, Any]') {
|
14542
14704
|
const dictType = getBuiltInType(declaration.node, 'dict');
|
14543
14705
|
if ((0, types_1.isInstantiableClass)(dictType)) {
|
14544
|
-
return
|
14545
|
-
|
14706
|
+
return {
|
14707
|
+
type: types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(dictType, [strType, types_1.AnyType.create()],
|
14708
|
+
/* isTypeArgumentExplicit */ true)),
|
14709
|
+
};
|
14546
14710
|
}
|
14547
14711
|
}
|
14548
14712
|
}
|
14549
|
-
return types_1.UnknownType.create();
|
14713
|
+
return { type: types_1.UnknownType.create() };
|
14550
14714
|
}
|
14551
14715
|
case 6 /* Class */: {
|
14552
14716
|
const classTypeInfo = getTypeOfClass(declaration.node);
|
14553
|
-
return classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.decoratedType;
|
14717
|
+
return { type: classTypeInfo === null || classTypeInfo === void 0 ? void 0 : classTypeInfo.decoratedType };
|
14554
14718
|
}
|
14555
14719
|
case 7 /* SpecialBuiltInClass */: {
|
14556
|
-
return getTypeOfAnnotation(declaration.node.typeAnnotation);
|
14720
|
+
return { type: getTypeOfAnnotation(declaration.node.typeAnnotation) };
|
14557
14721
|
}
|
14558
14722
|
case 5 /* Function */: {
|
14559
14723
|
const functionTypeInfo = getTypeOfFunction(declaration.node);
|
14560
|
-
return functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.decoratedType;
|
14724
|
+
return { type: functionTypeInfo === null || functionTypeInfo === void 0 ? void 0 : functionTypeInfo.decoratedType };
|
14561
14725
|
}
|
14562
14726
|
case 4 /* TypeAlias */: {
|
14563
|
-
return getTypeOfTypeAlias(declaration.node);
|
14727
|
+
return { type: getTypeOfTypeAlias(declaration.node) };
|
14564
14728
|
}
|
14565
14729
|
case 2 /* Parameter */: {
|
14566
14730
|
let typeAnnotationNode = declaration.node.typeAnnotation || declaration.node.typeAnnotationComment;
|
@@ -14579,19 +14743,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14579
14743
|
}
|
14580
14744
|
if (typeAnnotationNode) {
|
14581
14745
|
const declaredType = getTypeOfParameterAnnotation(typeAnnotationNode, declaration.node.category);
|
14582
|
-
return
|
14746
|
+
return {
|
14747
|
+
type: transformVariadicParamType(declaration.node, declaration.node.category, adjustParameterAnnotatedType(declaration.node, declaredType)),
|
14748
|
+
};
|
14583
14749
|
}
|
14584
|
-
return undefined;
|
14750
|
+
return { type: undefined };
|
14585
14751
|
}
|
14586
14752
|
case 3 /* TypeParameter */: {
|
14587
|
-
return getTypeOfTypeParameter(declaration.node);
|
14753
|
+
return { type: getTypeOfTypeParameter(declaration.node) };
|
14588
14754
|
}
|
14589
14755
|
case 1 /* Variable */: {
|
14590
14756
|
const typeAnnotationNode = declaration.typeAnnotationNode;
|
14591
14757
|
if (typeAnnotationNode) {
|
14592
|
-
const typeAliasNode = isDeclaredTypeAlias(typeAnnotationNode)
|
14593
|
-
? ParseTreeUtils.getTypeAnnotationNode(typeAnnotationNode)
|
14594
|
-
: undefined;
|
14595
14758
|
let declaredType;
|
14596
14759
|
if (declaration.isRuntimeTypeExpression) {
|
14597
14760
|
declaredType = (0, typeUtils_1.convertToInstance)(getTypeOfExpressionExpectingType(typeAnnotationNode, {
|
@@ -14608,24 +14771,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14608
14771
|
isVariableAnnotation: true,
|
14609
14772
|
allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(declNode),
|
14610
14773
|
allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(declNode),
|
14774
|
+
allowRequired: ParseTreeUtils.isRequiredAllowedForAssignmentTarget(declNode),
|
14611
14775
|
});
|
14612
14776
|
}
|
14613
14777
|
if (declaredType) {
|
14614
14778
|
// Apply enum transform if appropriate.
|
14615
14779
|
if (declaration.node.nodeType === 38 /* Name */) {
|
14616
14780
|
declaredType =
|
14617
|
-
(0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType)
|
14781
|
+
(_c = (0, enums_1.transformTypeForPossibleEnumClass)(evaluatorInterface, declaration.node, () => declaredType)) !== null && _c !== void 0 ? _c : declaredType;
|
14618
14782
|
}
|
14619
|
-
if (
|
14620
|
-
|
14783
|
+
if ((0, types_1.isClassInstance)(declaredType) && types_1.ClassType.isBuiltIn(declaredType, 'TypeAlias')) {
|
14784
|
+
return { type: undefined, isTypeAlias: true };
|
14621
14785
|
}
|
14622
|
-
return declaredType;
|
14786
|
+
return { type: declaredType };
|
14623
14787
|
}
|
14624
14788
|
}
|
14625
|
-
return undefined;
|
14789
|
+
return { type: undefined };
|
14626
14790
|
}
|
14627
14791
|
case 8 /* Alias */: {
|
14628
|
-
return undefined;
|
14792
|
+
return { type: undefined };
|
14629
14793
|
}
|
14630
14794
|
}
|
14631
14795
|
}
|
@@ -14794,8 +14958,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14794
14958
|
}
|
14795
14959
|
}
|
14796
14960
|
const declaredType = getTypeForDeclaration(resolvedDecl);
|
14797
|
-
if (declaredType) {
|
14798
|
-
return declaredType;
|
14961
|
+
if (declaredType.type) {
|
14962
|
+
return declaredType.type;
|
14799
14963
|
}
|
14800
14964
|
// If this is part of a "py.typed" package, don't fall back on type inference
|
14801
14965
|
// unless it's marked Final, is a constant, or is a declared type alias.
|
@@ -14815,7 +14979,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14815
14979
|
isUnambiguousType = true;
|
14816
14980
|
}
|
14817
14981
|
}
|
14818
|
-
if (resolvedDecl
|
14982
|
+
if (isFinalVariableDeclaration(resolvedDecl) || resolvedDecl.isConstant) {
|
14819
14983
|
isUnambiguousType = true;
|
14820
14984
|
}
|
14821
14985
|
}
|
@@ -14828,7 +14992,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14828
14992
|
})) === null || _a === void 0 ? void 0 : _a.type;
|
14829
14993
|
}
|
14830
14994
|
if (resolvedDecl.type === 1 /* Variable */ && resolvedDecl.inferredTypeSource) {
|
14831
|
-
const isTypeAlias =
|
14995
|
+
const isTypeAlias = isExplicitTypeAliasDeclaration(resolvedDecl) || isPossibleTypeAliasOrTypedDict(resolvedDecl);
|
14832
14996
|
// If this is a type alias, evaluate types for the entire assignment
|
14833
14997
|
// statement rather than just the RHS of the assignment.
|
14834
14998
|
const typeSource = isTypeAlias && resolvedDecl.inferredTypeSource.parent
|
@@ -14950,27 +15114,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14950
15114
|
}
|
14951
15115
|
function getEffectiveTypeOfSymbolForUsage(symbol, usageNode, useLastDecl = false) {
|
14952
15116
|
var _a, _b;
|
14953
|
-
// If there's a declared type, it takes precedence over inferred types.
|
14954
|
-
if (symbol.hasTypedDeclarations()) {
|
14955
|
-
const declaredType = getDeclaredTypeOfSymbol(symbol, usageNode);
|
14956
|
-
const typedDecls = symbol.getTypedDeclarations();
|
14957
|
-
let isIncomplete = false;
|
14958
|
-
if (declaredType) {
|
14959
|
-
if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
|
14960
|
-
isIncomplete = true;
|
14961
|
-
}
|
14962
|
-
else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
|
14963
|
-
isIncomplete = true;
|
14964
|
-
}
|
14965
|
-
}
|
14966
|
-
return {
|
14967
|
-
type: declaredType !== null && declaredType !== void 0 ? declaredType : types_1.UnknownType.create(),
|
14968
|
-
isIncomplete,
|
14969
|
-
includesVariableDecl: typedDecls.some((decl) => decl.type === 1 /* Variable */),
|
14970
|
-
includesIllegalTypeAliasDecl: !typedDecls.every((decl) => (0, declarationUtils_1.isPossibleTypeAliasDeclaration)(decl)),
|
14971
|
-
isRecursiveDefinition: !declaredType,
|
14972
|
-
};
|
14973
|
-
}
|
14974
15117
|
// Look in the cache to see if we've computed this already.
|
14975
15118
|
let cacheEntries = effectiveTypeCache.get(symbol.id);
|
14976
15119
|
const usageNodeId = usageNode ? usageNode.id : undefined;
|
@@ -14983,6 +15126,34 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14983
15126
|
}
|
14984
15127
|
}
|
14985
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
|
+
}
|
14986
15157
|
// Infer the type.
|
14987
15158
|
const typesToCombine = [];
|
14988
15159
|
const decls = symbol.getDeclarations();
|
@@ -14993,13 +15164,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14993
15164
|
let declIndexToConsider;
|
14994
15165
|
// Limit the number of declarations to explore.
|
14995
15166
|
if (decls.length > maxDeclarationsToUseForInference) {
|
14996
|
-
|
15167
|
+
const result = {
|
14997
15168
|
type: types_1.UnknownType.create(),
|
14998
15169
|
isIncomplete: false,
|
14999
15170
|
includesVariableDecl: false,
|
15000
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) =>
|
15171
|
+
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15001
15172
|
isRecursiveDefinition: false,
|
15002
15173
|
};
|
15174
|
+
addToEffectiveTypeCache(result);
|
15175
|
+
return result;
|
15003
15176
|
}
|
15004
15177
|
// If the caller has requested that we use only the last decl, we
|
15005
15178
|
// will use only the last one, but we'll ignore decls that are in
|
@@ -15049,7 +15222,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15049
15222
|
const resolvedDecl = (_a = resolveAliasDeclaration(decl,
|
15050
15223
|
/* resolveLocalNames */ true,
|
15051
15224
|
/* allowExternallyHiddenAccess */ AnalyzerNodeInfo.getFileInfo(decl.node).isStubFile)) !== null && _a !== void 0 ? _a : decl;
|
15052
|
-
const isExplicitTypeAlias =
|
15225
|
+
const isExplicitTypeAlias = isExplicitTypeAliasDeclaration(decl);
|
15053
15226
|
const isTypeAlias = isExplicitTypeAlias || isPossibleTypeAliasOrTypedDict(decl);
|
15054
15227
|
if (isExplicitTypeAlias) {
|
15055
15228
|
sawExplicitTypeAlias = true;
|
@@ -15060,14 +15233,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15060
15233
|
resolvedDecl.type === 1 /* Variable */ &&
|
15061
15234
|
((_c = (_b = resolvedDecl.inferredTypeSource) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 3 /* Assignment */) {
|
15062
15235
|
evaluateTypesForAssignmentStatement(resolvedDecl.inferredTypeSource.parent);
|
15063
|
-
if (resolvedDecl.typeAliasAnnotation) {
|
15064
|
-
// Mark "TypeAlias" declaration as accessed.
|
15065
|
-
getTypeOfAnnotation(resolvedDecl.typeAliasAnnotation, {
|
15066
|
-
isVariableAnnotation: true,
|
15067
|
-
allowFinal: ParseTreeUtils.isFinalAllowedForAssignmentTarget(resolvedDecl.node),
|
15068
|
-
allowClassVar: ParseTreeUtils.isClassVarAllowedForAssignmentTarget(resolvedDecl.node),
|
15069
|
-
});
|
15070
|
-
}
|
15071
15236
|
}
|
15072
15237
|
if (pushSymbolResolution(symbol, decl)) {
|
15073
15238
|
try {
|
@@ -15085,7 +15250,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15085
15250
|
}
|
15086
15251
|
let isConstant = false;
|
15087
15252
|
if (resolvedDecl.type === 1 /* Variable */) {
|
15088
|
-
if (resolvedDecl.isConstant || resolvedDecl
|
15253
|
+
if (resolvedDecl.isConstant || isFinalVariableDeclaration(resolvedDecl)) {
|
15089
15254
|
isConstant = true;
|
15090
15255
|
}
|
15091
15256
|
}
|
@@ -15142,17 +15307,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15142
15307
|
type: (0, types_1.combineTypes)(typesToCombine),
|
15143
15308
|
isIncomplete,
|
15144
15309
|
includesVariableDecl,
|
15145
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) =>
|
15310
|
+
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15146
15311
|
isRecursiveDefinition: false,
|
15147
15312
|
evaluationAttempts,
|
15148
15313
|
};
|
15149
15314
|
if (!includesSpeculativeResult) {
|
15150
|
-
|
15151
|
-
if (!cacheEntries) {
|
15152
|
-
cacheEntries = new Map();
|
15153
|
-
effectiveTypeCache.set(symbol.id, cacheEntries);
|
15154
|
-
}
|
15155
|
-
cacheEntries.set(effectiveTypeCacheKey, result);
|
15315
|
+
addToEffectiveTypeCache(result);
|
15156
15316
|
}
|
15157
15317
|
return result;
|
15158
15318
|
}
|
@@ -15160,19 +15320,33 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15160
15320
|
type: types_1.UnboundType.create(),
|
15161
15321
|
isIncomplete,
|
15162
15322
|
includesVariableDecl,
|
15163
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) =>
|
15323
|
+
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15164
15324
|
isRecursiveDefinition: false,
|
15165
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
|
+
}
|
15166
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.
|
15167
15341
|
function getDeclaredTypeOfSymbol(symbol, usageNode) {
|
15168
15342
|
const synthesizedType = symbol.getSynthesizedType();
|
15169
15343
|
if (synthesizedType) {
|
15170
|
-
return synthesizedType;
|
15344
|
+
return { type: synthesizedType };
|
15171
15345
|
}
|
15172
15346
|
let typedDecls = symbol.getTypedDeclarations();
|
15173
15347
|
if (typedDecls.length === 0) {
|
15174
15348
|
// There was no declaration with a defined type.
|
15175
|
-
return undefined;
|
15349
|
+
return { type: undefined };
|
15176
15350
|
}
|
15177
15351
|
// If there is more than one typed decl, filter out any that are not
|
15178
15352
|
// reachable from the usage node (if specified). This can happen in
|
@@ -15193,7 +15367,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15193
15367
|
return true;
|
15194
15368
|
});
|
15195
15369
|
if (filteredTypedDecls.length === 0) {
|
15196
|
-
return types_1.UnboundType.create();
|
15370
|
+
return { type: types_1.UnboundType.create() };
|
15197
15371
|
}
|
15198
15372
|
typedDecls = filteredTypedDecls;
|
15199
15373
|
}
|
@@ -15209,12 +15383,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15209
15383
|
// for recursive symbol resolution, return it as the resolved type.
|
15210
15384
|
const partialType = getSymbolResolutionPartialType(symbol, decl);
|
15211
15385
|
if (partialType) {
|
15212
|
-
return partialType;
|
15386
|
+
return { type: partialType };
|
15213
15387
|
}
|
15214
15388
|
if (getIndexOfSymbolResolution(symbol, decl) < 0) {
|
15215
15389
|
if (pushSymbolResolution(symbol, decl)) {
|
15216
15390
|
try {
|
15217
|
-
const
|
15391
|
+
const declaredTypeInfo = getTypeForDeclaration(decl);
|
15218
15392
|
// If there was recursion detected, don't use this declaration.
|
15219
15393
|
// The exception is it's a class declaration because getTypeOfClass
|
15220
15394
|
// handles recursion by populating a partially-created class type
|
@@ -15222,7 +15396,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15222
15396
|
// circular dependency between the "type" and "object" classes in
|
15223
15397
|
// builtins.pyi (since "object" is a "type" and "type" is an "object").
|
15224
15398
|
if (popSymbolResolution(symbol) || decl.type === 6 /* Class */) {
|
15225
|
-
return
|
15399
|
+
return declaredTypeInfo;
|
15226
15400
|
}
|
15227
15401
|
}
|
15228
15402
|
catch (e) {
|
@@ -15234,7 +15408,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15234
15408
|
}
|
15235
15409
|
declIndex--;
|
15236
15410
|
}
|
15237
|
-
return undefined;
|
15411
|
+
return { type: undefined };
|
15238
15412
|
}
|
15239
15413
|
function inferReturnTypeIfNecessary(type) {
|
15240
15414
|
if ((0, types_1.isFunction)(type)) {
|
@@ -15595,18 +15769,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15595
15769
|
return false;
|
15596
15770
|
}
|
15597
15771
|
// This function is used to validate or infer the variance of type
|
15598
|
-
// parameters within a class.
|
15599
|
-
|
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) {
|
15600
15777
|
(0, debug_1.assert)(types_1.ClassType.isSameGenericClass(destType, srcType));
|
15601
15778
|
(0, debug_1.assert)(destType.details.typeParameters.length > 0);
|
15602
15779
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
15603
15780
|
const typeVarContext = new typeVarContext_1.TypeVarContext();
|
15604
15781
|
let isAssignable = true;
|
15605
15782
|
destType.details.fields.forEach((symbol, name) => {
|
15783
|
+
var _a;
|
15606
15784
|
if (isAssignable && symbol.isClassMember() && !symbol.isIgnoredForProtocolMatch()) {
|
15607
15785
|
const memberInfo = (0, typeUtils_1.lookUpClassMember)(srcType, name);
|
15608
15786
|
(0, debug_1.assert)(memberInfo !== undefined);
|
15609
|
-
let destMemberType = getDeclaredTypeOfSymbol(symbol);
|
15787
|
+
let destMemberType = (_a = getDeclaredTypeOfSymbol(symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
15610
15788
|
if (destMemberType) {
|
15611
15789
|
const srcMemberType = getTypeOfMember(memberInfo);
|
15612
15790
|
destMemberType = (0, typeUtils_1.partiallySpecializeType)(destMemberType, destType);
|
@@ -15624,7 +15802,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15624
15802
|
const primaryDecl = symbol.getDeclarations()[0];
|
15625
15803
|
// Class and instance variables that are mutable need to
|
15626
15804
|
// enforce invariance.
|
15627
|
-
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)
|
15628
15806
|
? 1 /* EnforceInvariance */
|
15629
15807
|
: 0 /* Default */;
|
15630
15808
|
if (!assignType(destMemberType, srcMemberType, diag, typeVarContext,
|
@@ -15644,7 +15822,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15644
15822
|
baseClass.details.typeParameters.length > 0) {
|
15645
15823
|
const specializedDestBaseClass = (0, typeUtils_1.specializeForBaseClass)(destType, baseClass);
|
15646
15824
|
const specializedSrcBaseClass = (0, typeUtils_1.specializeForBaseClass)(srcType, baseClass);
|
15647
|
-
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)) {
|
15648
15850
|
isAssignable = false;
|
15649
15851
|
}
|
15650
15852
|
}
|
@@ -15817,12 +16019,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15817
16019
|
return true;
|
15818
16020
|
}
|
15819
16021
|
function getGetterTypeFromProperty(propertyClass, inferTypeIfNeeded) {
|
16022
|
+
var _a;
|
15820
16023
|
if (!types_1.ClassType.isPropertyClass(propertyClass)) {
|
15821
16024
|
return undefined;
|
15822
16025
|
}
|
15823
16026
|
const fgetSymbol = propertyClass.details.fields.get('fget');
|
15824
16027
|
if (fgetSymbol) {
|
15825
|
-
const fgetType = getDeclaredTypeOfSymbol(fgetSymbol);
|
16028
|
+
const fgetType = (_a = getDeclaredTypeOfSymbol(fgetSymbol)) === null || _a === void 0 ? void 0 : _a.type;
|
15826
16029
|
if (fgetType && (0, types_1.isFunction)(fgetType)) {
|
15827
16030
|
return getFunctionEffectiveReturnType(fgetType, /* args */ undefined, inferTypeIfNeeded);
|
15828
16031
|
}
|
@@ -16542,6 +16745,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16542
16745
|
});
|
16543
16746
|
}
|
16544
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
|
+
}
|
16545
16757
|
// If we've assigned all of the source subtypes but one or more dest
|
16546
16758
|
// TypeVars have gone unmatched, treat this as success.
|
16547
16759
|
return true;
|
@@ -17544,14 +17756,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17544
17756
|
// When a value is assigned to a variable with a declared type,
|
17545
17757
|
// we may be able to narrow the type based on the assignment.
|
17546
17758
|
function narrowTypeBasedOnAssignment(declaredType, assignedType) {
|
17547
|
-
const diag = new diagnostic_1.DiagnosticAddendum();
|
17548
17759
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(assignedType, (assignedSubtype) => {
|
17549
17760
|
const narrowedSubtype = (0, typeUtils_1.mapSubtypes)(declaredType, (declaredSubtype) => {
|
17550
17761
|
// We can't narrow "Any".
|
17551
17762
|
if ((0, types_1.isAnyOrUnknown)(declaredType)) {
|
17552
17763
|
return declaredType;
|
17553
17764
|
}
|
17554
|
-
if (assignType(declaredSubtype, assignedSubtype
|
17765
|
+
if (assignType(declaredSubtype, assignedSubtype)) {
|
17555
17766
|
// If the source is generic and has unspecified type arguments,
|
17556
17767
|
// see if we can determine then based on the declared type.
|
17557
17768
|
if ((0, types_1.isInstantiableClass)(declaredSubtype) && (0, types_1.isInstantiableClass)(assignedSubtype)) {
|
@@ -18035,7 +18246,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18035
18246
|
}
|
18036
18247
|
else if ((0, types_1.isOverloadedFunction)(memberType)) {
|
18037
18248
|
const newOverloadType = types_1.OverloadedFunctionType.create([]);
|
18038
|
-
|
18249
|
+
// Don't bother binding the implementation.
|
18250
|
+
types_1.OverloadedFunctionType.getOverloads(memberType).forEach((overload) => {
|
18039
18251
|
const boundMethod = bindFunctionToClassOrObject(baseType, overload, memberClass,
|
18040
18252
|
/* errorNode */ undefined, recursionCount, treatConstructorAsClassMember, firstParamType);
|
18041
18253
|
if (boundMethod) {
|
@@ -18072,7 +18284,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18072
18284
|
const memberTypeFirstParamType = types_1.FunctionType.getEffectiveParameterType(memberType, 0);
|
18073
18285
|
// Fill out the typeVarContext for the "self" or "cls" parameter.
|
18074
18286
|
typeVarContext.addSolveForScope((0, typeUtils_1.getTypeVarScopeId)(memberType));
|
18075
|
-
const diag = new diagnostic_1.DiagnosticAddendum();
|
18287
|
+
const diag = errorNode ? new diagnostic_1.DiagnosticAddendum() : undefined;
|
18076
18288
|
if ((0, types_1.isTypeVar)(memberTypeFirstParamType) &&
|
18077
18289
|
memberTypeFirstParamType.details.boundType &&
|
18078
18290
|
(0, types_1.isClassInstance)(memberTypeFirstParamType.details.boundType) &&
|
@@ -18098,7 +18310,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18098
18310
|
type: printType(baseType),
|
18099
18311
|
methodName: methodName,
|
18100
18312
|
paramName: memberTypeFirstParam.name,
|
18101
|
-
}) + diag.getString(), errorNode);
|
18313
|
+
}) + (diag === null || diag === void 0 ? void 0 : diag.getString()), errorNode);
|
18102
18314
|
}
|
18103
18315
|
else {
|
18104
18316
|
// If there was no errorNode, we couldn't report the error,
|
@@ -18115,6 +18327,60 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18115
18327
|
const specializedFunction = (0, typeUtils_1.applySolvedTypeVars)(memberType, typeVarContext);
|
18116
18328
|
return types_1.FunctionType.clone(specializedFunction, stripFirstParam, baseType, (0, typeUtils_1.getTypeVarScopeId)(baseType));
|
18117
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
|
+
}
|
18118
18384
|
function printObjectTypeForClass(type) {
|
18119
18385
|
return TypePrinter.printObjectTypeForClass(type, evaluatorOptions.printTypeFlags, getFunctionEffectiveReturnType);
|
18120
18386
|
}
|
@@ -18257,6 +18523,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18257
18523
|
verifyTypeArgumentsAssignable,
|
18258
18524
|
inferReturnTypeIfNecessary,
|
18259
18525
|
inferTypeParameterVarianceForClass,
|
18526
|
+
isFinalVariable,
|
18527
|
+
isFinalVariableDeclaration,
|
18528
|
+
isExplicitTypeAliasDeclaration,
|
18260
18529
|
addError,
|
18261
18530
|
addWarning,
|
18262
18531
|
addInformation,
|