@zzzen/pyright-internal 1.2.0-dev.20231217 → 1.2.0-dev.20231231
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 +11 -10
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.d.ts +1 -0
- package/dist/analyzer/checker.js +34 -14
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +10 -2
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +16 -16
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/dataClasses.js +2 -9
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/enums.js +1 -1
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/namedTuples.js +1 -1
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.d.ts +1 -1
- package/dist/analyzer/operations.js +16 -1
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.d.ts +5 -1
- package/dist/analyzer/packageTypeVerifier.js +58 -40
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.js +1 -1
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/protocols.js +21 -9
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +222 -196
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +5 -5
- package/dist/analyzer/typeGuards.js +9 -8
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +11 -10
- package/dist/analyzer/typeUtils.js +25 -17
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.d.ts +0 -1
- package/dist/analyzer/typeVarContext.js +4 -5
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typedDicts.js +24 -36
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +9 -3
- package/dist/analyzer/types.js +62 -11
- package/dist/analyzer/types.js.map +1 -1
- package/dist/common/diagnostic.d.ts +1 -0
- package/dist/common/diagnostic.js +18 -1
- package/dist/common/diagnostic.js.map +1 -1
- package/dist/common/uri/webUri.js +1 -25
- package/dist/common/uri/webUri.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.js +1 -1
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/localization/localize.d.ts +21 -0
- package/dist/localization/localize.js +13 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +14 -1
- package/dist/parser/parser.js +1 -1
- package/dist/parser/parser.js.map +1 -1
- package/dist/pyright.js +13 -7
- package/dist/pyright.js.map +1 -1
- package/dist/tests/fourslash/fourslash.d.ts +1 -0
- package/dist/tests/fourslash/fourslash.js +2 -2
- package/dist/tests/fourslash/rename.parens.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/rename.parens.fourslash.js +20 -0
- package/dist/tests/fourslash/rename.parens.fourslash.js.map +1 -0
- package/dist/tests/fourslash/typeVerifier.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/typeVerifier.fourslash.js +47 -0
- package/dist/tests/fourslash/typeVerifier.fourslash.js.map +1 -0
- package/dist/tests/harness/fourslash/testState.d.ts +2 -0
- package/dist/tests/harness/fourslash/testState.js +16 -0
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +2 -2
- package/dist/tests/typeEvaluator2.test.js +11 -7
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +4 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +6 -2
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +1 -1
- package/dist/tests/typePrinter.test.js +2 -10
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/dist/tests/uri.test.js +4 -0
- package/dist/tests/uri.test.js.map +1 -1
- package/package.json +1 -1
@@ -568,7 +568,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
568
568
|
break;
|
569
569
|
}
|
570
570
|
case 55 /* ParseNodeType.UnaryOperation */: {
|
571
|
-
typeResult = (0, operations_1.getTypeOfUnaryOperation)(evaluatorInterface, node, inferenceContext);
|
571
|
+
typeResult = (0, operations_1.getTypeOfUnaryOperation)(evaluatorInterface, node, flags, inferenceContext);
|
572
572
|
break;
|
573
573
|
}
|
574
574
|
case 7 /* ParseNodeType.BinaryOperation */: {
|
@@ -706,7 +706,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
706
706
|
// Make sure the resulting type is assignable to the expected type.
|
707
707
|
if (!assignType(inferenceContext.expectedType, typeResult.type, diag,
|
708
708
|
/* destTypeVarContext */ undefined,
|
709
|
-
/* srcTypeVarContext */ undefined,
|
709
|
+
/* srcTypeVarContext */ undefined, 1024 /* AssignTypeFlags.IgnoreTypeVarScope */)) {
|
710
710
|
typeResult.typeErrors = true;
|
711
711
|
typeResult.expectedTypeDiagAddendum = diag;
|
712
712
|
diag.addTextRange(node);
|
@@ -1333,7 +1333,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1333
1333
|
!types_1.ClassType.isBuiltIn(metaclass, 'type') &&
|
1334
1334
|
!types_1.ClassType.isSameGenericClass(metaclass, objectType)) {
|
1335
1335
|
const descMemberInfo = getTypeOfClassMemberName(errorNode, metaclass, memberName, usage,
|
1336
|
-
/* diag */ undefined, flags | 512 /* MemberAccessFlags.SkipAttributeAccessOverride */, objectType);
|
1336
|
+
/* diag */ undefined, flags | 512 /* MemberAccessFlags.SkipAttributeAccessOverride */, objectType, recursionCount);
|
1337
1337
|
if (descMemberInfo) {
|
1338
1338
|
const isProperty = (0, types_1.isClassInstance)(descMemberInfo.type) && types_1.ClassType.isPropertyClass(descMemberInfo.type);
|
1339
1339
|
if ((0, typeUtils_1.isDescriptorInstance)(descMemberInfo.type, /* requireSetter */ true) || isProperty) {
|
@@ -1353,7 +1353,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1353
1353
|
}
|
1354
1354
|
subDiag = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
|
1355
1355
|
// See if the member is present in the object itself.
|
1356
|
-
memberInfo = getTypeOfClassMemberName(errorNode, objectType, memberName, usage, subDiag, effectiveFlags, selfType);
|
1356
|
+
memberInfo = getTypeOfClassMemberName(errorNode, objectType, memberName, usage, subDiag, effectiveFlags, selfType, recursionCount);
|
1357
1357
|
}
|
1358
1358
|
// If it wasn't found on the object, see if it's part of the metaclass.
|
1359
1359
|
if (!memberInfo && metaclass && (0, types_1.isInstantiableClass)(metaclass)) {
|
@@ -1398,15 +1398,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1398
1398
|
}
|
1399
1399
|
return undefined;
|
1400
1400
|
}
|
1401
|
-
function getBoundMagicMethod(classType, memberName, selfType, recursionCount = 0) {
|
1401
|
+
function getBoundMagicMethod(classType, memberName, selfType, diag, recursionCount = 0) {
|
1402
1402
|
if (recursionCount > types_1.maxTypeRecursionCount) {
|
1403
1403
|
return undefined;
|
1404
1404
|
}
|
1405
1405
|
recursionCount++;
|
1406
1406
|
const boundMethodResult = getTypeOfBoundMember(
|
1407
1407
|
/* errorNode */ undefined, classType, memberName,
|
1408
|
-
/* usage */ undefined,
|
1409
|
-
/* diag */ undefined, 16 /* MemberAccessFlags.SkipInstanceMembers */ | 512 /* MemberAccessFlags.SkipAttributeAccessOverride */, selfType, recursionCount);
|
1408
|
+
/* usage */ undefined, diag, 16 /* MemberAccessFlags.SkipInstanceMembers */ | 512 /* MemberAccessFlags.SkipAttributeAccessOverride */, selfType, recursionCount);
|
1410
1409
|
if (!boundMethodResult || boundMethodResult.typeErrors) {
|
1411
1410
|
return undefined;
|
1412
1411
|
}
|
@@ -1414,7 +1413,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1414
1413
|
return boundMethodResult.type;
|
1415
1414
|
}
|
1416
1415
|
if ((0, types_1.isClassInstance)(boundMethodResult.type)) {
|
1417
|
-
return getBoundMagicMethod(boundMethodResult.type, '__call__', selfType !== null && selfType !== void 0 ? selfType : types_1.ClassType.cloneAsInstance(classType), recursionCount);
|
1416
|
+
return getBoundMagicMethod(boundMethodResult.type, '__call__', selfType !== null && selfType !== void 0 ? selfType : types_1.ClassType.cloneAsInstance(classType), diag, recursionCount);
|
1418
1417
|
}
|
1419
1418
|
if ((0, types_1.isAnyOrUnknown)(boundMethodResult.type)) {
|
1420
1419
|
return (0, typeUtils_1.getUnknownTypeForCallable)();
|
@@ -1574,10 +1573,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1574
1573
|
const kwSeparatorIndex = functionType.details.parameters.findIndex((param) => param.category === 1 /* ParameterCategory.ArgsList */);
|
1575
1574
|
// Add a keyword separator if necessary.
|
1576
1575
|
if (kwSeparatorIndex < 0) {
|
1577
|
-
types_1.FunctionType.
|
1578
|
-
category: 1 /* ParameterCategory.ArgsList */,
|
1579
|
-
type: types_1.AnyType.create(),
|
1580
|
-
});
|
1576
|
+
types_1.FunctionType.addKeywordOnlyParameterSeparator(newFunction);
|
1581
1577
|
}
|
1582
1578
|
tdEntries.forEach((tdEntry, name) => {
|
1583
1579
|
types_1.FunctionType.addParameter(newFunction, {
|
@@ -1738,6 +1734,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1738
1734
|
specializedType.typeArguments.length > 0) {
|
1739
1735
|
return specializedType.typeArguments[0];
|
1740
1736
|
}
|
1737
|
+
return types_1.UnknownType.create();
|
1741
1738
|
}
|
1742
1739
|
}
|
1743
1740
|
if (errorNode) {
|
@@ -2350,7 +2347,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2350
2347
|
// Is this subtype a tuple?
|
2351
2348
|
const tupleType = (0, typeUtils_1.getSpecializedTupleType)(subtype);
|
2352
2349
|
if (tupleType && tupleType.tupleTypeArguments) {
|
2353
|
-
const sourceEntryTypes = tupleType.tupleTypeArguments.map((t) => (0, typeUtils_1.addConditionToType)(t.type, (0, typeUtils_1.getTypeCondition)(subtype)));
|
2350
|
+
const sourceEntryTypes = tupleType.tupleTypeArguments.map((t) => (0, typeUtils_1.addConditionToType)(t.type, (0, typeUtils_1.getTypeCondition)(subtype), /* skipSelfCondition */ true));
|
2354
2351
|
const unboundedIndex = tupleType.tupleTypeArguments.findIndex((t) => t.isUnbounded);
|
2355
2352
|
if (unboundedIndex >= 0) {
|
2356
2353
|
if (sourceEntryTypes.length < targetTypes.length) {
|
@@ -2503,7 +2500,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2503
2500
|
}
|
2504
2501
|
return subtype;
|
2505
2502
|
}
|
2506
|
-
if ((0, types_1.isTypeVar)(subtype)
|
2503
|
+
if ((0, types_1.isTypeVar)(subtype)) {
|
2507
2504
|
// If this is a recursive type alias placeholder
|
2508
2505
|
// that hasn't yet been resolved, return it as is.
|
2509
2506
|
if (subtype.details.recursiveTypeAliasName) {
|
@@ -2516,7 +2513,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2516
2513
|
subtype.details.constraints.forEach((constraintType, constraintIndex) => {
|
2517
2514
|
if (conditionFilter) {
|
2518
2515
|
const typeVarName = types_1.TypeVarType.getNameWithScope(subtype);
|
2519
|
-
const applicableConstraint = conditionFilter.find((filter) => filter.
|
2516
|
+
const applicableConstraint = conditionFilter.find((filter) => filter.typeVar.nameWithScope === typeVarName);
|
2520
2517
|
// If this type variable is being constrained to a single index,
|
2521
2518
|
// don't include the other indices.
|
2522
2519
|
if (applicableConstraint && applicableConstraint.constraintIndex !== constraintIndex) {
|
@@ -2526,13 +2523,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2526
2523
|
if (types_1.TypeBase.isInstantiable(subtype)) {
|
2527
2524
|
constraintType = (0, typeUtils_1.convertToInstantiable)(constraintType);
|
2528
2525
|
}
|
2529
|
-
typesToCombine.push((0, typeUtils_1.addConditionToType)(constraintType, [
|
2530
|
-
{
|
2531
|
-
typeVarName: types_1.TypeVarType.getNameWithScope(subtype),
|
2532
|
-
constraintIndex,
|
2533
|
-
isConstrainedTypeVar: true,
|
2534
|
-
},
|
2535
|
-
]));
|
2526
|
+
typesToCombine.push((0, typeUtils_1.addConditionToType)(constraintType, [{ typeVar: subtype, constraintIndex }]));
|
2536
2527
|
});
|
2537
2528
|
return (0, types_1.combineTypes)(typesToCombine);
|
2538
2529
|
}
|
@@ -2542,17 +2533,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2542
2533
|
// Fall back to a bound of "object" if no bound is provided.
|
2543
2534
|
let boundType = (_b = (_a = subtype.details.boundType) !== null && _a !== void 0 ? _a : objectType) !== null && _b !== void 0 ? _b : types_1.UnknownType.create();
|
2544
2535
|
boundType = types_1.TypeBase.isInstantiable(subtype) ? (0, typeUtils_1.convertToInstantiable)(boundType) : boundType;
|
2545
|
-
|
2546
|
-
if (subtype.details.isSynthesizedSelf && (0, types_1.isClass)(boundType)) {
|
2547
|
-
return types_1.ClassType.cloneIncludeSubclasses(boundType);
|
2548
|
-
}
|
2549
|
-
return (0, typeUtils_1.addConditionToType)(boundType, [
|
2550
|
-
{
|
2551
|
-
typeVarName: types_1.TypeVarType.getNameWithScope(subtype),
|
2552
|
-
constraintIndex: 0,
|
2553
|
-
isConstrainedTypeVar: false,
|
2554
|
-
},
|
2555
|
-
]);
|
2536
|
+
return (0, typeUtils_1.addConditionToType)(boundType, [{ typeVar: subtype, constraintIndex: 0 }]);
|
2556
2537
|
}
|
2557
2538
|
return subtype;
|
2558
2539
|
});
|
@@ -2584,7 +2565,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2584
2565
|
}
|
2585
2566
|
if (transformedType) {
|
2586
2567
|
// Apply the type condition if it's associated with a constrained TypeVar.
|
2587
|
-
const typeCondition = (_a = (0, typeUtils_1.getTypeCondition)(subtype)) === null || _a === void 0 ? void 0 : _a.filter((condition) => condition.
|
2568
|
+
const typeCondition = (_a = (0, typeUtils_1.getTypeCondition)(subtype)) === null || _a === void 0 ? void 0 : _a.filter((condition) => condition.typeVar.details.constraints.length > 0);
|
2588
2569
|
if (typeCondition && typeCondition.length > 0) {
|
2589
2570
|
transformedType = (0, typeUtils_1.addConditionToType)(transformedType, typeCondition);
|
2590
2571
|
}
|
@@ -2986,9 +2967,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2986
2967
|
// Verify that the name does not refer to a (non type alias) variable.
|
2987
2968
|
if (effectiveTypeInfo.includesVariableDecl && !type.typeAliasInfo) {
|
2988
2969
|
let isAllowedTypeForVariable = (0, types_1.isTypeVar)(type) || (0, typeUtils_1.isTypeAliasPlaceholder)(type);
|
2989
|
-
if ((0, types_1.isClass)(type) &&
|
2970
|
+
if ((0, types_1.isClass)(type) &&
|
2971
|
+
!type.includeSubclasses &&
|
2972
|
+
!symbol.hasTypedDeclarations() &&
|
2973
|
+
types_1.ClassType.isValidTypeAliasClass(type)) {
|
2990
2974
|
// This check exempts class types that are created by calling
|
2991
|
-
// NewType, NamedTuple,
|
2975
|
+
// NewType, NamedTuple, etc.
|
2992
2976
|
isAllowedTypeForVariable = true;
|
2993
2977
|
}
|
2994
2978
|
// Disable for assignments in the typings.pyi file, since it defines special forms.
|
@@ -3582,7 +3566,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3582
3566
|
const typeResult = (_a = (0, enums_1.getTypeOfEnumMember)(evaluatorInterface, node, baseType, memberName, isIncomplete)) !== null && _a !== void 0 ? _a : getTypeOfBoundMember(node.memberName, baseType, memberName, usage, diag,
|
3583
3567
|
/* memberAccessFlags */ undefined, baseTypeResult.bindToSelfType);
|
3584
3568
|
if (typeResult) {
|
3585
|
-
type = (0, typeUtils_1.addConditionToType)(typeResult.type, (0, typeUtils_1.getTypeCondition)(baseType)
|
3569
|
+
type = (0, typeUtils_1.addConditionToType)(typeResult.type, (0, typeUtils_1.getTypeCondition)(baseType),
|
3570
|
+
/* skipSelfCondition */ true);
|
3586
3571
|
}
|
3587
3572
|
if (typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete) {
|
3588
3573
|
isIncomplete = true;
|
@@ -4545,10 +4530,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4545
4530
|
hasDeclaredType: true,
|
4546
4531
|
});
|
4547
4532
|
});
|
4548
|
-
|
4533
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
4534
|
+
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */);
|
4549
4535
|
}
|
4550
4536
|
else if ((0, types_1.isParamSpec)(typeArgType)) {
|
4551
|
-
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, (0, typeUtils_1.convertToInstance)(typeArgType), diag, typeVarContext,
|
4537
|
+
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, (0, typeUtils_1.convertToInstance)(typeArgType), diag, typeVarContext, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */);
|
4552
4538
|
}
|
4553
4539
|
else if ((0, types_1.isInstantiableClass)(typeArgType) && types_1.ClassType.isBuiltIn(typeArgType, 'Concatenate')) {
|
4554
4540
|
const concatTypeArgs = typeArgType.typeArguments;
|
@@ -4556,12 +4542,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4556
4542
|
if (concatTypeArgs && concatTypeArgs.length > 0) {
|
4557
4543
|
concatTypeArgs.forEach((typeArg, index) => {
|
4558
4544
|
if (index === concatTypeArgs.length - 1) {
|
4559
|
-
|
4560
|
-
types_1.FunctionType.addParameter(functionType, {
|
4561
|
-
category: 0 /* ParameterCategory.Simple */,
|
4562
|
-
isNameSynthesized: false,
|
4563
|
-
type: types_1.UnknownType.create(),
|
4564
|
-
});
|
4545
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
4565
4546
|
if ((0, types_1.isParamSpec)(typeArg)) {
|
4566
4547
|
functionType.details.paramSpec = typeArg;
|
4567
4548
|
}
|
@@ -4581,7 +4562,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4581
4562
|
}
|
4582
4563
|
});
|
4583
4564
|
}
|
4584
|
-
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext,
|
4565
|
+
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, functionType, diag, typeVarContext, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */);
|
4585
4566
|
}
|
4586
4567
|
else if ((0, typeUtils_1.isEllipsisType)(typeArgType)) {
|
4587
4568
|
const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */ | 32768 /* FunctionTypeFlags.SkipArgsKwargsCompatibilityCheck */);
|
@@ -4621,7 +4602,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4621
4602
|
}
|
4622
4603
|
}
|
4623
4604
|
}
|
4624
|
-
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, typeArgType, diag, typeVarContext,
|
4605
|
+
(0, constraintSolver_1.assignTypeToTypeVar)(evaluatorInterface, param, typeArgType, diag, typeVarContext, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */);
|
4625
4606
|
}
|
4626
4607
|
});
|
4627
4608
|
if (!diag.isEmpty()) {
|
@@ -5266,6 +5247,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5266
5247
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
5267
5248
|
diag.addMessage(localize_1.Localizer.DiagnosticAddendum.useTupleInstead());
|
5268
5249
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.tupleInAnnotation() + diag.getString(), node);
|
5250
|
+
return { type: types_1.UnknownType.create() };
|
5269
5251
|
}
|
5270
5252
|
if ((flags & 128 /* EvaluatorFlags.ExpectingInstantiableType */) !== 0 &&
|
5271
5253
|
node.expressions.length === 0 &&
|
@@ -5567,11 +5549,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5567
5549
|
if (lambdaParams.length > 0) {
|
5568
5550
|
const lastParam = lambdaParams[lambdaParams.length - 1];
|
5569
5551
|
if (lastParam.category === 0 /* ParameterCategory.Simple */ && !lastParam.name) {
|
5570
|
-
types_1.FunctionType.
|
5571
|
-
category: 0 /* ParameterCategory.Simple */,
|
5572
|
-
name: '',
|
5573
|
-
type: types_1.UnknownType.create(),
|
5574
|
-
});
|
5552
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(expectedType);
|
5575
5553
|
}
|
5576
5554
|
}
|
5577
5555
|
function getLambdaType() {
|
@@ -6557,8 +6535,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6557
6535
|
}
|
6558
6536
|
if (types_1.ClassType.isBuiltIn(expandedCallType)) {
|
6559
6537
|
const className = (_a = expandedCallType.aliasName) !== null && _a !== void 0 ? _a : expandedCallType.details.name;
|
6560
|
-
// Handle
|
6561
|
-
if (
|
6538
|
+
// Handle a call to a metaclass explicitly.
|
6539
|
+
if ((0, typeUtils_1.isInstantiableMetaclass)(expandedCallType)) {
|
6562
6540
|
if (expandedCallType.typeArguments && expandedCallType.isTypeArgumentExplicit) {
|
6563
6541
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.objectNotCallable().format({
|
6564
6542
|
type: printType(expandedCallType),
|
@@ -6567,9 +6545,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6567
6545
|
}
|
6568
6546
|
// Validate the constructor arguments.
|
6569
6547
|
(0, constructors_1.validateConstructorArguments)(evaluatorInterface, errorNode, argList, expandedCallType, skipUnknownArgCheck, inferenceContext);
|
6570
|
-
|
6571
|
-
|
6572
|
-
|
6548
|
+
// The one-parameter form of "type" returns the class
|
6549
|
+
// for the specified object.
|
6550
|
+
if (expandedCallType.details.name === 'type' && argList.length === 1) {
|
6573
6551
|
const argType = getTypeOfArgument(argList[0]).type;
|
6574
6552
|
const returnType = (0, typeUtils_1.mapSubtypes)(argType, (subtype) => {
|
6575
6553
|
if ((0, types_1.isClassInstance)(subtype) ||
|
@@ -6585,9 +6563,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6585
6563
|
return { returnType };
|
6586
6564
|
}
|
6587
6565
|
if (argList.length >= 2) {
|
6588
|
-
// The two-parameter form of
|
6566
|
+
// The two-parameter form of a call to a metaclass returns a new class
|
6589
6567
|
// built from the specified base types.
|
6590
|
-
return {
|
6568
|
+
return {
|
6569
|
+
returnType: createClassFromMetaclass(errorNode, argList, expandedCallType) || types_1.AnyType.create(),
|
6570
|
+
};
|
6591
6571
|
}
|
6592
6572
|
// If the parameter to type() is not statically known,
|
6593
6573
|
// fall back to Any.
|
@@ -7661,9 +7641,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7661
7641
|
}
|
7662
7642
|
const effectiveReturnType = getFunctionEffectiveReturnType(type);
|
7663
7643
|
let effectiveExpectedType = inferenceContext.expectedType;
|
7664
|
-
let effectiveFlags =
|
7644
|
+
let effectiveFlags = 2048 /* AssignTypeFlags.PopulatingExpectedType */;
|
7665
7645
|
if ((0, typeUtils_1.containsLiteralType)(effectiveExpectedType, /* includeTypeArgs */ true)) {
|
7666
|
-
effectiveFlags |=
|
7646
|
+
effectiveFlags |= 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
|
7667
7647
|
}
|
7668
7648
|
// If the expected type is a union, we don't know which type is expected.
|
7669
7649
|
// We may or may not be able to make use of the expected type. We'll evaluate
|
@@ -7719,7 +7699,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7719
7699
|
effectiveExpectedType = (0, typeUtils_1.applySolvedTypeVars)(genericReturnType, tempTypeVarContext, {
|
7720
7700
|
unknownIfNotFound: true,
|
7721
7701
|
});
|
7722
|
-
effectiveFlags |=
|
7702
|
+
effectiveFlags |= 4096 /* AssignTypeFlags.SkipPopulateUnknownExpectedType */;
|
7723
7703
|
}
|
7724
7704
|
}
|
7725
7705
|
}
|
@@ -7979,11 +7959,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7979
7959
|
!arg0Type.details.isParamSpec &&
|
7980
7960
|
arg0Type.details.constraints.length === 0) {
|
7981
7961
|
typeGuardType = (0, typeUtils_1.addConditionToType)(typeGuardType, [
|
7982
|
-
{
|
7983
|
-
typeVarName: types_1.TypeVarType.getNameWithScope(arg0Type),
|
7984
|
-
constraintIndex: 0,
|
7985
|
-
isConstrainedTypeVar: false,
|
7986
|
-
},
|
7962
|
+
{ typeVar: arg0Type, constraintIndex: 0 },
|
7987
7963
|
]);
|
7988
7964
|
}
|
7989
7965
|
}
|
@@ -8729,7 +8705,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8729
8705
|
if (errorNode.parent.nodeType !== 3 /* ParseNodeType.Assignment */ ||
|
8730
8706
|
errorNode.parent.rightExpression !== errorNode ||
|
8731
8707
|
errorNode.parent.leftExpression.nodeType !== 38 /* ParseNodeType.Name */) {
|
8732
|
-
|
8708
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasTypeMustBeAssigned(), errorNode);
|
8733
8709
|
return undefined;
|
8734
8710
|
}
|
8735
8711
|
const nameNode = errorNode.parent.leftExpression;
|
@@ -8737,11 +8713,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8737
8713
|
if (firstArg.valueExpression && firstArg.valueExpression.nodeType === 48 /* ParseNodeType.StringList */) {
|
8738
8714
|
const typeAliasName = firstArg.valueExpression.strings.map((s) => s.value).join('');
|
8739
8715
|
if (typeAliasName !== nameNode.value) {
|
8740
|
-
|
8716
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasTypeNameMismatch(), firstArg.valueExpression);
|
8741
8717
|
}
|
8742
8718
|
}
|
8743
8719
|
else {
|
8744
|
-
|
8720
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasTypeNameArg(), firstArg.valueExpression || errorNode);
|
8745
8721
|
return undefined;
|
8746
8722
|
}
|
8747
8723
|
let valueExpr;
|
@@ -8796,12 +8772,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8796
8772
|
}
|
8797
8773
|
});
|
8798
8774
|
if (!isTypeParamListValid) {
|
8799
|
-
|
8775
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeAliasTypeParamInvalid(), typeParamsExpr);
|
8800
8776
|
return undefined;
|
8801
8777
|
}
|
8802
8778
|
}
|
8803
8779
|
return getTypeOfTypeAliasCommon(nameNode, nameNode, valueExpr,
|
8804
|
-
/* isPep695Syntax */
|
8780
|
+
/* isPep695Syntax */ true,
|
8805
8781
|
/* typeParamNodes */ undefined, () => typeParameters);
|
8806
8782
|
}
|
8807
8783
|
function getBooleanValue(node) {
|
@@ -8813,7 +8789,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8813
8789
|
return true;
|
8814
8790
|
}
|
8815
8791
|
}
|
8816
|
-
|
8792
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.expectedBoolLiteral(), node);
|
8817
8793
|
return false;
|
8818
8794
|
}
|
8819
8795
|
function getFunctionFullName(functionNode, moduleName, functionName) {
|
@@ -8833,7 +8809,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8833
8809
|
// in the Python specification: The static type checker will treat
|
8834
8810
|
// the new type as if it were a subclass of the original type.
|
8835
8811
|
function createNewType(errorNode, argList) {
|
8836
|
-
var _a;
|
8812
|
+
var _a, _b, _c;
|
8837
8813
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
8838
8814
|
let className = '';
|
8839
8815
|
if (argList.length !== 2) {
|
@@ -8850,26 +8826,36 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8850
8826
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.newTypeBadName(), (_a = argList[0].node) !== null && _a !== void 0 ? _a : errorNode);
|
8851
8827
|
return undefined;
|
8852
8828
|
}
|
8829
|
+
if (((_b = errorNode.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 3 /* ParseNodeType.Assignment */ &&
|
8830
|
+
errorNode.parent.leftExpression.nodeType === 38 /* ParseNodeType.Name */ &&
|
8831
|
+
errorNode.parent.leftExpression.value !== className) {
|
8832
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.newTypeNameMismatch(), errorNode.parent.leftExpression);
|
8833
|
+
return undefined;
|
8834
|
+
}
|
8853
8835
|
let baseClass = getTypeOfArgumentExpectingType(argList[1]).type;
|
8854
8836
|
let isBaseClassAny = false;
|
8855
8837
|
if ((0, types_1.isAnyOrUnknown)(baseClass)) {
|
8856
8838
|
if (objectType && (0, types_1.isClassInstance)(objectType)) {
|
8857
8839
|
baseClass = types_1.ClassType.cloneAsInstantiable(objectType);
|
8858
8840
|
}
|
8841
|
+
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.newTypeAnyOrUnknown(), (_c = argList[1].node) !== null && _c !== void 0 ? _c : errorNode);
|
8859
8842
|
isBaseClassAny = true;
|
8860
8843
|
}
|
8861
8844
|
if (!(0, types_1.isInstantiableClass)(baseClass)) {
|
8862
8845
|
addError(localize_1.Localizer.Diagnostic.newTypeNotAClass(), argList[1].node || errorNode);
|
8863
8846
|
return undefined;
|
8864
8847
|
}
|
8865
|
-
if (types_1.ClassType.isProtocolClass(baseClass)) {
|
8848
|
+
if (types_1.ClassType.isProtocolClass(baseClass) || types_1.ClassType.isTypedDictClass(baseClass)) {
|
8866
8849
|
addError(localize_1.Localizer.Diagnostic.newTypeProtocolClass(), argList[1].node || errorNode);
|
8867
8850
|
}
|
8868
8851
|
else if (baseClass.literalValue !== undefined) {
|
8869
8852
|
addError(localize_1.Localizer.Diagnostic.newTypeLiteral(), argList[1].node || errorNode);
|
8870
8853
|
}
|
8854
|
+
else if (types_1.ClassType.isNewTypeClass(baseClass)) {
|
8855
|
+
addError(localize_1.Localizer.Diagnostic.newTypeNewTypeClass(), argList[1].node || errorNode);
|
8856
|
+
}
|
8871
8857
|
let classFlags = baseClass.details.flags & ~(1 /* ClassTypeFlags.BuiltInClass */ | 2 /* ClassTypeFlags.SpecialBuiltIn */);
|
8872
|
-
classFlags |= 4096 /* ClassTypeFlags.Final */;
|
8858
|
+
classFlags |= 4096 /* ClassTypeFlags.Final */ | 268435456 /* ClassTypeFlags.NewTypeClass */ | 536870912 /* ClassTypeFlags.ValidTypeAliasClass */;
|
8873
8859
|
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, classFlags, ParseTreeUtils.getTypeSourceId(errorNode),
|
8874
8860
|
/* declaredMetaclass */ undefined, baseClass.details.effectiveMetaclass);
|
8875
8861
|
classType.details.baseClasses.push(isBaseClassAny ? types_1.AnyType.create() : baseClass);
|
@@ -8906,7 +8892,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8906
8892
|
return classType;
|
8907
8893
|
}
|
8908
8894
|
// Implements the semantics of the multi-parameter variant of the "type" call.
|
8909
|
-
function
|
8895
|
+
function createClassFromMetaclass(errorNode, argList, metaclass) {
|
8910
8896
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
8911
8897
|
const arg0Type = getTypeOfArgument(argList[0]).type;
|
8912
8898
|
if (!(0, types_1.isClassInstance)(arg0Type) || !types_1.ClassType.isBuiltIn(arg0Type, 'str')) {
|
@@ -8914,10 +8900,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8914
8900
|
}
|
8915
8901
|
const className = arg0Type.literalValue || '_';
|
8916
8902
|
const arg1Type = getTypeOfArgument(argList[1]).type;
|
8903
|
+
// TODO - properly handle case where tuple of base classes is provided.
|
8917
8904
|
if (!(0, types_1.isClassInstance)(arg1Type) || !(0, typeUtils_1.isTupleClass)(arg1Type) || arg1Type.tupleTypeArguments === undefined) {
|
8918
8905
|
return undefined;
|
8919
8906
|
}
|
8920
|
-
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri,
|
8907
|
+
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, 536870912 /* ClassTypeFlags.ValidTypeAliasClass */, ParseTreeUtils.getTypeSourceId(errorNode),
|
8921
8908
|
/* declaredMetaclass */ undefined, arg1Type.details.effectiveMetaclass);
|
8922
8909
|
arg1Type.tupleTypeArguments.forEach((typeArg) => {
|
8923
8910
|
const specializedType = makeTopLevelTypeVarsConcrete(typeArg.type);
|
@@ -8955,14 +8942,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8955
8942
|
}
|
8956
8943
|
return { type: type !== null && type !== void 0 ? type : types_1.UnknownType.create() };
|
8957
8944
|
}
|
8958
|
-
function getTypeOfMagicMethodCall(objType, methodName, argList, errorNode, inferenceContext) {
|
8945
|
+
function getTypeOfMagicMethodCall(objType, methodName, argList, errorNode, inferenceContext, diag) {
|
8959
8946
|
let magicMethodSupported = true;
|
8960
8947
|
// Create a helper lambda for object subtypes.
|
8961
8948
|
const handleSubtype = (subtype) => {
|
8962
8949
|
let magicMethodType;
|
8963
8950
|
const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
|
8964
8951
|
if ((0, types_1.isClass)(concreteSubtype)) {
|
8965
|
-
magicMethodType = getBoundMagicMethod(concreteSubtype, methodName, subtype);
|
8952
|
+
magicMethodType = getBoundMagicMethod(concreteSubtype, methodName, subtype, diag);
|
8966
8953
|
}
|
8967
8954
|
if (magicMethodType) {
|
8968
8955
|
const functionArgs = argList.map((arg) => {
|
@@ -9302,7 +9289,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9302
9289
|
mappingType = (0, typeUtils_1.selfSpecializeClass)(mappingType);
|
9303
9290
|
if (assignType(types_1.ClassType.cloneAsInstance(mappingType), unexpandedType,
|
9304
9291
|
/* diag */ undefined, mappingTypeVarContext,
|
9305
|
-
/* srcTypeVarContext */ undefined,
|
9292
|
+
/* srcTypeVarContext */ undefined, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */)) {
|
9306
9293
|
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(mappingType, mappingTypeVarContext);
|
9307
9294
|
const typeArgs = specializedMapping.typeArguments;
|
9308
9295
|
if (typeArgs && typeArgs.length >= 2) {
|
@@ -9730,11 +9717,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9730
9717
|
if (paramsArePositionOnly &&
|
9731
9718
|
!isImplicitPositionOnlyParam &&
|
9732
9719
|
functionType.details.parameters.length > 0) {
|
9733
|
-
|
9734
|
-
types_1.FunctionType.addParameter(functionType, {
|
9735
|
-
category: 0 /* ParameterCategory.Simple */,
|
9736
|
-
type: types_1.UnknownType.create(),
|
9737
|
-
});
|
9720
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
9738
9721
|
}
|
9739
9722
|
if (!isImplicitPositionOnlyParam) {
|
9740
9723
|
paramsArePositionOnly = false;
|
@@ -9751,11 +9734,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9751
9734
|
types_1.FunctionType.addParameter(functionType, functionParam);
|
9752
9735
|
});
|
9753
9736
|
if (paramsArePositionOnly && functionType.details.parameters.length > 0) {
|
9754
|
-
|
9755
|
-
types_1.FunctionType.addParameter(functionType, {
|
9756
|
-
category: 0 /* ParameterCategory.Simple */,
|
9757
|
-
type: types_1.UnknownType.create(),
|
9758
|
-
});
|
9737
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
9759
9738
|
}
|
9760
9739
|
let typeErrors = false;
|
9761
9740
|
// If we're speculatively evaluating the lambda, create another speculative
|
@@ -10066,12 +10045,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10066
10045
|
});
|
10067
10046
|
});
|
10068
10047
|
if (typeList.length > 0) {
|
10069
|
-
|
10070
|
-
types_1.FunctionType.addParameter(functionType, {
|
10071
|
-
category: 0 /* ParameterCategory.Simple */,
|
10072
|
-
isNameSynthesized: false,
|
10073
|
-
type: types_1.UnknownType.create(),
|
10074
|
-
});
|
10048
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
10075
10049
|
}
|
10076
10050
|
}
|
10077
10051
|
else if ((0, typeUtils_1.isEllipsisType)(typeArgs[0].type)) {
|
@@ -10087,12 +10061,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10087
10061
|
if (concatTypeArgs && concatTypeArgs.length > 0) {
|
10088
10062
|
concatTypeArgs.forEach((typeArg, index) => {
|
10089
10063
|
if (index === concatTypeArgs.length - 1) {
|
10090
|
-
|
10091
|
-
types_1.FunctionType.addParameter(functionType, {
|
10092
|
-
category: 0 /* ParameterCategory.Simple */,
|
10093
|
-
isNameSynthesized: false,
|
10094
|
-
type: types_1.UnknownType.create(),
|
10095
|
-
});
|
10064
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
10096
10065
|
if ((0, types_1.isParamSpec)(typeArg)) {
|
10097
10066
|
functionType.details.paramSpec = typeArg;
|
10098
10067
|
}
|
@@ -10327,14 +10296,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10327
10296
|
name: classType.details.name,
|
10328
10297
|
}), (_a = typeArgs[0].node) !== null && _a !== void 0 ? _a : errorNode);
|
10329
10298
|
}
|
10330
|
-
|
10299
|
+
let enclosingClass = ParseTreeUtils.getEnclosingClass(errorNode);
|
10300
|
+
// If `Self` appears anywhere outside of the class body (e.g. a decorator,
|
10301
|
+
// base class list, metaclass argument, type parameter list), it is
|
10302
|
+
// considered illegal.
|
10303
|
+
if (enclosingClass && !ParseTreeUtils.isNodeContainedWithin(errorNode, enclosingClass.suite)) {
|
10304
|
+
enclosingClass = undefined;
|
10305
|
+
}
|
10331
10306
|
const enclosingClassTypeResult = enclosingClass ? getTypeOfClass(enclosingClass) : undefined;
|
10332
10307
|
if (!enclosingClassTypeResult) {
|
10333
|
-
if ((flags & 256 /* EvaluatorFlags.ExpectingTypeAnnotation */) !== 0) {
|
10308
|
+
if ((flags & (256 /* EvaluatorFlags.ExpectingTypeAnnotation */ | 128 /* EvaluatorFlags.ExpectingInstantiableType */)) !== 0) {
|
10334
10309
|
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.selfTypeContext(), errorNode);
|
10335
10310
|
}
|
10336
10311
|
return types_1.UnknownType.create();
|
10337
10312
|
}
|
10313
|
+
else if ((0, typeUtils_1.isInstantiableMetaclass)(enclosingClassTypeResult.classType)) {
|
10314
|
+
// If `Self` appears within a metaclass, it is considered illegal.
|
10315
|
+
addDiagnostic(fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.selfTypeMetaclass(), errorNode);
|
10316
|
+
return types_1.UnknownType.create();
|
10317
|
+
}
|
10338
10318
|
const enclosingFunction = ParseTreeUtils.getEnclosingFunction(errorNode);
|
10339
10319
|
if (enclosingFunction) {
|
10340
10320
|
const functionInfo = (0, decorators_1.getFunctionInfoFromDecorators)(evaluatorInterface, enclosingFunction,
|
@@ -10504,6 +10484,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10504
10484
|
if (!typeArgs || typeArgs.length === 0) {
|
10505
10485
|
return { type: types_1.AnyType.create() };
|
10506
10486
|
}
|
10487
|
+
if (typeArgs[0].typeList) {
|
10488
|
+
addError(localize_1.Localizer.Diagnostic.typeArgListNotAllowed(), typeArgs[0].node);
|
10489
|
+
}
|
10507
10490
|
return {
|
10508
10491
|
type: types_1.TypeBase.cloneForAnnotated(typeArgs[0].type),
|
10509
10492
|
isReadOnly: typeArgs[0].isReadOnly,
|
@@ -11113,11 +11096,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11113
11096
|
setSymbolResolutionPartialType(typeAliasSymbol.symbol, typeAliasDecl, typeAliasTypeVar);
|
11114
11097
|
}
|
11115
11098
|
const typeParameters = getTypeParamCallback();
|
11116
|
-
typeAliasTypeVar.details.recursiveTypeParameters = typeParameters;
|
11117
|
-
|
11118
|
-
|
11119
|
-
|
11120
|
-
|
11099
|
+
typeAliasTypeVar.details.recursiveTypeParameters = typeParameters || [];
|
11100
|
+
const aliasTypeResult = getTypeOfExpressionExpectingType(valueNode, {
|
11101
|
+
allowForwardReference: true,
|
11102
|
+
enforceTypeAnnotationRules: true,
|
11103
|
+
});
|
11121
11104
|
let isIncomplete = false;
|
11122
11105
|
let aliasType = aliasTypeResult.type;
|
11123
11106
|
if (aliasTypeResult.isIncomplete) {
|
@@ -11382,6 +11365,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11382
11365
|
}
|
11383
11366
|
}
|
11384
11367
|
}
|
11368
|
+
else if (types_1.ClassType.isTypedDictClass(classType)) {
|
11369
|
+
if (arg.name.value === 'total') {
|
11370
|
+
// The "total" and "readonly" parameters apply only for TypedDict classes.
|
11371
|
+
// PEP 589 specifies that the parameter must be either True or False.
|
11372
|
+
const constArgValue = (0, staticExpressions_1.evaluateStaticBoolExpression)(arg.valueExpression, fileInfo.executionEnvironment, fileInfo.definedConstants);
|
11373
|
+
if (constArgValue === undefined) {
|
11374
|
+
addError(localize_1.Localizer.Diagnostic.typedDictBoolParam().format({ name: arg.name.value }), arg.valueExpression);
|
11375
|
+
}
|
11376
|
+
else if (arg.name.value === 'total' && !constArgValue) {
|
11377
|
+
classType.details.flags |= 256 /* ClassTypeFlags.CanOmitDictValues */;
|
11378
|
+
}
|
11379
|
+
}
|
11380
|
+
else {
|
11381
|
+
addError(localize_1.Localizer.Diagnostic.typedDictInitsubclassParameter().format({ name: arg.name.value }), arg);
|
11382
|
+
}
|
11383
|
+
}
|
11385
11384
|
else if (arg.name.value === 'metaclass') {
|
11386
11385
|
if (metaclassNode) {
|
11387
11386
|
addError(localize_1.Localizer.Diagnostic.metaclassDuplicate(), arg);
|
@@ -11390,17 +11389,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11390
11389
|
metaclassNode = arg.valueExpression;
|
11391
11390
|
}
|
11392
11391
|
}
|
11393
|
-
else if (types_1.ClassType.isTypedDictClass(classType) && arg.name.value === 'total') {
|
11394
|
-
// The "total" and "readonly" parameters apply only for TypedDict classes.
|
11395
|
-
// PEP 589 specifies that the parameter must be either True or False.
|
11396
|
-
const constArgValue = (0, staticExpressions_1.evaluateStaticBoolExpression)(arg.valueExpression, fileInfo.executionEnvironment, fileInfo.definedConstants);
|
11397
|
-
if (constArgValue === undefined) {
|
11398
|
-
addError(localize_1.Localizer.Diagnostic.typedDictBoolParam().format({ name: arg.name.value }), arg.valueExpression);
|
11399
|
-
}
|
11400
|
-
else if (arg.name.value === 'total' && !constArgValue) {
|
11401
|
-
classType.details.flags |= 256 /* ClassTypeFlags.CanOmitDictValues */;
|
11402
|
-
}
|
11403
|
-
}
|
11404
11392
|
else {
|
11405
11393
|
// Collect arguments that will be passed to the `__init_subclass__`
|
11406
11394
|
// method described in PEP 487.
|
@@ -12291,11 +12279,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12291
12279
|
if (paramsArePositionOnly &&
|
12292
12280
|
!isImplicitPositionOnlyParam &&
|
12293
12281
|
functionType.details.parameters.length > firstNonClsSelfParamIndex) {
|
12294
|
-
|
12295
|
-
types_1.FunctionType.addParameter(functionType, {
|
12296
|
-
category: 0 /* ParameterCategory.Simple */,
|
12297
|
-
type: types_1.UnknownType.create(),
|
12298
|
-
});
|
12282
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
12299
12283
|
}
|
12300
12284
|
if (!isImplicitPositionOnlyParam) {
|
12301
12285
|
paramsArePositionOnly = false;
|
@@ -12334,11 +12318,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12334
12318
|
}
|
12335
12319
|
});
|
12336
12320
|
if (paramsArePositionOnly && functionType.details.parameters.length > firstNonClsSelfParamIndex) {
|
12337
|
-
|
12338
|
-
types_1.FunctionType.addParameter(functionType, {
|
12339
|
-
category: 0 /* ParameterCategory.Simple */,
|
12340
|
-
type: types_1.UnknownType.create(),
|
12341
|
-
});
|
12321
|
+
types_1.FunctionType.addPositionOnlyParameterSeparator(functionType);
|
12342
12322
|
}
|
12343
12323
|
// Update the types for the nodes associated with the parameters.
|
12344
12324
|
paramTypes.forEach((paramType, index) => {
|
@@ -12934,7 +12914,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12934
12914
|
const additionalHelp = new diagnostic_1.DiagnosticAddendum();
|
12935
12915
|
if ((0, types_1.isClass)(subtype)) {
|
12936
12916
|
let enterType = getTypeOfMagicMethodCall(subtype, enterMethodName, [], node.expression,
|
12937
|
-
/* inferenceContext */ undefined);
|
12917
|
+
/* inferenceContext */ undefined, additionalHelp.createAddendum());
|
12938
12918
|
if (enterType) {
|
12939
12919
|
// For "async while", an implicit "await" is performed.
|
12940
12920
|
if (isAsync) {
|
@@ -14524,7 +14504,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14524
14504
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
14525
14505
|
// Determine if this node is within a quoted type annotation.
|
14526
14506
|
const isWithinTypeAnnotation = ParseTreeUtils.isWithinTypeAnnotation(node, !(0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(AnalyzerNodeInfo.getFileInfo(node)));
|
14527
|
-
|
14507
|
+
// Determine if this is part of a "type" statement.
|
14508
|
+
const isWithinTypeAliasStatement = !!ParseTreeUtils.getParentNodeOfType(node, 77 /* ParseNodeType.TypeAlias */);
|
14509
|
+
const allowForwardReferences = isWithinTypeAnnotation || isWithinTypeAliasStatement || fileInfo.isStubFile;
|
14528
14510
|
let symbol;
|
14529
14511
|
const typeParamSymbol = AnalyzerNodeInfo.getTypeParameterSymbol(node);
|
14530
14512
|
if (typeParamSymbol) {
|
@@ -15009,30 +14991,32 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15009
14991
|
}
|
15010
14992
|
// Returns the type of the symbol. If the type is explicitly declared, that type
|
15011
14993
|
// is returned. If not, the type is inferred from assignments to the symbol. All
|
15012
|
-
// assigned types are evaluated and combined into a union.
|
15013
|
-
// node is specified, only declarations that are outside of the current execution
|
15014
|
-
// scope or that are reachable (as determined by code flow analysis) are considered.
|
15015
|
-
// This helps in cases where there are cyclical dependencies between symbols.
|
14994
|
+
// assigned types are evaluated and combined into a union.
|
15016
14995
|
function getEffectiveTypeOfSymbol(symbol) {
|
15017
14996
|
return getEffectiveTypeOfSymbolForUsage(symbol).type;
|
15018
14997
|
}
|
14998
|
+
// If a "usageNode" node is specified, only declarations that are outside
|
14999
|
+
// of the current execution scope or that are reachable (as determined by
|
15000
|
+
// code flow analysis) are considered. This helps in cases where there
|
15001
|
+
// are cyclical dependencies between symbols.
|
15019
15002
|
function getEffectiveTypeOfSymbolForUsage(symbol, usageNode, useLastDecl = false) {
|
15020
15003
|
let declaredTypeInfo;
|
15021
15004
|
// If there's a declared type, it takes precedence over inferred types.
|
15022
15005
|
if (symbol.hasTypedDeclarations()) {
|
15023
15006
|
declaredTypeInfo = getDeclaredTypeOfSymbol(symbol, usageNode);
|
15024
15007
|
const declaredType = declaredTypeInfo === null || declaredTypeInfo === void 0 ? void 0 : declaredTypeInfo.type;
|
15025
|
-
|
15026
|
-
if (declaredType
|
15027
|
-
|
15028
|
-
|
15029
|
-
if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
|
15030
|
-
isIncomplete = true;
|
15031
|
-
}
|
15032
|
-
else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
|
15033
|
-
isIncomplete = true;
|
15034
|
-
}
|
15008
|
+
let isIncomplete = false;
|
15009
|
+
if (declaredType) {
|
15010
|
+
if ((0, types_1.isFunction)(declaredType) && types_1.FunctionType.isPartiallyEvaluated(declaredType)) {
|
15011
|
+
isIncomplete = true;
|
15035
15012
|
}
|
15013
|
+
else if ((0, types_1.isClass)(declaredType) && types_1.ClassType.isPartiallyEvaluated(declaredType)) {
|
15014
|
+
isIncomplete = true;
|
15015
|
+
}
|
15016
|
+
}
|
15017
|
+
// If the "declared" type uses a "TypeAlias" type annotation, then
|
15018
|
+
// we need to use the inferred type path to evaluate its type.
|
15019
|
+
if (declaredType || !declaredTypeInfo.isTypeAlias) {
|
15036
15020
|
const typedDecls = symbol.getTypedDeclarations();
|
15037
15021
|
const result = {
|
15038
15022
|
type: declaredType !== null && declaredType !== void 0 ? declaredType : types_1.UnknownType.create(),
|
@@ -15045,6 +15029,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15045
15029
|
return result;
|
15046
15030
|
}
|
15047
15031
|
}
|
15032
|
+
return inferTypeOfSymbolForUsage(symbol, usageNode, useLastDecl);
|
15033
|
+
}
|
15034
|
+
function inferTypeOfSymbolForUsage(symbol, usageNode, useLastDecl = false) {
|
15048
15035
|
// Look in the inferred type cache to see if we've computed this already.
|
15049
15036
|
let cacheEntries = effectiveTypeCache.get(symbol.id);
|
15050
15037
|
const usageNodeId = usageNode ? usageNode.id : undefined;
|
@@ -15092,9 +15079,25 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15092
15079
|
}
|
15093
15080
|
// Determine which declarations to use for inference.
|
15094
15081
|
const declsToConsider = [];
|
15082
|
+
let includesVariableDecl = false;
|
15083
|
+
let includesIllegalTypeAliasDecl = false;
|
15095
15084
|
let sawExplicitTypeAlias = false;
|
15096
15085
|
decls.forEach((decl, index) => {
|
15097
15086
|
var _a, _b, _c;
|
15087
|
+
const resolvedDecl = (_a = resolveAliasDeclaration(decl, /* resolveLocalNames */ true, {
|
15088
|
+
allowExternallyHiddenAccess: AnalyzerNodeInfo.getFileInfo(decl.node).isStubFile,
|
15089
|
+
})) !== null && _a !== void 0 ? _a : decl;
|
15090
|
+
if (!isPossibleTypeAliasDeclaration(resolvedDecl)) {
|
15091
|
+
includesIllegalTypeAliasDecl = true;
|
15092
|
+
}
|
15093
|
+
if (resolvedDecl.type === 1 /* DeclarationType.Variable */) {
|
15094
|
+
// Exempt typing.pyi, which uses variables to define some
|
15095
|
+
// special forms like Any.
|
15096
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(resolvedDecl.node);
|
15097
|
+
if (!fileInfo.isTypingStubFile) {
|
15098
|
+
includesVariableDecl = true;
|
15099
|
+
}
|
15100
|
+
}
|
15098
15101
|
if (declIndexToConsider !== undefined && declIndexToConsider !== index) {
|
15099
15102
|
return;
|
15100
15103
|
}
|
@@ -15114,18 +15117,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15114
15117
|
if (usageNode !== undefined) {
|
15115
15118
|
if (decl.type !== 8 /* DeclarationType.Alias */) {
|
15116
15119
|
// Is the declaration in the same execution scope as the "usageNode" node?
|
15120
|
+
// If so, we can skip it because code flow analysis will allow us
|
15121
|
+
// to determine the type in this context.
|
15117
15122
|
const usageScope = ParseTreeUtils.getExecutionScopeNode(usageNode);
|
15118
15123
|
const declScope = ParseTreeUtils.getExecutionScopeNode(decl.node);
|
15119
15124
|
if (usageScope === declScope) {
|
15120
|
-
|
15121
|
-
return;
|
15122
|
-
}
|
15125
|
+
return;
|
15123
15126
|
}
|
15124
15127
|
}
|
15125
15128
|
}
|
15126
|
-
const resolvedDecl = (_a = resolveAliasDeclaration(decl, /* resolveLocalNames */ true, {
|
15127
|
-
allowExternallyHiddenAccess: AnalyzerNodeInfo.getFileInfo(decl.node).isStubFile,
|
15128
|
-
})) !== null && _a !== void 0 ? _a : decl;
|
15129
15129
|
const isExplicitTypeAlias = isExplicitTypeAliasDeclaration(resolvedDecl);
|
15130
15130
|
const isTypeAlias = isExplicitTypeAlias || isPossibleTypeAliasOrTypedDict(resolvedDecl);
|
15131
15131
|
if (isExplicitTypeAlias) {
|
@@ -15141,6 +15141,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15141
15141
|
declsToConsider.push(resolvedDecl);
|
15142
15142
|
});
|
15143
15143
|
const result = getTypeOfSymbolForDecls(symbol, declsToConsider, effectiveTypeCacheKey);
|
15144
|
+
result.includesVariableDecl = includesVariableDecl;
|
15145
|
+
result.includesIllegalTypeAliasDecl = includesIllegalTypeAliasDecl;
|
15144
15146
|
// Add the result to the effective type cache if it doesn't include speculative results.
|
15145
15147
|
if (!result.includesSpeculativeResult) {
|
15146
15148
|
addToEffectiveTypeCache(result);
|
@@ -15161,7 +15163,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15161
15163
|
const typesToCombine = [];
|
15162
15164
|
let isIncomplete = false;
|
15163
15165
|
let sawPendingEvaluation = false;
|
15164
|
-
let includesVariableDecl = false;
|
15165
15166
|
let includesSpeculativeResult = false;
|
15166
15167
|
decls.forEach((decl) => {
|
15167
15168
|
if (pushSymbolResolution(symbol, decl)) {
|
@@ -15172,12 +15173,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15172
15173
|
}
|
15173
15174
|
if (type) {
|
15174
15175
|
if (decl.type === 1 /* DeclarationType.Variable */) {
|
15175
|
-
// Exempt typing.pyi, which uses variables to define some
|
15176
|
-
// special forms like Any.
|
15177
|
-
const fileInfo = AnalyzerNodeInfo.getFileInfo(decl.node);
|
15178
|
-
if (!fileInfo.isTypingStubFile) {
|
15179
|
-
includesVariableDecl = true;
|
15180
|
-
}
|
15181
15176
|
let isConstant = false;
|
15182
15177
|
if (decl.type === 1 /* DeclarationType.Variable */) {
|
15183
15178
|
if (decl.isConstant || isFinalVariableDeclaration(decl)) {
|
@@ -15239,15 +15234,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15239
15234
|
else {
|
15240
15235
|
type = types_1.UnboundType.create();
|
15241
15236
|
}
|
15242
|
-
return {
|
15243
|
-
type,
|
15244
|
-
isIncomplete,
|
15245
|
-
includesVariableDecl,
|
15246
|
-
includesIllegalTypeAliasDecl: !decls.every((decl) => isPossibleTypeAliasDeclaration(decl)),
|
15247
|
-
includesSpeculativeResult,
|
15248
|
-
isRecursiveDefinition: false,
|
15249
|
-
evaluationAttempts,
|
15250
|
-
};
|
15237
|
+
return { type, isIncomplete, includesSpeculativeResult, evaluationAttempts };
|
15251
15238
|
}
|
15252
15239
|
// If a declaration has an explicit type (e.g. a variable with an annotation),
|
15253
15240
|
// this function evaluates the type and returns it. If the symbol has no
|
@@ -16071,16 +16058,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16071
16058
|
let errorSource;
|
16072
16059
|
let includeDiagAddendum = true;
|
16073
16060
|
if (variance === 3 /* Variance.Covariant */) {
|
16074
|
-
effectiveFlags = flags |
|
16061
|
+
effectiveFlags = flags | 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
|
16075
16062
|
errorSource = localize_1.Localizer.DiagnosticAddendum.typeVarIsCovariant;
|
16076
16063
|
}
|
16077
16064
|
else if (variance === 4 /* Variance.Contravariant */) {
|
16078
16065
|
effectiveFlags =
|
16079
|
-
(flags ^ 2 /* AssignTypeFlags.ReverseTypeVarMatching */) |
|
16066
|
+
(flags ^ 2 /* AssignTypeFlags.ReverseTypeVarMatching */) | 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
|
16080
16067
|
errorSource = localize_1.Localizer.DiagnosticAddendum.typeVarIsContravariant;
|
16081
16068
|
}
|
16082
16069
|
else {
|
16083
|
-
effectiveFlags = flags | 1 /* AssignTypeFlags.EnforceInvariance */ |
|
16070
|
+
effectiveFlags = flags | 1 /* AssignTypeFlags.EnforceInvariance */ | 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
|
16084
16071
|
errorSource = localize_1.Localizer.DiagnosticAddendum.typeVarIsInvariant;
|
16085
16072
|
// Omit the diagnostic addendum for the invariant case because it's obvious
|
16086
16073
|
// why two types are not the same.
|
@@ -16203,7 +16190,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16203
16190
|
}
|
16204
16191
|
// Strip flags we don't want to propagate to other calls.
|
16205
16192
|
const originalFlags = flags;
|
16206
|
-
flags &= ~
|
16193
|
+
flags &= ~128 /* AssignTypeFlags.AllowBoolTypeGuard */;
|
16207
16194
|
// Before performing any other checks, see if the dest type is a
|
16208
16195
|
// TypeVar that we are attempting to match.
|
16209
16196
|
if ((0, types_1.isTypeVar)(destType)) {
|
@@ -16228,7 +16215,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16228
16215
|
if (types_1.TypeBase.isInstantiable(destType) === types_1.TypeBase.isInstantiable(srcType) &&
|
16229
16216
|
srcType.condition &&
|
16230
16217
|
srcType.condition.some((cond) => {
|
16231
|
-
return
|
16218
|
+
return (cond.typeVar.details.constraints.length === 0 &&
|
16219
|
+
cond.typeVar.nameWithScope === destTypeVar.nameWithScope);
|
16232
16220
|
})) {
|
16233
16221
|
return true;
|
16234
16222
|
}
|
@@ -16427,7 +16415,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16427
16415
|
// PEP 544 says that if the dest type is a type[Proto] class,
|
16428
16416
|
// the source must be a "concrete" (non-protocol) class.
|
16429
16417
|
if (types_1.ClassType.isProtocolClass(destType) &&
|
16430
|
-
(flags &
|
16418
|
+
(flags & 16384 /* AssignTypeFlags.IgnoreProtocolAssignmentCheck */) === 0) {
|
16431
16419
|
if (types_1.ClassType.isProtocolClass(expandedSrcType) &&
|
16432
16420
|
(0, types_1.isInstantiableClass)(srcType) &&
|
16433
16421
|
!srcType.includeSubclasses) {
|
@@ -16479,7 +16467,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16479
16467
|
}
|
16480
16468
|
else if (types_1.ClassType.isBuiltIn(destType, 'TypeGuard')) {
|
16481
16469
|
// All the source to be a "bool".
|
16482
|
-
if ((originalFlags &
|
16470
|
+
if ((originalFlags & 128 /* AssignTypeFlags.AllowBoolTypeGuard */) !== 0) {
|
16483
16471
|
if ((0, types_1.isClassInstance)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'bool')) {
|
16484
16472
|
return true;
|
16485
16473
|
}
|
@@ -16573,7 +16561,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16573
16561
|
let concreteSrcType = makeTopLevelTypeVarsConcrete(srcType);
|
16574
16562
|
if ((0, types_1.isClassInstance)(concreteSrcType)) {
|
16575
16563
|
const boundMethod = getBoundMagicMethod(concreteSrcType, '__call__',
|
16576
|
-
/* selfType */ undefined,
|
16564
|
+
/* selfType */ undefined,
|
16565
|
+
/* diag */ undefined, recursionCount);
|
16577
16566
|
if (boundMethod) {
|
16578
16567
|
concreteSrcType = (0, typeUtils_1.removeParamSpecVariadicsFromSignature)(boundMethod);
|
16579
16568
|
}
|
@@ -16693,6 +16682,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16693
16682
|
}
|
16694
16683
|
// Sort the subtypes so we have a deterministic order for unions.
|
16695
16684
|
let sortedSrcTypes = (0, typeUtils_1.sortTypes)(srcType.subtypes);
|
16685
|
+
let matchedSomeSubtypes = false;
|
16696
16686
|
// Handle the case where the source and dest are both unions. Try
|
16697
16687
|
// to eliminate as many exact type matches between the src and dest.
|
16698
16688
|
if ((0, types_1.isUnion)(destType)) {
|
@@ -16719,6 +16709,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16719
16709
|
const srcTypeIndex = remainingSrcSubtypes.findIndex((srcSubtype) => (0, types_1.isTypeSame)(srcSubtype, destSubtype, {}, recursionCount));
|
16720
16710
|
if (srcTypeIndex >= 0) {
|
16721
16711
|
remainingSrcSubtypes.splice(srcTypeIndex, 1);
|
16712
|
+
matchedSomeSubtypes = true;
|
16722
16713
|
}
|
16723
16714
|
else {
|
16724
16715
|
remainingDestSubtypes.push(destSubtype);
|
@@ -16756,7 +16747,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16756
16747
|
return false;
|
16757
16748
|
});
|
16758
16749
|
if (destTypeIndex >= 0) {
|
16759
|
-
if (
|
16750
|
+
if (assignType(remainingDestSubtypes[destTypeIndex], srcSubtype, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), destTypeVarContext, srcTypeVarContext, flags, recursionCount)) {
|
16751
|
+
// Note that we have matched at least one subtype indicating
|
16752
|
+
// there is at least some overlap.
|
16753
|
+
matchedSomeSubtypes = true;
|
16754
|
+
}
|
16755
|
+
else {
|
16760
16756
|
canUseFastPath = false;
|
16761
16757
|
}
|
16762
16758
|
remainingDestSubtypes.splice(destTypeIndex, 1);
|
@@ -16794,7 +16790,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16794
16790
|
sortedSrcTypes = remainingSrcSubtypes;
|
16795
16791
|
}
|
16796
16792
|
else if (remainingSrcSubtypes.length === 0) {
|
16797
|
-
if ((flags &
|
16793
|
+
if ((flags & 2048 /* AssignTypeFlags.PopulatingExpectedType */) !== 0) {
|
16798
16794
|
// If we're populating an expected type, try not to leave
|
16799
16795
|
// any TypeVars unsolved. Assign the full type to the remaining
|
16800
16796
|
// dest TypeVars.
|
@@ -16817,6 +16813,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16817
16813
|
if (canUseFastPath) {
|
16818
16814
|
return true;
|
16819
16815
|
}
|
16816
|
+
// If we're looking for type overlaps and at least one type was matched,
|
16817
|
+
// consider it as assignable.
|
16818
|
+
if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlapCheck */) !== 0 && matchedSomeSubtypes) {
|
16819
|
+
return true;
|
16820
|
+
}
|
16820
16821
|
}
|
16821
16822
|
let isIncompatible = false;
|
16822
16823
|
sortedSrcTypes.forEach((subtype) => {
|
@@ -16835,8 +16836,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16835
16836
|
isIncompatible = true;
|
16836
16837
|
}
|
16837
16838
|
}
|
16839
|
+
else {
|
16840
|
+
matchedSomeSubtypes = true;
|
16841
|
+
}
|
16838
16842
|
}, /* sortSubtypes */ true);
|
16839
16843
|
if (isIncompatible) {
|
16844
|
+
// If we're looking for type overlaps and at least one type was matched,
|
16845
|
+
// consider it as assignable.
|
16846
|
+
if ((flags & 32 /* AssignTypeFlags.PartialOverloadOverlapCheck */) !== 0 && matchedSomeSubtypes) {
|
16847
|
+
return true;
|
16848
|
+
}
|
16840
16849
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.Localizer.DiagnosticAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
|
16841
16850
|
return false;
|
16842
16851
|
}
|
@@ -17019,13 +17028,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17019
17028
|
const destTypeVarName = types_1.TypeVarType.getNameWithScope(destType);
|
17020
17029
|
// Determine which conditions on this type apply to this type variable.
|
17021
17030
|
// There might be more than one of them.
|
17022
|
-
const applicableConditions = ((_a = (0, typeUtils_1.getTypeCondition)(srcSubtype)) !== null && _a !== void 0 ? _a : []).filter((constraint) => constraint.
|
17031
|
+
const applicableConditions = ((_a = (0, typeUtils_1.getTypeCondition)(srcSubtype)) !== null && _a !== void 0 ? _a : []).filter((constraint) => constraint.typeVar.nameWithScope === destTypeVarName);
|
17023
17032
|
// If there are no applicable conditions, it's not assignable.
|
17024
17033
|
if (applicableConditions.length === 0) {
|
17025
17034
|
return true;
|
17026
17035
|
}
|
17027
17036
|
return !applicableConditions.some((condition) => {
|
17028
|
-
if (condition.
|
17037
|
+
if (condition.typeVar.nameWithScope === types_1.TypeVarType.getNameWithScope(destType)) {
|
17029
17038
|
if (destType.details.boundType) {
|
17030
17039
|
(0, debug_1.assert)(condition.constraintIndex === 0, 'Expected constraint for bound TypeVar to have index of 0');
|
17031
17040
|
return assignType(destType.details.boundType, srcSubtype,
|
@@ -17074,7 +17083,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17074
17083
|
}
|
17075
17084
|
}
|
17076
17085
|
}
|
17077
|
-
const boundMethod = getBoundMagicMethod(objType, '__call__',
|
17086
|
+
const boundMethod = getBoundMagicMethod(objType, '__call__',
|
17087
|
+
/* selfType */ undefined,
|
17088
|
+
/* diag */ undefined, recursionCount);
|
17078
17089
|
if (boundMethod) {
|
17079
17090
|
return (0, typeUtils_1.removeParamSpecVariadicsFromSignature)(boundMethod);
|
17080
17091
|
}
|
@@ -17107,7 +17118,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17107
17118
|
// Is an additional specialization step required?
|
17108
17119
|
if (doSpecializationStep) {
|
17109
17120
|
assignType(specializedSrcType, specializedDestType,
|
17110
|
-
/* diag */ undefined, srcTypeVarContext, destTypeVarContext, (flags ^ 2 /* AssignTypeFlags.ReverseTypeVarMatching */) |
|
17121
|
+
/* diag */ undefined, srcTypeVarContext, destTypeVarContext, (flags ^ 2 /* AssignTypeFlags.ReverseTypeVarMatching */) | 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */, recursionCount);
|
17111
17122
|
if ((flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) === 0) {
|
17112
17123
|
specializedDestType = (0, typeUtils_1.applySolvedTypeVars)(destType, destTypeVarContext);
|
17113
17124
|
}
|
@@ -17221,9 +17232,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17221
17232
|
function assignFunction(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
17222
17233
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
17223
17234
|
let canAssign = true;
|
17224
|
-
const checkReturnType = (flags &
|
17235
|
+
const checkReturnType = (flags & 64 /* AssignTypeFlags.SkipFunctionReturnTypeCheck */) === 0;
|
17225
17236
|
const reverseMatching = (flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) !== 0;
|
17226
|
-
flags &= ~
|
17237
|
+
flags &= ~64 /* AssignTypeFlags.SkipFunctionReturnTypeCheck */;
|
17227
17238
|
destType = (0, typeUtils_1.removeParamSpecVariadicsFromFunction)(destType);
|
17228
17239
|
srcType = (0, typeUtils_1.removeParamSpecVariadicsFromFunction)(srcType);
|
17229
17240
|
const destParamDetails = (0, parameterUtils_1.getParameterListDetails)(destType);
|
@@ -17247,17 +17258,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17247
17258
|
const destParamType = destParam.type;
|
17248
17259
|
const destParamName = (_c = destParam.param.name) !== null && _c !== void 0 ? _c : '';
|
17249
17260
|
const srcParamName = (_d = srcParam.param.name) !== null && _d !== void 0 ? _d : '';
|
17250
|
-
if (destParamName
|
17261
|
+
if (destParamName) {
|
17251
17262
|
const isDestPositionalOnly = destParam.source === parameterUtils_1.ParameterSource.PositionOnly;
|
17252
17263
|
if (!isDestPositionalOnly &&
|
17253
17264
|
destParam.param.category !== 1 /* ParameterCategory.ArgsList */ &&
|
17254
|
-
srcParam.param.category !== 1 /* ParameterCategory.ArgsList */
|
17255
|
-
|
17256
|
-
|
17257
|
-
|
17258
|
-
|
17259
|
-
|
17260
|
-
|
17265
|
+
srcParam.param.category !== 1 /* ParameterCategory.ArgsList */) {
|
17266
|
+
if (srcParam.source === parameterUtils_1.ParameterSource.PositionOnly) {
|
17267
|
+
diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.Localizer.DiagnosticAddendum.functionParamPositionOnly().format({
|
17268
|
+
name: destParamName,
|
17269
|
+
}));
|
17270
|
+
canAssign = false;
|
17271
|
+
}
|
17272
|
+
else if (destParamName !== srcParamName) {
|
17273
|
+
diag === null || diag === void 0 ? void 0 : diag.createAddendum().addMessage(localize_1.Localizer.DiagnosticAddendum.functionParamName().format({
|
17274
|
+
srcName: srcParamName,
|
17275
|
+
destName: destParamName,
|
17276
|
+
}));
|
17277
|
+
canAssign = false;
|
17278
|
+
}
|
17261
17279
|
}
|
17262
17280
|
}
|
17263
17281
|
if (!!destParam.param.hasDefault &&
|
@@ -17282,7 +17300,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17282
17300
|
if (!assignFunctionParameter(destParamType, srcParamType, paramIndex, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), destTypeVarContext, srcTypeVarContext, flags, recursionCount)) {
|
17283
17301
|
// Handle the special case where the source parameter is a synthesized
|
17284
17302
|
// TypeVar for "self" or "cls".
|
17285
|
-
if ((flags &
|
17303
|
+
if ((flags & 512 /* AssignTypeFlags.SkipSelfClsTypeCheck */) === 0 ||
|
17286
17304
|
!(0, types_1.isTypeVar)(srcParamType) ||
|
17287
17305
|
!srcParamType.details.isSynthesized) {
|
17288
17306
|
canAssign = false;
|
@@ -17612,7 +17630,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17612
17630
|
// solver to retain literals.
|
17613
17631
|
if (srcType.details.declaredReturnType &&
|
17614
17632
|
(0, typeUtils_1.containsLiteralType)(srcType.details.declaredReturnType, /* includeTypeArgs */ true)) {
|
17615
|
-
effectiveFlags |=
|
17633
|
+
effectiveFlags |= 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
|
17616
17634
|
}
|
17617
17635
|
if ((0, types_1.isNever)(srcReturnType)) {
|
17618
17636
|
// We'll allow any function that returns NoReturn to match any
|
@@ -18356,7 +18374,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18356
18374
|
}
|
18357
18375
|
}
|
18358
18376
|
else if (!assignType(memberTypeFirstParamType, firstParamType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), typeVarContext,
|
18359
|
-
/* srcTypeVarContext */ undefined,
|
18377
|
+
/* srcTypeVarContext */ undefined, 8192 /* AssignTypeFlags.AllowUnspecifiedTypeArguments */, recursionCount)) {
|
18360
18378
|
if (memberTypeFirstParam.name &&
|
18361
18379
|
!memberTypeFirstParam.isNameSynthesized &&
|
18362
18380
|
memberTypeFirstParam.hasDeclaredType) {
|
@@ -18428,6 +18446,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18428
18446
|
case 31 /* ParseNodeType.List */:
|
18429
18447
|
case 45 /* ParseNodeType.Set */:
|
18430
18448
|
return false;
|
18449
|
+
case 7 /* ParseNodeType.BinaryOperation */:
|
18450
|
+
return (node.operator === 6 /* OperatorType.BitwiseOr */ &&
|
18451
|
+
isLegalTypeAliasExpressionForm(node.leftExpression) &&
|
18452
|
+
isLegalTypeAliasExpressionForm(node.rightExpression));
|
18453
|
+
case 24 /* ParseNodeType.Index */:
|
18454
|
+
return isLegalTypeAliasExpressionForm(node.baseExpression);
|
18455
|
+
case 35 /* ParseNodeType.MemberAccess */:
|
18456
|
+
return isLegalTypeAliasExpressionForm(node.leftExpression);
|
18431
18457
|
}
|
18432
18458
|
return true;
|
18433
18459
|
}
|