@zzzen/pyright-internal 1.2.0-dev.20220814 → 1.2.0-dev.20220904
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.js +28 -27
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +15 -1
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/patternMatching.js +10 -0
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +1 -0
- package/dist/analyzer/program.js +8 -1
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/service.d.ts +1 -1
- package/dist/analyzer/service.js +3 -1
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.d.ts +2 -1
- package/dist/analyzer/sourceFile.js +4 -3
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +136 -83
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeGuards.js +44 -14
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +1 -1
- package/dist/analyzer/typeUtils.js +2 -2
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.d.ts +3 -3
- package/dist/analyzer/typedDicts.js +9 -5
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.js +8 -7
- package/dist/analyzer/types.js.map +1 -1
- package/dist/common/diagnostic.d.ts +5 -1
- package/dist/common/diagnostic.js +34 -0
- package/dist/common/diagnostic.js.map +1 -1
- package/dist/languageServerBase.d.ts +1 -1
- package/dist/languageServerBase.js +22 -17
- package/dist/languageServerBase.js.map +1 -1
- package/dist/localization/localize.d.ts +1 -0
- package/dist/localization/localize.js +1 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +2 -1
- package/dist/parser/parser.js +16 -6
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/checker.test.js +2 -2
- package/dist/tests/harness/fourslash/testState.d.ts +8 -2
- package/dist/tests/harness/fourslash/testState.js +46 -40
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/ipythonMode.test.js +91 -0
- package/dist/tests/ipythonMode.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +5 -1
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +5 -1
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +6 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +6 -2
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/package.json +1 -1
@@ -180,7 +180,11 @@ const maxReturnTypeInferenceArgumentCount = 6;
|
|
180
180
|
// we will analyze to determine the return type of a function
|
181
181
|
// when its parameters are unannotated? We want to keep this
|
182
182
|
// pretty low because this can be very costly.
|
183
|
-
const maxReturnTypeInferenceCodeFlowComplexity =
|
183
|
+
const maxReturnTypeInferenceCodeFlowComplexity = 32;
|
184
|
+
// What is the max complexity of the code flow graph for
|
185
|
+
// call-site type inference? This is very expensive, so we
|
186
|
+
// want to keep this very low.
|
187
|
+
const maxReturnCallSiteTypeInferenceCodeFlowComplexity = 8;
|
184
188
|
// What is the max number of return types cached per function
|
185
189
|
// when using call-site inference?
|
186
190
|
const maxCallSiteReturnTypeCacheSize = 8;
|
@@ -695,6 +699,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
695
699
|
/* allowSpeculativeCaching */ true);
|
696
700
|
if (expectedType && !(0, types_1.isAnyOrUnknown)(expectedType) && !(0, types_1.isNever)(expectedType)) {
|
697
701
|
expectedTypeCache.set(node.id, expectedType);
|
702
|
+
if (!typeResult.isIncomplete && !typeResult.expectedTypeDiagAddendum) {
|
703
|
+
const diag = new diagnostic_1.DiagnosticAddendum();
|
704
|
+
if (!assignType(expectedType, typeResult.type, diag)) {
|
705
|
+
typeResult.expectedTypeDiagAddendum = diag;
|
706
|
+
diag.addTextRange(node);
|
707
|
+
}
|
708
|
+
}
|
698
709
|
}
|
699
710
|
if (printExpressionTypes) {
|
700
711
|
printExpressionSpaceCount--;
|
@@ -1884,11 +1895,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1884
1895
|
return (suppressedNodeStack.some((suppressedNode) => ParseTreeUtils.isNodeContainedWithin(node, suppressedNode)) ||
|
1885
1896
|
speculativeTypeTracker.isSpeculative(node));
|
1886
1897
|
}
|
1887
|
-
function addDiagnostic(diagLevel, rule, message, node) {
|
1898
|
+
function addDiagnostic(diagLevel, rule, message, node, range) {
|
1888
1899
|
if (diagLevel === 'none') {
|
1889
1900
|
return undefined;
|
1890
1901
|
}
|
1891
|
-
const diagnostic = addDiagnosticWithSuppressionCheck(diagLevel, message, node);
|
1902
|
+
const diagnostic = addDiagnosticWithSuppressionCheck(diagLevel, message, node, range);
|
1892
1903
|
if (diagnostic) {
|
1893
1904
|
diagnostic.setRule(rule);
|
1894
1905
|
}
|
@@ -1917,6 +1928,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1917
1928
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeExpectedClass().format({ type: printType(type) }) + diag.getString(), node);
|
1918
1929
|
}
|
1919
1930
|
function assignTypeToNameNode(nameNode, type, isTypeIncomplete, ignoreEmptyContainers, srcExpression, allowAssignmentToFinalVar = false, expectedTypeDiagAddendum) {
|
1931
|
+
var _a, _b;
|
1920
1932
|
const nameValue = nameNode.value;
|
1921
1933
|
const symbolWithScope = lookUpSymbolRecursive(nameNode, nameValue, /* honorCodeFlow */ false);
|
1922
1934
|
if (!symbolWithScope) {
|
@@ -1954,7 +1966,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1954
1966
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAssignmentMismatch().format({
|
1955
1967
|
sourceType: printType(type),
|
1956
1968
|
destType: printType(declaredType),
|
1957
|
-
}) + diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode);
|
1969
|
+
}) + diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode, (_b = (_a = diagAddendum.getEffectiveTextRange()) !== null && _a !== void 0 ? _a : srcExpression) !== null && _b !== void 0 ? _b : nameNode);
|
1958
1970
|
// Replace the assigned type with the (unnarrowed) declared type.
|
1959
1971
|
destType = declaredType;
|
1960
1972
|
}
|
@@ -3145,7 +3157,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3145
3157
|
return typeResult;
|
3146
3158
|
}
|
3147
3159
|
function getTypeOfMemberAccessWithBaseType(node, baseTypeResult, usage, flags) {
|
3148
|
-
var _a;
|
3160
|
+
var _a, _b;
|
3149
3161
|
let baseType = baseTypeResult.type;
|
3150
3162
|
const memberName = node.memberName.value;
|
3151
3163
|
let diag = new diagnostic_1.DiagnosticAddendum();
|
@@ -3415,7 +3427,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3415
3427
|
const [ruleSet, rule] = isFunctionRule
|
3416
3428
|
? [fileInfo.diagnosticRuleSet.reportFunctionMemberAccess, diagnosticRules_1.DiagnosticRule.reportFunctionMemberAccess]
|
3417
3429
|
: [fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues];
|
3418
|
-
addDiagnostic(ruleSet, rule, diagMessage.format({ name: memberName, type: printType(baseType) }) + diag.getString(), node.memberName);
|
3430
|
+
addDiagnostic(ruleSet, rule, diagMessage.format({ name: memberName, type: printType(baseType) }) + diag.getString(), node.memberName, (_a = diag.getEffectiveTextRange()) !== null && _a !== void 0 ? _a : node.memberName);
|
3419
3431
|
}
|
3420
3432
|
// If this is member access on a function, use "Any" so if the
|
3421
3433
|
// reportFunctionMemberAccess rule is disabled, we don't trigger
|
@@ -3436,7 +3448,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3436
3448
|
// It can also come up in cases like "isinstance(x, (list, dict))".
|
3437
3449
|
if ((0, types_1.isInstantiableClass)(type)) {
|
3438
3450
|
const argNode = ParseTreeUtils.getParentNodeOfType(node, 1 /* Argument */);
|
3439
|
-
if (argNode && ((
|
3451
|
+
if (argNode && ((_b = argNode === null || argNode === void 0 ? void 0 : argNode.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 9 /* Call */) {
|
3440
3452
|
skipPartialUnknownCheck = true;
|
3441
3453
|
}
|
3442
3454
|
}
|
@@ -3852,7 +3864,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3852
3864
|
}
|
3853
3865
|
}
|
3854
3866
|
if (enforceTargetType) {
|
3855
|
-
let effectiveType =
|
3867
|
+
let effectiveType = subtype;
|
3856
3868
|
// If the code is patching a method (defined on the class)
|
3857
3869
|
// with an object-level function, strip the "self" parameter
|
3858
3870
|
// off the original type. This is sometimes done for test
|
@@ -4731,19 +4743,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4731
4743
|
});
|
4732
4744
|
effectiveExpectedType = matchingSubtype;
|
4733
4745
|
}
|
4746
|
+
let expectedTypeDiagAddendum;
|
4734
4747
|
if (effectiveExpectedType) {
|
4735
4748
|
const result = getTypeOfTupleExpected(node, effectiveExpectedType);
|
4736
|
-
if (result) {
|
4749
|
+
if (result && !result.typeErrors) {
|
4737
4750
|
return result;
|
4738
4751
|
}
|
4752
|
+
expectedTypeDiagAddendum = result === null || result === void 0 ? void 0 : result.expectedTypeDiagAddendum;
|
4739
4753
|
}
|
4740
|
-
const
|
4754
|
+
const typeResult = getTypeOfTupleInferred(node);
|
4741
4755
|
// If there was an expected type of Any, replace the resulting type
|
4742
4756
|
// with Any rather than return a type with unknowns.
|
4743
4757
|
if (expectedTypeContainsAny) {
|
4744
|
-
|
4758
|
+
typeResult.type = types_1.AnyType.create();
|
4745
4759
|
}
|
4746
|
-
return
|
4760
|
+
return { ...typeResult, expectedTypeDiagAddendum };
|
4747
4761
|
}
|
4748
4762
|
function getTypeOfTupleExpected(node, expectedType) {
|
4749
4763
|
expectedType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedType);
|
@@ -4787,7 +4801,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4787
4801
|
/* flags */ undefined, index < expectedTypes.length ? expectedTypes[index] : undefined));
|
4788
4802
|
const type = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, buildTupleTypesList(entryTypeResults),
|
4789
4803
|
/* isTypeArgumentExplicit */ true));
|
4790
|
-
|
4804
|
+
// Copy any expected type diag addenda for precision error reporting.
|
4805
|
+
let expectedTypeDiagAddendum;
|
4806
|
+
if (entryTypeResults.some((result) => result.expectedTypeDiagAddendum)) {
|
4807
|
+
expectedTypeDiagAddendum = new diagnostic_1.DiagnosticAddendum();
|
4808
|
+
entryTypeResults.forEach((result) => {
|
4809
|
+
if (result.expectedTypeDiagAddendum) {
|
4810
|
+
expectedTypeDiagAddendum.addAddendum(result.expectedTypeDiagAddendum);
|
4811
|
+
}
|
4812
|
+
});
|
4813
|
+
}
|
4814
|
+
return { type, expectedTypeDiagAddendum };
|
4791
4815
|
}
|
4792
4816
|
function getTypeOfTupleInferred(node) {
|
4793
4817
|
const entryTypeResults = node.expressions.map((expr) => getTypeOfExpression(expr));
|
@@ -5810,8 +5834,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5810
5834
|
// the call is a constructor but the proper TypeVar scope has been lost.
|
5811
5835
|
// We'll add a wildcard TypeVar scope here. This is a bit of a hack and
|
5812
5836
|
// we may need to revisit this in the future.
|
5813
|
-
if (
|
5814
|
-
types_1.FunctionType.isConstructorMethod(expandedSubtype)) {
|
5837
|
+
if (types_1.FunctionType.isConstructorMethod(expandedSubtype)) {
|
5815
5838
|
effectiveTypeVarContext.addSolveForScope(types_1.WildcardTypeVarScopeId);
|
5816
5839
|
}
|
5817
5840
|
}
|
@@ -6802,12 +6825,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6802
6825
|
// types of each argument expression and validates that the resulting type is
|
6803
6826
|
// compatible with the declared type of the corresponding parameter.
|
6804
6827
|
function validateFunctionArgumentTypesWithExpectedType(errorNode, matchResults, typeVarContext, skipUnknownArgCheck = false, expectedType) {
|
6828
|
+
var _a;
|
6805
6829
|
const type = matchResults.overload;
|
6806
6830
|
if (!expectedType ||
|
6807
6831
|
(0, types_1.isAnyOrUnknown)(expectedType) ||
|
6808
6832
|
(0, types_1.isNever)(expectedType) ||
|
6809
6833
|
(0, typeUtils_1.requiresSpecialization)(expectedType) ||
|
6810
|
-
!type.details.declaredReturnType
|
6834
|
+
!type.details.declaredReturnType ||
|
6835
|
+
!(0, typeUtils_1.requiresSpecialization)((_a = types_1.FunctionType.getSpecializedReturnType(type)) !== null && _a !== void 0 ? _a : types_1.UnknownType.create())) {
|
6811
6836
|
return validateFunctionArgumentTypes(errorNode, matchResults, typeVarContext, skipUnknownArgCheck);
|
6812
6837
|
}
|
6813
6838
|
const effectiveReturnType = getFunctionEffectiveReturnType(type);
|
@@ -7207,6 +7232,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7207
7232
|
return !reportedArgError;
|
7208
7233
|
}
|
7209
7234
|
function validateArgType(argParam, typeVarContext, functionType, skipUnknownCheck, skipOverloadArg, useNarrowBoundOnly, conditionFilter) {
|
7235
|
+
var _a;
|
7210
7236
|
let argType;
|
7211
7237
|
let expectedTypeDiag;
|
7212
7238
|
let isTypeIncomplete = false;
|
@@ -7380,7 +7406,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7380
7406
|
if (expectedTypeDiag) {
|
7381
7407
|
diag = expectedTypeDiag;
|
7382
7408
|
}
|
7383
|
-
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, message + diag.getString(), argParam.errorNode);
|
7409
|
+
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, message + diag.getString(), argParam.errorNode, (_a = diag.getEffectiveTextRange()) !== null && _a !== void 0 ? _a : argParam.errorNode);
|
7384
7410
|
}
|
7385
7411
|
return { isCompatible: false, argType, isTypeIncomplete, condition };
|
7386
7412
|
}
|
@@ -8435,7 +8461,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8435
8461
|
isIncomplete = true;
|
8436
8462
|
}
|
8437
8463
|
if (types_1.ClassType.isTypedDictClass(expectedType)) {
|
8438
|
-
const resultTypedDict = (0, typedDicts_1.assignToTypedDict)(evaluatorInterface, expectedType, keyTypes, valueTypes,
|
8464
|
+
const resultTypedDict = (0, typedDicts_1.assignToTypedDict)(evaluatorInterface, expectedType, keyTypes, valueTypes,
|
8465
|
+
// Don't overwrite existing expectedDiagAddendum messages if they were
|
8466
|
+
// already provided by getKeyValueTypesFromDictionary.
|
8467
|
+
(expectedDiagAddendum === null || expectedDiagAddendum === void 0 ? void 0 : expectedDiagAddendum.isEmpty()) ? expectedDiagAddendum : undefined);
|
8439
8468
|
if (resultTypedDict) {
|
8440
8469
|
return {
|
8441
8470
|
type: resultTypedDict,
|
@@ -8469,8 +8498,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8469
8498
|
// and Iterable use covariant value types, so they can be narrowed.
|
8470
8499
|
const isValueTypeInvariant = (0, types_1.isClassInstance)(expectedType) &&
|
8471
8500
|
(types_1.ClassType.isBuiltIn(expectedType, 'dict') || types_1.ClassType.isBuiltIn(expectedType, 'MutableMapping'));
|
8472
|
-
const specializedKeyType = inferTypeArgFromExpectedType(expectedKeyType, keyTypes
|
8473
|
-
|
8501
|
+
const specializedKeyType = inferTypeArgFromExpectedType(expectedKeyType, keyTypes.map((result) => result.type),
|
8502
|
+
/* isNarrowable */ false);
|
8503
|
+
const specializedValueType = inferTypeArgFromExpectedType(expectedValueType, valueTypes.map((result) => result.type),
|
8474
8504
|
/* isNarrowable */ !isValueTypeInvariant);
|
8475
8505
|
if (!specializedKeyType || !specializedValueType) {
|
8476
8506
|
return undefined;
|
@@ -8484,17 +8514,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8484
8514
|
const fallbackType = hasExpectedType ? types_1.AnyType.create() : types_1.UnknownType.create();
|
8485
8515
|
let keyType = fallbackType;
|
8486
8516
|
let valueType = fallbackType;
|
8487
|
-
|
8488
|
-
|
8517
|
+
const keyTypeResults = [];
|
8518
|
+
const valueTypeResults = [];
|
8489
8519
|
let isEmptyContainer = false;
|
8490
8520
|
let isIncomplete = false;
|
8491
8521
|
// Infer the key and value types if possible.
|
8492
|
-
if (getKeyAndValueTypesFromDictionary(node,
|
8522
|
+
if (getKeyAndValueTypesFromDictionary(node, keyTypeResults, valueTypeResults,
|
8523
|
+
/* forceStrictInference */ hasExpectedType)) {
|
8493
8524
|
isIncomplete = true;
|
8494
8525
|
}
|
8495
8526
|
// Strip any literal values.
|
8496
|
-
keyTypes =
|
8497
|
-
valueTypes =
|
8527
|
+
const keyTypes = keyTypeResults.map((t) => stripLiteralValue(t.type));
|
8528
|
+
const valueTypes = valueTypeResults.map((t) => stripLiteralValue(t.type));
|
8498
8529
|
keyType = keyTypes.length > 0 ? (0, types_1.combineTypes)(keyTypes) : fallbackType;
|
8499
8530
|
// If the value type differs and we're not using "strict inference mode",
|
8500
8531
|
// we need to back off because we can't properly represent the mappings
|
@@ -8556,8 +8587,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8556
8587
|
isIncomplete = true;
|
8557
8588
|
}
|
8558
8589
|
if (forceStrictInference || index < maxEntriesToUseForInference) {
|
8559
|
-
keyTypes.push(keyType);
|
8560
|
-
valueTypes.push(valueType);
|
8590
|
+
keyTypes.push({ node: entryNode.keyExpression, type: keyType });
|
8591
|
+
valueTypes.push({ node: entryNode.valueExpression, type: valueType });
|
8561
8592
|
}
|
8562
8593
|
addUnknown = false;
|
8563
8594
|
}
|
@@ -8578,8 +8609,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8578
8609
|
/* allowNarrowed */ true);
|
8579
8610
|
tdEntries.forEach((entry, name) => {
|
8580
8611
|
if (entry.isRequired || entry.isProvided) {
|
8581
|
-
keyTypes.push(types_1.ClassType.cloneWithLiteral(strObject, name));
|
8582
|
-
valueTypes.push(entry.valueType);
|
8612
|
+
keyTypes.push({ node: entryNode, type: types_1.ClassType.cloneWithLiteral(strObject, name) });
|
8613
|
+
valueTypes.push({ node: entryNode, type: entry.valueType });
|
8583
8614
|
}
|
8584
8615
|
});
|
8585
8616
|
addUnknown = false;
|
@@ -8604,8 +8635,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8604
8635
|
const typeArgs = specializedMapping.typeArguments;
|
8605
8636
|
if (typeArgs && typeArgs.length >= 2) {
|
8606
8637
|
if (forceStrictInference || index < maxEntriesToUseForInference) {
|
8607
|
-
keyTypes.push(typeArgs[0]);
|
8608
|
-
valueTypes.push(typeArgs[1]);
|
8638
|
+
keyTypes.push({ node: entryNode, type: typeArgs[0] });
|
8639
|
+
valueTypes.push({ node: entryNode, type: typeArgs[1] });
|
8609
8640
|
}
|
8610
8641
|
addUnknown = false;
|
8611
8642
|
}
|
@@ -8628,8 +8659,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8628
8659
|
const typeArgs = (_a = dictEntryType.tupleTypeArguments) === null || _a === void 0 ? void 0 : _a.map((t) => t.type);
|
8629
8660
|
if (typeArgs && typeArgs.length === 2) {
|
8630
8661
|
if (forceStrictInference || index < maxEntriesToUseForInference) {
|
8631
|
-
keyTypes.push(typeArgs[0]);
|
8632
|
-
valueTypes.push(typeArgs[1]);
|
8662
|
+
keyTypes.push({ node: entryNode, type: typeArgs[0] });
|
8663
|
+
valueTypes.push({ node: entryNode, type: typeArgs[1] });
|
8633
8664
|
}
|
8634
8665
|
addUnknown = false;
|
8635
8666
|
}
|
@@ -8637,8 +8668,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8637
8668
|
}
|
8638
8669
|
if (addUnknown) {
|
8639
8670
|
if (forceStrictInference || index < maxEntriesToUseForInference) {
|
8640
|
-
keyTypes.push(types_1.UnknownType.create());
|
8641
|
-
valueTypes.push(types_1.UnknownType.create());
|
8671
|
+
keyTypes.push({ node: entryNode, type: types_1.UnknownType.create() });
|
8672
|
+
valueTypes.push({ node: entryNode, type: types_1.UnknownType.create() });
|
8642
8673
|
}
|
8643
8674
|
}
|
8644
8675
|
});
|
@@ -8662,13 +8693,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8662
8693
|
});
|
8663
8694
|
effectiveExpectedType = matchingSubtype;
|
8664
8695
|
}
|
8696
|
+
let expectedTypeDiagAddendum;
|
8665
8697
|
if (effectiveExpectedType) {
|
8666
8698
|
const result = getTypeOfListOrSetExpected(node, effectiveExpectedType);
|
8667
|
-
if (result) {
|
8699
|
+
if (result && !result.typeErrors) {
|
8668
8700
|
return result;
|
8669
8701
|
}
|
8702
|
+
expectedTypeDiagAddendum = result === null || result === void 0 ? void 0 : result.expectedTypeDiagAddendum;
|
8670
8703
|
}
|
8671
|
-
|
8704
|
+
const typeResult = getTypeOfListOrSetInferred(node, /* hasExpectedType */ expectedType !== undefined);
|
8705
|
+
return { ...typeResult, expectedTypeDiagAddendum };
|
8672
8706
|
}
|
8673
8707
|
// Attempts to determine the type of a list or set statement based on an expected type.
|
8674
8708
|
// Returns undefined if that type cannot be honored.
|
@@ -8694,6 +8728,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8694
8728
|
}
|
8695
8729
|
const expectedEntryType = specializedListOrSet.typeArguments[0];
|
8696
8730
|
const entryTypes = [];
|
8731
|
+
const expectedTypeDiagAddendum = new diagnostic_1.DiagnosticAddendum();
|
8697
8732
|
node.entries.forEach((entry) => {
|
8698
8733
|
let entryTypeResult;
|
8699
8734
|
if (entry.nodeType === 32 /* ListComprehension */) {
|
@@ -8709,15 +8744,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8709
8744
|
if (entryTypeResult.typeErrors) {
|
8710
8745
|
typeErrors = true;
|
8711
8746
|
}
|
8747
|
+
if (entryTypeResult.expectedTypeDiagAddendum) {
|
8748
|
+
expectedTypeDiagAddendum.addAddendum(entryTypeResult.expectedTypeDiagAddendum);
|
8749
|
+
}
|
8712
8750
|
});
|
8713
8751
|
const isExpectedTypeListOrSet = (0, types_1.isClassInstance)(expectedType) && types_1.ClassType.isBuiltIn(expectedType, builtInClassName);
|
8714
8752
|
const specializedEntryType = inferTypeArgFromExpectedType(expectedEntryType, entryTypes,
|
8715
8753
|
/* isNarrowable */ !isExpectedTypeListOrSet);
|
8716
8754
|
if (!specializedEntryType) {
|
8717
|
-
return
|
8755
|
+
return { type: types_1.UnknownType.create(), isIncomplete, typeErrors: true, expectedTypeDiagAddendum };
|
8718
8756
|
}
|
8719
8757
|
const type = getBuiltInObject(node, builtInClassName, [specializedEntryType]);
|
8720
|
-
return { type, isIncomplete, typeErrors };
|
8758
|
+
return { type, isIncomplete, typeErrors, expectedTypeDiagAddendum };
|
8721
8759
|
}
|
8722
8760
|
// Attempts to infer the type of a list or set statement with no "expected type".
|
8723
8761
|
function getTypeOfListOrSetInferred(node, hasExpectedType) {
|
@@ -10355,6 +10393,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10355
10393
|
node.arguments.forEach((arg) => {
|
10356
10394
|
// Ignore unpacked arguments.
|
10357
10395
|
if (arg.argumentCategory !== 0 /* Simple */) {
|
10396
|
+
// Evaluate the expression's type so symbols are marked accessed
|
10397
|
+
// and errors are reported.
|
10398
|
+
getTypeOfExpression(arg.valueExpression);
|
10358
10399
|
return;
|
10359
10400
|
}
|
10360
10401
|
if (!arg.name) {
|
@@ -10622,7 +10663,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10622
10663
|
if (metaclassNode) {
|
10623
10664
|
const metaclassType = getTypeOfExpression(metaclassNode, exprFlags).type;
|
10624
10665
|
if ((0, types_1.isInstantiableClass)(metaclassType) || (0, types_1.isUnknown)(metaclassType)) {
|
10625
|
-
if ((0, typeUtils_1.requiresSpecialization)(metaclassType)) {
|
10666
|
+
if ((0, typeUtils_1.requiresSpecialization)(metaclassType, /* ignorePseudoGeneric */ true)) {
|
10626
10667
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.metaclassIsGeneric(), metaclassNode);
|
10627
10668
|
}
|
10628
10669
|
classType.details.declaredMetaclass = metaclassType;
|
@@ -10688,25 +10729,29 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10688
10729
|
// See if there's already a non-synthesized __init__ method.
|
10689
10730
|
// We shouldn't override it.
|
10690
10731
|
if (!skipSynthesizedInit) {
|
10691
|
-
const initSymbol = (0, typeUtils_1.lookUpClassMember)(classType, '__init__',
|
10732
|
+
const initSymbol = (0, typeUtils_1.lookUpClassMember)(classType, '__init__', 4 /* SkipObjectBaseClass */);
|
10692
10733
|
if (initSymbol) {
|
10693
|
-
|
10694
|
-
|
10695
|
-
if (
|
10734
|
+
if (!(0, types_1.isClass)(initSymbol.classType) || !types_1.ClassType.isBuiltIn(initSymbol.classType, 'NamedTuple')) {
|
10735
|
+
const initSymbolType = getTypeOfMember(initSymbol);
|
10736
|
+
if ((0, types_1.isFunction)(initSymbolType)) {
|
10737
|
+
if (!types_1.FunctionType.isSynthesizedMethod(initSymbolType)) {
|
10738
|
+
hasExistingInitMethod = true;
|
10739
|
+
}
|
10740
|
+
}
|
10741
|
+
else {
|
10696
10742
|
hasExistingInitMethod = true;
|
10697
10743
|
}
|
10698
10744
|
}
|
10699
|
-
else {
|
10700
|
-
hasExistingInitMethod = true;
|
10701
|
-
}
|
10702
10745
|
}
|
10703
10746
|
}
|
10704
10747
|
let skipSynthesizeHash = false;
|
10705
|
-
const hashSymbol = (0, typeUtils_1.lookUpClassMember)(classType, '__hash__',
|
10748
|
+
const hashSymbol = (0, typeUtils_1.lookUpClassMember)(classType, '__hash__', 4 /* SkipObjectBaseClass */);
|
10706
10749
|
if (hashSymbol) {
|
10707
|
-
|
10708
|
-
|
10709
|
-
|
10750
|
+
if (!(0, types_1.isClass)(hashSymbol.classType) || !types_1.ClassType.isBuiltIn(hashSymbol.classType, 'NamedTuple')) {
|
10751
|
+
const hashSymbolType = getTypeOfMember(hashSymbol);
|
10752
|
+
if ((0, types_1.isFunction)(hashSymbolType) && !types_1.FunctionType.isSynthesizedMethod(hashSymbolType)) {
|
10753
|
+
skipSynthesizeHash = true;
|
10754
|
+
}
|
10710
10755
|
}
|
10711
10756
|
}
|
10712
10757
|
(0, dataClasses_1.synthesizeDataClassMethods)(evaluatorInterface, node, classType, skipSynthesizedInit, hasExistingInitMethod, skipSynthesizeHash);
|
@@ -14276,7 +14321,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14276
14321
|
}
|
14277
14322
|
const functionNode = type.details.declaration.node;
|
14278
14323
|
const codeFlowComplexity = AnalyzerNodeInfo.getCodeFlowComplexity(functionNode);
|
14279
|
-
if (codeFlowComplexity >=
|
14324
|
+
if (codeFlowComplexity >= maxReturnCallSiteTypeInferenceCodeFlowComplexity) {
|
14280
14325
|
return undefined;
|
14281
14326
|
}
|
14282
14327
|
// If an arg hasn't been matched to a specific named parameter,
|
@@ -14592,16 +14637,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14592
14637
|
// If the source is unbounded, expand the unbounded argument to try
|
14593
14638
|
// to make the source and dest arg counts match.
|
14594
14639
|
if (srcUnboundedIndex >= 0) {
|
14595
|
-
const requiredSrcArgCount = destVariadicIndex >= 0 || destUnboundedIndex >= 0 ? destTypeArgs.length - 1 : destTypeArgs.length;
|
14596
14640
|
const typeToReplicate = srcTypeArgs.length > 0 ? srcTypeArgs[srcUnboundedIndex].type : types_1.AnyType.create();
|
14597
|
-
while (srcTypeArgs.length <
|
14598
|
-
srcTypeArgs.splice(srcUnboundedIndex, 0, { type: typeToReplicate, isUnbounded:
|
14641
|
+
while (srcTypeArgs.length < destTypeArgs.length) {
|
14642
|
+
srcTypeArgs.splice(srcUnboundedIndex, 0, { type: typeToReplicate, isUnbounded: true });
|
14599
14643
|
}
|
14600
14644
|
}
|
14601
|
-
if (destVariadicIndex >= 0 && srcUnboundedIndex >= 0) {
|
14602
|
-
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typeVarTupleRequiresKnownLength());
|
14603
|
-
return false;
|
14604
|
-
}
|
14605
14645
|
// If the dest is unbounded or contains a variadic, determine which
|
14606
14646
|
// source args map to the unbounded or variadic arg.
|
14607
14647
|
if (destUnboundedIndex >= 0 || destVariadicIndex >= 0) {
|
@@ -14614,7 +14654,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14614
14654
|
const removedArgs = srcTypeArgs.splice(destVariadicIndex, srcArgsToCapture);
|
14615
14655
|
// Package up the remaining type arguments into a tuple object.
|
14616
14656
|
const variadicTuple = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, removedArgs.map((typeArg) => {
|
14617
|
-
return { type: stripLiteralValue(typeArg.type), isUnbounded:
|
14657
|
+
return { type: stripLiteralValue(typeArg.type), isUnbounded: typeArg.isUnbounded };
|
14618
14658
|
}),
|
14619
14659
|
/* isTypeArgumentExplicit */ true,
|
14620
14660
|
/* isUnpackedTuple */ true));
|
@@ -14672,13 +14712,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14672
14712
|
// specified inheritance chain, taking into account its type arguments.
|
14673
14713
|
function assignClassWithTypeArgs(destType, srcType, inheritanceChain, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
14674
14714
|
let curSrcType = srcType;
|
14675
|
-
let curTypeVarContext = destTypeVarContext
|
14715
|
+
let curTypeVarContext = destTypeVarContext;
|
14676
14716
|
let effectiveFlags = flags;
|
14677
14717
|
inferTypeParameterVarianceForClass(destType);
|
14678
|
-
|
14718
|
+
effectiveFlags |= 8 /* SkipSolveTypeVars */;
|
14679
14719
|
if (!destTypeVarContext) {
|
14720
|
+
curTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(destType));
|
14680
14721
|
effectiveFlags &= ~8 /* SkipSolveTypeVars */;
|
14681
14722
|
}
|
14723
|
+
else {
|
14724
|
+
// If we're using the caller's type var context, don't solve the
|
14725
|
+
// type vars in this pass. We'll do this after we're done looping
|
14726
|
+
// through the inheritance chain.
|
14727
|
+
effectiveFlags |= 8 /* SkipSolveTypeVars */;
|
14728
|
+
}
|
14682
14729
|
for (let ancestorIndex = inheritanceChain.length - 1; ancestorIndex >= 0; ancestorIndex--) {
|
14683
14730
|
const ancestorType = inheritanceChain[ancestorIndex];
|
14684
14731
|
// If we've hit an "unknown", all bets are off, and we need to assume
|
@@ -14846,22 +14893,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14846
14893
|
if ((0, types_1.isUnion)(srcType) && srcType.subtypes.length === 1 && (0, types_1.isVariadicTypeVar)(srcType.subtypes[0])) {
|
14847
14894
|
srcType = srcType.subtypes[0];
|
14848
14895
|
}
|
14849
|
-
|
14850
|
-
|
14851
|
-
|
14852
|
-
|
14853
|
-
|
14854
|
-
if ((0, types_1.isTypeVar)(destType) &&
|
14855
|
-
!destType.details.isParamSpec &&
|
14856
|
-
!destType.details.isVariadic &&
|
14857
|
-
destType.scopeType === 0 /* Class */ &&
|
14858
|
-
destTypeVarContext &&
|
14859
|
-
!destTypeVarContext.isLocked() &&
|
14860
|
-
destTypeVarContext.hasSolveForScope(destType.scopeId) &&
|
14861
|
-
!destTypeVarContext.getTypeVar(destType) &&
|
14862
|
-
(flags & (8 /* SkipSolveTypeVars */ | 2 /* ReverseTypeVarMatching */)) === 0) {
|
14863
|
-
destTypeVarContext.setTypeVarType(destType, srcType);
|
14864
|
-
}
|
14896
|
+
// Handle the case where the dest and src types are the same object.
|
14897
|
+
// We can normally shortcut this and say that they are compatible,
|
14898
|
+
// but if the type includes TypeVars, we need to go through
|
14899
|
+
// the rest of the logic.
|
14900
|
+
if (destType === srcType && !(0, typeUtils_1.requiresSpecialization)(destType)) {
|
14865
14901
|
return true;
|
14866
14902
|
}
|
14867
14903
|
if (recursionCount > types_1.maxTypeRecursionCount) {
|
@@ -14917,13 +14953,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14917
14953
|
// Before performing any other checks, see if the dest type is a
|
14918
14954
|
// TypeVar that we are attempting to match.
|
14919
14955
|
if ((0, types_1.isTypeVar)(destType)) {
|
14956
|
+
if ((0, types_1.isTypeSame)(destType, srcType)) {
|
14957
|
+
if (destType.scopeId && (destTypeVarContext === null || destTypeVarContext === void 0 ? void 0 : destTypeVarContext.hasSolveForScope(destType.scopeId))) {
|
14958
|
+
return (0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, destType, srcType, diag, destTypeVarContext, flags, recursionCount);
|
14959
|
+
}
|
14960
|
+
return true;
|
14961
|
+
}
|
14920
14962
|
// If the dest is a constrained or bound type variable and all of the
|
14921
14963
|
// types in the source are conditioned on that same type variable
|
14922
14964
|
// and have compatible types, we'll consider it assignable.
|
14923
14965
|
if (assignConditionalTypeToTypeVar(destType, srcType, recursionCount)) {
|
14924
14966
|
return true;
|
14925
14967
|
}
|
14926
|
-
|
14968
|
+
// If the source is a conditional type associated with a bound TypeVar
|
14969
|
+
// and the bound TypeVar matches the condition, the types are compatible.
|
14970
|
+
const destTypeVar = destType;
|
14971
|
+
if (types_1.TypeBase.isInstantiable(destType) === types_1.TypeBase.isInstantiable(srcType) &&
|
14972
|
+
srcType.condition &&
|
14973
|
+
srcType.condition.some((cond) => {
|
14974
|
+
return !cond.isConstrainedTypeVar && cond.typeVarName === destTypeVar.nameWithScope;
|
14975
|
+
})) {
|
14927
14976
|
return true;
|
14928
14977
|
}
|
14929
14978
|
if ((0, types_1.isUnion)(srcType)) {
|
@@ -15445,7 +15494,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15445
15494
|
// For union sources, all of the types need to be assignable to the dest.
|
15446
15495
|
let isIncompatible = false;
|
15447
15496
|
// Sort the subtypes so we have a deterministic order for unions.
|
15448
|
-
(0, typeUtils_1.doForEachSubtype)(srcType, (subtype, subtypeIndex) => {
|
15497
|
+
(0, typeUtils_1.doForEachSubtype)(srcType, (subtype, subtypeIndex, allSubtypes) => {
|
15449
15498
|
if (isIncompatible) {
|
15450
15499
|
return;
|
15451
15500
|
}
|
@@ -15455,7 +15504,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15455
15504
|
// Determine if the current subtype is subsumed by another subtype
|
15456
15505
|
// in the same union. If so, we can ignore this.
|
15457
15506
|
let isSubtypeSubsumed = false;
|
15458
|
-
|
15507
|
+
allSubtypes.forEach((innerSubtype, innerSubtypeIndex) => {
|
15459
15508
|
if (isSubtypeSubsumed || subtypeIndex === innerSubtypeIndex || (0, types_1.isAnyOrUnknown)(innerSubtype)) {
|
15460
15509
|
return;
|
15461
15510
|
}
|
@@ -15796,7 +15845,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15796
15845
|
let specializedDestType = destType;
|
15797
15846
|
let reverseMatchingFailed = false;
|
15798
15847
|
if ((flags & 2 /* ReverseTypeVarMatching */) === 0) {
|
15799
|
-
specializedDestType = (0, typeUtils_1.applySolvedTypeVars)(destType, destTypeVarContext
|
15848
|
+
specializedDestType = (0, typeUtils_1.applySolvedTypeVars)(destType, destTypeVarContext,
|
15849
|
+
/* unknownIfNotFound */ undefined,
|
15850
|
+
/* useNarrowBoundOnly */ true);
|
15800
15851
|
if ((0, typeUtils_1.requiresSpecialization)(specializedDestType)) {
|
15801
15852
|
reverseMatchingFailed = !assignType(specializedSrcType, specializedDestType,
|
15802
15853
|
/* diag */ undefined, srcTypeVarContext, destTypeVarContext, (flags ^ 2 /* ReverseTypeVarMatching */) |
|
@@ -15806,7 +15857,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15806
15857
|
}
|
15807
15858
|
}
|
15808
15859
|
else {
|
15809
|
-
specializedSrcType = (0, typeUtils_1.applySolvedTypeVars)(srcType, srcTypeVarContext
|
15860
|
+
specializedSrcType = (0, typeUtils_1.applySolvedTypeVars)(srcType, srcTypeVarContext,
|
15861
|
+
/* unknownIfNotFound */ undefined,
|
15862
|
+
/* useNarrowBoundOnly */ true);
|
15810
15863
|
if ((0, typeUtils_1.requiresSpecialization)(specializedSrcType)) {
|
15811
15864
|
reverseMatchingFailed = !assignType(specializedSrcType, specializedDestType,
|
15812
15865
|
/* diag */ undefined, srcTypeVarContext, destTypeVarContext, (flags ^ 2 /* ReverseTypeVarMatching */) |
|