@zzzen/pyright-internal 1.2.0-dev.20230618 → 1.2.0-dev.20230625
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/codeFlowEngine.js +28 -4
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/codeFlowUtils.js +2 -0
- package/dist/analyzer/codeFlowUtils.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +15 -1
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructors.js +6 -2
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.js +64 -0
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/decorators.d.ts +7 -0
- package/dist/analyzer/decorators.js +438 -0
- package/dist/analyzer/decorators.js.map +1 -0
- package/dist/analyzer/operations.d.ts +1 -1
- package/dist/analyzer/operations.js +4 -4
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/parameterUtils.d.ts +2 -1
- package/dist/analyzer/parameterUtils.js +1 -0
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/protocols.js +2 -2
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +191 -577
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +3 -2
- package/dist/analyzer/typeEvaluatorTypes.js +2 -2
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +21 -18
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +2 -1
- package/dist/analyzer/typePrinter.js +27 -5
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +3 -1
- package/dist/analyzer/typeUtils.js +30 -10
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.js +9 -11
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +5 -3
- package/dist/analyzer/types.js +49 -11
- package/dist/analyzer/types.js.map +1 -1
- package/dist/common/extensibility.d.ts +3 -3
- package/dist/common/extensibility.js.map +1 -1
- package/dist/languageService/hoverProvider.js +13 -9
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/localization/localize.d.ts +0 -4
- package/dist/localization/localize.js +0 -1
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +0 -1
- package/dist/pyright.js +39 -2
- package/dist/pyright.js.map +1 -1
- package/dist/tests/checker.test.js +1 -1
- package/dist/tests/fourslash/completions.typeAlias.fourslash.js +1 -1
- package/dist/tests/fourslash/completions.variableDocStrings.fourslash.js +1 -1
- package/dist/tests/fourslash/hover.docstring.alias.fourslash.js +3 -3
- package/dist/tests/fourslash/hover.init.fourslash.js +1 -1
- package/dist/tests/fourslash/hover.variable.docString.fourslash.js +1 -1
- package/dist/tests/positionUtils.test.js +10 -0
- package/dist/tests/positionUtils.test.js.map +1 -1
- package/dist/tests/tokenizer.test.js +12 -1
- package/dist/tests/tokenizer.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +184 -136
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +248 -330
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +218 -177
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +58 -46
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typePrinter.test.js +2 -2
- package/package.json +1 -1
@@ -54,6 +54,7 @@ const constraintSolver_1 = require("./constraintSolver");
|
|
54
54
|
const constructors_1 = require("./constructors");
|
55
55
|
const dataClasses_1 = require("./dataClasses");
|
56
56
|
const declarationUtils_1 = require("./declarationUtils");
|
57
|
+
const decorators_1 = require("./decorators");
|
57
58
|
const enums_1 = require("./enums");
|
58
59
|
const functionTransform_1 = require("./functionTransform");
|
59
60
|
const namedTuples_1 = require("./namedTuples");
|
@@ -211,7 +212,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
211
212
|
return typeCache.size;
|
212
213
|
}
|
213
214
|
// This function should be called immediately prior to discarding
|
214
|
-
// the type evaluator. It forcibly
|
215
|
+
// the type evaluator. It forcibly replaces existing cache maps
|
215
216
|
// with empty equivalents. This shouldn't be necessary, but there
|
216
217
|
// is apparently a bug in the v8 GC where it is unable to detect
|
217
218
|
// circular references in complex data structures, so it fails
|
@@ -371,10 +372,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
371
372
|
// context, logging any errors in the process. This may require the
|
372
373
|
// type of surrounding statements to be evaluated.
|
373
374
|
function getType(node) {
|
374
|
-
var _a;
|
375
|
-
|
375
|
+
var _a, _b;
|
376
|
+
let type = (_a = evaluateTypeForSubnode(node, () => {
|
376
377
|
evaluateTypesForExpressionInContext(node);
|
377
378
|
})) === null || _a === void 0 ? void 0 : _a.type;
|
379
|
+
// If this is a type parameter with a calculated variance, see if we
|
380
|
+
// can swap it out for a version that has a computed variance.
|
381
|
+
if (type && (0, types_1.isTypeVar)(type) && type.details.declaredVariance === 0 /* Auto */) {
|
382
|
+
const typeVarType = type;
|
383
|
+
const typeParamListNode = ParseTreeUtils.getParentNodeOfType(node, 76 /* TypeParameterList */);
|
384
|
+
if (((_b = typeParamListNode === null || typeParamListNode === void 0 ? void 0 : typeParamListNode.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 10 /* Class */) {
|
385
|
+
const classTypeResult = getTypeOfClass(typeParamListNode.parent);
|
386
|
+
if (classTypeResult) {
|
387
|
+
inferTypeParameterVarianceForClass(classTypeResult.classType);
|
388
|
+
const typeParam = classTypeResult.classType.details.typeParameters.find((param) => (0, types_1.isTypeSame)(param, typeVarType, { ignoreTypeFlags: true }));
|
389
|
+
if (typeParam) {
|
390
|
+
type = types_1.TypeBase.isInstance(type) ? types_1.TypeVarType.cloneAsInstance(typeParam) : typeParam;
|
391
|
+
}
|
392
|
+
}
|
393
|
+
}
|
394
|
+
}
|
395
|
+
return type;
|
378
396
|
}
|
379
397
|
function getTypeResult(node) {
|
380
398
|
return evaluateTypeForSubnode(node, () => {
|
@@ -446,7 +464,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
446
464
|
}
|
447
465
|
}
|
448
466
|
function getTypeOfExpression(node, flags = 0 /* None */, inferenceContext) {
|
449
|
-
var _a;
|
450
467
|
// Is this type already cached?
|
451
468
|
const cacheEntry = readTypeCacheEntry(node);
|
452
469
|
if (cacheEntry &&
|
@@ -485,7 +502,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
485
502
|
// at that point.
|
486
503
|
initializedBasicTypes(node);
|
487
504
|
let typeResult;
|
488
|
-
let
|
505
|
+
let expectingInstantiable = (flags & 128 /* ExpectingInstantiableType */) !== 0;
|
489
506
|
switch (node.nodeType) {
|
490
507
|
case 38 /* Name */: {
|
491
508
|
typeResult = getTypeOfName(node, flags);
|
@@ -500,11 +517,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
500
517
|
break;
|
501
518
|
}
|
502
519
|
case 9 /* Call */: {
|
503
|
-
typeResult = getTypeOfCall(node,
|
520
|
+
typeResult = getTypeOfCall(node, flags, inferenceContext);
|
504
521
|
break;
|
505
522
|
}
|
506
523
|
case 52 /* Tuple */: {
|
507
|
-
typeResult = getTypeOfTuple(node,
|
524
|
+
typeResult = getTypeOfTuple(node, flags, inferenceContext);
|
508
525
|
break;
|
509
526
|
}
|
510
527
|
case 11 /* Constant */: {
|
@@ -516,7 +533,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
516
533
|
if (isExpectingType) {
|
517
534
|
// Don't report expecting type errors again. We will have already
|
518
535
|
// reported them when analyzing the contents of the string.
|
519
|
-
|
536
|
+
expectingInstantiable = false;
|
520
537
|
}
|
521
538
|
typeResult = getTypeOfStringList(node, flags, isExpectingType);
|
522
539
|
break;
|
@@ -534,7 +551,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
534
551
|
break;
|
535
552
|
}
|
536
553
|
case 7 /* BinaryOperation */: {
|
537
|
-
|
554
|
+
let effectiveFlags = flags;
|
555
|
+
// If we're expecting an instantiable type and this isn't a union operator,
|
556
|
+
// don't require that the two operands are also instantiable types.
|
557
|
+
if (expectingInstantiable && node.operator !== 6 /* BitwiseOr */) {
|
558
|
+
effectiveFlags &= ~128 /* ExpectingInstantiableType */;
|
559
|
+
}
|
560
|
+
typeResult = (0, operations_1.getTypeOfBinaryOperation)(evaluatorInterface, node, effectiveFlags, inferenceContext);
|
538
561
|
break;
|
539
562
|
}
|
540
563
|
case 5 /* AugmentedAssignment */: {
|
@@ -596,7 +619,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
596
619
|
break;
|
597
620
|
}
|
598
621
|
case 54 /* TypeAnnotation */: {
|
599
|
-
typeResult = getTypeOfExpression(node.typeAnnotation, 128 /*
|
622
|
+
typeResult = getTypeOfExpression(node.typeAnnotation, 128 /* ExpectingInstantiableType */ |
|
600
623
|
256 /* ExpectingTypeAnnotation */ |
|
601
624
|
8 /* EvaluateStringLiteralAsType */ |
|
602
625
|
32 /* DisallowParamSpec */ |
|
@@ -627,23 +650,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
627
650
|
// We shouldn't get here. If we do, report an error.
|
628
651
|
(0, debug_1.fail)(`Unhandled expression type '${ParseTreeUtils.printExpression(node)}'`);
|
629
652
|
}
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
addError(localize_1.Localizer.Diagnostic.typeVarTupleContext(), node);
|
634
|
-
typeResult.type = types_1.UnknownType.create();
|
635
|
-
}
|
636
|
-
}
|
637
|
-
if (!(0, typeUtils_1.isEffectivelyInstantiable)(typeResult.type)) {
|
638
|
-
const isEmptyVariadic = (0, types_1.isClassInstance)(typeResult.type) &&
|
639
|
-
types_1.ClassType.isTupleClass(typeResult.type) &&
|
640
|
-
((_a = typeResult.type.tupleTypeArguments) === null || _a === void 0 ? void 0 : _a.length) === 0;
|
641
|
-
if (!isEmptyVariadic) {
|
642
|
-
addExpectedClassDiagnostic(typeResult.type, node);
|
643
|
-
typeResult.type = types_1.UnknownType.create();
|
644
|
-
typeResult.typeErrors = true;
|
645
|
-
}
|
646
|
-
}
|
653
|
+
// Do we need to validate that the type is instantiable?
|
654
|
+
if (expectingInstantiable) {
|
655
|
+
validateTypeIsInstantiable(typeResult, flags, node);
|
647
656
|
}
|
648
657
|
writeTypeCache(node, typeResult, flags, inferenceContext, /* allowSpeculativeCaching */ true);
|
649
658
|
// If there was an expected type, make sure that the result type is compatible.
|
@@ -674,6 +683,29 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
674
683
|
}
|
675
684
|
return typeResult;
|
676
685
|
}
|
686
|
+
function validateTypeIsInstantiable(typeResult, flags, node) {
|
687
|
+
var _a;
|
688
|
+
// If the type is incomplete, don't log any diagnostics yet.
|
689
|
+
if (typeResult.isIncomplete) {
|
690
|
+
return;
|
691
|
+
}
|
692
|
+
if (flags & 64 /* DisallowTypeVarTuple */) {
|
693
|
+
if ((0, types_1.isVariadicTypeVar)(typeResult.type) && !typeResult.type.isVariadicInUnion) {
|
694
|
+
addError(localize_1.Localizer.Diagnostic.typeVarTupleContext(), node);
|
695
|
+
typeResult.type = types_1.UnknownType.create();
|
696
|
+
}
|
697
|
+
}
|
698
|
+
if (!(0, typeUtils_1.isEffectivelyInstantiable)(typeResult.type)) {
|
699
|
+
const isEmptyVariadic = (0, types_1.isClassInstance)(typeResult.type) &&
|
700
|
+
types_1.ClassType.isTupleClass(typeResult.type) &&
|
701
|
+
((_a = typeResult.type.tupleTypeArguments) === null || _a === void 0 ? void 0 : _a.length) === 0;
|
702
|
+
if (!isEmptyVariadic) {
|
703
|
+
addExpectedClassDiagnostic(typeResult.type, node);
|
704
|
+
typeResult.type = types_1.UnknownType.create();
|
705
|
+
typeResult.typeErrors = true;
|
706
|
+
}
|
707
|
+
}
|
708
|
+
}
|
677
709
|
function getTypeOfAwaitOperator(node, flags, inferenceContext) {
|
678
710
|
const effectiveExpectedType = inferenceContext
|
679
711
|
? createAwaitableReturnType(node, inferenceContext.expectedType, /* isGenerator */ false)
|
@@ -750,7 +782,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
750
782
|
function getTypeOfStringList(node, flags, isExpectingType) {
|
751
783
|
let typeResult;
|
752
784
|
if (isExpectingType) {
|
753
|
-
let updatedFlags = flags | 4 /* AllowForwardReferences */ | 128 /*
|
785
|
+
let updatedFlags = flags | 4 /* AllowForwardReferences */ | 128 /* ExpectingInstantiableType */;
|
754
786
|
// In most cases, annotations within a string are not parsed by the interpreter.
|
755
787
|
// There are a few exceptions (e.g. the "bound" value for a TypeVar constructor).
|
756
788
|
if ((flags & 4194304 /* InterpreterParsesStringLiteral */) === 0) {
|
@@ -913,7 +945,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
913
945
|
return specialType;
|
914
946
|
}
|
915
947
|
}
|
916
|
-
let evaluatorFlags = 128 /*
|
948
|
+
let evaluatorFlags = 128 /* ExpectingInstantiableType */ |
|
917
949
|
256 /* ExpectingTypeAnnotation */ |
|
918
950
|
1 /* ConvertEllipsisToAny */ |
|
919
951
|
8 /* EvaluateStringLiteralAsType */;
|
@@ -977,71 +1009,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
977
1009
|
}
|
978
1010
|
return (0, typeUtils_1.convertToInstance)(annotationType);
|
979
1011
|
}
|
980
|
-
function getTypeOfDecorator(node, functionOrClassType) {
|
981
|
-
// Evaluate the type of the decorator expression.
|
982
|
-
let flags = AnalyzerNodeInfo.getFileInfo(node).isStubFile
|
983
|
-
? 4 /* AllowForwardReferences */
|
984
|
-
: 0 /* None */;
|
985
|
-
if (node.expression.nodeType !== 9 /* Call */) {
|
986
|
-
flags |= 2 /* DoNotSpecialize */;
|
987
|
-
}
|
988
|
-
const decoratorTypeResult = getTypeOfExpression(node.expression, flags);
|
989
|
-
// Special-case typing.type_check_only. It's used in many stdlib
|
990
|
-
// functions, and pyright treats it as a no-op, so don't waste
|
991
|
-
// time evaluating it.
|
992
|
-
if ((0, types_1.isFunction)(decoratorTypeResult.type) &&
|
993
|
-
decoratorTypeResult.type.details.builtInName === 'type_check_only') {
|
994
|
-
return functionOrClassType;
|
995
|
-
}
|
996
|
-
// Special-case the combination of a classmethod decorator applied
|
997
|
-
// to a property. This is allowed in Python 3.9, but it's not reflected
|
998
|
-
// in the builtins.pyi stub for classmethod.
|
999
|
-
if ((0, types_1.isInstantiableClass)(decoratorTypeResult.type) &&
|
1000
|
-
types_1.ClassType.isBuiltIn(decoratorTypeResult.type, 'classmethod') &&
|
1001
|
-
(0, typeUtils_1.isProperty)(functionOrClassType)) {
|
1002
|
-
return functionOrClassType;
|
1003
|
-
}
|
1004
|
-
const argList = [
|
1005
|
-
{
|
1006
|
-
argumentCategory: 0 /* Simple */,
|
1007
|
-
typeResult: { type: functionOrClassType },
|
1008
|
-
},
|
1009
|
-
];
|
1010
|
-
const returnType = validateCallArguments(node.expression, argList, decoratorTypeResult,
|
1011
|
-
/* typeVarContext */ undefined,
|
1012
|
-
/* skipUnknownArgCheck */ true).returnType || types_1.UnknownType.create();
|
1013
|
-
// If the return type is a function that has no annotations
|
1014
|
-
// and just *args and **kwargs parameters, assume that it
|
1015
|
-
// preserves the type of the input function.
|
1016
|
-
if ((0, types_1.isFunction)(returnType) && !returnType.details.declaredReturnType) {
|
1017
|
-
if (!returnType.details.parameters.some((param, index) => {
|
1018
|
-
// Don't allow * or / separators or params with declared types.
|
1019
|
-
if (!param.name || param.hasDeclaredType) {
|
1020
|
-
return true;
|
1021
|
-
}
|
1022
|
-
// Allow *args or **kwargs parameters.
|
1023
|
-
if (param.category !== 0 /* Simple */) {
|
1024
|
-
return false;
|
1025
|
-
}
|
1026
|
-
// Allow inferred "self" or "cls" parameters.
|
1027
|
-
return index !== 0 || !param.isTypeInferred;
|
1028
|
-
})) {
|
1029
|
-
return functionOrClassType;
|
1030
|
-
}
|
1031
|
-
}
|
1032
|
-
// If the decorator is completely unannotated and the return type
|
1033
|
-
// includes unknowns, assume that it preserves the type of the input
|
1034
|
-
// function.
|
1035
|
-
if ((0, typeUtils_1.isPartlyUnknown)(returnType)) {
|
1036
|
-
if ((0, types_1.isFunction)(decoratorTypeResult.type)) {
|
1037
|
-
if (!decoratorTypeResult.type.details.parameters.find((param) => param.typeAnnotation !== undefined) &&
|
1038
|
-
decoratorTypeResult.type.details.declaredReturnType === undefined) {
|
1039
|
-
return functionOrClassType;
|
1040
|
-
}
|
1041
|
-
}
|
1042
|
-
}
|
1043
|
-
return returnType;
|
1044
|
-
}
|
1045
1012
|
function canBeFalsy(type, recursionCount = 0) {
|
1046
1013
|
type = makeTopLevelTypeVarsConcrete(type);
|
1047
1014
|
if (recursionCount > types_1.maxTypeRecursionCount) {
|
@@ -1268,21 +1235,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1268
1235
|
memberInfo = getTypeOfClassMemberName(errorNode, classType,
|
1269
1236
|
/* isAccessedThroughObject */ false, memberName, usage, classDiag, memberAccessFlags | 1 /* AccessClassMembersOnly */, bindToType);
|
1270
1237
|
}
|
1271
|
-
// If this is a protocol class X and we're accessing a non ClassVar,
|
1272
|
-
// emit an error.
|
1273
|
-
if (memberInfo &&
|
1274
|
-
memberInfo.classType &&
|
1275
|
-
memberInfo.symbol &&
|
1276
|
-
(0, types_1.isClass)(memberInfo.classType) &&
|
1277
|
-
types_1.ClassType.isProtocolClass(memberInfo.classType)) {
|
1278
|
-
const primaryDecl = (0, symbolUtils_1.getLastTypedDeclaredForSymbol)(memberInfo.symbol);
|
1279
|
-
if (primaryDecl && primaryDecl.type === 1 /* Variable */ && !memberInfo.isClassVar) {
|
1280
|
-
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.protocolMemberNotClassVar().format({
|
1281
|
-
memberName,
|
1282
|
-
className: memberInfo.classType.details.name,
|
1283
|
-
}), errorNode);
|
1284
|
-
}
|
1285
|
-
}
|
1286
1238
|
const isMemberPresentOnClass = (memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.classType) !== undefined;
|
1287
1239
|
// If it wasn't found on the class, see if it's part of the metaclass.
|
1288
1240
|
if (!memberInfo) {
|
@@ -2905,7 +2857,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2905
2857
|
if ((0, types_1.isTypeVar)(type) &&
|
2906
2858
|
!type.details.isParamSpec &&
|
2907
2859
|
!type.isVariadicInUnion &&
|
2908
|
-
(flags & 128 /*
|
2860
|
+
(flags & 128 /* ExpectingInstantiableType */) === 0 &&
|
2909
2861
|
type.details.name === name) {
|
2910
2862
|
// Handle the special case of a PEP 604 union. These can appear within
|
2911
2863
|
// an implied type alias where we are not expecting a type.
|
@@ -2925,7 +2877,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2925
2877
|
}
|
2926
2878
|
}
|
2927
2879
|
}
|
2928
|
-
if ((flags & 128 /*
|
2880
|
+
if ((flags & 128 /* ExpectingInstantiableType */) !== 0) {
|
2929
2881
|
if ((flags & 1024 /* AllowGenericClassType */) === 0) {
|
2930
2882
|
if ((0, types_1.isInstantiableClass)(type) && types_1.ClassType.isBuiltIn(type, 'Generic')) {
|
2931
2883
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.genericNotAllowed(), node);
|
@@ -3106,7 +3058,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3106
3058
|
}
|
3107
3059
|
// Is this a generic class that needs to be specialized?
|
3108
3060
|
if ((0, types_1.isInstantiableClass)(type)) {
|
3109
|
-
if ((flags & 128 /*
|
3061
|
+
if ((flags & 128 /* ExpectingInstantiableType */) !== 0 &&
|
3062
|
+
(flags & 512 /* AllowMissingTypeArgs */) === 0) {
|
3110
3063
|
if (!type.typeAliasInfo && (0, typeUtils_1.requiresTypeArguments)(type)) {
|
3111
3064
|
if (!type.typeArguments || !type.isTypeArgumentExplicit) {
|
3112
3065
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportMissingTypeArgument, diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.Localizer.Diagnostic.typeArgsMissingForClass().format({
|
@@ -3120,7 +3073,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3120
3073
|
}
|
3121
3074
|
}
|
3122
3075
|
// Is this a generic type alias that needs to be specialized?
|
3123
|
-
if ((flags & 128 /*
|
3076
|
+
if ((flags & 128 /* ExpectingInstantiableType */) !== 0 &&
|
3124
3077
|
type.typeAliasInfo &&
|
3125
3078
|
type.typeAliasInfo.typeParameters &&
|
3126
3079
|
type.typeAliasInfo.typeParameters.length > 0 &&
|
@@ -4218,7 +4171,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4218
4171
|
// If this is meant to be a type and the base expression is a string expression,
|
4219
4172
|
// emit an error because this will generate a runtime exception in Python versions
|
4220
4173
|
// less than 3.10.
|
4221
|
-
if (flags & 128 /*
|
4174
|
+
if (flags & 128 /* ExpectingInstantiableType */) {
|
4222
4175
|
if (node.baseExpression.nodeType === 48 /* StringList */) {
|
4223
4176
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
4224
4177
|
if (!fileInfo.isStubFile && fileInfo.executionEnvironment.pythonVersion < pythonVersion_1.PythonVersion.V3_10) {
|
@@ -4550,7 +4503,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4550
4503
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
4551
4504
|
return concreteSubtype;
|
4552
4505
|
}
|
4553
|
-
if (flags & 128 /*
|
4506
|
+
if (flags & 128 /* ExpectingInstantiableType */) {
|
4554
4507
|
if ((0, types_1.isTypeVar)(unexpandedSubtype)) {
|
4555
4508
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarNotSubscriptable().format({
|
4556
4509
|
type: printType(unexpandedSubtype),
|
@@ -4565,7 +4518,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4565
4518
|
if (concreteSubtype.details.effectiveMetaclass &&
|
4566
4519
|
(0, types_1.isInstantiableClass)(concreteSubtype.details.effectiveMetaclass) &&
|
4567
4520
|
!types_1.ClassType.isBuiltIn(concreteSubtype.details.effectiveMetaclass, ['type', '_InitVarMeta']) &&
|
4568
|
-
(flags & 128 /*
|
4521
|
+
(flags & 128 /* ExpectingInstantiableType */) === 0) {
|
4569
4522
|
const itemMethodType = getTypeOfClassMember(node, concreteSubtype, getIndexAccessMagicMethodName(usage),
|
4570
4523
|
/* usage */ undefined,
|
4571
4524
|
/* diag */ undefined, 64 /* SkipAttributeAccessOverride */ | 32 /* ConsiderMetaclassOnly */);
|
@@ -5072,7 +5025,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5072
5025
|
function getTypeArg(node, flags, supportsDictExpression) {
|
5073
5026
|
let typeResult;
|
5074
5027
|
let adjustedFlags = flags |
|
5075
|
-
128 /*
|
5028
|
+
128 /* ExpectingInstantiableType */ |
|
5076
5029
|
1 /* ConvertEllipsisToAny */ |
|
5077
5030
|
8 /* EvaluateStringLiteralAsType */;
|
5078
5031
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
@@ -5119,8 +5072,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5119
5072
|
}
|
5120
5073
|
return typeResult;
|
5121
5074
|
}
|
5122
|
-
function getTypeOfTuple(node,
|
5123
|
-
if ((flags & 128 /*
|
5075
|
+
function getTypeOfTuple(node, flags, inferenceContext) {
|
5076
|
+
if ((flags & 128 /* ExpectingInstantiableType */) !== 0 &&
|
5077
|
+
node.expressions.length === 0 &&
|
5078
|
+
!inferenceContext) {
|
5124
5079
|
return { type: makeTupleObject([]), isEmptyTupleShorthand: true };
|
5125
5080
|
}
|
5126
5081
|
// If the expected type is a union, recursively call for each of the subtypes
|
@@ -5272,7 +5227,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
5272
5227
|
}
|
5273
5228
|
return entryTypes;
|
5274
5229
|
}
|
5275
|
-
function getTypeOfCall(node,
|
5230
|
+
function getTypeOfCall(node, flags, inferenceContext) {
|
5276
5231
|
var _a;
|
5277
5232
|
let baseTypeResult;
|
5278
5233
|
// Handle immediate calls of lambdas specially.
|
@@ -6211,8 +6166,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6211
6166
|
// Verify that the cast is necessary.
|
6212
6167
|
const castToType = getTypeOfArgumentExpectingType(argList[0]).type;
|
6213
6168
|
const castFromType = getTypeOfArgument(argList[1]).type;
|
6214
|
-
if (
|
6215
|
-
if ((0, types_1.isTypeSame)(
|
6169
|
+
if (types_1.TypeBase.isInstantiable(castToType) && !(0, types_1.isUnknown)(castToType)) {
|
6170
|
+
if ((0, types_1.isTypeSame)((0, typeUtils_1.convertToInstance)(castToType), castFromType, {
|
6216
6171
|
ignorePseudoGeneric: true,
|
6217
6172
|
})) {
|
6218
6173
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportUnnecessaryCast, diagnosticRules_1.DiagnosticRule.reportUnnecessaryCast, localize_1.Localizer.Diagnostic.unnecessaryCast().format({
|
@@ -6909,7 +6864,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6909
6864
|
while (argIndex < argList.length) {
|
6910
6865
|
if (argList[argIndex].argumentCategory === 2 /* UnpackedDictionary */) {
|
6911
6866
|
// Verify that the type used in this expression is a SupportsKeysAndGetItem[str, T].
|
6912
|
-
const argType = getTypeOfArgument(argList[argIndex]).type;
|
6867
|
+
const argType = getTypeOfArgument(argList[argIndex], (0, typeUtils_1.makeInferenceContext)(paramDetails.unpackedKwargsTypedDictType)).type;
|
6913
6868
|
if ((0, types_1.isAnyOrUnknown)(argType)) {
|
6914
6869
|
unpackedDictionaryArgType = argType;
|
6915
6870
|
}
|
@@ -7693,6 +7648,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7693
7648
|
// list and reports any mismatches in types or counts. Returns the
|
7694
7649
|
// specialized return type of the call.
|
7695
7650
|
function validateFunctionArguments(errorNode, argList, typeResult, typeVarContext, skipUnknownArgCheck = false, inferenceContext) {
|
7651
|
+
var _a;
|
7652
|
+
const signatureTracker = (_a = inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.signatureTracker) !== null && _a !== void 0 ? _a : new typeUtils_1.UniqueSignatureTracker();
|
7653
|
+
typeResult.type = (0, typeUtils_1.ensureFunctionSignaturesAreUnique)(typeResult.type, signatureTracker, errorNode.start);
|
7696
7654
|
const matchResults = matchFunctionArgumentsToParameters(errorNode, argList, typeResult, 0);
|
7697
7655
|
if (matchResults.argumentErrors) {
|
7698
7656
|
// Evaluate types of all args. This will ensure that referenced symbols are
|
@@ -7708,7 +7666,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7708
7666
|
overloadsUsedForCall: [],
|
7709
7667
|
};
|
7710
7668
|
}
|
7711
|
-
return validateFunctionArgumentTypesWithContext(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, inferenceContext);
|
7669
|
+
return validateFunctionArgumentTypesWithContext(errorNode, matchResults, typeVarContext, skipUnknownArgCheck, (0, typeUtils_1.makeInferenceContext)(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType, inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete, signatureTracker));
|
7712
7670
|
}
|
7713
7671
|
// Determines whether the specified argument list satisfies the function
|
7714
7672
|
// signature bound to the specified ParamSpec. Return value indicates success.
|
@@ -7902,7 +7860,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7902
7860
|
8 /* EvaluateStringLiteralAsType */ |
|
7903
7861
|
32 /* DisallowParamSpec */ |
|
7904
7862
|
64 /* DisallowTypeVarTuple */
|
7905
|
-
:
|
7863
|
+
: 2 /* DoNotSpecialize */;
|
7906
7864
|
const exprTypeResult = getTypeOfExpression(argParam.argument.valueExpression, flags, (0, typeUtils_1.makeInferenceContext)(expectedType, !!(typeResult === null || typeResult === void 0 ? void 0 : typeResult.isIncomplete), signatureTracker));
|
7907
7865
|
argType = exprTypeResult.type;
|
7908
7866
|
// If the type includes multiple instances of a generic function
|
@@ -7937,15 +7895,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7937
7895
|
if (argParam.argType) {
|
7938
7896
|
argType = argParam.argType;
|
7939
7897
|
}
|
7940
|
-
else if (argParam.isinstanceParam && !argParam.argument.typeResult && argParam.argument.valueExpression) {
|
7941
|
-
const argTypeResult = getTypeOfExpression(argParam.argument.valueExpression, 8 /* EvaluateStringLiteralAsType */ |
|
7942
|
-
32 /* DisallowParamSpec */ |
|
7943
|
-
64 /* DisallowTypeVarTuple */);
|
7944
|
-
argType = argTypeResult.type;
|
7945
|
-
if (argTypeResult.isIncomplete) {
|
7946
|
-
isTypeIncomplete = true;
|
7947
|
-
}
|
7948
|
-
}
|
7949
7898
|
else {
|
7950
7899
|
const argTypeResult = getTypeOfArgument(argParam.argument);
|
7951
7900
|
argType = argTypeResult.type;
|
@@ -8461,7 +8410,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8461
8410
|
typeParameters = [];
|
8462
8411
|
let isTypeParamListValid = true;
|
8463
8412
|
typeParamsExpr.expressions.map((expr) => {
|
8464
|
-
let entryType = getTypeOfExpression(expr, 128 /*
|
8413
|
+
let entryType = getTypeOfExpression(expr, 128 /* ExpectingInstantiableType */).type;
|
8465
8414
|
if ((0, types_1.isTypeVar)(entryType)) {
|
8466
8415
|
if (entryType.scopeId) {
|
8467
8416
|
isTypeParamListValid = false;
|
@@ -8481,7 +8430,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8481
8430
|
}
|
8482
8431
|
}
|
8483
8432
|
return getTypeOfTypeAliasCommon(nameNode, nameNode, valueExpr,
|
8484
|
-
/* typeParamNodes */ undefined, () => typeParameters);
|
8433
|
+
/* typeParamNodes */ undefined, () => typeParameters !== null && typeParameters !== void 0 ? typeParameters : []);
|
8485
8434
|
}
|
8486
8435
|
function getBooleanValue(node) {
|
8487
8436
|
if (node.nodeType === 11 /* Constant */) {
|
@@ -8615,7 +8564,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8615
8564
|
function getTypeOfConstant(node, flags) {
|
8616
8565
|
let type;
|
8617
8566
|
if (node.constType === 26 /* None */) {
|
8618
|
-
type =
|
8567
|
+
type =
|
8568
|
+
(flags & 128 /* ExpectingInstantiableType */) !== 0
|
8569
|
+
? types_1.NoneType.createType()
|
8570
|
+
: types_1.NoneType.createInstance();
|
8619
8571
|
}
|
8620
8572
|
else if (node.constType === 33 /* True */ ||
|
8621
8573
|
node.constType === 15 /* False */ ||
|
@@ -9305,13 +9257,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9305
9257
|
// For now, use only the first expected type.
|
9306
9258
|
const expectedFunctionType = expectedFunctionTypes.length > 0 ? expectedFunctionTypes[0] : undefined;
|
9307
9259
|
let paramsArePositionOnly = true;
|
9260
|
+
const expectedParamDetails = expectedFunctionType ? (0, parameterUtils_1.getParameterListDetails)(expectedFunctionType) : undefined;
|
9308
9261
|
node.parameters.forEach((param, index) => {
|
9309
|
-
let paramType
|
9310
|
-
if (
|
9311
|
-
|
9262
|
+
let paramType;
|
9263
|
+
if (expectedParamDetails) {
|
9264
|
+
if (index < expectedParamDetails.params.length) {
|
9265
|
+
paramType = expectedParamDetails.params[index].type;
|
9266
|
+
}
|
9267
|
+
else if (param.defaultValue) {
|
9268
|
+
// If the lambda param has a default value but there is no associated
|
9269
|
+
// parameter in the expected type, assume that the default value is
|
9270
|
+
// being used to explicitly capture a value from an outer scope. Infer
|
9271
|
+
// its type from the default value expression.
|
9272
|
+
paramType = getTypeOfExpression(param.defaultValue, undefined, inferenceContext).type;
|
9273
|
+
}
|
9312
9274
|
}
|
9313
9275
|
if (param.name) {
|
9314
|
-
writeTypeCache(param.name, { type: transformVariadicParamType(node, param.category, paramType) }, 0 /* None */);
|
9276
|
+
writeTypeCache(param.name, { type: transformVariadicParamType(node, param.category, paramType !== null && paramType !== void 0 ? paramType : types_1.UnknownType.create()) }, 0 /* None */);
|
9315
9277
|
}
|
9316
9278
|
if (param.defaultValue) {
|
9317
9279
|
// Evaluate the default value if it's present.
|
@@ -9349,7 +9311,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9349
9311
|
hasDefault: !!param.defaultValue,
|
9350
9312
|
defaultValueExpression: param.defaultValue,
|
9351
9313
|
hasDeclaredType: true,
|
9352
|
-
type: paramType,
|
9314
|
+
type: paramType !== null && paramType !== void 0 ? paramType : types_1.UnknownType.create(),
|
9353
9315
|
};
|
9354
9316
|
types_1.FunctionType.addParameter(functionType, functionParam);
|
9355
9317
|
});
|
@@ -9655,11 +9617,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9655
9617
|
hasDeclaredType: true,
|
9656
9618
|
});
|
9657
9619
|
});
|
9658
|
-
|
9659
|
-
|
9660
|
-
|
9661
|
-
|
9662
|
-
|
9620
|
+
if (typeList.length > 0) {
|
9621
|
+
// Add a positional-only separator to the end of the parameter list.
|
9622
|
+
types_1.FunctionType.addParameter(functionType, {
|
9623
|
+
category: 0 /* Simple */,
|
9624
|
+
isNameSynthesized: false,
|
9625
|
+
type: types_1.UnknownType.create(),
|
9626
|
+
});
|
9627
|
+
}
|
9663
9628
|
}
|
9664
9629
|
else if ((0, typeUtils_1.isEllipsisType)(typeArgs[0].type)) {
|
9665
9630
|
types_1.FunctionType.addDefaultParameters(functionType);
|
@@ -9918,7 +9883,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9918
9883
|
}
|
9919
9884
|
const enclosingFunction = ParseTreeUtils.getEnclosingFunction(errorNode);
|
9920
9885
|
if (enclosingFunction) {
|
9921
|
-
const functionFlags = getFunctionFlagsFromDecorators(enclosingFunction,
|
9886
|
+
const functionFlags = (0, decorators_1.getFunctionFlagsFromDecorators)(evaluatorInterface, enclosingFunction,
|
9887
|
+
/* isInClass */ true);
|
9922
9888
|
const isInnerFunction = !!ParseTreeUtils.getEnclosingFunction(enclosingFunction);
|
9923
9889
|
if (!isInnerFunction) {
|
9924
9890
|
// Check for static methods.
|
@@ -10502,7 +10468,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10502
10468
|
}
|
10503
10469
|
if (isDeclaredTypeAlias(node.leftExpression)) {
|
10504
10470
|
flags |=
|
10505
|
-
128 /*
|
10471
|
+
128 /* ExpectingInstantiableType */ |
|
10506
10472
|
256 /* ExpectingTypeAnnotation */ |
|
10507
10473
|
8 /* EvaluateStringLiteralAsType */ |
|
10508
10474
|
32 /* DisallowParamSpec */ |
|
@@ -10788,7 +10754,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10788
10754
|
let protocolTypeParameters;
|
10789
10755
|
const initSubclassArgs = [];
|
10790
10756
|
let metaclassNode;
|
10791
|
-
let exprFlags = 128 /*
|
10757
|
+
let exprFlags = 128 /* ExpectingInstantiableType */ |
|
10792
10758
|
1024 /* AllowGenericClassType */ |
|
10793
10759
|
262144 /* DisallowNakedGeneric */ |
|
10794
10760
|
2048 /* DisallowTypeVarsWithScopeId */ |
|
@@ -11123,7 +11089,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11123
11089
|
let foundUnknown = false;
|
11124
11090
|
for (let i = node.decorators.length - 1; i >= 0; i--) {
|
11125
11091
|
const decorator = node.decorators[i];
|
11126
|
-
const newDecoratedType = applyClassDecorator(decoratedType, classType, decorator);
|
11092
|
+
const newDecoratedType = (0, decorators_1.applyClassDecorator)(evaluatorInterface, decoratedType, classType, decorator);
|
11127
11093
|
const unknownOrAny = (0, typeUtils_1.containsAnyOrUnknown)(newDecoratedType, /* recurse */ false);
|
11128
11094
|
if (unknownOrAny && (0, types_1.isUnknown)(unknownOrAny)) {
|
11129
11095
|
// Report this error only on the first unknown type.
|
@@ -11391,95 +11357,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11391
11357
|
addDiagnostic(AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.typeVarsNotInGenericOrProtocol() + diag.getString(), errorNode);
|
11392
11358
|
}
|
11393
11359
|
}
|
11394
|
-
function applyClassDecorator(inputClassType, originalClassType, decoratorNode) {
|
11395
|
-
const fileInfo = AnalyzerNodeInfo.getFileInfo(decoratorNode);
|
11396
|
-
let flags = fileInfo.isStubFile ? 4 /* AllowForwardReferences */ : 0 /* None */;
|
11397
|
-
if (decoratorNode.expression.nodeType !== 9 /* Call */) {
|
11398
|
-
flags |= 2 /* DoNotSpecialize */;
|
11399
|
-
}
|
11400
|
-
const decoratorType = getTypeOfExpression(decoratorNode.expression, flags).type;
|
11401
|
-
if (decoratorNode.expression.nodeType === 9 /* Call */) {
|
11402
|
-
const decoratorCallType = getTypeOfExpression(decoratorNode.expression.leftExpression, flags | 2 /* DoNotSpecialize */).type;
|
11403
|
-
if ((0, types_1.isFunction)(decoratorCallType)) {
|
11404
|
-
if (decoratorCallType.details.name === '__dataclass_transform__' ||
|
11405
|
-
decoratorCallType.details.builtInName === 'dataclass_transform') {
|
11406
|
-
originalClassType.details.classDataClassTransform = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
|
11407
|
-
}
|
11408
|
-
else if (decoratorCallType.details.builtInName === 'deprecated') {
|
11409
|
-
originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
11410
|
-
return inputClassType;
|
11411
|
-
}
|
11412
|
-
}
|
11413
|
-
if ((0, types_1.isOverloadedFunction)(decoratorCallType)) {
|
11414
|
-
if (decoratorCallType.overloads.length > 0 &&
|
11415
|
-
decoratorCallType.overloads[0].details.builtInName === 'deprecated') {
|
11416
|
-
originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
11417
|
-
return inputClassType;
|
11418
|
-
}
|
11419
|
-
}
|
11420
|
-
}
|
11421
|
-
if ((0, types_1.isOverloadedFunction)(decoratorType)) {
|
11422
|
-
const dataclassBehaviors = (0, dataClasses_1.getDataclassDecoratorBehaviors)(decoratorType);
|
11423
|
-
if (dataclassBehaviors) {
|
11424
|
-
(0, dataClasses_1.applyDataClassDecorator)(evaluatorInterface, decoratorNode, originalClassType, dataclassBehaviors,
|
11425
|
-
/* callNode */ undefined);
|
11426
|
-
return inputClassType;
|
11427
|
-
}
|
11428
|
-
if (decoratorType.overloads.length > 0 && decoratorType.overloads[0].details.builtInName === 'deprecated') {
|
11429
|
-
originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
11430
|
-
return inputClassType;
|
11431
|
-
}
|
11432
|
-
}
|
11433
|
-
else if ((0, types_1.isFunction)(decoratorType)) {
|
11434
|
-
if (decoratorType.details.builtInName === 'final') {
|
11435
|
-
originalClassType.details.flags |= 4096 /* Final */;
|
11436
|
-
// Don't call getTypeOfDecorator for final. We'll hard-code its
|
11437
|
-
// behavior because its function definition results in a cyclical
|
11438
|
-
// dependency between builtins, typing and _typeshed stubs.
|
11439
|
-
return inputClassType;
|
11440
|
-
}
|
11441
|
-
if (decoratorType.details.builtInName === 'deprecated') {
|
11442
|
-
originalClassType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
11443
|
-
return inputClassType;
|
11444
|
-
}
|
11445
|
-
if (decoratorType.details.builtInName === 'runtime_checkable') {
|
11446
|
-
originalClassType.details.flags |= 32768 /* RuntimeCheckable */;
|
11447
|
-
// Don't call getTypeOfDecorator for runtime_checkable. It appears
|
11448
|
-
// frequently in stubs, and it's a waste of time to validate its
|
11449
|
-
// parameters.
|
11450
|
-
return inputClassType;
|
11451
|
-
}
|
11452
|
-
// Is this a dataclass decorator?
|
11453
|
-
let dataclassBehaviors;
|
11454
|
-
let callNode;
|
11455
|
-
if (decoratorNode.expression.nodeType === 9 /* Call */) {
|
11456
|
-
callNode = decoratorNode.expression;
|
11457
|
-
const decoratorCallType = getTypeOfExpression(callNode.leftExpression, flags | 2 /* DoNotSpecialize */).type;
|
11458
|
-
dataclassBehaviors = (0, dataClasses_1.getDataclassDecoratorBehaviors)(decoratorCallType);
|
11459
|
-
}
|
11460
|
-
else {
|
11461
|
-
const decoratorType = getTypeOfExpression(decoratorNode.expression, flags).type;
|
11462
|
-
dataclassBehaviors = (0, dataClasses_1.getDataclassDecoratorBehaviors)(decoratorType);
|
11463
|
-
}
|
11464
|
-
if (dataclassBehaviors) {
|
11465
|
-
(0, dataClasses_1.applyDataClassDecorator)(evaluatorInterface, decoratorNode, originalClassType, dataclassBehaviors, callNode);
|
11466
|
-
return inputClassType;
|
11467
|
-
}
|
11468
|
-
}
|
11469
|
-
return getTypeOfDecorator(decoratorNode, inputClassType);
|
11470
|
-
}
|
11471
|
-
// Given a @typing.deprecated decorator node, returns either '' or a custom
|
11472
|
-
// deprecation message if one is provided.
|
11473
|
-
function getCustomDeprecationMessage(decorator) {
|
11474
|
-
if (decorator.expression.nodeType === 9 /* Call */ &&
|
11475
|
-
decorator.expression.arguments.length > 0 &&
|
11476
|
-
decorator.expression.arguments[0].argumentCategory === 0 /* Simple */ &&
|
11477
|
-
decorator.expression.arguments[0].valueExpression.nodeType === 48 /* StringList */ &&
|
11478
|
-
decorator.expression.arguments[0].valueExpression.strings.length === 1) {
|
11479
|
-
return decorator.expression.arguments[0].valueExpression.strings[0].value;
|
11480
|
-
}
|
11481
|
-
return '';
|
11482
|
-
}
|
11483
11360
|
// Runs any registered "callback hooks" that depend on the specified class type.
|
11484
11361
|
// This allows us to complete any work that requires dependent classes to be
|
11485
11362
|
// completed.
|
@@ -11626,7 +11503,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11626
11503
|
}
|
11627
11504
|
containingClassType = classInfo.classType;
|
11628
11505
|
}
|
11629
|
-
let functionFlags = getFunctionFlagsFromDecorators(node, !!containingClassNode);
|
11506
|
+
let functionFlags = (0, decorators_1.getFunctionFlagsFromDecorators)(evaluatorInterface, node, !!containingClassNode);
|
11630
11507
|
if (functionDecl === null || functionDecl === void 0 ? void 0 : functionDecl.isGenerator) {
|
11631
11508
|
functionFlags |= 16 /* Generator */;
|
11632
11509
|
}
|
@@ -11952,7 +11829,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11952
11829
|
let foundUnknown = false;
|
11953
11830
|
for (let i = node.decorators.length - 1; i >= 0; i--) {
|
11954
11831
|
const decorator = node.decorators[i];
|
11955
|
-
const newDecoratedType = applyFunctionDecorator(decoratedType, functionType, decorator, node);
|
11832
|
+
const newDecoratedType = (0, decorators_1.applyFunctionDecorator)(evaluatorInterface, decoratedType, functionType, decorator, node);
|
11956
11833
|
const unknownOrAny = (0, typeUtils_1.containsAnyOrUnknown)(newDecoratedType, /* recurse */ false);
|
11957
11834
|
if (unknownOrAny && (0, types_1.isUnknown)(unknownOrAny)) {
|
11958
11835
|
// Report this error only on the first unknown type.
|
@@ -11974,7 +11851,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11974
11851
|
markParamAccessed(param);
|
11975
11852
|
});
|
11976
11853
|
}
|
11977
|
-
decoratedType = addOverloadsToFunctionType(node, decoratedType);
|
11854
|
+
decoratedType = (0, decorators_1.addOverloadsToFunctionType)(evaluatorInterface, node, decoratedType);
|
11978
11855
|
}
|
11979
11856
|
writeTypeCache(node.name, { type: functionType }, 0 /* None */);
|
11980
11857
|
writeTypeCache(node, { type: decoratedType }, 0 /* None */);
|
@@ -12055,6 +11932,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12055
11932
|
const baseClassParamAnnotation = (_a = baseClassParam.typeAnnotation) !== null && _a !== void 0 ? _a : baseClassParam.typeAnnotationComment;
|
12056
11933
|
if (baseClassParamAnnotation) {
|
12057
11934
|
let inferredParamType = getTypeOfParameterAnnotation(baseClassParamAnnotation, functionNode.parameters[paramIndex].category);
|
11935
|
+
// If the parameter type is generic, specialize it in the context
|
11936
|
+
// of the child class.
|
11937
|
+
if ((0, typeUtils_1.requiresSpecialization)(inferredParamType) && (0, types_1.isClass)(baseClassMemberInfo.classType)) {
|
11938
|
+
const typeVarContext = (0, typeUtils_1.buildTypeVarContextFromSpecializedClass)(baseClassMemberInfo.classType);
|
11939
|
+
inferredParamType = (0, typeUtils_1.applySolvedTypeVars)(inferredParamType, typeVarContext);
|
11940
|
+
}
|
12058
11941
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(functionNode);
|
12059
11942
|
if (fileInfo.isInPyTypedPackage && !fileInfo.isStubFile) {
|
12060
11943
|
inferredParamType = types_1.TypeBase.cloneForAmbiguousType(inferredParamType);
|
@@ -12147,268 +12030,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12147
12030
|
}
|
12148
12031
|
}
|
12149
12032
|
}
|
12150
|
-
// Scans through the decorators to find a few built-in decorators
|
12151
|
-
// that affect the function flags.
|
12152
|
-
function getFunctionFlagsFromDecorators(node, isInClass) {
|
12153
|
-
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
12154
|
-
let flags = 0 /* None */;
|
12155
|
-
if (isInClass) {
|
12156
|
-
// The "__new__" magic method is not an instance method.
|
12157
|
-
// It acts as a static method instead.
|
12158
|
-
if (node.name.value === '__new__') {
|
12159
|
-
flags |= 1 /* ConstructorMethod */;
|
12160
|
-
}
|
12161
|
-
// Several magic methods are treated as class methods implicitly
|
12162
|
-
// by the runtime. Check for these here.
|
12163
|
-
const implicitClassMethods = ['__init_subclass__', '__class_getitem__'];
|
12164
|
-
if (implicitClassMethods.some((name) => node.name.value === name)) {
|
12165
|
-
flags |= 2 /* ClassMethod */;
|
12166
|
-
}
|
12167
|
-
}
|
12168
|
-
for (const decoratorNode of node.decorators) {
|
12169
|
-
// Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators.
|
12170
|
-
let evaluatorFlags = fileInfo.isStubFile ? 4 /* AllowForwardReferences */ : 0 /* None */;
|
12171
|
-
if (decoratorNode.expression.nodeType !== 9 /* Call */) {
|
12172
|
-
evaluatorFlags |= 2 /* DoNotSpecialize */;
|
12173
|
-
}
|
12174
|
-
const decoratorTypeResult = getTypeOfExpression(decoratorNode.expression, evaluatorFlags);
|
12175
|
-
const decoratorType = decoratorTypeResult.type;
|
12176
|
-
if ((0, types_1.isFunction)(decoratorType)) {
|
12177
|
-
if (decoratorType.details.builtInName === 'abstractmethod') {
|
12178
|
-
if (isInClass) {
|
12179
|
-
flags |= 8 /* AbstractMethod */;
|
12180
|
-
}
|
12181
|
-
}
|
12182
|
-
else if (decoratorType.details.builtInName === 'final') {
|
12183
|
-
flags |= 8192 /* Final */;
|
12184
|
-
}
|
12185
|
-
else if (decoratorType.details.builtInName === 'override') {
|
12186
|
-
flags |= 262144 /* Overridden */;
|
12187
|
-
}
|
12188
|
-
}
|
12189
|
-
else if ((0, types_1.isInstantiableClass)(decoratorType)) {
|
12190
|
-
if (types_1.ClassType.isBuiltIn(decoratorType, 'staticmethod')) {
|
12191
|
-
if (isInClass) {
|
12192
|
-
flags |= 4 /* StaticMethod */;
|
12193
|
-
}
|
12194
|
-
}
|
12195
|
-
else if (types_1.ClassType.isBuiltIn(decoratorType, 'classmethod')) {
|
12196
|
-
if (isInClass) {
|
12197
|
-
flags |= 2 /* ClassMethod */;
|
12198
|
-
}
|
12199
|
-
}
|
12200
|
-
}
|
12201
|
-
}
|
12202
|
-
return flags;
|
12203
|
-
}
|
12204
|
-
// Transforms the input function type into an output type based on the
|
12205
|
-
// decorator function described by the decoratorNode.
|
12206
|
-
function applyFunctionDecorator(inputFunctionType, undecoratedType, decoratorNode, functionNode) {
|
12207
|
-
const fileInfo = AnalyzerNodeInfo.getFileInfo(decoratorNode);
|
12208
|
-
// Some stub files (e.g. builtins.pyi) rely on forward declarations of decorators.
|
12209
|
-
let evaluatorFlags = fileInfo.isStubFile ? 4 /* AllowForwardReferences */ : 0 /* None */;
|
12210
|
-
if (decoratorNode.expression.nodeType !== 9 /* Call */) {
|
12211
|
-
evaluatorFlags |= 2 /* DoNotSpecialize */;
|
12212
|
-
}
|
12213
|
-
const decoratorTypeResult = getTypeOfExpression(decoratorNode.expression, evaluatorFlags);
|
12214
|
-
const decoratorType = decoratorTypeResult.type;
|
12215
|
-
// Special-case the "overload" because it has no definition. Older versions of typeshed
|
12216
|
-
// defined "overload" as an object, but newer versions define it as a function.
|
12217
|
-
if (((0, types_1.isInstantiableClass)(decoratorType) && types_1.ClassType.isSpecialBuiltIn(decoratorType, 'overload')) ||
|
12218
|
-
((0, types_1.isFunction)(decoratorType) && decoratorType.details.builtInName === 'overload')) {
|
12219
|
-
if ((0, types_1.isFunction)(inputFunctionType)) {
|
12220
|
-
inputFunctionType.details.flags |= 256 /* Overloaded */;
|
12221
|
-
undecoratedType.details.flags |= 256 /* Overloaded */;
|
12222
|
-
return inputFunctionType;
|
12223
|
-
}
|
12224
|
-
}
|
12225
|
-
if (decoratorNode.expression.nodeType === 9 /* Call */) {
|
12226
|
-
const decoratorCallType = getTypeOfExpression(decoratorNode.expression.leftExpression, evaluatorFlags | 2 /* DoNotSpecialize */).type;
|
12227
|
-
if ((0, types_1.isFunction)(decoratorCallType)) {
|
12228
|
-
if (decoratorCallType.details.name === '__dataclass_transform__' ||
|
12229
|
-
decoratorCallType.details.builtInName === 'dataclass_transform') {
|
12230
|
-
undecoratedType.details.decoratorDataClassBehaviors = (0, dataClasses_1.validateDataClassTransformDecorator)(evaluatorInterface, decoratorNode.expression);
|
12231
|
-
return inputFunctionType;
|
12232
|
-
}
|
12233
|
-
if (decoratorCallType.details.builtInName === 'deprecated') {
|
12234
|
-
undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
12235
|
-
return inputFunctionType;
|
12236
|
-
}
|
12237
|
-
}
|
12238
|
-
if ((0, types_1.isOverloadedFunction)(decoratorCallType)) {
|
12239
|
-
if (decoratorCallType.overloads.length > 0 &&
|
12240
|
-
decoratorCallType.overloads[0].details.builtInName === 'deprecated') {
|
12241
|
-
undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
12242
|
-
return inputFunctionType;
|
12243
|
-
}
|
12244
|
-
}
|
12245
|
-
}
|
12246
|
-
let returnType = getTypeOfDecorator(decoratorNode, inputFunctionType);
|
12247
|
-
// Check for some built-in decorator types with known semantics.
|
12248
|
-
if ((0, types_1.isFunction)(decoratorType)) {
|
12249
|
-
if (decoratorType.details.builtInName === 'abstractmethod') {
|
12250
|
-
return inputFunctionType;
|
12251
|
-
}
|
12252
|
-
if (decoratorType.details.builtInName === 'deprecated') {
|
12253
|
-
undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
12254
|
-
return inputFunctionType;
|
12255
|
-
}
|
12256
|
-
// Handle property setters and deleters.
|
12257
|
-
if (decoratorNode.expression.nodeType === 35 /* MemberAccess */) {
|
12258
|
-
const baseType = getTypeOfExpression(decoratorNode.expression.leftExpression, evaluatorFlags | 2 /* DoNotSpecialize */).type;
|
12259
|
-
if ((0, typeUtils_1.isProperty)(baseType)) {
|
12260
|
-
const memberName = decoratorNode.expression.memberName.value;
|
12261
|
-
if (memberName === 'setter') {
|
12262
|
-
if ((0, types_1.isFunction)(inputFunctionType)) {
|
12263
|
-
(0, properties_1.validatePropertyMethod)(evaluatorInterface, inputFunctionType, decoratorNode);
|
12264
|
-
return (0, properties_1.clonePropertyWithSetter)(evaluatorInterface, baseType, inputFunctionType, functionNode);
|
12265
|
-
}
|
12266
|
-
else {
|
12267
|
-
return inputFunctionType;
|
12268
|
-
}
|
12269
|
-
}
|
12270
|
-
else if (memberName === 'deleter') {
|
12271
|
-
if ((0, types_1.isFunction)(inputFunctionType)) {
|
12272
|
-
(0, properties_1.validatePropertyMethod)(evaluatorInterface, inputFunctionType, decoratorNode);
|
12273
|
-
return (0, properties_1.clonePropertyWithDeleter)(evaluatorInterface, baseType, inputFunctionType, functionNode);
|
12274
|
-
}
|
12275
|
-
else {
|
12276
|
-
return inputFunctionType;
|
12277
|
-
}
|
12278
|
-
}
|
12279
|
-
}
|
12280
|
-
}
|
12281
|
-
}
|
12282
|
-
else if ((0, types_1.isOverloadedFunction)(decoratorType)) {
|
12283
|
-
if (decoratorType.overloads.length > 0 && decoratorType.overloads[0].details.builtInName === 'deprecated') {
|
12284
|
-
undecoratedType.details.deprecatedMessage = getCustomDeprecationMessage(decoratorNode);
|
12285
|
-
return inputFunctionType;
|
12286
|
-
}
|
12287
|
-
}
|
12288
|
-
else if ((0, types_1.isInstantiableClass)(decoratorType)) {
|
12289
|
-
if (types_1.ClassType.isBuiltIn(decoratorType)) {
|
12290
|
-
switch (decoratorType.details.name) {
|
12291
|
-
case 'classmethod':
|
12292
|
-
case 'staticmethod': {
|
12293
|
-
const requiredFlag = decoratorType.details.name === 'classmethod'
|
12294
|
-
? 2 /* ClassMethod */
|
12295
|
-
: 4 /* StaticMethod */;
|
12296
|
-
// If the function isn't currently a class method or static method
|
12297
|
-
// (which can happen if the function was wrapped in a decorator),
|
12298
|
-
// add the appropriate flag.
|
12299
|
-
if ((0, types_1.isFunction)(inputFunctionType) && (inputFunctionType.details.flags & requiredFlag) === 0) {
|
12300
|
-
const newFunction = types_1.FunctionType.clone(inputFunctionType);
|
12301
|
-
newFunction.details.flags &= ~(1 /* ConstructorMethod */ |
|
12302
|
-
4 /* StaticMethod */ |
|
12303
|
-
2 /* ClassMethod */);
|
12304
|
-
newFunction.details.flags |= requiredFlag;
|
12305
|
-
return newFunction;
|
12306
|
-
}
|
12307
|
-
return inputFunctionType;
|
12308
|
-
}
|
12309
|
-
}
|
12310
|
-
}
|
12311
|
-
// Handle properties and subclasses of properties specially.
|
12312
|
-
if (types_1.ClassType.isPropertyClass(decoratorType)) {
|
12313
|
-
if ((0, types_1.isFunction)(inputFunctionType)) {
|
12314
|
-
(0, properties_1.validatePropertyMethod)(evaluatorInterface, inputFunctionType, decoratorNode);
|
12315
|
-
return (0, properties_1.createProperty)(evaluatorInterface, decoratorNode, decoratorType, inputFunctionType);
|
12316
|
-
}
|
12317
|
-
else if ((0, types_1.isClassInstance)(inputFunctionType)) {
|
12318
|
-
const boundMethod = getBoundMethod(inputFunctionType, '__call__');
|
12319
|
-
if (boundMethod && (0, types_1.isFunction)(boundMethod)) {
|
12320
|
-
return (0, properties_1.createProperty)(evaluatorInterface, decoratorNode, decoratorType, boundMethod);
|
12321
|
-
}
|
12322
|
-
return types_1.UnknownType.create();
|
12323
|
-
}
|
12324
|
-
}
|
12325
|
-
}
|
12326
|
-
if ((0, types_1.isFunction)(inputFunctionType) && (0, types_1.isFunction)(returnType)) {
|
12327
|
-
returnType = types_1.FunctionType.clone(returnType);
|
12328
|
-
// Copy the overload flag from the input function type.
|
12329
|
-
if (types_1.FunctionType.isOverloaded(inputFunctionType)) {
|
12330
|
-
returnType.details.flags |= 256 /* Overloaded */;
|
12331
|
-
}
|
12332
|
-
// Copy the docstrings from the input function type if the
|
12333
|
-
// decorator didn't have its own docstring.
|
12334
|
-
if (!returnType.details.docString) {
|
12335
|
-
returnType.details.docString = inputFunctionType.details.docString;
|
12336
|
-
}
|
12337
|
-
}
|
12338
|
-
return returnType;
|
12339
|
-
}
|
12340
|
-
// Given a function node and the function type associated with it, this
|
12341
|
-
// method searches for prior function nodes that are marked as @overload
|
12342
|
-
// and creates an OverloadedFunctionType that includes this function and
|
12343
|
-
// all previous ones.
|
12344
|
-
function addOverloadsToFunctionType(node, type) {
|
12345
|
-
let functionDecl;
|
12346
|
-
const decl = AnalyzerNodeInfo.getDeclaration(node);
|
12347
|
-
if (decl) {
|
12348
|
-
functionDecl = decl;
|
12349
|
-
}
|
12350
|
-
const symbolWithScope = lookUpSymbolRecursive(node, node.name.value, /* honorCodeFlow */ false);
|
12351
|
-
if (symbolWithScope) {
|
12352
|
-
const decls = symbolWithScope.symbol.getDeclarations();
|
12353
|
-
// Find this function's declaration.
|
12354
|
-
const declIndex = decls.findIndex((decl) => decl === functionDecl);
|
12355
|
-
if (declIndex > 0) {
|
12356
|
-
// Evaluate all of the previous function declarations. They will
|
12357
|
-
// be cached. We do it in this order to avoid a stack overflow due
|
12358
|
-
// to recursion if there is a large number (1000's) of overloads.
|
12359
|
-
for (let i = 0; i < declIndex; i++) {
|
12360
|
-
const decl = decls[i];
|
12361
|
-
if (decl.type === 5 /* Function */) {
|
12362
|
-
getTypeOfFunction(decl.node);
|
12363
|
-
}
|
12364
|
-
}
|
12365
|
-
let overloadedTypes = [];
|
12366
|
-
// Look at the previous declaration's type.
|
12367
|
-
const prevDecl = decls[declIndex - 1];
|
12368
|
-
if (prevDecl.type === 5 /* Function */) {
|
12369
|
-
const prevDeclDeclTypeInfo = getTypeOfFunction(prevDecl.node);
|
12370
|
-
if (prevDeclDeclTypeInfo) {
|
12371
|
-
if ((0, types_1.isFunction)(prevDeclDeclTypeInfo.decoratedType)) {
|
12372
|
-
if (types_1.FunctionType.isOverloaded(prevDeclDeclTypeInfo.decoratedType)) {
|
12373
|
-
overloadedTypes.push(prevDeclDeclTypeInfo.decoratedType);
|
12374
|
-
}
|
12375
|
-
}
|
12376
|
-
else if ((0, types_1.isOverloadedFunction)(prevDeclDeclTypeInfo.decoratedType)) {
|
12377
|
-
// If the previous declaration was itself an overloaded function,
|
12378
|
-
// copy the entries from it.
|
12379
|
-
(0, collectionUtils_1.appendArray)(overloadedTypes, prevDeclDeclTypeInfo.decoratedType.overloads);
|
12380
|
-
}
|
12381
|
-
}
|
12382
|
-
}
|
12383
|
-
overloadedTypes.push(type);
|
12384
|
-
if (overloadedTypes.length === 1) {
|
12385
|
-
return overloadedTypes[0];
|
12386
|
-
}
|
12387
|
-
// Apply the implementation's docstring to any overloads that don't
|
12388
|
-
// have their own docstrings.
|
12389
|
-
const implementation = overloadedTypes.find((signature) => !types_1.FunctionType.isOverloaded(signature));
|
12390
|
-
if (implementation === null || implementation === void 0 ? void 0 : implementation.details.docString) {
|
12391
|
-
overloadedTypes = overloadedTypes.map((overload) => {
|
12392
|
-
if (types_1.FunctionType.isOverloaded(overload) && !overload.details.docString) {
|
12393
|
-
return types_1.FunctionType.cloneWithDocString(overload, implementation.details.docString);
|
12394
|
-
}
|
12395
|
-
return overload;
|
12396
|
-
});
|
12397
|
-
}
|
12398
|
-
// Create a new overloaded type that copies the contents of the previous
|
12399
|
-
// one and adds a new function.
|
12400
|
-
const newOverload = types_1.OverloadedFunctionType.create(overloadedTypes);
|
12401
|
-
const prevOverload = overloadedTypes[overloadedTypes.length - 2];
|
12402
|
-
const isPrevOverloadAbstract = types_1.FunctionType.isAbstractMethod(prevOverload);
|
12403
|
-
const isCurrentOverloadAbstract = types_1.FunctionType.isAbstractMethod(type);
|
12404
|
-
if (isPrevOverloadAbstract !== isCurrentOverloadAbstract) {
|
12405
|
-
addDiagnostic(AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.reportGeneralTypeIssues, diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.Localizer.Diagnostic.overloadAbstractMismatch().format({ name: node.name.value }), node.name);
|
12406
|
-
}
|
12407
|
-
return newOverload;
|
12408
|
-
}
|
12409
|
-
}
|
12410
|
-
return type;
|
12411
|
-
}
|
12412
12033
|
function createAsyncFunction(node, functionType) {
|
12413
12034
|
// Clone the original function and replace its return type with an
|
12414
12035
|
// Awaitable[<returnType>].
|
@@ -13314,7 +12935,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13314
12935
|
// See if the function is a method in a child class. We may be able to
|
13315
12936
|
// infer the type of the parameter from a method of the same name in
|
13316
12937
|
// a parent class if it has an annotated type.
|
13317
|
-
const functionFlags = getFunctionFlagsFromDecorators(functionNode, /* isInClass */ true);
|
12938
|
+
const functionFlags = (0, decorators_1.getFunctionFlagsFromDecorators)(evaluatorInterface, functionNode, /* isInClass */ true);
|
13318
12939
|
const inferredParamType = inferParameterType(functionNode, functionFlags, paramIndex, classInfo === null || classInfo === void 0 ? void 0 : classInfo.classType);
|
13319
12940
|
writeTypeCache(node.name, { type: transformVariadicParamType(node, node.category, inferredParamType !== null && inferredParamType !== void 0 ? inferredParamType : types_1.UnknownType.create()) }, 0 /* None */);
|
13320
12941
|
}
|
@@ -13823,7 +13444,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13823
13444
|
const specializedClass = types_1.ClassType.cloneForSpecialization(classType, typeArgTypes, typeArgs !== undefined);
|
13824
13445
|
return { type: specializedClass };
|
13825
13446
|
}
|
13826
|
-
function getTypeOfArgument(arg) {
|
13447
|
+
function getTypeOfArgument(arg, inferenceContext) {
|
13827
13448
|
if (arg.typeResult) {
|
13828
13449
|
return { type: arg.typeResult.type, isIncomplete: arg.typeResult.isIncomplete };
|
13829
13450
|
}
|
@@ -13833,7 +13454,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13833
13454
|
}
|
13834
13455
|
// If there was no defined type provided, there should always
|
13835
13456
|
// be a value expression from which we can retrieve the type.
|
13836
|
-
return getTypeOfExpression(arg.valueExpression);
|
13457
|
+
return getTypeOfExpression(arg.valueExpression, /* flags */ undefined, inferenceContext);
|
13837
13458
|
}
|
13838
13459
|
// This function is like getTypeOfArgument except that it is
|
13839
13460
|
// used in cases where the argument is expected to be a type
|
@@ -13848,7 +13469,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13848
13469
|
return getTypeOfExpressionExpectingType(arg.valueExpression);
|
13849
13470
|
}
|
13850
13471
|
function getTypeOfExpressionExpectingType(node, options) {
|
13851
|
-
let flags = 128 /*
|
13472
|
+
let flags = 128 /* ExpectingInstantiableType */ |
|
13473
|
+
8 /* EvaluateStringLiteralAsType */ |
|
13474
|
+
131072 /* DisallowClassVar */;
|
13852
13475
|
if (!(options === null || options === void 0 ? void 0 : options.allowTypeVarsWithoutScopeId)) {
|
13853
13476
|
flags |= 4096 /* DisallowTypeVarsWithoutScopeId */;
|
13854
13477
|
}
|
@@ -15420,34 +15043,39 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15420
15043
|
let isAssignable = true;
|
15421
15044
|
destType.details.fields.forEach((symbol, name) => {
|
15422
15045
|
var _a;
|
15423
|
-
if (isAssignable
|
15424
|
-
|
15425
|
-
|
15426
|
-
|
15427
|
-
|
15428
|
-
|
15429
|
-
|
15430
|
-
|
15431
|
-
|
15432
|
-
|
15433
|
-
|
15434
|
-
|
15435
|
-
|
15436
|
-
|
15437
|
-
|
15438
|
-
|
15046
|
+
if (!isAssignable || !symbol.isClassMember() || symbol.isIgnoredForProtocolMatch()) {
|
15047
|
+
return;
|
15048
|
+
}
|
15049
|
+
// Constructor methods are exempt from variance calculations.
|
15050
|
+
if (name === '__new__' || name === '__init__') {
|
15051
|
+
return;
|
15052
|
+
}
|
15053
|
+
const memberInfo = (0, typeUtils_1.lookUpClassMember)(srcType, name);
|
15054
|
+
(0, debug_1.assert)(memberInfo !== undefined);
|
15055
|
+
let destMemberType = (_a = getDeclaredTypeOfSymbol(symbol)) === null || _a === void 0 ? void 0 : _a.type;
|
15056
|
+
if (destMemberType) {
|
15057
|
+
const srcMemberType = getTypeOfMember(memberInfo);
|
15058
|
+
destMemberType = (0, typeUtils_1.partiallySpecializeType)(destMemberType, destType);
|
15059
|
+
// Properties require special processing.
|
15060
|
+
if ((0, types_1.isClassInstance)(destMemberType) &&
|
15061
|
+
types_1.ClassType.isPropertyClass(destMemberType) &&
|
15062
|
+
(0, types_1.isClassInstance)(srcMemberType) &&
|
15063
|
+
types_1.ClassType.isPropertyClass(srcMemberType)) {
|
15064
|
+
if (!(0, properties_1.assignProperty)(evaluatorInterface, types_1.ClassType.cloneAsInstantiable(destMemberType), types_1.ClassType.cloneAsInstantiable(srcMemberType), destType, srcType, diag, typeVarContext,
|
15065
|
+
/* selfTypeVarContext */ undefined, recursionCount)) {
|
15066
|
+
isAssignable = false;
|
15439
15067
|
}
|
15440
|
-
|
15441
|
-
|
15442
|
-
|
15443
|
-
|
15444
|
-
|
15445
|
-
|
15446
|
-
|
15447
|
-
|
15448
|
-
|
15449
|
-
|
15450
|
-
|
15068
|
+
}
|
15069
|
+
else {
|
15070
|
+
const primaryDecl = symbol.getDeclarations()[0];
|
15071
|
+
// Class and instance variables that are mutable need to
|
15072
|
+
// enforce invariance.
|
15073
|
+
const flags = (primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* Variable */ && !isFinalVariableDeclaration(primaryDecl)
|
15074
|
+
? 1 /* EnforceInvariance */
|
15075
|
+
: 0 /* Default */;
|
15076
|
+
if (!assignType(destMemberType, srcMemberType, diag, typeVarContext,
|
15077
|
+
/* srcTypeVarContext */ undefined, flags, recursionCount)) {
|
15078
|
+
isAssignable = false;
|
15451
15079
|
}
|
15452
15080
|
}
|
15453
15081
|
}
|
@@ -15544,7 +15172,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15544
15172
|
}
|
15545
15173
|
}
|
15546
15174
|
}
|
15547
|
-
function
|
15175
|
+
function assignTupleTypeArguments(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
15548
15176
|
var _a, _b;
|
15549
15177
|
const destTypeArgs = [...((_a = destType.tupleTypeArguments) !== null && _a !== void 0 ? _a : [])];
|
15550
15178
|
const srcTypeArgs = [...((_b = srcType.tupleTypeArguments) !== null && _b !== void 0 ? _b : [])];
|
@@ -15586,32 +15214,28 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15586
15214
|
function assignClassWithTypeArgs(destType, srcType, inheritanceChain, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
15587
15215
|
let curSrcType = srcType;
|
15588
15216
|
let prevSrcType;
|
15589
|
-
let curDestTypeVarContext = destTypeVarContext;
|
15590
|
-
let effectiveFlags = flags;
|
15591
15217
|
inferTypeParameterVarianceForClass(destType);
|
15592
|
-
|
15593
|
-
if (
|
15594
|
-
|
15595
|
-
|
15218
|
+
// If we're enforcing invariance, literal types must match.
|
15219
|
+
if ((flags & 1 /* EnforceInvariance */) !== 0) {
|
15220
|
+
const srcIsLiteral = srcType.literalValue !== undefined;
|
15221
|
+
const destIsLiteral = destType.literalValue !== undefined;
|
15222
|
+
if (srcIsLiteral !== destIsLiteral) {
|
15223
|
+
return false;
|
15224
|
+
}
|
15596
15225
|
}
|
15597
15226
|
else {
|
15598
|
-
// If
|
15599
|
-
|
15600
|
-
|
15601
|
-
|
15227
|
+
// If the dest is an 'object', it's assignable.
|
15228
|
+
if (types_1.ClassType.isBuiltIn(destType, 'object')) {
|
15229
|
+
return true;
|
15230
|
+
}
|
15602
15231
|
}
|
15603
15232
|
for (let ancestorIndex = inheritanceChain.length - 1; ancestorIndex >= 0; ancestorIndex--) {
|
15604
15233
|
const ancestorType = inheritanceChain[ancestorIndex];
|
15605
|
-
const curSrcTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(curSrcType));
|
15606
15234
|
// If we've hit an "unknown", all bets are off, and we need to assume
|
15607
15235
|
// that the type is assignable.
|
15608
15236
|
if ((0, types_1.isUnknown)(ancestorType)) {
|
15609
15237
|
return true;
|
15610
15238
|
}
|
15611
|
-
// If we've hit an 'object', it's assignable.
|
15612
|
-
if (types_1.ClassType.isBuiltIn(ancestorType, 'object')) {
|
15613
|
-
return true;
|
15614
|
-
}
|
15615
15239
|
// If this isn't the first time through the loop, specialize
|
15616
15240
|
// for the next ancestor in the chain.
|
15617
15241
|
if (ancestorIndex < inheritanceChain.length - 1) {
|
@@ -15626,11 +15250,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15626
15250
|
}
|
15627
15251
|
curSrcType = (0, typeUtils_1.specializeForBaseClass)(effectiveCurSrcType, ancestorType);
|
15628
15252
|
}
|
15629
|
-
// Handle built-in types that support arbitrary numbers
|
15630
|
-
// of type parameters like Tuple.
|
15631
|
-
if (ancestorIndex === 0 && destType.tupleTypeArguments && curSrcType.tupleTypeArguments) {
|
15632
|
-
return assignTupleTypeArgs(destType, curSrcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
|
15633
|
-
}
|
15634
15253
|
// If there are no type parameters on this class, we're done.
|
15635
15254
|
const ancestorTypeParams = types_1.ClassType.getTypeParameters(ancestorType);
|
15636
15255
|
if (ancestorTypeParams.length === 0) {
|
@@ -15640,13 +15259,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15640
15259
|
if (!ancestorType.typeArguments) {
|
15641
15260
|
return true;
|
15642
15261
|
}
|
15643
|
-
// Validate that the type arguments match.
|
15644
|
-
if (!verifyTypeArgumentsAssignable(ancestorType, curSrcType, diag, curDestTypeVarContext, curSrcTypeVarContext, effectiveFlags, recursionCount)) {
|
15645
|
-
return false;
|
15646
|
-
}
|
15647
|
-
// Allocate a new type var map for the next time through the loop.
|
15648
|
-
curDestTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(ancestorType));
|
15649
|
-
effectiveFlags &= ~8 /* SkipSolveTypeVars */;
|
15650
15262
|
prevSrcType = curSrcType;
|
15651
15263
|
}
|
15652
15264
|
// If we're enforcing invariance, literal types must match as well.
|
@@ -15657,17 +15269,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15657
15269
|
return false;
|
15658
15270
|
}
|
15659
15271
|
}
|
15272
|
+
// Handle tuple, which supports a variable number of type arguments.
|
15273
|
+
if (destType.tupleTypeArguments && curSrcType.tupleTypeArguments) {
|
15274
|
+
return assignTupleTypeArguments(destType, curSrcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
|
15275
|
+
}
|
15660
15276
|
if (destType.typeArguments) {
|
15661
15277
|
// If the dest type is specialized, make sure the specialized source
|
15662
15278
|
// type arguments are assignable to the dest type arguments.
|
15663
|
-
|
15664
|
-
return false;
|
15665
|
-
}
|
15279
|
+
return assignTypeArguments(destType, curSrcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount);
|
15666
15280
|
}
|
15667
|
-
|
15668
|
-
destType.details.typeParameters.length > 0 &&
|
15669
|
-
curSrcType.typeArguments &&
|
15670
|
-
!destTypeVarContext.isLocked()) {
|
15281
|
+
if (destTypeVarContext && curSrcType.typeArguments && !destTypeVarContext.isLocked()) {
|
15671
15282
|
// Populate the typeVar map with type arguments of the source.
|
15672
15283
|
const srcTypeArgs = curSrcType.typeArguments;
|
15673
15284
|
for (let i = 0; i < destType.details.typeParameters.length; i++) {
|
@@ -15693,7 +15304,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15693
15304
|
}
|
15694
15305
|
return undefined;
|
15695
15306
|
}
|
15696
|
-
function
|
15307
|
+
function assignTypeArguments(destType, srcType, diag, destTypeVarContext, srcTypeVarContext, flags, recursionCount) {
|
15697
15308
|
var _a, _b, _c;
|
15698
15309
|
(0, debug_1.assert)(types_1.ClassType.isSameGenericClass(destType, srcType));
|
15699
15310
|
inferTypeParameterVarianceForClass(destType);
|
@@ -15754,7 +15365,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15754
15365
|
}
|
15755
15366
|
}
|
15756
15367
|
else {
|
15757
|
-
if (!assignType(destTypeArg, srcTypeArg, assignmentDiag, destTypeVarContext, srcTypeVarContext, flags | 1 /* EnforceInvariance */, recursionCount)) {
|
15368
|
+
if (!assignType(destTypeArg, srcTypeArg, assignmentDiag, destTypeVarContext, srcTypeVarContext, flags | 1 /* EnforceInvariance */ | 128 /* RetainLiteralsForTypeVar */, recursionCount)) {
|
15758
15369
|
// Don't report errors with type variables in "pseudo-random"
|
15759
15370
|
// classes since these type variables are not real.
|
15760
15371
|
if (!types_1.ClassType.isPseudoGenericClass(destType)) {
|
@@ -16262,7 +15873,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16262
15873
|
return true;
|
16263
15874
|
}
|
16264
15875
|
if ((0, types_1.isFunction)(concreteSrcType)) {
|
16265
|
-
if (assignFunction(destType, concreteSrcType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), destTypeVarContext !== null && destTypeVarContext !== void 0 ? destTypeVarContext : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(destType)), srcTypeVarContext !== null && srcTypeVarContext !== void 0 ? srcTypeVarContext : new typeVarContext_1.TypeVarContext((0, typeUtils_1.
|
15876
|
+
if (assignFunction(destType, concreteSrcType, diag === null || diag === void 0 ? void 0 : diag.createAddendum(), destTypeVarContext !== null && destTypeVarContext !== void 0 ? destTypeVarContext : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(destType)), srcTypeVarContext !== null && srcTypeVarContext !== void 0 ? srcTypeVarContext : new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeIds)(concreteSrcType)), flags, recursionCount)) {
|
16266
15877
|
return true;
|
16267
15878
|
}
|
16268
15879
|
}
|
@@ -17833,7 +17444,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17833
17444
|
let isAbstract;
|
17834
17445
|
const decl = (0, symbolUtils_1.getLastTypedDeclaredForSymbol)(symbol);
|
17835
17446
|
if (decl && decl.type === 5 /* Function */) {
|
17836
|
-
const functionFlags = getFunctionFlagsFromDecorators(decl.node, true);
|
17447
|
+
const functionFlags = (0, decorators_1.getFunctionFlagsFromDecorators)(evaluatorInterface, decl.node, true);
|
17837
17448
|
isAbstract = !!(functionFlags & 8 /* AbstractMethod */);
|
17838
17449
|
}
|
17839
17450
|
else {
|
@@ -17964,7 +17575,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17964
17575
|
}
|
17965
17576
|
}
|
17966
17577
|
else if (!assignType(memberTypeFirstParamType, firstParamType, diag, typeVarContext,
|
17967
|
-
/* srcTypeVarContext */ undefined,
|
17578
|
+
/* srcTypeVarContext */ undefined, 2048 /* AllowUnspecifiedTypeArguments */, recursionCount)) {
|
17968
17579
|
if (memberTypeFirstParam.name &&
|
17969
17580
|
!memberTypeFirstParam.isNameSynthesized &&
|
17970
17581
|
memberTypeFirstParam.hasDeclaredType) {
|
@@ -18066,6 +17677,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18066
17677
|
if (options === null || options === void 0 ? void 0 : options.printUnknownWithAny) {
|
18067
17678
|
flags |= 1 /* PrintUnknownWithAny */;
|
18068
17679
|
}
|
17680
|
+
if (options === null || options === void 0 ? void 0 : options.printTypeVarVariance) {
|
17681
|
+
flags |= 2048 /* PrintTypeVarVariance */;
|
17682
|
+
}
|
18069
17683
|
if (options === null || options === void 0 ? void 0 : options.omitTypeArgumentsIfUnknown) {
|
18070
17684
|
flags |= 2 /* OmitTypeArgumentsIfUnknown */;
|
18071
17685
|
}
|
@@ -18208,7 +17822,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18208
17822
|
getObjectType,
|
18209
17823
|
getBuiltInObject,
|
18210
17824
|
getTypingType,
|
18211
|
-
|
17825
|
+
assignTypeArguments,
|
18212
17826
|
reportMissingTypeArguments,
|
18213
17827
|
inferReturnTypeIfNecessary,
|
18214
17828
|
inferTypeParameterVarianceForClass,
|