@zzzen/pyright-internal 1.2.0-dev.20240218 → 1.2.0-dev.20240307
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 +5 -3
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.d.ts +1 -2
- package/dist/analyzer/checker.js +138 -49
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.d.ts +3 -1
- package/dist/analyzer/codeFlowEngine.js +19 -14
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/dataClasses.js +6 -6
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/decorators.js +6 -6
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/enums.js +10 -3
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/importResolver.js +11 -3
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/importStatementUtils.js +2 -0
- package/dist/analyzer/importStatementUtils.js.map +1 -1
- package/dist/analyzer/namedTuples.js +1 -1
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +12 -3
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +14 -6
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.js +15 -4
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +3 -3
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/properties.js +2 -2
- package/dist/analyzer/service.js +1 -1
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.js +4 -4
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +207 -131
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +1 -0
- package/dist/analyzer/typeGuards.js +5 -5
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.js +1 -1
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +2 -0
- package/dist/analyzer/typeUtils.js +45 -8
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.d.ts +6 -3
- package/dist/analyzer/typedDicts.js +292 -65
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +31 -23
- package/dist/analyzer/types.js +60 -45
- package/dist/analyzer/types.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +18 -18
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/cancellationUtils.d.ts +4 -0
- package/dist/common/cancellationUtils.js +2 -1
- package/dist/common/cancellationUtils.js.map +1 -1
- package/dist/common/configOptions.d.ts +2 -2
- package/dist/common/configOptions.js +21 -4
- package/dist/common/configOptions.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +1 -0
- package/dist/languageService/autoImporter.js +3 -1
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/completionProvider.js +4 -4
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.js +1 -1
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/localization/localize.d.ts +31 -2
- package/dist/localization/localize.js +10 -2
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.cs.json +6 -8
- package/dist/localization/package.nls.de.json +6 -8
- package/dist/localization/package.nls.en-us.json +19 -11
- package/dist/localization/package.nls.es.json +6 -8
- package/dist/localization/package.nls.fr.json +5 -7
- package/dist/localization/package.nls.it.json +6 -8
- package/dist/localization/package.nls.ja.json +7 -9
- package/dist/localization/package.nls.ko.json +6 -8
- package/dist/localization/package.nls.pl.json +6 -8
- package/dist/localization/package.nls.pt-br.json +3 -5
- package/dist/localization/package.nls.qps-ploc.json +0 -2
- package/dist/localization/package.nls.ru.json +3 -5
- package/dist/localization/package.nls.tr.json +6 -8
- package/dist/localization/package.nls.zh-cn.json +3 -5
- package/dist/localization/package.nls.zh-tw.json +6 -8
- package/dist/tests/checker.test.js +0 -4
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/fourslash/import.nameconflict.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/import.nameconflict.fourslash.js +17 -0
- package/dist/tests/fourslash/import.nameconflict.fourslash.js.map +1 -0
- package/dist/tests/harness/fourslash/testState.js +11 -0
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/importStatementUtils.test.js +8 -0
- package/dist/tests/importStatementUtils.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +13 -1
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +2 -2
- package/dist/tests/typeEvaluator3.test.js +25 -9
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +1 -1
- package/dist/tests/typeEvaluator5.test.js +42 -2
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/package.json +4 -4
- package/dist/analyzer/regions.d.ts +0 -11
- package/dist/analyzer/regions.js +0 -62
- package/dist/analyzer/regions.js.map +0 -1
@@ -198,6 +198,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
198
198
|
let dictClassType;
|
199
199
|
let typedDictClassType;
|
200
200
|
let typedDictPrivateClassType;
|
201
|
+
let supportsKeysAndGetItemProtocolType;
|
201
202
|
let mappingType;
|
202
203
|
let printExpressionSpaceCount = 0;
|
203
204
|
let incompleteGenerationCount = 0;
|
@@ -466,7 +467,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
466
467
|
functionObj = getBuiltInObject(node, 'function');
|
467
468
|
unionClassType = getTypesType(node, 'UnionType');
|
468
469
|
if (unionClassType && (0, types_1.isClass)(unionClassType)) {
|
469
|
-
unionClassType.details.flags |=
|
470
|
+
unionClassType.details.flags |= -2147483648 /* ClassTypeFlags.SpecialFormClass */;
|
470
471
|
}
|
471
472
|
// Initialize and cache "Collection" to break a cyclical dependency
|
472
473
|
// that occurs when resolving tuple below.
|
@@ -483,10 +484,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
483
484
|
typedDictClassType = getTypingType(node, 'TypedDict');
|
484
485
|
typedDictPrivateClassType = getTypingType(node, '_TypedDict');
|
485
486
|
awaitableProtocolType = getTypingType(node, 'Awaitable');
|
486
|
-
mappingType =
|
487
|
-
|
487
|
+
mappingType = getTypingType(node, 'Mapping');
|
488
|
+
supportsKeysAndGetItemProtocolType = getTypeshedType(node, 'SupportsKeysAndGetItem');
|
489
|
+
if (!supportsKeysAndGetItemProtocolType) {
|
488
490
|
// Fall back on 'Mapping' if 'SupportsKeysAndGetItem' is not available.
|
489
|
-
|
491
|
+
supportsKeysAndGetItemProtocolType = mappingType;
|
490
492
|
}
|
491
493
|
// Wire up the `Any` class to the special-form version of our internal AnyType.
|
492
494
|
const objectClass = getBuiltInType(node, 'object');
|
@@ -494,7 +496,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
494
496
|
(0, types_1.isInstantiableClass)(objectClass) &&
|
495
497
|
typeClassType &&
|
496
498
|
(0, types_1.isInstantiableClass)(typeClassType)) {
|
497
|
-
const anyClass = types_1.ClassType.createInstantiable('Any', 'typing.Any', 'typing', uri_1.Uri.empty(), 1 /* ClassTypeFlags.BuiltInClass */ |
|
499
|
+
const anyClass = types_1.ClassType.createInstantiable('Any', 'typing.Any', 'typing', uri_1.Uri.empty(), 1 /* ClassTypeFlags.BuiltInClass */ | -2147483648 /* ClassTypeFlags.SpecialFormClass */,
|
498
500
|
/* typeSourceId */ -1,
|
499
501
|
/* declaredMetaclass */ undefined,
|
500
502
|
/* effectiveMetaclass */ typeClassType);
|
@@ -803,6 +805,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
803
805
|
const exprTypeResult = getTypeOfExpression(node.expression, flags, (0, typeUtils_1.makeInferenceContext)(effectiveExpectedType));
|
804
806
|
const typeResult = {
|
805
807
|
type: getTypeOfAwaitable(exprTypeResult.type, node.expression),
|
808
|
+
isIncomplete: exprTypeResult.isIncomplete,
|
809
|
+
typeErrors: exprTypeResult.typeErrors,
|
806
810
|
};
|
807
811
|
if (exprTypeResult.isIncomplete) {
|
808
812
|
typeResult.isIncomplete = true;
|
@@ -1547,7 +1551,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1547
1551
|
// see if we can find a better signature from the `__new__` method.
|
1548
1552
|
if (!constructorType || isObjectInit || isDefaultParams) {
|
1549
1553
|
const newMethodResult = (0, constructors_1.getBoundNewMethod)(evaluatorInterface, callNode, subtype, 0 /* MemberAccessFlags.Default */);
|
1550
|
-
if (newMethodResult && !newMethodResult.typeErrors
|
1554
|
+
if (newMethodResult && !newMethodResult.typeErrors) {
|
1551
1555
|
if ((0, types_1.isFunction)(newMethodResult.type) &&
|
1552
1556
|
newMethodResult.type.details.fullName !== 'builtins.object.__new__') {
|
1553
1557
|
constructorType = newMethodResult.type;
|
@@ -1582,7 +1586,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1582
1586
|
// If the function includes a `**kwargs: Unpack[TypedDict]` parameter, the
|
1583
1587
|
// parameter is expanded to include individual keyword args.
|
1584
1588
|
function expandTypedKwargs(functionType) {
|
1585
|
-
var _a;
|
1589
|
+
var _a, _b;
|
1586
1590
|
const kwargsIndex = functionType.details.parameters.findIndex((param) => param.category === 2 /* ParameterCategory.KwargsDict */);
|
1587
1591
|
if (kwargsIndex < 0) {
|
1588
1592
|
return functionType;
|
@@ -1592,7 +1596,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
1592
1596
|
if (!(0, types_1.isClassInstance)(kwargsType) || !types_1.ClassType.isTypedDictClass(kwargsType) || !kwargsType.isUnpacked) {
|
1593
1597
|
return functionType;
|
1594
1598
|
}
|
1595
|
-
const tdEntries = (_a = kwargsType.typedDictNarrowedEntries) !== null && _a !== void 0 ? _a : kwargsType.details.typedDictEntries;
|
1599
|
+
const tdEntries = (_a = kwargsType.typedDictNarrowedEntries) !== null && _a !== void 0 ? _a : (_b = kwargsType.details.typedDictEntries) === null || _b === void 0 ? void 0 : _b.knownItems;
|
1596
1600
|
if (!tdEntries) {
|
1597
1601
|
return functionType;
|
1598
1602
|
}
|
@@ -2034,10 +2038,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2034
2038
|
if (checkCodeFlowTooComplex(node)) {
|
2035
2039
|
return true;
|
2036
2040
|
}
|
2037
|
-
const codeFlowResult = analyzer.getTypeFromCodeFlow(flowNode,
|
2038
|
-
|
2039
|
-
|
2040
|
-
/* typeAtStart */ types_1.UnboundType.create());
|
2041
|
+
const codeFlowResult = analyzer.getTypeFromCodeFlow(flowNode, /* reference */ undefined, {
|
2042
|
+
typeAtStart: types_1.UnboundType.create(),
|
2043
|
+
});
|
2041
2044
|
return codeFlowResult.type !== undefined && !(0, types_1.isNever)(codeFlowResult.type);
|
2042
2045
|
}
|
2043
2046
|
// Determines whether there is a code flow path from sourceNode to sinkNode.
|
@@ -2192,8 +2195,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2192
2195
|
if (expectedTypeDiagAddendum) {
|
2193
2196
|
diagAddendum = expectedTypeDiagAddendum;
|
2194
2197
|
}
|
2195
|
-
|
2196
|
-
|
2198
|
+
if (!isTypeIncomplete) {
|
2199
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssignmentType, localize_1.LocMessage.typeAssignmentMismatch().format(printSrcDestTypes(type, declaredType)) +
|
2200
|
+
diagAddendum.getString(), srcExpression !== null && srcExpression !== void 0 ? srcExpression : nameNode, (_c = (_b = diagAddendum.getEffectiveTextRange()) !== null && _b !== void 0 ? _b : srcExpression) !== null && _c !== void 0 ? _c : nameNode);
|
2201
|
+
}
|
2197
2202
|
// Replace the assigned type with the (unnarrowed) declared type.
|
2198
2203
|
destType = declaredType;
|
2199
2204
|
}
|
@@ -2977,8 +2982,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
2977
2982
|
}
|
2978
2983
|
}
|
2979
2984
|
}
|
2980
|
-
const codeFlowTypeResult = getFlowTypeOfReference(node,
|
2981
|
-
|
2985
|
+
const codeFlowTypeResult = getFlowTypeOfReference(node, /* startNode */ undefined, {
|
2986
|
+
targetSymbolId: symbol.id,
|
2987
|
+
typeAtStart,
|
2982
2988
|
skipConditionalNarrowing: (flags & 256 /* EvaluatorFlags.ExpectingTypeAnnotation */) !== 0,
|
2983
2989
|
});
|
2984
2990
|
if (codeFlowTypeResult.type) {
|
@@ -3131,7 +3137,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3131
3137
|
if (symbolWithScope.symbol.isInitiallyUnbound()) {
|
3132
3138
|
typeAtStart = types_1.UnboundType.create();
|
3133
3139
|
}
|
3134
|
-
return getFlowTypeOfReference(node,
|
3140
|
+
return getFlowTypeOfReference(node, innerScopeNode, {
|
3141
|
+
targetSymbolId: symbolWithScope.symbol.id,
|
3142
|
+
typeAtStart,
|
3143
|
+
});
|
3135
3144
|
}
|
3136
3145
|
}
|
3137
3146
|
}
|
@@ -3455,8 +3464,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
3455
3464
|
}
|
3456
3465
|
}
|
3457
3466
|
// See if we can refine the type based on code flow analysis.
|
3458
|
-
const codeFlowTypeResult = getFlowTypeOfReference(node,
|
3459
|
-
|
3467
|
+
const codeFlowTypeResult = getFlowTypeOfReference(node, /* startNode */ undefined, {
|
3468
|
+
targetSymbolId: symbol_1.indeterminateSymbolId,
|
3469
|
+
typeAtStart,
|
3460
3470
|
isTypeAtStartIncomplete,
|
3461
3471
|
skipConditionalNarrowing: (flags & 256 /* EvaluatorFlags.ExpectingTypeAnnotation */) !== 0,
|
3462
3472
|
});
|
@@ -4383,10 +4393,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4383
4393
|
});
|
4384
4394
|
if (baseTypeSupportsIndexNarrowing) {
|
4385
4395
|
// Before performing code flow analysis, update the cache to prevent recursion.
|
4386
|
-
writeTypeCache(node, indexTypeResult, flags);
|
4396
|
+
writeTypeCache(node, { ...indexTypeResult, isIncomplete: true }, flags);
|
4387
4397
|
// See if we can refine the type based on code flow analysis.
|
4388
|
-
const codeFlowTypeResult = getFlowTypeOfReference(node,
|
4389
|
-
|
4398
|
+
const codeFlowTypeResult = getFlowTypeOfReference(node, /* startNode */ undefined, {
|
4399
|
+
targetSymbolId: symbol_1.indeterminateSymbolId,
|
4400
|
+
typeAtStart: indexTypeResult.type,
|
4390
4401
|
isTypeAtStartIncomplete: !!baseTypeResult.isIncomplete || !!indexTypeResult.isIncomplete,
|
4391
4402
|
skipConditionalNarrowing: (flags & 256 /* EvaluatorFlags.ExpectingTypeAnnotation */) !== 0,
|
4392
4403
|
});
|
@@ -4748,8 +4759,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
4748
4759
|
return createLiteralType(concreteSubtype, node, flags);
|
4749
4760
|
}
|
4750
4761
|
if (types_1.ClassType.isBuiltIn(concreteSubtype, 'InitVar')) {
|
4751
|
-
// Special-case InitVar, used in
|
4762
|
+
// Special-case InitVar, used in dataclasses.
|
4752
4763
|
const typeArgs = getTypeArgs(node, flags);
|
4764
|
+
if ((flags & 256 /* EvaluatorFlags.ExpectingTypeAnnotation */) !== 0) {
|
4765
|
+
if ((flags & 32768 /* EvaluatorFlags.VariableTypeAnnotation */) === 0) {
|
4766
|
+
addError(localize_1.LocMessage.initVarNotAllowed(), node.baseExpression);
|
4767
|
+
}
|
4768
|
+
}
|
4753
4769
|
if (typeArgs.length === 1) {
|
4754
4770
|
return typeArgs[0].type;
|
4755
4771
|
}
|
@@ -6870,7 +6886,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
6870
6886
|
function evaluateCastCall(argList, errorNode) {
|
6871
6887
|
// Verify that the cast is necessary.
|
6872
6888
|
const castToType = getTypeOfArgumentExpectingType(argList[0], { enforceTypeAnnotationRules: true }).type;
|
6873
|
-
|
6889
|
+
let castFromType = getTypeOfArgument(argList[1]).type;
|
6890
|
+
if (castFromType.specialForm) {
|
6891
|
+
castFromType = castFromType.specialForm;
|
6892
|
+
}
|
6874
6893
|
if (types_1.TypeBase.isInstantiable(castToType) && !(0, types_1.isUnknown)(castToType)) {
|
6875
6894
|
if ((0, types_1.isTypeSame)((0, typeUtils_1.convertToInstance)(castToType), castFromType, {
|
6876
6895
|
ignorePseudoGeneric: true,
|
@@ -7345,7 +7364,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7345
7364
|
// keys are present.
|
7346
7365
|
const typedDictEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, argType);
|
7347
7366
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
7348
|
-
typedDictEntries.forEach((entry, name) => {
|
7367
|
+
typedDictEntries.knownItems.forEach((entry, name) => {
|
7349
7368
|
const paramEntry = paramMap.get(name);
|
7350
7369
|
if (paramEntry && !paramEntry.isPositionalOnly) {
|
7351
7370
|
if (paramEntry.argsReceived > 0) {
|
@@ -7422,20 +7441,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7422
7441
|
}
|
7423
7442
|
else {
|
7424
7443
|
const strObjType = getBuiltInObject(errorNode, 'str');
|
7425
|
-
if (
|
7426
|
-
(0, types_1.isInstantiableClass)(
|
7444
|
+
if (supportsKeysAndGetItemProtocolType &&
|
7445
|
+
(0, types_1.isInstantiableClass)(supportsKeysAndGetItemProtocolType) &&
|
7427
7446
|
strObjType &&
|
7428
7447
|
(0, types_1.isClassInstance)(strObjType)) {
|
7429
|
-
const mappingTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(
|
7448
|
+
const mappingTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(supportsKeysAndGetItemProtocolType));
|
7430
7449
|
let isValidMappingType = false;
|
7431
7450
|
// If this was a TypeVar (e.g. for pseudo-generic classes),
|
7432
7451
|
// don't emit this error.
|
7433
7452
|
if ((0, types_1.isTypeVar)(argType)) {
|
7434
7453
|
isValidMappingType = true;
|
7435
7454
|
}
|
7436
|
-
else if (assignType(types_1.ClassType.cloneAsInstance(
|
7455
|
+
else if (assignType(types_1.ClassType.cloneAsInstance(supportsKeysAndGetItemProtocolType), argType,
|
7437
7456
|
/* diag */ undefined, mappingTypeVarContext)) {
|
7438
|
-
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(
|
7457
|
+
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(supportsKeysAndGetItemProtocolType, mappingTypeVarContext);
|
7439
7458
|
const typeArgs = specializedMapping.typeArguments;
|
7440
7459
|
if (typeArgs && typeArgs.length >= 2) {
|
7441
7460
|
if (assignType(strObjType, typeArgs[0])) {
|
@@ -7645,6 +7664,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
7645
7664
|
argumentCategory: 0 /* ArgumentCategory.Simple */,
|
7646
7665
|
typeResult: { type: defaultArgType },
|
7647
7666
|
},
|
7667
|
+
isDefaultArg: true,
|
7648
7668
|
errorNode,
|
7649
7669
|
paramName: param.name,
|
7650
7670
|
isParamNameSynthesized: param.isNameSynthesized,
|
@@ -8076,15 +8096,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8076
8096
|
if ((0, types_1.isUnpackedClass)(specializedReturnType)) {
|
8077
8097
|
specializedReturnType = types_1.ClassType.cloneForUnpacked(specializedReturnType, /* isUnpackedTuple */ false);
|
8078
8098
|
}
|
8079
|
-
// Handle 'TypeGuard' specially. We'll transform the return type
|
8080
|
-
// object with a type argument that reflects the narrowed type.
|
8099
|
+
// Handle 'TypeGuard' and 'TypeIs' specially. We'll transform the return type
|
8100
|
+
// into a 'bool' object with a type argument that reflects the narrowed type.
|
8081
8101
|
if ((0, types_1.isClassInstance)(specializedReturnType) &&
|
8082
|
-
types_1.ClassType.isBuiltIn(specializedReturnType, 'TypeGuard') &&
|
8102
|
+
types_1.ClassType.isBuiltIn(specializedReturnType, ['TypeGuard', 'TypeIs']) &&
|
8083
8103
|
specializedReturnType.typeArguments &&
|
8084
|
-
specializedReturnType.typeArguments.length > 0
|
8085
|
-
(0, types_1.isClassInstance)(returnType) &&
|
8086
|
-
returnType.typeArguments &&
|
8087
|
-
returnType.typeArguments.length > 0) {
|
8104
|
+
specializedReturnType.typeArguments.length > 0) {
|
8088
8105
|
if (boolClassType && (0, types_1.isInstantiableClass)(boolClassType)) {
|
8089
8106
|
let typeGuardType = specializedReturnType.typeArguments[0];
|
8090
8107
|
// If the first argument is a simple (non-constrained) TypeVar,
|
@@ -8099,21 +8116,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8099
8116
|
]);
|
8100
8117
|
}
|
8101
8118
|
}
|
8102
|
-
|
8103
|
-
|
8104
|
-
// Determine the type of the first parameter.
|
8105
|
-
const paramIndex = type.boundToType ? 1 : 0;
|
8106
|
-
if (paramIndex < type.details.parameters.length) {
|
8107
|
-
const paramType = types_1.FunctionType.getEffectiveParameterType(type, paramIndex);
|
8108
|
-
// If the type guard meets the requirements that the first parameter
|
8109
|
-
// type is a proper subtype of the return type, we can use strict
|
8110
|
-
// type guard semantics.
|
8111
|
-
if (assignType(paramType, returnType.typeArguments[0])) {
|
8112
|
-
useStrictTypeGuardSemantics = true;
|
8113
|
-
}
|
8114
|
-
}
|
8115
|
-
}
|
8116
|
-
specializedReturnType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForTypeGuard(boolClassType, typeGuardType, useStrictTypeGuardSemantics));
|
8119
|
+
const useTypeIsSemantics = types_1.ClassType.isBuiltIn(specializedReturnType, 'TypeIs');
|
8120
|
+
specializedReturnType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForTypeGuard(boolClassType, typeGuardType, useTypeIsSemantics));
|
8117
8121
|
}
|
8118
8122
|
}
|
8119
8123
|
specializedReturnType = adjustCallableReturnType(specializedReturnType, signatureTracker.getTrackedSignatures());
|
@@ -8391,6 +8395,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8391
8395
|
isTypeIncomplete = true;
|
8392
8396
|
}
|
8393
8397
|
}
|
8398
|
+
// If the argument came from a parameter's default argument value,
|
8399
|
+
// we may need to specialize the type.
|
8400
|
+
if (argParam.isDefaultArg) {
|
8401
|
+
argType = (0, typeUtils_1.applySolvedTypeVars)(argType, typeVarContext);
|
8402
|
+
}
|
8394
8403
|
}
|
8395
8404
|
// If we're assigning to a var arg dictionary with a TypeVar type,
|
8396
8405
|
// strip literals before performing the assignment. This is used in
|
@@ -8986,6 +8995,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8986
8995
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeAnyOrUnknown(), (_c = argList[1].node) !== null && _c !== void 0 ? _c : errorNode);
|
8987
8996
|
isBaseClassAny = true;
|
8988
8997
|
}
|
8998
|
+
// Specifically disallow Annotated.
|
8999
|
+
if (baseClass.specialForm &&
|
9000
|
+
(0, types_1.isInstantiableClass)(baseClass.specialForm) &&
|
9001
|
+
types_1.ClassType.isBuiltIn(baseClass.specialForm, 'Annotated')) {
|
9002
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeNotAClass(), argList[1].node || errorNode);
|
9003
|
+
return undefined;
|
9004
|
+
}
|
8989
9005
|
if (!(0, types_1.isInstantiableClass)(baseClass)) {
|
8990
9006
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeNotAClass(), argList[1].node || errorNode);
|
8991
9007
|
return undefined;
|
@@ -8997,7 +9013,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
8997
9013
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.newTypeLiteral(), argList[1].node || errorNode);
|
8998
9014
|
}
|
8999
9015
|
let classFlags = baseClass.details.flags & ~(1 /* ClassTypeFlags.BuiltInClass */ | 2 /* ClassTypeFlags.SpecialBuiltIn */);
|
9000
|
-
classFlags |=
|
9016
|
+
classFlags |= 8192 /* ClassTypeFlags.Final */ | 536870912 /* ClassTypeFlags.NewTypeClass */ | 1073741824 /* ClassTypeFlags.ValidTypeAliasClass */;
|
9001
9017
|
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, classFlags, ParseTreeUtils.getTypeSourceId(errorNode),
|
9002
9018
|
/* declaredMetaclass */ undefined, baseClass.details.effectiveMetaclass);
|
9003
9019
|
classType.details.baseClasses.push(isBaseClassAny ? types_1.AnyType.create() : baseClass);
|
@@ -9046,7 +9062,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9046
9062
|
if (!(0, types_1.isClassInstance)(arg1Type) || !(0, typeUtils_1.isTupleClass)(arg1Type) || arg1Type.tupleTypeArguments === undefined) {
|
9047
9063
|
return undefined;
|
9048
9064
|
}
|
9049
|
-
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri,
|
9065
|
+
const classType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, 1073741824 /* ClassTypeFlags.ValidTypeAliasClass */, ParseTreeUtils.getTypeSourceId(errorNode),
|
9050
9066
|
/* declaredMetaclass */ undefined, arg1Type.details.effectiveMetaclass);
|
9051
9067
|
arg1Type.tupleTypeArguments.forEach((typeArg) => {
|
9052
9068
|
const specializedType = makeTopLevelTypeVarsConcrete(typeArg.type);
|
@@ -9356,8 +9372,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9356
9372
|
(0, types_1.isClassInstance)(keyType) &&
|
9357
9373
|
types_1.ClassType.isBuiltIn(keyType, 'str') &&
|
9358
9374
|
(0, typeUtils_1.isLiteralType)(keyType) &&
|
9359
|
-
expectedTypedDictEntries.has(keyType.literalValue)) {
|
9360
|
-
const effectiveValueType = expectedTypedDictEntries.get(keyType.literalValue).valueType;
|
9375
|
+
expectedTypedDictEntries.knownItems.has(keyType.literalValue)) {
|
9376
|
+
const effectiveValueType = expectedTypedDictEntries.knownItems.get(keyType.literalValue).valueType;
|
9361
9377
|
entryInferenceContext = (0, typeUtils_1.makeInferenceContext)(effectiveValueType);
|
9362
9378
|
valueTypeResult = getTypeOfExpression(entryNode.valueExpression,
|
9363
9379
|
/* flags */ undefined, entryInferenceContext);
|
@@ -9390,8 +9406,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9390
9406
|
else if (entryNode.nodeType === 16 /* ParseNodeType.DictionaryExpandEntry */) {
|
9391
9407
|
let expectedType;
|
9392
9408
|
if (expectedKeyType && expectedValueType) {
|
9393
|
-
if (
|
9394
|
-
expectedType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(
|
9409
|
+
if (supportsKeysAndGetItemProtocolType && (0, types_1.isInstantiableClass)(supportsKeysAndGetItemProtocolType)) {
|
9410
|
+
expectedType = types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(supportsKeysAndGetItemProtocolType, [expectedKeyType, expectedValueType],
|
9395
9411
|
/* isTypeArgumentExplicit */ true));
|
9396
9412
|
}
|
9397
9413
|
}
|
@@ -9417,7 +9433,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9417
9433
|
const strObject = types_1.ClassType.cloneAsInstance(strClassType);
|
9418
9434
|
const tdEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, unexpandedType,
|
9419
9435
|
/* allowNarrowed */ true);
|
9420
|
-
tdEntries.forEach((entry, name) => {
|
9436
|
+
tdEntries.knownItems.forEach((entry, name) => {
|
9421
9437
|
if (entry.isRequired || entry.isProvided) {
|
9422
9438
|
keyTypes.push({ node: entryNode, type: types_1.ClassType.cloneWithLiteral(strObject, name) });
|
9423
9439
|
valueTypes.push({ node: entryNode, type: entry.valueType });
|
@@ -9426,13 +9442,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9426
9442
|
addUnknown = false;
|
9427
9443
|
}
|
9428
9444
|
}
|
9429
|
-
else if (
|
9430
|
-
|
9431
|
-
|
9432
|
-
|
9445
|
+
else if (supportsKeysAndGetItemProtocolType &&
|
9446
|
+
(0, types_1.isInstantiableClass)(supportsKeysAndGetItemProtocolType)) {
|
9447
|
+
const mappingTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(supportsKeysAndGetItemProtocolType));
|
9448
|
+
supportsKeysAndGetItemProtocolType = (0, typeUtils_1.selfSpecializeClass)(supportsKeysAndGetItemProtocolType);
|
9449
|
+
if (assignType(types_1.ClassType.cloneAsInstance(supportsKeysAndGetItemProtocolType), unexpandedType,
|
9433
9450
|
/* diag */ undefined, mappingTypeVarContext,
|
9434
9451
|
/* srcTypeVarContext */ undefined, 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */)) {
|
9435
|
-
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(
|
9452
|
+
const specializedMapping = (0, typeUtils_1.applySolvedTypeVars)(supportsKeysAndGetItemProtocolType, mappingTypeVarContext);
|
9436
9453
|
const typeArgs = specializedMapping.typeArguments;
|
9437
9454
|
if (typeArgs && typeArgs.length >= 2) {
|
9438
9455
|
if (forceStrictInference || index < maxEntriesToUseForInference) {
|
@@ -9907,7 +9924,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
9907
9924
|
}
|
9908
9925
|
}
|
9909
9926
|
}, {
|
9910
|
-
dependentType: expectedType,
|
9927
|
+
dependentType: inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.expectedType,
|
9911
9928
|
allowDiagnostics: !forceSpeculative && !isDiagnosticSuppressedForNode(node) && !(inferenceContext === null || inferenceContext === void 0 ? void 0 : inferenceContext.isTypeIncomplete),
|
9912
9929
|
});
|
9913
9930
|
// Mark the function type as no longer being evaluated.
|
@@ -10405,7 +10422,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10405
10422
|
}
|
10406
10423
|
return type;
|
10407
10424
|
}
|
10408
|
-
// Creates a "TypeGuard" type. This is an alias for 'bool', which
|
10425
|
+
// Creates a "TypeGuard" and "TypeIs" type. This is an alias for 'bool', which
|
10409
10426
|
// isn't a generic type and therefore doesn't have a typeParameter.
|
10410
10427
|
// We'll abuse our internal types a bit by specializing it with
|
10411
10428
|
// a type argument anyway.
|
@@ -10923,12 +10940,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10923
10940
|
/* declaredMetaclass */ undefined,
|
10924
10941
|
/* effectiveMetaclass */ undefined);
|
10925
10942
|
if (aliasMapEntry.isSpecialForm) {
|
10926
|
-
specialClassType.details.flags |=
|
10943
|
+
specialClassType.details.flags |= -2147483648 /* ClassTypeFlags.SpecialFormClass */;
|
10927
10944
|
}
|
10928
10945
|
const specialBuiltInClassDeclaration = ((_a = AnalyzerNodeInfo.getDeclaration(node)) !== null && _a !== void 0 ? _a : (node.parent ? AnalyzerNodeInfo.getDeclaration(node.parent) : undefined));
|
10929
10946
|
specialClassType.details.declaration = specialBuiltInClassDeclaration;
|
10930
10947
|
if (fileInfo.isTypingExtensionsStubFile) {
|
10931
|
-
specialClassType.details.flags |=
|
10948
|
+
specialClassType.details.flags |= 131072 /* ClassTypeFlags.TypingExtensionClass */;
|
10932
10949
|
}
|
10933
10950
|
const baseClassName = aliasMapEntry.alias || 'object';
|
10934
10951
|
let baseClass;
|
@@ -10948,7 +10965,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
10948
10965
|
// cause the TypedDict to be marked as abstract.
|
10949
10966
|
if ((0, types_1.isInstantiableClass)(baseClass) && types_1.ClassType.isBuiltIn(baseClass, '_TypedDict')) {
|
10950
10967
|
baseClass = types_1.ClassType.cloneWithNewFlags(baseClass, baseClass.details.flags &
|
10951
|
-
~(
|
10968
|
+
~(2048 /* ClassTypeFlags.SupportsAbstractMethods */ | 268435456 /* ClassTypeFlags.TypeCheckOnly */));
|
10952
10969
|
}
|
10953
10970
|
}
|
10954
10971
|
}
|
@@ -11004,6 +11021,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11004
11021
|
['Never', { alias: '', module: 'builtins', isSpecialForm: true }],
|
11005
11022
|
['LiteralString', { alias: '', module: 'builtins', isSpecialForm: true }],
|
11006
11023
|
['ReadOnly', { alias: '', module: 'builtins', isSpecialForm: true }],
|
11024
|
+
['TypeIs', { alias: '', module: 'builtins', isSpecialForm: true }],
|
11007
11025
|
]);
|
11008
11026
|
const aliasMapEntry = specialTypes.get(assignedName);
|
11009
11027
|
if (aliasMapEntry) {
|
@@ -11332,17 +11350,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11332
11350
|
fileInfo.isTypeshedStubFile) {
|
11333
11351
|
classFlags |= 1 /* ClassTypeFlags.BuiltInClass */;
|
11334
11352
|
if (fileInfo.isTypingExtensionsStubFile) {
|
11335
|
-
classFlags |=
|
11353
|
+
classFlags |= 131072 /* ClassTypeFlags.TypingExtensionClass */;
|
11336
11354
|
}
|
11337
11355
|
if (node.name.value === 'property') {
|
11338
|
-
classFlags |=
|
11356
|
+
classFlags |= 4096 /* ClassTypeFlags.PropertyClass */;
|
11339
11357
|
}
|
11340
11358
|
if (node.name.value === 'tuple') {
|
11341
|
-
classFlags |=
|
11359
|
+
classFlags |= 1048576 /* ClassTypeFlags.TupleClass */;
|
11342
11360
|
}
|
11343
11361
|
}
|
11344
11362
|
if (fileInfo.isStubFile) {
|
11345
|
-
classFlags |=
|
11363
|
+
classFlags |= 16777216 /* ClassTypeFlags.DefinedInStub */;
|
11346
11364
|
}
|
11347
11365
|
const classType = types_1.ClassType.createInstantiable(node.name.value, ParseTreeUtils.getClassFullName(node, fileInfo.moduleName, node.name.value), fileInfo.moduleName, fileInfo.fileUri, classFlags,
|
11348
11366
|
/* typeSourceId */ 0,
|
@@ -11365,7 +11383,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11365
11383
|
if (classDecl && classSymbol) {
|
11366
11384
|
setSymbolResolutionPartialType(classSymbol, classDecl, classType);
|
11367
11385
|
}
|
11368
|
-
classType.details.flags |=
|
11386
|
+
classType.details.flags |= 262144 /* ClassTypeFlags.PartiallyEvaluated */;
|
11369
11387
|
classType.details.declaration = classDecl;
|
11370
11388
|
try {
|
11371
11389
|
writeTypeCache(node, { type: classType }, /* flags */ undefined);
|
@@ -11450,10 +11468,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11450
11468
|
fileInfo.executionEnvironment.pythonVersion < pythonVersion_1.PythonVersion.V3_7) {
|
11451
11469
|
addError(localize_1.LocMessage.protocolIllegal(), arg.valueExpression);
|
11452
11470
|
}
|
11453
|
-
classType.details.flags |=
|
11471
|
+
classType.details.flags |= 16384 /* ClassTypeFlags.ProtocolClass */;
|
11454
11472
|
}
|
11455
11473
|
if (types_1.ClassType.isBuiltIn(argType, 'property')) {
|
11456
|
-
classType.details.flags |=
|
11474
|
+
classType.details.flags |= 4096 /* ClassTypeFlags.PropertyClass */;
|
11457
11475
|
}
|
11458
11476
|
// If the class directly derives from NamedTuple (in Python 3.6 or
|
11459
11477
|
// newer), it's considered a (read-only) dataclass.
|
@@ -11462,13 +11480,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11462
11480
|
classType.details.flags |=
|
11463
11481
|
4 /* ClassTypeFlags.DataClass */ |
|
11464
11482
|
32 /* ClassTypeFlags.SkipSynthesizedDataClassEq */ |
|
11465
|
-
|
11483
|
+
33554432 /* ClassTypeFlags.ReadOnlyInstanceVariables */;
|
11466
11484
|
}
|
11467
11485
|
}
|
11468
11486
|
// If the class directly derives from TypedDict or from a class that is
|
11469
11487
|
// a TypedDict, it is considered a TypedDict.
|
11470
11488
|
if (types_1.ClassType.isBuiltIn(argType, 'TypedDict') || types_1.ClassType.isTypedDictClass(argType)) {
|
11471
11489
|
classType.details.flags |= 128 /* ClassTypeFlags.TypedDictClass */;
|
11490
|
+
// Propagate the "effectively closed" flag from base classes.
|
11491
|
+
if (types_1.ClassType.isTypedDictEffectivelyClosed(argType)) {
|
11492
|
+
classType.details.flags |= 512 /* ClassTypeFlags.TypedDictEffectivelyClosed */;
|
11493
|
+
}
|
11472
11494
|
}
|
11473
11495
|
// Validate that the class isn't deriving from itself, creating a
|
11474
11496
|
// circular dependency.
|
@@ -11498,7 +11520,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11498
11520
|
classType.details.baseClasses.push(argType);
|
11499
11521
|
if ((0, types_1.isInstantiableClass)(argType)) {
|
11500
11522
|
if (types_1.ClassType.isEnumClass(argType)) {
|
11501
|
-
classType.details.flags |=
|
11523
|
+
classType.details.flags |= 2097152 /* ClassTypeFlags.EnumClass */;
|
11502
11524
|
}
|
11503
11525
|
// Determine if the class is abstract. Protocol classes support abstract methods
|
11504
11526
|
// because they are constructed by the _ProtocolMeta metaclass, which derives
|
@@ -11507,10 +11529,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11507
11529
|
// because of dependencies on some of these built-in protocol classes.
|
11508
11530
|
if (types_1.ClassType.supportsAbstractMethods(argType) ||
|
11509
11531
|
(types_1.ClassType.isProtocolClass(argType) && !types_1.ClassType.isBuiltIn(argType))) {
|
11510
|
-
classType.details.flags |=
|
11532
|
+
classType.details.flags |= 2048 /* ClassTypeFlags.SupportsAbstractMethods */;
|
11511
11533
|
}
|
11512
11534
|
if (types_1.ClassType.isPropertyClass(argType)) {
|
11513
|
-
classType.details.flags |=
|
11535
|
+
classType.details.flags |= 4096 /* ClassTypeFlags.PropertyClass */;
|
11514
11536
|
}
|
11515
11537
|
if (types_1.ClassType.isFinal(argType)) {
|
11516
11538
|
const className = printObjectTypeForClass(argType);
|
@@ -11552,7 +11574,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11552
11574
|
}
|
11553
11575
|
}
|
11554
11576
|
else if (types_1.ClassType.isTypedDictClass(classType)) {
|
11555
|
-
if (arg.name.value === 'total') {
|
11577
|
+
if (arg.name.value === 'total' || arg.name.value === 'closed') {
|
11556
11578
|
// The "total" and "readonly" parameters apply only for TypedDict classes.
|
11557
11579
|
// PEP 589 specifies that the parameter must be either True or False.
|
11558
11580
|
const constArgValue = (0, staticExpressions_1.evaluateStaticBoolExpression)(arg.valueExpression, fileInfo.executionEnvironment, fileInfo.definedConstants);
|
@@ -11560,7 +11582,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11560
11582
|
addError(localize_1.LocMessage.typedDictBoolParam().format({ name: arg.name.value }), arg.valueExpression);
|
11561
11583
|
}
|
11562
11584
|
else if (arg.name.value === 'total' && !constArgValue) {
|
11563
|
-
classType.details.flags |=
|
11585
|
+
classType.details.flags |= 1024 /* ClassTypeFlags.CanOmitDictValues */;
|
11586
|
+
}
|
11587
|
+
else if (arg.name.value === 'closed' && constArgValue) {
|
11588
|
+
if (AnalyzerNodeInfo.getFileInfo(node).diagnosticRuleSet.enableExperimentalFeatures) {
|
11589
|
+
classType.details.flags |=
|
11590
|
+
256 /* ClassTypeFlags.TypedDictMarkedClosed */ | 512 /* ClassTypeFlags.TypedDictEffectivelyClosed */;
|
11591
|
+
}
|
11564
11592
|
}
|
11565
11593
|
}
|
11566
11594
|
else {
|
@@ -11681,7 +11709,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11681
11709
|
param.category === 0 /* ParameterCategory.Simple */ &&
|
11682
11710
|
!param.defaultValue);
|
11683
11711
|
if (genericParams.length > 0) {
|
11684
|
-
classType.details.flags |=
|
11712
|
+
classType.details.flags |= 32768 /* ClassTypeFlags.PseudoGenericClass */;
|
11685
11713
|
// Create a type parameter for each simple, named parameter
|
11686
11714
|
// in the __init__ method.
|
11687
11715
|
classType.details.typeParameters = genericParams.map((param) => {
|
@@ -11702,7 +11730,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11702
11730
|
if (classType.details.typeParameters.length === 0 && !types_1.ClassType.isBuiltIn(classType, 'type')) {
|
11703
11731
|
if (classType.details.baseClasses.some((baseClass) => (0, types_1.isInstantiableClass)(baseClass) && types_1.ClassType.hasCustomClassGetItem(baseClass)) ||
|
11704
11732
|
classType.details.fields.has('__class_getitem__')) {
|
11705
|
-
classType.details.flags |=
|
11733
|
+
classType.details.flags |= 524288 /* ClassTypeFlags.HasCustomClassGetItem */;
|
11706
11734
|
}
|
11707
11735
|
}
|
11708
11736
|
// Determine the effective metaclass.
|
@@ -11715,17 +11743,17 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11715
11743
|
classType.details.declaredMetaclass = metaclassType;
|
11716
11744
|
if ((0, types_1.isInstantiableClass)(metaclassType)) {
|
11717
11745
|
if ((0, enums_1.isEnumMetaclass)(metaclassType)) {
|
11718
|
-
classType.details.flags |=
|
11746
|
+
classType.details.flags |= 2097152 /* ClassTypeFlags.EnumClass */;
|
11719
11747
|
}
|
11720
11748
|
if (types_1.ClassType.isBuiltIn(metaclassType, 'ABCMeta')) {
|
11721
|
-
classType.details.flags |=
|
11749
|
+
classType.details.flags |= 2048 /* ClassTypeFlags.SupportsAbstractMethods */;
|
11722
11750
|
}
|
11723
11751
|
}
|
11724
11752
|
}
|
11725
11753
|
}
|
11726
11754
|
const effectiveMetaclass = computeEffectiveMetaclass(classType, node.name);
|
11727
11755
|
// Clear the "partially constructed" flag.
|
11728
|
-
classType.details.flags &= ~
|
11756
|
+
classType.details.flags &= ~262144 /* ClassTypeFlags.PartiallyEvaluated */;
|
11729
11757
|
// Now determine the decorated type of the class.
|
11730
11758
|
let decoratedType = classType;
|
11731
11759
|
let foundUnknown = false;
|
@@ -11787,7 +11815,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
11787
11815
|
if (foundInvalidBaseClass) {
|
11788
11816
|
addError(localize_1.LocMessage.typedDictBaseClass() + diag.getString(), node.name);
|
11789
11817
|
}
|
11790
|
-
(0, typedDicts_1.synthesizeTypedDictClassMethods)(evaluatorInterface, node, classType
|
11818
|
+
(0, typedDicts_1.synthesizeTypedDictClassMethods)(evaluatorInterface, node, classType);
|
11791
11819
|
}
|
11792
11820
|
// Synthesize dataclass methods.
|
11793
11821
|
if (types_1.ClassType.isDataClass(classType)) {
|
@@ -12460,8 +12488,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
12460
12488
|
if (index >= firstNonClsSelfParamIndex) {
|
12461
12489
|
let isImplicitPositionOnlyParam = false;
|
12462
12490
|
if (param.category === 0 /* ParameterCategory.Simple */ && param.name) {
|
12463
|
-
if ((0, symbolNameUtils_1.isPrivateName)(param.name.value)
|
12491
|
+
if ((0, symbolNameUtils_1.isPrivateName)(param.name.value) &&
|
12492
|
+
!node.parameters.some((p) => p.category === 0 /* ParameterCategory.Simple */ && !p.name)) {
|
12464
12493
|
isImplicitPositionOnlyParam = true;
|
12494
|
+
// If the parameter name indicates an implicit position-only parameter
|
12495
|
+
// but we have already seen non-position-only parameters, report an error.
|
12496
|
+
if (!paramsArePositionOnly &&
|
12497
|
+
functionType.details.parameters.every((p) => p.category === 0 /* ParameterCategory.Simple */)) {
|
12498
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.positionOnlyAfterNon(), param.name);
|
12499
|
+
}
|
12465
12500
|
}
|
12466
12501
|
}
|
12467
12502
|
else {
|
@@ -13858,7 +13893,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13858
13893
|
// flow analysis starts from the reference node, but startNode can be
|
13859
13894
|
// specified to override this in a few special cases (functions and
|
13860
13895
|
// lambdas) to support analysis of captured variables.
|
13861
|
-
function getFlowTypeOfReference(reference,
|
13896
|
+
function getFlowTypeOfReference(reference, startNode, options) {
|
13862
13897
|
var _a;
|
13863
13898
|
// See if this execution scope requires code flow for this reference expression.
|
13864
13899
|
const referenceKey = (0, codeFlowTypes_1.createKeyForReference)(reference);
|
@@ -13887,7 +13922,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13887
13922
|
if (flowNode === undefined) {
|
13888
13923
|
return { type: undefined, isIncomplete: false };
|
13889
13924
|
}
|
13890
|
-
return analyzer.getTypeFromCodeFlow(flowNode, reference,
|
13925
|
+
return analyzer.getTypeFromCodeFlow(flowNode, reference, options);
|
13891
13926
|
}
|
13892
13927
|
// Specializes the specified (potentially generic) class type using
|
13893
13928
|
// the specified type arguments, reporting errors as appropriate.
|
@@ -13980,7 +14015,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13980
14015
|
case 'Concatenate': {
|
13981
14016
|
return { type: createConcatenateType(classType, errorNode, typeArgs, flags) };
|
13982
14017
|
}
|
13983
|
-
case 'TypeGuard':
|
14018
|
+
case 'TypeGuard':
|
14019
|
+
case 'TypeIs': {
|
13984
14020
|
return { type: createTypeGuardType(classType, errorNode, typeArgs, flags) };
|
13985
14021
|
}
|
13986
14022
|
case 'Unpack': {
|
@@ -13991,10 +14027,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
13991
14027
|
return createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags);
|
13992
14028
|
}
|
13993
14029
|
case 'ReadOnly': {
|
13994
|
-
|
13995
|
-
return createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags);
|
13996
|
-
}
|
13997
|
-
break;
|
14030
|
+
return createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags);
|
13998
14031
|
}
|
13999
14032
|
case 'Self': {
|
14000
14033
|
return { type: createSelfType(classType, errorNode, typeArgs, flags) };
|
@@ -14527,7 +14560,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14527
14560
|
// is a valid entry in the TypedDict to avoid resolving declarations for
|
14528
14561
|
// synthesized symbols such as 'get'.
|
14529
14562
|
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
14530
|
-
const entry = (_a = subtype.details.typedDictEntries) === null || _a === void 0 ? void 0 : _a.get(node.value);
|
14563
|
+
const entry = (_a = subtype.details.typedDictEntries) === null || _a === void 0 ? void 0 : _a.knownItems.get(node.value);
|
14531
14564
|
if (entry) {
|
14532
14565
|
const symbol = (_b = (0, typeUtils_1.lookUpObjectMember)(subtype, node.value)) === null || _b === void 0 ? void 0 : _b.symbol;
|
14533
14566
|
if (symbol) {
|
@@ -14895,7 +14928,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
14895
14928
|
}
|
14896
14929
|
}
|
14897
14930
|
else {
|
14898
|
-
const boundType = getTypeOfExpressionExpectingType(node.boundExpression
|
14931
|
+
const boundType = getTypeOfExpressionExpectingType(node.boundExpression, {
|
14932
|
+
disallowProtocolAndTypedDict: true,
|
14933
|
+
}).type;
|
14899
14934
|
if ((0, typeUtils_1.requiresSpecialization)(boundType, { ignorePseudoGeneric: true })) {
|
14900
14935
|
addError(localize_1.LocMessage.typeVarConstraintGeneric(), node.boundExpression);
|
14901
14936
|
}
|
@@ -15544,7 +15579,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15544
15579
|
// into account argument types to infer the return type.
|
15545
15580
|
function getFunctionEffectiveReturnType(type, args, inferTypeIfNeeded = true) {
|
15546
15581
|
const specializedReturnType = types_1.FunctionType.getSpecializedReturnType(type, /* includeInferred */ false);
|
15547
|
-
if (specializedReturnType) {
|
15582
|
+
if (specializedReturnType && !(0, types_1.isUnknown)(specializedReturnType)) {
|
15548
15583
|
return adjustCallableReturnType(specializedReturnType, /* trackedSignatures */ undefined);
|
15549
15584
|
}
|
15550
15585
|
if (inferTypeIfNeeded) {
|
@@ -15844,26 +15879,50 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
15844
15879
|
}
|
15845
15880
|
// Handle typed dicts. They also use a form of structural typing for type
|
15846
15881
|
// checking, as defined in PEP 589.
|
15847
|
-
if (types_1.ClassType.isTypedDictClass(
|
15848
|
-
types_1.ClassType.isTypedDictClass(srcType)
|
15849
|
-
|
15850
|
-
|
15851
|
-
|
15882
|
+
if (types_1.ClassType.isTypedDictClass(srcType)) {
|
15883
|
+
if (types_1.ClassType.isTypedDictClass(destType) && !types_1.ClassType.isSameGenericClass(destType, srcType)) {
|
15884
|
+
if (!(0, typedDicts_1.assignTypedDictToTypedDict)(evaluatorInterface, destType, srcType, diag, destTypeVarContext, flags, recursionCount)) {
|
15885
|
+
return false;
|
15886
|
+
}
|
15887
|
+
if (types_1.ClassType.isFinal(destType) !== types_1.ClassType.isFinal(srcType)) {
|
15888
|
+
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typedDictFinalMismatch().format({
|
15889
|
+
sourceType: printType((0, typeUtils_1.convertToInstance)(srcType)),
|
15890
|
+
destType: printType((0, typeUtils_1.convertToInstance)(destType)),
|
15891
|
+
}));
|
15892
|
+
return false;
|
15893
|
+
}
|
15894
|
+
// If invariance is being enforced, the two TypedDicts must be assignable to each other.
|
15895
|
+
if ((flags & 1 /* AssignTypeFlags.EnforceInvariance */) !== 0) {
|
15896
|
+
return (0, typedDicts_1.assignTypedDictToTypedDict)(evaluatorInterface, srcType, destType,
|
15897
|
+
/* diag */ undefined,
|
15898
|
+
/* typeVarContext */ undefined, flags, recursionCount);
|
15899
|
+
}
|
15900
|
+
return true;
|
15852
15901
|
}
|
15853
|
-
|
15854
|
-
|
15855
|
-
|
15856
|
-
|
15857
|
-
|
15858
|
-
|
15902
|
+
// Handle some special cases where a TypedDict can act like
|
15903
|
+
// a Mapping[str, T] or a dict[str, T].
|
15904
|
+
if (types_1.ClassType.isBuiltIn(destType, 'Mapping')) {
|
15905
|
+
const mappingValueType = (0, typedDicts_1.getTypedDictMappingEquivalent)(evaluatorInterface, srcType);
|
15906
|
+
if (mappingValueType &&
|
15907
|
+
mappingType &&
|
15908
|
+
(0, types_1.isInstantiableClass)(mappingType) &&
|
15909
|
+
strClassType &&
|
15910
|
+
(0, types_1.isInstantiableClass)(strClassType)) {
|
15911
|
+
srcType = types_1.ClassType.cloneForSpecialization(mappingType, [types_1.ClassType.cloneAsInstance(strClassType), mappingValueType],
|
15912
|
+
/* isTypeArgumentExplicit */ true);
|
15913
|
+
}
|
15859
15914
|
}
|
15860
|
-
|
15861
|
-
|
15862
|
-
|
15863
|
-
|
15864
|
-
|
15915
|
+
else if (types_1.ClassType.isBuiltIn(destType, ['dict', 'MutableMapping'])) {
|
15916
|
+
const dictValueType = (0, typedDicts_1.getTypedDictDictEquivalent)(evaluatorInterface, srcType, recursionCount);
|
15917
|
+
if (dictValueType &&
|
15918
|
+
dictClassType &&
|
15919
|
+
(0, types_1.isInstantiableClass)(dictClassType) &&
|
15920
|
+
strClassType &&
|
15921
|
+
(0, types_1.isInstantiableClass)(strClassType)) {
|
15922
|
+
srcType = types_1.ClassType.cloneForSpecialization(dictClassType, [types_1.ClassType.cloneAsInstance(strClassType), dictValueType],
|
15923
|
+
/* isTypeArgumentExplicit */ true);
|
15924
|
+
}
|
15865
15925
|
}
|
15866
|
-
return true;
|
15867
15926
|
}
|
15868
15927
|
// Handle special-case type promotions.
|
15869
15928
|
if (destType.includePromotions) {
|
@@ -16111,15 +16170,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16111
16170
|
if ((0, types_1.isVariadicTypeVar)(destTypeArgs[destUnboundedOrVariadicIndex].type)) {
|
16112
16171
|
if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
|
16113
16172
|
const removedArgs = srcTypeArgs.splice(destUnboundedOrVariadicIndex, srcArgsToCapture);
|
16114
|
-
|
16115
|
-
|
16116
|
-
|
16117
|
-
|
16118
|
-
|
16119
|
-
|
16120
|
-
|
16121
|
-
|
16122
|
-
|
16173
|
+
let variadicTuple;
|
16174
|
+
// If we're left with a single unpacked variadic type var, there's no
|
16175
|
+
// need to wrap it in a nested tuple.
|
16176
|
+
if (removedArgs.length === 1 && (0, types_1.isUnpackedVariadicTypeVar)(removedArgs[0].type)) {
|
16177
|
+
variadicTuple = removedArgs[0].type;
|
16178
|
+
}
|
16179
|
+
else {
|
16180
|
+
// Package up the remaining type arguments into a tuple object.
|
16181
|
+
variadicTuple = makeTupleObject(removedArgs.map((typeArg) => {
|
16182
|
+
return {
|
16183
|
+
type: typeArg.type,
|
16184
|
+
isUnbounded: typeArg.isUnbounded,
|
16185
|
+
isOptional: typeArg.isOptional,
|
16186
|
+
};
|
16187
|
+
}),
|
16188
|
+
/* isUnpackedTuple */ true);
|
16189
|
+
}
|
16123
16190
|
srcTypeArgs.splice(destUnboundedOrVariadicIndex, 0, {
|
16124
16191
|
type: variadicTuple,
|
16125
16192
|
isUnbounded: false,
|
@@ -16635,7 +16702,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16635
16702
|
}
|
16636
16703
|
if ((0, types_1.isNever)(srcType)) {
|
16637
16704
|
if ((flags & 1 /* AssignTypeFlags.EnforceInvariance */) !== 0) {
|
16638
|
-
|
16705
|
+
if ((0, types_1.isNever)(destType)) {
|
16706
|
+
return true;
|
16707
|
+
}
|
16708
|
+
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
|
16709
|
+
return false;
|
16639
16710
|
}
|
16640
16711
|
const targetTypeVarContext = (flags & 2 /* AssignTypeFlags.ReverseTypeVarMatching */) === 0 ? destTypeVarContext : srcTypeVarContext;
|
16641
16712
|
if (targetTypeVarContext) {
|
@@ -16768,7 +16839,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
16768
16839
|
return !isLiteral;
|
16769
16840
|
}
|
16770
16841
|
}
|
16771
|
-
else if (types_1.ClassType.isBuiltIn(destType, 'TypeGuard')) {
|
16842
|
+
else if (types_1.ClassType.isBuiltIn(destType, ['TypeGuard', 'TypeIs'])) {
|
16772
16843
|
// All the source to be a "bool".
|
16773
16844
|
if ((originalFlags & 128 /* AssignTypeFlags.AllowBoolTypeGuard */) !== 0) {
|
16774
16845
|
if ((0, types_1.isClassInstance)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'bool')) {
|
@@ -17335,6 +17406,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17335
17406
|
if ((0, types_1.isTypeSame)(destType, srcSubtype, { ignorePseudoGeneric: true }, recursionCount)) {
|
17336
17407
|
return false;
|
17337
17408
|
}
|
17409
|
+
if ((0, typeUtils_1.isIncompleteUnknown)(srcSubtype)) {
|
17410
|
+
return false;
|
17411
|
+
}
|
17338
17412
|
const destTypeVarName = types_1.TypeVarType.getNameWithScope(destType);
|
17339
17413
|
// Determine which conditions on this type apply to this type variable.
|
17340
17414
|
// There might be more than one of them.
|
@@ -17946,10 +18020,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
17946
18020
|
isReturnTypeCompatible = true;
|
17947
18021
|
}
|
17948
18022
|
else {
|
17949
|
-
// Handle the special case where the return type is a TypeGuard[T]
|
17950
|
-
// This should also act as a bool, since that's its
|
18023
|
+
// Handle the special case where the return type is a TypeGuard[T]
|
18024
|
+
// or TypeIs[T]. This should also act as a bool, since that's its
|
18025
|
+
// type at runtime.
|
17951
18026
|
if ((0, types_1.isClassInstance)(srcReturnType) &&
|
17952
|
-
types_1.ClassType.isBuiltIn(srcReturnType, 'TypeGuard') &&
|
18027
|
+
types_1.ClassType.isBuiltIn(srcReturnType, ['TypeGuard', 'TypeIs']) &&
|
17953
18028
|
boolClassType &&
|
17954
18029
|
(0, types_1.isInstantiableClass)(boolClassType)) {
|
17955
18030
|
if (assignType(destReturnType, types_1.ClassType.cloneAsInstance(boolClassType), returnDiag === null || returnDiag === void 0 ? void 0 : returnDiag.createAddendum(), destTypeVarContext, srcTypeVarContext, flags, recursionCount)) {
|
@@ -18311,9 +18386,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions) {
|
|
18311
18386
|
!(0, symbolNameUtils_1.isPrivateOrProtectedName)(baseParam.name || '') &&
|
18312
18387
|
baseParamDetails.params[i].source !== parameterUtils_1.ParameterSource.PositionOnly &&
|
18313
18388
|
baseParam.category === 0 /* ParameterCategory.Simple */ &&
|
18389
|
+
enforceParamNames &&
|
18314
18390
|
baseParam.name !== overrideParam.name) {
|
18315
18391
|
if (overrideParam.category === 0 /* ParameterCategory.Simple */) {
|
18316
|
-
if (
|
18392
|
+
if (!baseParam.isNameSynthesized) {
|
18317
18393
|
if (overrideParamDetails.params[i].source === parameterUtils_1.ParameterSource.PositionOnly) {
|
18318
18394
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.overrideParamNamePositionOnly().format({
|
18319
18395
|
index: i + 1,
|