@zzzen/pyright-internal 1.2.0-dev.20240901 → 1.2.0-dev.20240915
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/analysis.d.ts +1 -0
- package/dist/analyzer/analysis.js +2 -0
- package/dist/analyzer/analysis.js.map +1 -1
- package/dist/analyzer/backgroundAnalysisProgram.js +6 -2
- package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
- package/dist/analyzer/binder.js +14 -8
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/cacheManager.d.ts +2 -2
- package/dist/analyzer/cacheManager.js.map +1 -1
- package/dist/analyzer/checker.js +53 -192
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +7 -2
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/constraintSolver.d.ts +1 -2
- package/dist/analyzer/constraintSolver.js +2 -2
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructors.js +7 -1
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.js +6 -1
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/namedTuples.js +4 -1
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +5 -0
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/parseTreeWalker.js +1 -1
- package/dist/analyzer/parseTreeWalker.js.map +1 -1
- package/dist/analyzer/patternMatching.js +3 -1
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.js +2 -3
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.d.ts +1 -2
- package/dist/analyzer/protocols.js +5 -8
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/pythonPathUtils.d.ts +1 -1
- package/dist/analyzer/service.d.ts +3 -1
- package/dist/analyzer/service.js +50 -33
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceMapper.js +10 -5
- package/dist/analyzer/sourceMapper.js.map +1 -1
- package/dist/analyzer/tuples.d.ts +1 -2
- package/dist/analyzer/tuples.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +599 -181
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +33 -6
- package/dist/analyzer/typeEvaluatorTypes.js +66 -2
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.d.ts +3 -12
- package/dist/analyzer/typeGuards.js +286 -461
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeStubWriter.js +12 -1
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +4 -17
- package/dist/analyzer/typeUtils.js +61 -60
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.d.ts +1 -2
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +9 -4
- package/dist/analyzer/types.js +80 -26
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +23 -16
- package/dist/backgroundAnalysisBase.js +64 -35
- package/dist/backgroundAnalysisBase.js.map +1 -1
- package/dist/backgroundThreadBase.js +9 -0
- package/dist/backgroundThreadBase.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +0 -1
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/languageServerInterface.d.ts +1 -1
- package/dist/common/uri/uriUtils.d.ts +1 -1
- package/dist/common/uri/uriUtils.js +6 -1
- package/dist/common/uri/uriUtils.js.map +1 -1
- package/dist/languageServerBase.d.ts +2 -2
- package/dist/languageServerBase.js +1 -3
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/analyzerServiceExecutor.d.ts +6 -1
- package/dist/languageService/analyzerServiceExecutor.js +7 -6
- package/dist/languageService/analyzerServiceExecutor.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +2 -1
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.js +12 -12
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/definitionProvider.js +6 -6
- package/dist/languageService/definitionProvider.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.js +9 -9
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/fileWatcherDynamicFeature.d.ts +5 -1
- package/dist/languageService/fileWatcherDynamicFeature.js +12 -2
- package/dist/languageService/fileWatcherDynamicFeature.js.map +1 -1
- package/dist/languageService/hoverProvider.d.ts +1 -0
- package/dist/languageService/hoverProvider.js +30 -3
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/signatureHelpProvider.js +2 -2
- package/dist/languageService/signatureHelpProvider.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +2 -2
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/localization/localize.d.ts +10 -0
- package/dist/localization/localize.js +4 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.cs.json +155 -153
- package/dist/localization/package.nls.de.json +128 -126
- package/dist/localization/package.nls.en-us.json +1632 -404
- package/dist/localization/package.nls.es.json +151 -149
- package/dist/localization/package.nls.fr.json +129 -127
- package/dist/localization/package.nls.it.json +143 -141
- package/dist/localization/package.nls.ja.json +142 -140
- package/dist/localization/package.nls.ko.json +155 -153
- package/dist/localization/package.nls.pl.json +178 -176
- package/dist/localization/package.nls.pt-br.json +139 -137
- package/dist/localization/package.nls.qps-ploc.json +392 -390
- package/dist/localization/package.nls.ru.json +170 -168
- package/dist/localization/package.nls.tr.json +153 -151
- package/dist/localization/package.nls.zh-cn.json +149 -147
- package/dist/localization/package.nls.zh-tw.json +133 -131
- package/dist/parser/parseNodes.d.ts +2 -3
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.js +6 -20
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/checker.test.js +14 -1
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/envVarUtils.test.js +0 -3
- package/dist/tests/envVarUtils.test.js.map +1 -1
- package/dist/tests/harness/fourslash/testLanguageService.js +0 -3
- package/dist/tests/harness/fourslash/testLanguageService.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.js +2 -5
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/harness/fourslash/testStateUtils.d.ts +1 -0
- package/dist/tests/harness/fourslash/testStateUtils.js +5 -0
- package/dist/tests/harness/fourslash/testStateUtils.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.d.ts +3 -2
- package/dist/tests/harness/vfs/filesystem.js +6 -3
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/importStatementUtils.test.js +1 -1
- package/dist/tests/importStatementUtils.test.js.map +1 -1
- package/dist/tests/service.test.js +31 -0
- package/dist/tests/service.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +5 -1
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +13 -1
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +5 -1
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +6 -0
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/typeEvaluator6.test.js +5 -1
- package/dist/tests/typeEvaluator6.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +1 -1
- package/dist/tests/typeEvaluator8.test.js +40 -0
- package/dist/tests/typeEvaluator8.test.js.map +1 -1
- package/dist/workspaceFactory.d.ts +2 -20
- package/dist/workspaceFactory.js +26 -64
- package/dist/workspaceFactory.js.map +1 -1
- package/package.json +1 -1
@@ -156,6 +156,11 @@ const maxInferFunctionReturnRecursionCount = 12;
|
|
156
156
|
// (tuples, lists, sets, or dicts) which can lead to infinite type analysis.
|
157
157
|
// This limits the depth.
|
158
158
|
const maxInferredContainerDepth = 8;
|
159
|
+
// If a tuple expression with no declared type contains a large number
|
160
|
+
// of elements, it can cause performance issues. This value limits the
|
161
|
+
// number of elements that will be included in the tuple type before
|
162
|
+
// we default to tuple[Unknown, ...].
|
163
|
+
const maxInferredTupleEntryCount = 256;
|
159
164
|
// Maximum recursion amount when comparing two recursive type aliases.
|
160
165
|
// Increasing this can greatly increase the time required to evaluate
|
161
166
|
// two recursive type aliases that have the same definition. Decreasing
|
@@ -429,6 +434,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
429
434
|
// context. For example, if it's a subexpression of an argument expression,
|
430
435
|
// the associated parameter type might inform the expected type.
|
431
436
|
function getExpectedType(node) {
|
437
|
+
// This is a primary entry point called by language server providers,
|
438
|
+
// and it might be called before any other type evaluation has occurred.
|
439
|
+
// Use this opportunity to do some initialization.
|
440
|
+
initializePrefetchedTypes(node);
|
432
441
|
// Scan up the parse tree to find the top-most expression node
|
433
442
|
// so we can evaluate the entire expression.
|
434
443
|
let topExpression = node;
|
@@ -500,6 +509,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
500
509
|
const anySpecialForm = types_1.AnyType.createSpecialForm();
|
501
510
|
if ((0, types_1.isAny)(anySpecialForm)) {
|
502
511
|
types_1.TypeBase.setSpecialForm(anySpecialForm, anyClass);
|
512
|
+
if (isTypeFormSupported(node)) {
|
513
|
+
types_1.TypeBase.setTypeForm(anySpecialForm, (0, typeUtils_1.convertToInstance)(anySpecialForm));
|
514
|
+
}
|
503
515
|
}
|
504
516
|
}
|
505
517
|
}
|
@@ -555,6 +567,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
555
567
|
};
|
556
568
|
}
|
557
569
|
}
|
570
|
+
if (inferenceContext) {
|
571
|
+
// Handle TypeForm assignments.
|
572
|
+
typeResult.type = convertToTypeFormType(inferenceContext.expectedType, typeResult.type);
|
573
|
+
}
|
558
574
|
// Don't allow speculative caching for assignment expressions because
|
559
575
|
// the target name node won't have a corresponding type cached speculatively.
|
560
576
|
const allowSpeculativeCaching = node.nodeType !== 4 /* ParseNodeType.AssignmentExpression */;
|
@@ -588,7 +604,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
588
604
|
}
|
589
605
|
// This is a helper function that implements the core of getTypeOfExpression.
|
590
606
|
function getTypeOfExpressionCore(node, flags = 0 /* EvalFlags.None */, inferenceContext) {
|
591
|
-
var _a;
|
607
|
+
var _a, _b;
|
592
608
|
let typeResult;
|
593
609
|
let expectingInstantiable = (flags & 128 /* EvalFlags.InstantiableType */) !== 0;
|
594
610
|
switch (node.nodeType) {
|
@@ -745,8 +761,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
745
761
|
}
|
746
762
|
// If this is a PEP 695 type alias, remove the special form so the type
|
747
763
|
// printer prints it as its aliased type rather than TypeAliasType.
|
748
|
-
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
749
|
-
const specialForm = (
|
764
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0 && ((_a = typeResult.type.props) === null || _a === void 0 ? void 0 : _a.typeForm) === undefined) {
|
765
|
+
const specialForm = (_b = typeResult.type.props) === null || _b === void 0 ? void 0 : _b.specialForm;
|
750
766
|
if (specialForm && types_1.ClassType.isBuiltIn(specialForm, 'TypeAliasType')) {
|
751
767
|
typeResult.type = types_1.TypeBase.cloneAsSpecialForm(typeResult.type, undefined);
|
752
768
|
}
|
@@ -889,10 +905,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
889
905
|
return typeResult;
|
890
906
|
}
|
891
907
|
function getTypeOfStringList(node, flags) {
|
908
|
+
var _a;
|
892
909
|
let typeResult;
|
893
|
-
if ((flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0) {
|
910
|
+
if ((flags & 8 /* EvalFlags.StrLiteralAsType */) !== 0 && (flags & 1073741824 /* EvalFlags.TypeFormArg */) === 0) {
|
894
911
|
return getTypeOfStringListAsType(node, flags);
|
895
912
|
}
|
913
|
+
const isBytesNode = (node) => (node.d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
|
914
|
+
// Check for mixing of bytes and str, which is not allowed.
|
915
|
+
const firstStrIndex = node.d.strings.findIndex((str) => !isBytesNode(str));
|
916
|
+
const firstBytesIndex = node.d.strings.findIndex((str) => isBytesNode(str));
|
917
|
+
if (firstStrIndex >= 0 && firstBytesIndex >= 0) {
|
918
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.mixingBytesAndStr(), node.d.strings[Math.max(firstBytesIndex, firstStrIndex)]);
|
919
|
+
return { type: types_1.UnknownType.create() };
|
920
|
+
}
|
921
|
+
const isBytes = firstBytesIndex >= 0;
|
896
922
|
let isLiteralString = true;
|
897
923
|
let isIncomplete = false;
|
898
924
|
node.d.strings.forEach((expr) => {
|
@@ -913,7 +939,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
913
939
|
isLiteralString = false;
|
914
940
|
}
|
915
941
|
});
|
916
|
-
const isBytes = (node.d.strings[0].d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
|
917
942
|
// Don't create a literal type if it's an f-string.
|
918
943
|
if (node.d.strings.some((str) => str.nodeType === 30 /* ParseNodeType.FormatString */)) {
|
919
944
|
if (isLiteralString) {
|
@@ -935,6 +960,26 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
935
960
|
isIncomplete,
|
936
961
|
};
|
937
962
|
}
|
963
|
+
if (node.d.strings.length !== 1 ||
|
964
|
+
node.d.strings[0].nodeType !== 49 /* ParseNodeType.String */ ||
|
965
|
+
!isTypeFormSupported(node)) {
|
966
|
+
return typeResult;
|
967
|
+
}
|
968
|
+
// For performance reasons, do not attempt to treat the string literal
|
969
|
+
// as a TypeForm if it's going to fail anyway or is unlikely to be a
|
970
|
+
// TypeForm (really long, triple-quoted, etc.).
|
971
|
+
const stringNode = node.d.strings[0];
|
972
|
+
const tokenFlags = stringNode.d.token.flags;
|
973
|
+
const disallowedTokenFlags = 32 /* StringTokenFlags.Bytes */ | 8 /* StringTokenFlags.Raw */ | 64 /* StringTokenFlags.Format */ | 4 /* StringTokenFlags.Triplicate */;
|
974
|
+
const maxTypeFormStringLength = 256;
|
975
|
+
if ((tokenFlags & disallowedTokenFlags) !== 0 ||
|
976
|
+
stringNode.d.token.escapedValue.length >= maxTypeFormStringLength) {
|
977
|
+
return typeResult;
|
978
|
+
}
|
979
|
+
const typeFormResult = getTypeOfStringListAsType(node, flags);
|
980
|
+
if ((_a = typeFormResult.type.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
|
981
|
+
typeResult.type = types_1.TypeBase.cloneWithTypeForm(typeResult.type, typeFormResult.type.props.typeForm);
|
982
|
+
}
|
938
983
|
return typeResult;
|
939
984
|
}
|
940
985
|
function getTypeOfStringListAsType(node, flags) {
|
@@ -946,6 +991,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
946
991
|
if ((flags & 8388608 /* EvalFlags.ParsesStringLiteral */) === 0) {
|
947
992
|
updatedFlags |= 524288 /* EvalFlags.NotParsed */;
|
948
993
|
}
|
994
|
+
updatedFlags &= ~1073741824 /* EvalFlags.TypeFormArg */;
|
949
995
|
if (node.d.annotation) {
|
950
996
|
return getTypeOfExpression(node.d.annotation, updatedFlags);
|
951
997
|
}
|
@@ -1984,6 +2030,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
1984
2030
|
function getTupleClassType() {
|
1985
2031
|
return tupleClass && (0, types_1.isInstantiableClass)(tupleClass) ? tupleClass : undefined;
|
1986
2032
|
}
|
2033
|
+
function getDictClassType() {
|
2034
|
+
return dictClass && (0, types_1.isInstantiableClass)(dictClass) ? dictClass : undefined;
|
2035
|
+
}
|
2036
|
+
function getStrClassType() {
|
2037
|
+
return strClass && (0, types_1.isInstantiableClass)(strClass) ? strClass : undefined;
|
2038
|
+
}
|
1987
2039
|
function getObjectType() {
|
1988
2040
|
return objectClass ? (0, typeUtils_1.convertToInstance)(objectClass) : types_1.UnknownType.create();
|
1989
2041
|
}
|
@@ -2260,13 +2312,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2260
2312
|
// If this is a member name (within a class scope) and the member name
|
2261
2313
|
// appears to be a constant, use the strict source type. If it's a member
|
2262
2314
|
// variable that can be overridden by a child class, use the more general
|
2263
|
-
// version by stripping off the literal.
|
2315
|
+
// version by stripping off the literal and TypeForm.
|
2264
2316
|
const scope = ScopeUtils.getScopeForNode(nameNode);
|
2265
2317
|
if ((scope === null || scope === void 0 ? void 0 : scope.type) === 3 /* ScopeType.Class */) {
|
2266
2318
|
if (types_1.TypeBase.isInstance(destType) &&
|
2267
2319
|
!(0, symbolNameUtils_1.isConstantName)(nameValue) &&
|
2268
2320
|
!isFinalVariable(symbolWithScope.symbol)) {
|
2269
|
-
destType = stripLiteralValue(destType);
|
2321
|
+
destType = (0, typeUtils_1.stripTypeForm)(stripLiteralValue(destType));
|
2270
2322
|
}
|
2271
2323
|
}
|
2272
2324
|
}
|
@@ -2567,6 +2619,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2567
2619
|
// If conditionFilter is specified and the TypeVar is a constrained
|
2568
2620
|
// TypeVar, only the conditions that match the filter will be included.
|
2569
2621
|
function makeTopLevelTypeVarsConcrete(type, makeParamSpecsConcrete = false, conditionFilter) {
|
2622
|
+
type = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(type);
|
2570
2623
|
return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
|
2571
2624
|
var _a;
|
2572
2625
|
if ((0, types_1.isParamSpec)(subtype)) {
|
@@ -2869,55 +2922,53 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
2869
2922
|
}
|
2870
2923
|
function verifyRaiseExceptionType(node) {
|
2871
2924
|
const baseExceptionType = getBuiltInType(node, 'BaseException');
|
2872
|
-
|
2873
|
-
|
2874
|
-
|
2875
|
-
|
2876
|
-
|
2877
|
-
|
2878
|
-
|
2879
|
-
|
2880
|
-
|
2881
|
-
|
2882
|
-
|
2883
|
-
|
2884
|
-
|
2885
|
-
|
2886
|
-
|
2887
|
-
|
2888
|
-
|
2889
|
-
|
2890
|
-
|
2891
|
-
|
2892
|
-
|
2893
|
-
|
2894
|
-
|
2895
|
-
|
2896
|
-
|
2897
|
-
|
2898
|
-
type: printType(subtype),
|
2899
|
-
}));
|
2900
|
-
}
|
2901
|
-
}
|
2902
|
-
}
|
2903
|
-
else if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
2904
|
-
if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(concreteSubtype), baseExceptionType,
|
2905
|
-
/* ignoreUnknown */ false)) {
|
2906
|
-
diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
|
2907
|
-
type: printType(subtype),
|
2908
|
-
}));
|
2909
|
-
}
|
2910
|
-
}
|
2911
|
-
else {
|
2912
|
-
diagAddendum.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
|
2925
|
+
const exceptionType = getTypeOfExpression(node).type;
|
2926
|
+
// Validate that the argument of "raise" is an exception object or class.
|
2927
|
+
// If it is a class, validate that the class's constructor accepts zero
|
2928
|
+
// arguments.
|
2929
|
+
if (exceptionType && baseExceptionType && (0, types_1.isInstantiableClass)(baseExceptionType)) {
|
2930
|
+
const diag = new diagnostic_1.DiagnosticAddendum();
|
2931
|
+
(0, typeUtils_1.doForEachSubtype)(exceptionType, (subtype) => {
|
2932
|
+
const concreteSubtype = makeTopLevelTypeVarsConcrete(subtype);
|
2933
|
+
if ((0, types_1.isAnyOrUnknown)(concreteSubtype) || (0, types_1.isNever)(concreteSubtype) || (0, typeUtils_1.isNoneInstance)(concreteSubtype)) {
|
2934
|
+
return;
|
2935
|
+
}
|
2936
|
+
if ((0, types_1.isInstantiableClass)(concreteSubtype) && concreteSubtype.priv.literalValue === undefined) {
|
2937
|
+
if (!(0, typeUtils_1.derivesFromClassRecursive)(concreteSubtype, baseExceptionType, /* ignoreUnknown */ false)) {
|
2938
|
+
diag.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
|
2939
|
+
type: printType(subtype),
|
2940
|
+
}));
|
2941
|
+
}
|
2942
|
+
else {
|
2943
|
+
let callResult;
|
2944
|
+
suppressDiagnostics(node, () => {
|
2945
|
+
callResult = (0, constructors_1.validateConstructorArgs)(evaluatorInterface, node, [], concreteSubtype,
|
2946
|
+
/* skipUnknownArgCheck */ false,
|
2947
|
+
/* inferenceContext */ undefined);
|
2948
|
+
});
|
2949
|
+
if (callResult && callResult.argumentErrors) {
|
2950
|
+
diag.addMessage(localize_1.LocMessage.exceptionTypeNotInstantiable().format({
|
2913
2951
|
type: printType(subtype),
|
2914
2952
|
}));
|
2915
2953
|
}
|
2916
2954
|
}
|
2917
|
-
});
|
2918
|
-
if (!diagAddendum.isEmpty()) {
|
2919
|
-
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedExceptionClass() + diagAddendum.getString(), node.d.typeExpression);
|
2920
2955
|
}
|
2956
|
+
else if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
2957
|
+
if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(concreteSubtype), baseExceptionType,
|
2958
|
+
/* ignoreUnknown */ false)) {
|
2959
|
+
diag.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
|
2960
|
+
type: printType(subtype),
|
2961
|
+
}));
|
2962
|
+
}
|
2963
|
+
}
|
2964
|
+
else {
|
2965
|
+
diag.addMessage(localize_1.LocMessage.exceptionTypeIncorrect().format({
|
2966
|
+
type: printType(subtype),
|
2967
|
+
}));
|
2968
|
+
}
|
2969
|
+
});
|
2970
|
+
if (!diag.isEmpty()) {
|
2971
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.expectedExceptionClass() + diag.getString(), node);
|
2921
2972
|
}
|
2922
2973
|
}
|
2923
2974
|
}
|
@@ -3073,6 +3124,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3073
3124
|
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
3074
3125
|
type = validateSymbolIsTypeExpression(node, type, !!effectiveTypeInfo.includesVariableDecl);
|
3075
3126
|
}
|
3127
|
+
// Add TypeForm details if appropriate.
|
3128
|
+
type = addTypeFormForSymbol(node, type, flags, !!effectiveTypeInfo.includesVariableDecl);
|
3076
3129
|
}
|
3077
3130
|
else {
|
3078
3131
|
// Handle the special case of "reveal_type" and "reveal_locals".
|
@@ -3106,19 +3159,71 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3106
3159
|
}
|
3107
3160
|
return { type, isIncomplete };
|
3108
3161
|
}
|
3109
|
-
|
3110
|
-
|
3111
|
-
|
3112
|
-
|
3113
|
-
|
3162
|
+
function addTypeFormForSymbol(node, type, flags, includesVarDecl) {
|
3163
|
+
var _a, _b;
|
3164
|
+
if (!isTypeFormSupported(node)) {
|
3165
|
+
return type;
|
3166
|
+
}
|
3167
|
+
const isValid = isSymbolValidTypeExpression(type, includesVarDecl);
|
3168
|
+
// If the type already has type information associated with it, don't replace.
|
3169
|
+
if ((_a = type.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
|
3170
|
+
// If the NoConvertSpecialForm flag is set, we are evaluating in
|
3171
|
+
// the interior of a type expression, so variables are not allowed.
|
3172
|
+
// Clear any existing type form type for this symbol in this case.
|
3173
|
+
if ((flags & 33554432 /* EvalFlags.NoConvertSpecialForm */) !== 0 && !isValid) {
|
3174
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, undefined);
|
3175
|
+
}
|
3114
3176
|
return type;
|
3115
3177
|
}
|
3116
|
-
|
3178
|
+
// If the symbol is not valid for a type expression (e.g. it's a variable),
|
3179
|
+
// don't add TypeForm info.
|
3180
|
+
if (!isValid) {
|
3117
3181
|
return type;
|
3118
3182
|
}
|
3119
|
-
|
3120
|
-
|
3183
|
+
if ((0, types_1.isTypeVar)(type) && type.priv.scopeId && !type.shared.isSynthesized) {
|
3184
|
+
if (!(0, types_1.isTypeVarTuple)(type) || !type.priv.isInUnion) {
|
3185
|
+
const liveScopeIds = ParseTreeUtils.getTypeVarScopesForNode(node);
|
3186
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, (0, typeUtils_1.convertToInstance)((0, typeUtils_1.makeTypeVarsBound)(type, liveScopeIds)));
|
3187
|
+
}
|
3188
|
+
}
|
3189
|
+
else if ((0, types_1.isInstantiableClass)(type) && !type.priv.includeSubclasses && !types_1.ClassType.isSpecialBuiltIn(type)) {
|
3190
|
+
if (types_1.ClassType.isBuiltIn(type, 'Any')) {
|
3191
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, types_1.AnyType.create());
|
3192
|
+
}
|
3193
|
+
else {
|
3194
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeWithDefaultTypeArgs)(type)));
|
3195
|
+
}
|
3196
|
+
}
|
3197
|
+
if (((_b = type.props) === null || _b === void 0 ? void 0 : _b.typeAliasInfo) && types_1.TypeBase.isInstantiable(type)) {
|
3198
|
+
let typeFormType = type;
|
3199
|
+
if ((flags & 2 /* EvalFlags.NoSpecialize */) === 0) {
|
3200
|
+
typeFormType = specializeTypeAliasWithDefaults(typeFormType, /* errorNode */ undefined);
|
3201
|
+
}
|
3202
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, (0, typeUtils_1.convertToInstance)(typeFormType));
|
3203
|
+
}
|
3204
|
+
return type;
|
3205
|
+
}
|
3206
|
+
function isSymbolValidTypeExpression(type, includesVarDecl) {
|
3207
|
+
var _a;
|
3208
|
+
// Verify that the name does not refer to a (non type alias) variable.
|
3209
|
+
if (!includesVarDecl || ((_a = type.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo)) {
|
3210
|
+
return true;
|
3211
|
+
}
|
3212
|
+
if ((0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
|
3213
|
+
return true;
|
3214
|
+
}
|
3215
|
+
if ((0, types_1.isTypeVar)(type) && !type.priv.scopeId) {
|
3216
|
+
return true;
|
3217
|
+
}
|
3218
|
+
// Exempts class types that are created by calling NewType, NamedTuple, etc.
|
3121
3219
|
if ((0, types_1.isClass)(type) && !type.priv.includeSubclasses && types_1.ClassType.isValidTypeAliasClass(type)) {
|
3220
|
+
return true;
|
3221
|
+
}
|
3222
|
+
return false;
|
3223
|
+
}
|
3224
|
+
// Reports diagnostics if type isn't valid within a type expression.
|
3225
|
+
function validateSymbolIsTypeExpression(node, type, includesVarDecl) {
|
3226
|
+
if (isSymbolValidTypeExpression(type, includesVarDecl)) {
|
3122
3227
|
return type;
|
3123
3228
|
}
|
3124
3229
|
// Disable for assignments in the typings.pyi file, since it defines special forms.
|
@@ -3133,7 +3238,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3133
3238
|
// evaluated in a value expression context, convert it from its special
|
3134
3239
|
// meaning to its runtime value.
|
3135
3240
|
function convertSpecialFormToRuntimeValue(type, flags) {
|
3136
|
-
var _a, _b;
|
3241
|
+
var _a, _b, _c, _d;
|
3137
3242
|
const exemptFlags = 256 /* EvalFlags.TypeExpression */ | 128 /* EvalFlags.InstantiableType */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
|
3138
3243
|
if ((flags & exemptFlags) !== 0) {
|
3139
3244
|
return type;
|
@@ -3148,6 +3253,16 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3148
3253
|
if (!((_b = type.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
|
3149
3254
|
return type;
|
3150
3255
|
}
|
3256
|
+
// If this is a type alias and we are not supposed to specialize it, return it as is.
|
3257
|
+
if ((flags & 2 /* EvalFlags.NoSpecialize */) !== 0 && ((_c = type.props) === null || _c === void 0 ? void 0 : _c.typeAliasInfo)) {
|
3258
|
+
// Special-case TypeAliasType which should be converted in this case.
|
3259
|
+
if (!types_1.ClassType.isBuiltIn(type.props.specialForm, 'TypeAliasType')) {
|
3260
|
+
return type;
|
3261
|
+
}
|
3262
|
+
}
|
3263
|
+
if ((_d = type.props) === null || _d === void 0 ? void 0 : _d.typeForm) {
|
3264
|
+
return types_1.TypeBase.cloneWithTypeForm(type.props.specialForm, type.props.typeForm);
|
3265
|
+
}
|
3151
3266
|
return type.props.specialForm;
|
3152
3267
|
}
|
3153
3268
|
// Handles the case where a variable or parameter is defined in an outer
|
@@ -3257,7 +3372,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3257
3372
|
if ((0, types_1.isUnpackedTypeVarTuple)(type)) {
|
3258
3373
|
type = types_1.TypeVarType.cloneForPacked(type);
|
3259
3374
|
}
|
3260
|
-
if ((flags &
|
3375
|
+
if ((flags & -2147483648 /* EvalFlags.EnforceClassTypeVarScope */) !== 0 && !enforceClassTypeVarScope(node, type)) {
|
3261
3376
|
return types_1.UnknownType.create();
|
3262
3377
|
}
|
3263
3378
|
return type;
|
@@ -3553,6 +3668,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3553
3668
|
}
|
3554
3669
|
// Detect, report, and fill in missing type arguments if appropriate.
|
3555
3670
|
typeResult.type = reportMissingTypeArgs(node, typeResult.type, flags);
|
3671
|
+
// Add TypeForm details if appropriate.
|
3672
|
+
typeResult.type = addTypeFormForSymbol(node, typeResult.type, flags, /* includesVarDecl */ false);
|
3556
3673
|
}
|
3557
3674
|
if (baseTypeResult.isIncomplete) {
|
3558
3675
|
typeResult.isIncomplete = true;
|
@@ -3580,7 +3697,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3580
3697
|
return typeResult;
|
3581
3698
|
}
|
3582
3699
|
function getTypeOfMemberAccessWithBaseType(node, baseTypeResult, usage, flags) {
|
3583
|
-
var _a, _b, _c;
|
3700
|
+
var _a, _b, _c, _d;
|
3584
3701
|
let baseType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(baseTypeResult.type);
|
3585
3702
|
const memberName = node.d.member.d.value;
|
3586
3703
|
let diag = new diagnostic_1.DiagnosticAddendum();
|
@@ -3593,12 +3710,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3593
3710
|
const isRequired = false;
|
3594
3711
|
const isNotRequired = false;
|
3595
3712
|
let memberAccessDeprecationInfo;
|
3713
|
+
if ((_a = usage === null || usage === void 0 ? void 0 : usage.setType) === null || _a === void 0 ? void 0 : _a.isIncomplete) {
|
3714
|
+
isIncomplete = true;
|
3715
|
+
}
|
3596
3716
|
// If the base type was incomplete and unbound, don't proceed
|
3597
3717
|
// because false positive errors will be generated.
|
3598
3718
|
if (baseTypeResult.isIncomplete && (0, types_1.isUnbound)(baseType)) {
|
3599
3719
|
return { type: types_1.UnknownType.create(/* isIncomplete */ true), isIncomplete: true };
|
3600
3720
|
}
|
3601
|
-
if (((
|
3721
|
+
if (((_b = baseType.props) === null || _b === void 0 ? void 0 : _b.specialForm) && (flags & 256 /* EvalFlags.TypeExpression */) === 0) {
|
3602
3722
|
baseType = baseType.props.specialForm;
|
3603
3723
|
}
|
3604
3724
|
if ((0, types_1.isParamSpec)(baseType) && baseType.priv.paramSpecAccess) {
|
@@ -3676,7 +3796,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3676
3796
|
types_1.ClassType.isSameGenericClass(enumMemberResult.type, baseType) &&
|
3677
3797
|
enumMemberResult.type.priv.literalValue !== undefined) {
|
3678
3798
|
const diagMessage = usage.method === 'set' ? localize_1.LocMessage.enumMemberSet() : localize_1.LocMessage.enumMemberDelete();
|
3679
|
-
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAttributeAccessIssue, diagMessage.format({ name: memberName }) + diag.getString(), node.d.member, (
|
3799
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAttributeAccessIssue, diagMessage.format({ name: memberName }) + diag.getString(), node.d.member, (_c = diag.getEffectiveTextRange()) !== null && _c !== void 0 ? _c : node.d.member);
|
3680
3800
|
}
|
3681
3801
|
}
|
3682
3802
|
}
|
@@ -3720,6 +3840,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3720
3840
|
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
3721
3841
|
type = validateSymbolIsTypeExpression(node, type, !!typeResult.includesVariableDecl);
|
3722
3842
|
}
|
3843
|
+
// Add TypeForm details if appropriate.
|
3844
|
+
type = addTypeFormForSymbol(node, type, flags, !!typeResult.includesVariableDecl);
|
3723
3845
|
if ((0, types_1.isTypeVar)(type)) {
|
3724
3846
|
type = validateTypeVarUsage(node, type, flags);
|
3725
3847
|
}
|
@@ -3877,7 +3999,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
3877
3999
|
const rule = isFunctionRule
|
3878
4000
|
? diagnosticRules_1.DiagnosticRule.reportFunctionMemberAccess
|
3879
4001
|
: diagnosticRules_1.DiagnosticRule.reportAttributeAccessIssue;
|
3880
|
-
addDiagnostic(rule, diagMessage.format({ name: memberName, type: printType(baseType) }) + diag.getString(), node.d.member, (
|
4002
|
+
addDiagnostic(rule, diagMessage.format({ name: memberName, type: printType(baseType) }) + diag.getString(), node.d.member, (_d = diag.getEffectiveTextRange()) !== null && _d !== void 0 ? _d : node.d.member);
|
3881
4003
|
}
|
3882
4004
|
// If this is member access on a function, use "Any" so if the
|
3883
4005
|
// reportFunctionMemberAccess rule is disabled, we don't trigger
|
@@ -4083,9 +4205,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4083
4205
|
}
|
4084
4206
|
// Check for an attempt to overwrite or delete an instance variable that is
|
4085
4207
|
// read-only (e.g. in a named tuple).
|
4086
|
-
if ((memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.isInstanceMember) &&
|
4087
|
-
(0, types_1.isClass)(memberInfo.classType) &&
|
4088
|
-
types_1.ClassType.isReadOnlyInstanceVariables(memberInfo.classType)) {
|
4208
|
+
if ((memberInfo === null || memberInfo === void 0 ? void 0 : memberInfo.isInstanceMember) && (0, types_1.isClass)(memberInfo.classType) && memberInfo.isReadOnly) {
|
4089
4209
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.readOnlyAttribute().format({ name: memberName }));
|
4090
4210
|
isDescriptorError = true;
|
4091
4211
|
}
|
@@ -4729,13 +4849,18 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4729
4849
|
// Handles index expressions that are providing type arguments for a
|
4730
4850
|
// generic type alias.
|
4731
4851
|
function createSpecializedTypeAlias(node, baseType, flags) {
|
4732
|
-
var _a, _b, _c;
|
4733
|
-
|
4852
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
4853
|
+
let aliasInfo = (_a = baseType.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo;
|
4854
|
+
let aliasBaseType = baseType;
|
4855
|
+
if (!aliasInfo && ((_b = baseType.props) === null || _b === void 0 ? void 0 : _b.typeForm)) {
|
4856
|
+
aliasInfo = (_d = (_c = baseType.props.typeForm) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.typeAliasInfo;
|
4857
|
+
aliasBaseType = (0, typeUtils_1.convertToInstantiable)(baseType.props.typeForm);
|
4858
|
+
}
|
4734
4859
|
if (!(aliasInfo === null || aliasInfo === void 0 ? void 0 : aliasInfo.typeParams) || (aliasInfo.typeParams.length === 0 && aliasInfo.typeArgs)) {
|
4735
4860
|
return undefined;
|
4736
4861
|
}
|
4737
4862
|
// If this is not instantiable, then the index expression isn't a specialization.
|
4738
|
-
if (!types_1.TypeBase.isInstantiable(
|
4863
|
+
if (!types_1.TypeBase.isInstantiable(aliasBaseType)) {
|
4739
4864
|
return undefined;
|
4740
4865
|
}
|
4741
4866
|
// If this is already specialized, the index expression isn't a specialization.
|
@@ -4744,6 +4869,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4744
4869
|
}
|
4745
4870
|
const typeParams = aliasInfo.typeParams;
|
4746
4871
|
let typeArgs = adjustTypeArgsForTypeVarTuple(getTypeArgs(node, flags), typeParams, node);
|
4872
|
+
let reportedError = false;
|
4747
4873
|
// PEP 612 says that if the class has only one type parameter consisting
|
4748
4874
|
// of a ParamSpec, the list of arguments does not need to be enclosed in
|
4749
4875
|
// a list. We'll handle that case specially here. Presumably this applies to
|
@@ -4766,21 +4892,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4766
4892
|
}
|
4767
4893
|
if (typeArgs.length > typeParams.length) {
|
4768
4894
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
|
4769
|
-
name: printType(
|
4895
|
+
name: printType(aliasBaseType),
|
4770
4896
|
expected: typeParams.length,
|
4771
4897
|
received: typeArgs.length,
|
4772
4898
|
}), typeArgs[typeParams.length].node);
|
4899
|
+
reportedError = true;
|
4773
4900
|
}
|
4774
4901
|
else if (typeArgs.length < minTypeArgCount) {
|
4775
4902
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooFew().format({
|
4776
|
-
name: printType(
|
4903
|
+
name: printType(aliasBaseType),
|
4777
4904
|
expected: typeParams.length,
|
4778
4905
|
received: typeArgs.length,
|
4779
4906
|
}), node.d.items[node.d.items.length - 1]);
|
4907
|
+
reportedError = true;
|
4780
4908
|
}
|
4781
4909
|
// Handle the mypy_extensions.FlexibleAlias type specially.
|
4782
|
-
if ((0, types_1.isInstantiableClass)(
|
4783
|
-
|
4910
|
+
if ((0, types_1.isInstantiableClass)(aliasBaseType) &&
|
4911
|
+
aliasBaseType.shared.fullName === 'mypy_extensions.FlexibleAlias' &&
|
4784
4912
|
typeArgs.length >= 1) {
|
4785
4913
|
return { node, type: typeArgs[0].type };
|
4786
4914
|
}
|
@@ -4832,11 +4960,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4832
4960
|
}
|
4833
4961
|
else {
|
4834
4962
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListExpected(), typeArgs[index].node);
|
4963
|
+
reportedError = true;
|
4835
4964
|
}
|
4836
4965
|
}
|
4837
4966
|
else {
|
4838
4967
|
if (index < typeArgs.length && typeArgs[index].typeList) {
|
4839
4968
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[index].node);
|
4969
|
+
reportedError = true;
|
4840
4970
|
}
|
4841
4971
|
let typeArgType;
|
4842
4972
|
if (index < typeArgs.length) {
|
@@ -4854,7 +4984,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4854
4984
|
typeArgType = types_1.UnknownType.create();
|
4855
4985
|
}
|
4856
4986
|
if ((flags & 16384 /* EvalFlags.EnforceVarianceConsistency */) !== 0) {
|
4857
|
-
const usageVariances = inferVarianceForTypeAlias(
|
4987
|
+
const usageVariances = inferVarianceForTypeAlias(aliasBaseType);
|
4858
4988
|
if (usageVariances && index < usageVariances.length) {
|
4859
4989
|
const usageVariance = usageVariances[index];
|
4860
4990
|
if (!(0, typeUtils_1.isVarianceOfTypeArgCompatible)(typeArgType, usageVariance)) {
|
@@ -4877,11 +5007,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4877
5007
|
}
|
4878
5008
|
});
|
4879
5009
|
if (!diag.isEmpty()) {
|
4880
|
-
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeNotSpecializable().format({ type: printType(
|
5010
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeNotSpecializable().format({ type: printType(aliasBaseType) }) + diag.getString(), node, (_e = diag.getEffectiveTextRange()) !== null && _e !== void 0 ? _e : node);
|
5011
|
+
reportedError = true;
|
4881
5012
|
}
|
4882
5013
|
const solutionSet = (0, constraintSolver_1.solveConstraints)(evaluatorInterface, constraints).getMainSolutionSet();
|
4883
5014
|
const aliasTypeArgs = [];
|
4884
|
-
(
|
5015
|
+
(_f = aliasInfo.typeParams) === null || _f === void 0 ? void 0 : _f.forEach((typeParam) => {
|
4885
5016
|
let typeVarType = solutionSet.getType(typeParam);
|
4886
5017
|
// Fill in any unsolved type arguments with unknown.
|
4887
5018
|
if (!typeVarType) {
|
@@ -4890,18 +5021,46 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
4890
5021
|
}
|
4891
5022
|
aliasTypeArgs.push(typeVarType);
|
4892
5023
|
});
|
4893
|
-
|
5024
|
+
let type = types_1.TypeBase.cloneForTypeAlias(solveAndApplyConstraints(aliasBaseType, constraints), {
|
4894
5025
|
...aliasInfo,
|
4895
5026
|
typeArgs: aliasTypeArgs,
|
4896
5027
|
});
|
4897
|
-
|
5028
|
+
if (isTypeFormSupported(node)) {
|
5029
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, reportedError ? undefined : (0, typeUtils_1.convertToInstance)(type));
|
5030
|
+
}
|
5031
|
+
if ((_g = baseType.props) === null || _g === void 0 ? void 0 : _g.typeAliasInfo) {
|
5032
|
+
return { type, node };
|
5033
|
+
}
|
5034
|
+
return { type: types_1.TypeBase.cloneWithTypeForm(baseType, (0, typeUtils_1.convertToInstance)(type)), node };
|
4898
5035
|
}
|
4899
5036
|
function getTypeOfIndexWithBaseType(node, baseTypeResult, usage, flags) {
|
5037
|
+
var _a, _b, _c;
|
4900
5038
|
// Handle the case where we're specializing a generic type alias.
|
4901
5039
|
const typeAliasResult = createSpecializedTypeAlias(node, baseTypeResult.type, flags);
|
4902
5040
|
if (typeAliasResult) {
|
4903
5041
|
return typeAliasResult;
|
4904
5042
|
}
|
5043
|
+
// Handle the case where Never or NoReturn are being specialized.
|
5044
|
+
if ((0, types_1.isNever)(baseTypeResult.type) && ((_a = baseTypeResult.type.props) === null || _a === void 0 ? void 0 : _a.specialForm)) {
|
5045
|
+
// Swap in the special form type, which is the Never or NoReturn class.
|
5046
|
+
baseTypeResult = { ...baseTypeResult, type: baseTypeResult.type.props.specialForm };
|
5047
|
+
}
|
5048
|
+
// Handle the case where a TypeAliasType symbol is being specialized
|
5049
|
+
// in a value expression.
|
5050
|
+
if ((0, types_1.isClassInstance)(baseTypeResult.type) &&
|
5051
|
+
types_1.ClassType.isBuiltIn(baseTypeResult.type, 'TypeAliasType') &&
|
5052
|
+
((_b = baseTypeResult.type.props) === null || _b === void 0 ? void 0 : _b.typeForm)) {
|
5053
|
+
const typeAliasInfo = (_c = baseTypeResult.type.props.typeForm.props) === null || _c === void 0 ? void 0 : _c.typeAliasInfo;
|
5054
|
+
if (typeAliasInfo && typeAliasInfo.typeParams) {
|
5055
|
+
const origTypeAlias = types_1.TypeBase.cloneForTypeAlias((0, typeUtils_1.convertToInstantiable)(baseTypeResult.type.props.typeForm), { ...typeAliasInfo, typeArgs: undefined });
|
5056
|
+
const typeFormType = createSpecializedTypeAlias(node, origTypeAlias, flags);
|
5057
|
+
if (typeFormType) {
|
5058
|
+
return {
|
5059
|
+
type: types_1.TypeBase.cloneWithTypeForm(baseTypeResult.type, (0, typeUtils_1.convertToInstance)(typeFormType.type)),
|
5060
|
+
};
|
5061
|
+
}
|
5062
|
+
}
|
5063
|
+
}
|
4905
5064
|
if ((0, types_1.isTypeVar)(baseTypeResult.type) && (0, typeUtils_1.isTypeAliasPlaceholder)(baseTypeResult.type)) {
|
4906
5065
|
const typeArgTypes = getTypeArgs(node, flags).map((t) => (0, typeUtils_1.convertToInstance)(t.type));
|
4907
5066
|
const type = types_1.TypeBase.cloneForTypeAlias(baseTypeResult.type, {
|
@@ -5324,6 +5483,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5324
5483
|
function getTypeArgs(node, flags, options) {
|
5325
5484
|
const typeArgs = [];
|
5326
5485
|
let adjFlags = flags | 33554432 /* EvalFlags.NoConvertSpecialForm */;
|
5486
|
+
adjFlags &= ~1073741824 /* EvalFlags.TypeFormArg */;
|
5327
5487
|
const allowFinalClassVar = () => {
|
5328
5488
|
// If the annotation is a variable within the body of a dataclass, a
|
5329
5489
|
// Final is allowed with a ClassVar annotation. In all other cases,
|
@@ -5558,7 +5718,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5558
5718
|
}
|
5559
5719
|
const entryTypeResults = node.d.items.map((expr, index) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */, (0, typeUtils_1.makeInferenceContext)(index < expectedTypes.length ? expectedTypes[index] : undefined, inferenceContext.isTypeIncomplete)));
|
5560
5720
|
const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
|
5561
|
-
const type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
|
5562
5721
|
// Copy any expected type diag addenda for precision error reporting.
|
5563
5722
|
let expectedTypeDiagAddendum;
|
5564
5723
|
if (entryTypeResults.some((result) => result.expectedTypeDiagAddendum)) {
|
@@ -5569,11 +5728,27 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5569
5728
|
}
|
5570
5729
|
});
|
5571
5730
|
}
|
5731
|
+
// If the tuple contains a very large number of entries, it's probably
|
5732
|
+
// generated code. If we encounter type errors, don't bother building
|
5733
|
+
// the full tuple type.
|
5734
|
+
let type;
|
5735
|
+
if (node.d.items.length > maxInferredTupleEntryCount && entryTypeResults.some((result) => result.typeErrors)) {
|
5736
|
+
type = makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]);
|
5737
|
+
}
|
5738
|
+
else {
|
5739
|
+
type = makeTupleObject(buildTupleTypesList(entryTypeResults, /* stripLiterals */ false));
|
5740
|
+
}
|
5572
5741
|
return { type, expectedTypeDiagAddendum, isIncomplete };
|
5573
5742
|
}
|
5574
5743
|
function getTypeOfTupleInferred(node, flags) {
|
5575
5744
|
const entryTypeResults = node.d.items.map((expr) => getTypeOfExpression(expr, flags | 268435456 /* EvalFlags.StripTupleLiterals */));
|
5576
5745
|
const isIncomplete = entryTypeResults.some((result) => result.isIncomplete);
|
5746
|
+
// If the tuple contains a very large number of entries, it's probably
|
5747
|
+
// generated code. Rather than taking the time to evaluate every entry,
|
5748
|
+
// simply return an unknown type in this case.
|
5749
|
+
if (node.d.items.length > maxInferredTupleEntryCount) {
|
5750
|
+
return { type: makeTupleObject([{ type: types_1.UnknownType.create(), isUnbounded: true }]) };
|
5751
|
+
}
|
5577
5752
|
const type = makeTupleObject(buildTupleTypesList(entryTypeResults, (flags & 268435456 /* EvalFlags.StripTupleLiterals */) !== 0));
|
5578
5753
|
if (isIncomplete) {
|
5579
5754
|
if ((0, typeUtils_1.getContainerDepth)(type) > maxInferredContainerDepth) {
|
@@ -5611,7 +5786,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5611
5786
|
entryTypes.push({ type: types_1.UnknownType.create(/* isIncomplete */ true), isUnbounded: false });
|
5612
5787
|
}
|
5613
5788
|
else {
|
5614
|
-
|
5789
|
+
let entryType = convertSpecialFormToRuntimeValue(typeResult.type, 0 /* EvalFlags.None */);
|
5790
|
+
entryType = stripLiterals ? (0, typeUtils_1.stripTypeForm)(stripLiteralValue(entryType)) : entryType;
|
5615
5791
|
entryTypes.push({ type: entryType, isUnbounded: !!typeResult.unpackedType });
|
5616
5792
|
}
|
5617
5793
|
}
|
@@ -5675,6 +5851,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5675
5851
|
// Handle the "typing.assert_type" call.
|
5676
5852
|
typeResult = getTypeOfAssertType(node, inferenceContext);
|
5677
5853
|
}
|
5854
|
+
else if ((0, types_1.isClass)(baseTypeResult.type) && types_1.ClassType.isBuiltIn(baseTypeResult.type, 'TypeForm')) {
|
5855
|
+
// Handle the "typing.TypeForm" call.
|
5856
|
+
typeResult = getTypeOfTypeForm(node, baseTypeResult.type);
|
5857
|
+
}
|
5678
5858
|
else if ((0, types_1.isAnyOrUnknown)(baseTypeResult.type) &&
|
5679
5859
|
node.d.leftExpr.nodeType === 38 /* ParseNodeType.Name */ &&
|
5680
5860
|
node.d.leftExpr.d.value === 'reveal_locals') {
|
@@ -5778,6 +5958,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5778
5958
|
}
|
5779
5959
|
return typeResult;
|
5780
5960
|
}
|
5961
|
+
function getTypeOfTypeForm(node, typeFormClass) {
|
5962
|
+
var _a;
|
5963
|
+
if (node.d.args.length !== 1 ||
|
5964
|
+
node.d.args[0].d.argCategory !== 0 /* ArgCategory.Simple */ ||
|
5965
|
+
node.d.args[0].d.name !== undefined) {
|
5966
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.typeFormArgs(), node);
|
5967
|
+
return { type: types_1.UnknownType.create() };
|
5968
|
+
}
|
5969
|
+
const typeFormResult = getTypeOfArgExpectingType((0, typeUtils_1.convertNodeToArg)(node.d.args[0]), {
|
5970
|
+
typeFormArg: isTypeFormSupported(node),
|
5971
|
+
noNonTypeSpecialForms: true,
|
5972
|
+
typeExpression: true,
|
5973
|
+
});
|
5974
|
+
if (!typeFormResult.typeErrors && ((_a = typeFormResult.type.props) === null || _a === void 0 ? void 0 : _a.typeForm)) {
|
5975
|
+
typeFormResult.type = (0, typeUtils_1.convertToInstance)(types_1.ClassType.specialize(typeFormClass, [(0, typeUtils_1.convertToInstance)(typeFormResult.type.props.typeForm)]));
|
5976
|
+
}
|
5977
|
+
return typeFormResult;
|
5978
|
+
}
|
5781
5979
|
function getTypeOfAssertType(node, inferenceContext) {
|
5782
5980
|
if (node.d.args.length !== 2 ||
|
5783
5981
|
node.d.args[0].d.argCategory !== 0 /* ArgCategory.Simple */ ||
|
@@ -5798,7 +5996,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5798
5996
|
// The spec is unclear on whether this is the correct behavior, but it seems to be
|
5799
5997
|
// what mypy does -- and what various library authors expect.
|
5800
5998
|
const arg0Type = stripTypeGuard(arg0TypeResult.type);
|
5801
|
-
if (!(0, types_1.isTypeSame)(assertedType, arg0Type, {
|
5999
|
+
if (!(0, types_1.isTypeSame)(assertedType, arg0Type, {
|
6000
|
+
treatAnySameAsUnknown: true,
|
6001
|
+
ignorePseudoGeneric: true,
|
6002
|
+
})) {
|
5802
6003
|
const srcDestTypes = printSrcDestTypes(arg0TypeResult.type, assertedType, { expandTypeAlias: true });
|
5803
6004
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportAssertTypeFailure, localize_1.LocMessage.assertTypeTypeMismatch().format({
|
5804
6005
|
expected: srcDestTypes.destType,
|
@@ -5944,30 +6145,33 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
5944
6145
|
}
|
5945
6146
|
const concreteTargetClassType = makeTopLevelTypeVarsConcrete(targetClassType);
|
5946
6147
|
// Determine whether to further narrow the type.
|
6148
|
+
let secondArgType;
|
5947
6149
|
let bindToType;
|
5948
6150
|
if (node.d.args.length > 1) {
|
5949
|
-
|
6151
|
+
secondArgType = getTypeOfExpression(node.d.args[1].d.valueExpr).type;
|
6152
|
+
const secondArgConcreteType = makeTopLevelTypeVarsConcrete(secondArgType);
|
5950
6153
|
let reportError = false;
|
5951
|
-
if ((0, types_1.isAnyOrUnknown)(
|
6154
|
+
if ((0, types_1.isAnyOrUnknown)(secondArgConcreteType)) {
|
5952
6155
|
// Ignore unknown or any types.
|
5953
6156
|
}
|
5954
|
-
else if ((0, types_1.isClassInstance)(
|
6157
|
+
else if ((0, types_1.isClassInstance)(secondArgConcreteType)) {
|
5955
6158
|
if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
|
5956
|
-
if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(
|
6159
|
+
if (!(0, typeUtils_1.derivesFromClassRecursive)(types_1.ClassType.cloneAsInstantiable(secondArgConcreteType), concreteTargetClassType,
|
5957
6160
|
/* ignoreUnknown */ true)) {
|
5958
6161
|
reportError = true;
|
5959
6162
|
}
|
5960
6163
|
}
|
5961
|
-
bindToType =
|
6164
|
+
bindToType = secondArgConcreteType;
|
5962
6165
|
}
|
5963
|
-
else if ((0, types_1.isInstantiableClass)(
|
6166
|
+
else if ((0, types_1.isInstantiableClass)(secondArgConcreteType)) {
|
5964
6167
|
if ((0, types_1.isInstantiableClass)(concreteTargetClassType)) {
|
5965
6168
|
if (!types_1.ClassType.isBuiltIn(concreteTargetClassType, 'type') &&
|
5966
|
-
!(0, typeUtils_1.derivesFromClassRecursive)(
|
6169
|
+
!(0, typeUtils_1.derivesFromClassRecursive)(secondArgConcreteType, concreteTargetClassType,
|
6170
|
+
/* ignoreUnknown */ true)) {
|
5967
6171
|
reportError = true;
|
5968
6172
|
}
|
5969
6173
|
}
|
5970
|
-
bindToType =
|
6174
|
+
bindToType = secondArgConcreteType;
|
5971
6175
|
}
|
5972
6176
|
else {
|
5973
6177
|
reportError = true;
|
@@ -6069,10 +6273,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
6069
6273
|
}
|
6070
6274
|
let bindToSelfType;
|
6071
6275
|
if (bindToType) {
|
6072
|
-
if (
|
6073
|
-
// If
|
6074
|
-
//
|
6075
|
-
|
6276
|
+
if (secondArgType) {
|
6277
|
+
// If a TypeVar was passed as the second argument, use it
|
6278
|
+
// to derive the the self type.
|
6279
|
+
if ((0, types_1.isTypeVar)(secondArgType)) {
|
6280
|
+
bindToSelfType = (0, typeUtils_1.convertToInstance)(secondArgType);
|
6281
|
+
}
|
6076
6282
|
}
|
6077
6283
|
else {
|
6078
6284
|
// If this is a zero-argument form of super(), synthesize
|
@@ -6879,18 +7085,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
6879
7085
|
if (expandedCallType.shared.name === 'type' && argList.length === 1) {
|
6880
7086
|
const argType = getTypeOfArg(argList[0], /* inferenceContext */ undefined).type;
|
6881
7087
|
const returnType = (0, typeUtils_1.mapSubtypes)(argType, (subtype) => {
|
6882
|
-
if ((0, types_1.isInstantiableClass)(subtype) && subtype.shared.effectiveMetaclass) {
|
6883
|
-
return subtype.shared.effectiveMetaclass;
|
6884
|
-
}
|
6885
7088
|
if ((0, types_1.isNever)(subtype)) {
|
6886
7089
|
return subtype;
|
6887
7090
|
}
|
7091
|
+
if ((0, types_1.isClass)(subtype)) {
|
7092
|
+
return (0, typeUtils_1.convertToInstantiable)(stripLiteralValue(subtype));
|
7093
|
+
}
|
6888
7094
|
if (types_1.TypeBase.isInstance(subtype)) {
|
6889
|
-
if ((0, types_1.
|
6890
|
-
return (0, typeUtils_1.convertToInstantiable)(
|
6891
|
-
}
|
6892
|
-
if ((0, types_1.isFunction)(subtype)) {
|
6893
|
-
return types_1.FunctionType.cloneAsInstantiable(subtype);
|
7095
|
+
if ((0, types_1.isFunction)(subtype) || (0, types_1.isTypeVar)(subtype)) {
|
7096
|
+
return (0, typeUtils_1.convertToInstantiable)(subtype);
|
6894
7097
|
}
|
6895
7098
|
}
|
6896
7099
|
return types_1.ClassType.specialize(types_1.ClassType.cloneAsInstance(expandedCallType), [
|
@@ -7145,7 +7348,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7145
7348
|
// validation is left to the caller.
|
7146
7349
|
// This logic is based on PEP 3102: https://www.python.org/dev/peps/pep-3102/
|
7147
7350
|
function matchArgsToParams(errorNode, argList, typeResult, overloadIndex) {
|
7148
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
7351
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
7149
7352
|
const overload = typeResult.type;
|
7150
7353
|
const paramDetails = (0, parameterUtils_1.getParamListDetails)(overload);
|
7151
7354
|
const paramSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(overload);
|
@@ -7562,9 +7765,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7562
7765
|
else if ((0, types_1.isClassInstance)(argType) && types_1.ClassType.isTypedDictClass(argType)) {
|
7563
7766
|
// Handle the special case where it is a TypedDict and we know which
|
7564
7767
|
// keys are present.
|
7565
|
-
const
|
7768
|
+
const tdEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluatorInterface, argType);
|
7566
7769
|
const diag = new diagnostic_1.DiagnosticAddendum();
|
7567
|
-
|
7770
|
+
tdEntries.knownItems.forEach((entry, name) => {
|
7771
|
+
var _a, _b;
|
7568
7772
|
const paramEntry = paramMap.get(name);
|
7569
7773
|
if (paramEntry && !paramEntry.isPositionalOnly) {
|
7570
7774
|
if (paramEntry.argsReceived > 0) {
|
@@ -7583,7 +7787,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7583
7787
|
argCategory: 0 /* ArgCategory.Simple */,
|
7584
7788
|
typeResult: { type: entry.valueType },
|
7585
7789
|
},
|
7586
|
-
errorNode: argList[argIndex].valueExpression
|
7790
|
+
errorNode: (_a = argList[argIndex].valueExpression) !== null && _a !== void 0 ? _a : errorNode,
|
7587
7791
|
paramName: name,
|
7588
7792
|
});
|
7589
7793
|
}
|
@@ -7598,7 +7802,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7598
7802
|
argCategory: 0 /* ArgCategory.Simple */,
|
7599
7803
|
typeResult: { type: entry.valueType },
|
7600
7804
|
},
|
7601
|
-
errorNode: argList[argIndex].valueExpression
|
7805
|
+
errorNode: (_b = argList[argIndex].valueExpression) !== null && _b !== void 0 ? _b : errorNode,
|
7602
7806
|
paramName: name,
|
7603
7807
|
});
|
7604
7808
|
// Remember that this parameter has already received a value.
|
@@ -7618,6 +7822,23 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7618
7822
|
}
|
7619
7823
|
}
|
7620
7824
|
});
|
7825
|
+
const extraItemsType = (_g = (_f = tdEntries.extraItems) === null || _f === void 0 ? void 0 : _f.valueType) !== null && _g !== void 0 ? _g : getObjectType();
|
7826
|
+
if (!(0, types_1.isNever)(extraItemsType)) {
|
7827
|
+
if (paramDetails.kwargsIndex !== undefined) {
|
7828
|
+
const kwargsParam = paramDetails.params[paramDetails.kwargsIndex];
|
7829
|
+
validateArgTypeParams.push({
|
7830
|
+
paramCategory: 2 /* ParamCategory.KwargsDict */,
|
7831
|
+
paramType: kwargsParam.declaredType,
|
7832
|
+
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(kwargsParam.declaredType),
|
7833
|
+
argument: {
|
7834
|
+
argCategory: 2 /* ArgCategory.UnpackedDictionary */,
|
7835
|
+
typeResult: { type: extraItemsType },
|
7836
|
+
},
|
7837
|
+
errorNode: (_h = argList[argIndex].valueExpression) !== null && _h !== void 0 ? _h : errorNode,
|
7838
|
+
paramName: kwargsParam.param.name,
|
7839
|
+
});
|
7840
|
+
}
|
7841
|
+
}
|
7621
7842
|
if (!diag.isEmpty()) {
|
7622
7843
|
if (!canSkipDiagnosticForNode(errorNode) && !isTypeIncomplete) {
|
7623
7844
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportCallIssue, localize_1.LocMessage.unpackedTypedDictArgument() + diag.getString(), argList[argIndex].valueExpression || errorNode);
|
@@ -7724,7 +7945,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7724
7945
|
paramType,
|
7725
7946
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
7726
7947
|
argument: argList[argIndex],
|
7727
|
-
errorNode: (
|
7948
|
+
errorNode: (_j = argList[argIndex].valueExpression) !== null && _j !== void 0 ? _j : errorNode,
|
7728
7949
|
paramName: paramNameValue,
|
7729
7950
|
});
|
7730
7951
|
trySetActive(argList[argIndex], paramDetails.params[paramInfoIndex].param);
|
@@ -7747,7 +7968,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7747
7968
|
paramType,
|
7748
7969
|
requiresTypeVarMatching: (0, typeUtils_1.requiresSpecialization)(paramType),
|
7749
7970
|
argument: argList[argIndex],
|
7750
|
-
errorNode: (
|
7971
|
+
errorNode: (_k = argList[argIndex].valueExpression) !== null && _k !== void 0 ? _k : errorNode,
|
7751
7972
|
paramName: paramNameValue,
|
7752
7973
|
});
|
7753
7974
|
// Remember that this parameter has already received a value.
|
@@ -7798,7 +8019,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
7798
8019
|
requiresTypeVarMatching: false,
|
7799
8020
|
argument: argList[argIndex],
|
7800
8021
|
argType: (0, types_1.isParamSpec)(argType) ? undefined : types_1.AnyType.create(),
|
7801
|
-
errorNode: (
|
8022
|
+
errorNode: (_l = argList[argIndex].valueExpression) !== null && _l !== void 0 ? _l : errorNode,
|
7802
8023
|
});
|
7803
8024
|
}
|
7804
8025
|
}
|
@@ -9208,6 +9429,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9208
9429
|
if (node.d.constType === 26 /* KeywordType.None */) {
|
9209
9430
|
if (noneTypeClass) {
|
9210
9431
|
type = (flags & 128 /* EvalFlags.InstantiableType */) !== 0 ? noneTypeClass : (0, typeUtils_1.convertToInstance)(noneTypeClass);
|
9432
|
+
if (isTypeFormSupported(node)) {
|
9433
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, (0, typeUtils_1.convertToInstance)(type));
|
9434
|
+
}
|
9211
9435
|
}
|
9212
9436
|
}
|
9213
9437
|
else if (node.d.constType === 33 /* KeywordType.True */ ||
|
@@ -9472,9 +9696,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9472
9696
|
if (keyValueResult.typeErrors) {
|
9473
9697
|
typeErrors = true;
|
9474
9698
|
}
|
9475
|
-
// Strip any literal values.
|
9476
|
-
const keyTypes = keyTypeResults.map((t) => stripLiteralValue(t.type));
|
9477
|
-
const valueTypes = valueTypeResults.map((t) => stripLiteralValue(t.type));
|
9699
|
+
// Strip any literal values and TypeForm types.
|
9700
|
+
const keyTypes = keyTypeResults.map((t) => (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(t.type), flags)));
|
9701
|
+
const valueTypes = valueTypeResults.map((t) => (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(t.type), flags)));
|
9478
9702
|
keyType = keyTypes.length > 0 ? (0, types_1.combineTypes)(keyTypes) : fallbackType;
|
9479
9703
|
// If the value type differs and we're not using "strict inference mode",
|
9480
9704
|
// we need to back off because we can't properly represent the mappings
|
@@ -9825,6 +10049,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
9825
10049
|
else {
|
9826
10050
|
entryTypeResult = getTypeOfExpression(entry, flags | 268435456 /* EvalFlags.StripTupleLiterals */);
|
9827
10051
|
}
|
10052
|
+
entryTypeResult.type = (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(entryTypeResult.type, flags));
|
9828
10053
|
if (entryTypeResult.isIncomplete) {
|
9829
10054
|
isIncomplete = true;
|
9830
10055
|
}
|
@@ -10359,8 +10584,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10359
10584
|
// a ParamSpec, a Concatenate, or a list of positional parameter types.
|
10360
10585
|
// The second argument, if present, should specify the return type.
|
10361
10586
|
function createCallableType(classType, typeArgs, errorNode) {
|
10362
|
-
|
10587
|
+
let functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
|
10363
10588
|
let paramSpec;
|
10589
|
+
let isValidTypeForm = true;
|
10364
10590
|
types_1.TypeBase.setSpecialForm(functionType, types_1.ClassType.cloneAsInstance(classType));
|
10365
10591
|
functionType.shared.declaredReturnType = types_1.UnknownType.create();
|
10366
10592
|
functionType.shared.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(errorNode);
|
@@ -10376,6 +10602,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10376
10602
|
if (!reportedUnpackedError) {
|
10377
10603
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), entry.node);
|
10378
10604
|
reportedUnpackedError = true;
|
10605
|
+
isValidTypeForm = false;
|
10379
10606
|
}
|
10380
10607
|
}
|
10381
10608
|
sawUnpacked = true;
|
@@ -10437,6 +10664,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10437
10664
|
}
|
10438
10665
|
else {
|
10439
10666
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableFirstArg(), typeArgs[0].node);
|
10667
|
+
isValidTypeForm = false;
|
10440
10668
|
}
|
10441
10669
|
}
|
10442
10670
|
if (typeArgs.length > 1) {
|
@@ -10449,22 +10677,31 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10449
10677
|
else {
|
10450
10678
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.callableSecondArg(), errorNode);
|
10451
10679
|
functionType.shared.declaredReturnType = types_1.UnknownType.create();
|
10680
|
+
isValidTypeForm = false;
|
10452
10681
|
}
|
10453
10682
|
if (typeArgs.length > 2) {
|
10454
10683
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableExtraArgs(), typeArgs[2].node);
|
10684
|
+
isValidTypeForm = false;
|
10455
10685
|
}
|
10456
10686
|
}
|
10457
10687
|
else {
|
10458
10688
|
types_1.FunctionType.addDefaultParams(functionType, /* useUnknown */ true);
|
10459
10689
|
functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
10690
|
+
if (typeArgs && typeArgs.length === 0) {
|
10691
|
+
isValidTypeForm = false;
|
10692
|
+
}
|
10460
10693
|
}
|
10461
10694
|
if (paramSpec) {
|
10462
10695
|
types_1.FunctionType.addParamSpecVariadics(functionType, (0, typeUtils_1.convertToInstance)(paramSpec));
|
10463
10696
|
}
|
10697
|
+
if (isTypeFormSupported(errorNode) && isValidTypeForm) {
|
10698
|
+
functionType = types_1.TypeBase.cloneWithTypeForm(functionType, (0, typeUtils_1.convertToInstance)(functionType));
|
10699
|
+
}
|
10464
10700
|
return functionType;
|
10465
10701
|
}
|
10466
10702
|
// Creates an Optional[X] type.
|
10467
10703
|
function createOptionalType(classType, errorNode, typeArgs, flags) {
|
10704
|
+
var _a;
|
10468
10705
|
if (!typeArgs) {
|
10469
10706
|
// If no type arguments are provided, the resulting type
|
10470
10707
|
// depends on whether we're evaluating a type annotation or
|
@@ -10487,6 +10724,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10487
10724
|
if (unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
|
10488
10725
|
optionalType = types_1.TypeBase.cloneAsSpecialForm(optionalType, types_1.ClassType.cloneAsInstance(unionTypeClass));
|
10489
10726
|
}
|
10727
|
+
if ((_a = typeArg0Type.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
|
10728
|
+
const typeFormType = (0, types_1.combineTypes)([
|
10729
|
+
typeArg0Type.props.typeForm,
|
10730
|
+
(0, typeUtils_1.convertToInstance)(noneTypeClass !== null && noneTypeClass !== void 0 ? noneTypeClass : types_1.UnknownType.create()),
|
10731
|
+
]);
|
10732
|
+
optionalType = types_1.TypeBase.cloneWithTypeForm(optionalType, typeFormType);
|
10733
|
+
}
|
10490
10734
|
return optionalType;
|
10491
10735
|
}
|
10492
10736
|
function cloneBuiltinObjectWithLiteral(node, builtInName, value) {
|
@@ -10605,6 +10849,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10605
10849
|
if ((0, types_1.isUnion)(result) && unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
|
10606
10850
|
result = types_1.TypeBase.cloneAsSpecialForm(result, types_1.ClassType.cloneAsInstance(unionTypeClass));
|
10607
10851
|
}
|
10852
|
+
if (isTypeFormSupported(node)) {
|
10853
|
+
result = types_1.TypeBase.cloneWithTypeForm(result, (0, typeUtils_1.convertToInstance)(result));
|
10854
|
+
}
|
10608
10855
|
return result;
|
10609
10856
|
}
|
10610
10857
|
// Creates a ClassVar type.
|
@@ -10634,11 +10881,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10634
10881
|
return type;
|
10635
10882
|
}
|
10636
10883
|
function createTypeFormType(classType, errorNode, typeArgs) {
|
10637
|
-
|
10638
|
-
|
10639
|
-
// we're in some other context.
|
10640
|
-
if (!typeArgs) {
|
10641
|
-
return classType;
|
10884
|
+
if (!typeArgs || typeArgs.length === 0) {
|
10885
|
+
return types_1.ClassType.specialize(classType, [types_1.UnknownType.create()]);
|
10642
10886
|
}
|
10643
10887
|
if (typeArgs.length > 1) {
|
10644
10888
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
|
@@ -10651,7 +10895,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10651
10895
|
const convertedTypeArgs = typeArgs.map((typeArg) => {
|
10652
10896
|
return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
|
10653
10897
|
});
|
10654
|
-
|
10898
|
+
let resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
|
10899
|
+
if (isTypeFormSupported(errorNode)) {
|
10900
|
+
resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
|
10901
|
+
}
|
10655
10902
|
return resultType;
|
10656
10903
|
}
|
10657
10904
|
// Creates a "TypeGuard" and "TypeIs" type.
|
@@ -10672,7 +10919,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10672
10919
|
const convertedTypeArgs = typeArgs.map((typeArg) => {
|
10673
10920
|
return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
|
10674
10921
|
});
|
10675
|
-
|
10922
|
+
let resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
|
10923
|
+
if (isTypeFormSupported(errorNode)) {
|
10924
|
+
resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
|
10925
|
+
}
|
10676
10926
|
return resultType;
|
10677
10927
|
}
|
10678
10928
|
function createSelfType(classType, errorNode, typeArgs, flags) {
|
@@ -10892,10 +11142,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
10892
11142
|
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
|
10893
11143
|
}
|
10894
11144
|
function createAnnotatedType(classType, errorNode, typeArgs, flags) {
|
11145
|
+
var _a;
|
10895
11146
|
let type;
|
10896
11147
|
const typeExprFlags = 256 /* EvalFlags.TypeExpression */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
|
10897
11148
|
if ((flags & typeExprFlags) === 0) {
|
10898
11149
|
type = types_1.ClassType.cloneAsInstance(classType);
|
11150
|
+
if (typeArgs && typeArgs.length >= 1 && ((_a = typeArgs[0].type.props) === null || _a === void 0 ? void 0 : _a.typeForm)) {
|
11151
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, typeArgs[0].type.props.typeForm);
|
11152
|
+
}
|
10899
11153
|
return { type };
|
10900
11154
|
}
|
10901
11155
|
if (typeArgs && typeArgs.length > 0) {
|
@@ -11085,9 +11339,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11085
11339
|
}
|
11086
11340
|
// Unpacks the index expression for a "Union[X, Y, Z]" type annotation.
|
11087
11341
|
function createUnionType(classType, errorNode, typeArgs, flags) {
|
11342
|
+
var _a;
|
11088
11343
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(errorNode);
|
11089
11344
|
const types = [];
|
11090
11345
|
let allowSingleTypeArg = false;
|
11346
|
+
let isValidTypeForm = true;
|
11091
11347
|
if (!typeArgs) {
|
11092
11348
|
// If no type arguments are provided, the resulting type
|
11093
11349
|
// depends on whether we're evaluating a type annotation or
|
@@ -11117,6 +11373,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11117
11373
|
else {
|
11118
11374
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unionUnpackedTypeVarTuple(), errorNode);
|
11119
11375
|
typeArgType = types_1.UnknownType.create();
|
11376
|
+
isValidTypeForm = false;
|
11120
11377
|
}
|
11121
11378
|
}
|
11122
11379
|
types.push(typeArgType);
|
@@ -11126,11 +11383,21 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11126
11383
|
// since it is used to define NoReturn in typeshed stubs).
|
11127
11384
|
if (types.length === 1 && !allowSingleTypeArg && !(0, typeUtils_1.isNoneInstance)(types[0])) {
|
11128
11385
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.unionTypeArgCount(), errorNode);
|
11386
|
+
isValidTypeForm = false;
|
11129
11387
|
}
|
11130
11388
|
let unionType = (0, types_1.combineTypes)(types, { skipElideRedundantLiterals: true });
|
11131
11389
|
if (unionTypeClass && (0, types_1.isInstantiableClass)(unionTypeClass)) {
|
11132
11390
|
unionType = types_1.TypeBase.cloneAsSpecialForm(unionType, types_1.ClassType.cloneAsInstance(unionTypeClass));
|
11133
11391
|
}
|
11392
|
+
if (!isValidTypeForm || types.some((t) => { var _a; return !((_a = t.props) === null || _a === void 0 ? void 0 : _a.typeForm); })) {
|
11393
|
+
if ((_a = unionType.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
|
11394
|
+
unionType = types_1.TypeBase.cloneWithTypeForm(unionType, undefined);
|
11395
|
+
}
|
11396
|
+
}
|
11397
|
+
else if (isTypeFormSupported(errorNode)) {
|
11398
|
+
const typeFormType = (0, types_1.combineTypes)(types.map((t) => t.props.typeForm));
|
11399
|
+
unionType = types_1.TypeBase.cloneWithTypeForm(unionType, typeFormType);
|
11400
|
+
}
|
11134
11401
|
return unionType;
|
11135
11402
|
}
|
11136
11403
|
// Creates a type that represents "Generic[T1, T2, ...]", used in the
|
@@ -11167,6 +11434,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11167
11434
|
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
|
11168
11435
|
}
|
11169
11436
|
function transformTypeForTypeAlias(type, name, errorNode, isPep695Syntax, isPep695TypeVarType, typeParams, typeParamNodes) {
|
11437
|
+
var _a;
|
11170
11438
|
if (!types_1.TypeBase.isInstantiable(type)) {
|
11171
11439
|
return type;
|
11172
11440
|
}
|
@@ -11248,6 +11516,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11248
11516
|
typeAlias = types_1.TypeBase.cloneAsSpecialForm(typeAlias, types_1.ClassType.cloneAsInstance(typeAliasTypeClass));
|
11249
11517
|
}
|
11250
11518
|
}
|
11519
|
+
// Delete the TypeForm info. The type alias serves as its own TypeForm info.
|
11520
|
+
if ((_a = typeAlias.props) === null || _a === void 0 ? void 0 : _a.typeForm) {
|
11521
|
+
typeAlias = types_1.TypeBase.cloneWithTypeForm(typeAlias, undefined);
|
11522
|
+
}
|
11251
11523
|
return typeAlias;
|
11252
11524
|
}
|
11253
11525
|
function createSpecialBuiltInClass(node, assignedName, aliasMapEntry) {
|
@@ -11375,15 +11647,24 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11375
11647
|
if (aliasMapEntry) {
|
11376
11648
|
const cachedType = readTypeCache(node, 0 /* EvalFlags.None */);
|
11377
11649
|
if (cachedType) {
|
11378
|
-
(0, debug_1.assert)((0, types_1.isInstantiableClass)(cachedType));
|
11379
11650
|
return cachedType;
|
11380
11651
|
}
|
11381
|
-
|
11652
|
+
let specialType = createSpecialBuiltInClass(node, assignedName, aliasMapEntry);
|
11382
11653
|
// Handle 'LiteralString' specially because we want it to act as
|
11383
11654
|
// though it derives from 'str'.
|
11384
11655
|
if (assignedName === 'LiteralString') {
|
11385
11656
|
specialType.shared.baseClasses.push(strClass !== null && strClass !== void 0 ? strClass : types_1.AnyType.create());
|
11386
11657
|
(0, typeUtils_1.computeMroLinearization)(specialType);
|
11658
|
+
if (isTypeFormSupported(node)) {
|
11659
|
+
specialType = types_1.TypeBase.cloneWithTypeForm(specialType, (0, typeUtils_1.convertToInstance)(specialType));
|
11660
|
+
}
|
11661
|
+
}
|
11662
|
+
// Handle 'Never' and 'NoReturn' specially.
|
11663
|
+
if (assignedName === 'Never' || assignedName === 'NoReturn') {
|
11664
|
+
specialType = types_1.TypeBase.cloneAsSpecialForm(assignedName === 'Never' ? types_1.NeverType.createNever() : types_1.NeverType.createNoReturn(), specialType);
|
11665
|
+
if (isTypeFormSupported(node)) {
|
11666
|
+
specialType = types_1.TypeBase.cloneWithTypeForm(specialType, (0, typeUtils_1.convertToInstance)(specialType));
|
11667
|
+
}
|
11387
11668
|
}
|
11388
11669
|
writeTypeCache(node, { type: specialType }, 0 /* EvalFlags.None */);
|
11389
11670
|
return specialType;
|
@@ -11475,7 +11756,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11475
11756
|
64 /* EvalFlags.NoTypeVarTuple */ |
|
11476
11757
|
131072 /* EvalFlags.NoClassVar */;
|
11477
11758
|
typeAliasNameNode = node.d.leftExpr.d.valueExpr;
|
11478
|
-
if (!isLegalTypeAliasExpressionForm(node.d.rightExpr)) {
|
11759
|
+
if (!isLegalTypeAliasExpressionForm(node.d.rightExpr, /* allowStrLiteral */ true)) {
|
11479
11760
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeAliasIllegalExpressionForm(), node.d.rightExpr);
|
11480
11761
|
}
|
11481
11762
|
}
|
@@ -11783,6 +12064,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11783
12064
|
if ((0, types_1.isAny)(argType) && ((_b = argType.props) === null || _b === void 0 ? void 0 : _b.specialForm)) {
|
11784
12065
|
argType = types_1.AnyType.create();
|
11785
12066
|
}
|
12067
|
+
argType = (0, typeUtils_1.stripTypeFormRecursive)(argType);
|
11786
12068
|
if (!(0, types_1.isAnyOrUnknown)(argType) && !(0, types_1.isUnbound)(argType)) {
|
11787
12069
|
if ((0, typeUtils_1.isMetaclassInstance)(argType)) {
|
11788
12070
|
(0, debug_1.assert)((0, types_1.isClassInstance)(argType));
|
@@ -11819,7 +12101,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
11819
12101
|
if (pythonVersion_1.PythonVersion.isGreaterOrEqualTo(fileInfo.executionEnvironment.pythonVersion, pythonVersion_1.pythonVersion3_6)) {
|
11820
12102
|
if (types_1.ClassType.isBuiltIn(argType, 'NamedTuple')) {
|
11821
12103
|
isNamedTupleSubclass = true;
|
11822
|
-
classType.shared.flags |= 524288 /* ClassTypeFlags.ReadOnlyInstanceVariables */;
|
11823
12104
|
}
|
11824
12105
|
}
|
11825
12106
|
// If the class directly derives from TypedDict or from a class that is
|
@@ -12097,7 +12378,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
12097
12378
|
if ((0, enums_1.isEnumMetaclass)(metaclassType)) {
|
12098
12379
|
classType.shared.flags |= 65536 /* ClassTypeFlags.EnumClass */;
|
12099
12380
|
}
|
12100
|
-
if (
|
12381
|
+
if ((0, typeUtils_1.derivesFromStdlibClass)(metaclassType, 'ABCMeta')) {
|
12101
12382
|
classType.shared.flags |= 64 /* ClassTypeFlags.SupportsAbstractMethods */;
|
12102
12383
|
}
|
12103
12384
|
}
|
@@ -13103,7 +13384,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13103
13384
|
skipInference = true;
|
13104
13385
|
}
|
13105
13386
|
if (!skipInference) {
|
13106
|
-
inferredParamType =
|
13387
|
+
inferredParamType = convertSpecialFormToRuntimeValue(defaultValueType, 0 /* EvalFlags.None */);
|
13388
|
+
inferredParamType = (0, typeUtils_1.stripTypeForm)(inferredParamType);
|
13389
|
+
inferredParamType = stripLiteralValue(inferredParamType);
|
13107
13390
|
}
|
13108
13391
|
}
|
13109
13392
|
if (inferredParamType) {
|
@@ -13276,6 +13559,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13276
13559
|
}
|
13277
13560
|
return subtype;
|
13278
13561
|
});
|
13562
|
+
// Do not retain TypeForm types in inferred return types.
|
13563
|
+
returnType = (0, typeUtils_1.stripTypeForm)(returnType);
|
13279
13564
|
inferredReturnTypes.push(returnType);
|
13280
13565
|
}
|
13281
13566
|
else {
|
@@ -13384,10 +13669,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
13384
13669
|
return false;
|
13385
13670
|
}
|
13386
13671
|
for (const raiseStatement of functionDecl.raiseStatements) {
|
13387
|
-
if (!raiseStatement.d.
|
13672
|
+
if (!raiseStatement.d.expr || raiseStatement.d.fromExpr) {
|
13388
13673
|
return false;
|
13389
13674
|
}
|
13390
|
-
const raiseType = getTypeOfExpression(raiseStatement.d.
|
13675
|
+
const raiseType = getTypeOfExpression(raiseStatement.d.expr).type;
|
13391
13676
|
const classType = (0, types_1.isInstantiableClass)(raiseType)
|
13392
13677
|
? raiseType
|
13393
13678
|
: (0, types_1.isClassInstance)(raiseType)
|
@@ -14314,6 +14599,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14314
14599
|
// Returns the specialized type and a boolean indicating whether
|
14315
14600
|
// the type indicates a class type (true) or an object type (false).
|
14316
14601
|
function createSpecializedClassType(classType, typeArgs, flags, errorNode) {
|
14602
|
+
let isValidTypeForm = true;
|
14317
14603
|
// Handle the special-case classes that are not defined
|
14318
14604
|
// in the type stubs.
|
14319
14605
|
if (types_1.ClassType.isSpecialBuiltIn(classType)) {
|
@@ -14327,7 +14613,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14327
14613
|
if (typeArgs && typeArgs.length > 0) {
|
14328
14614
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: aliasedName }), typeArgs[0].node);
|
14329
14615
|
}
|
14330
|
-
|
14616
|
+
let resultType = aliasedName === 'Never' ? types_1.NeverType.createNever() : types_1.NeverType.createNoReturn();
|
14617
|
+
resultType = types_1.TypeBase.cloneAsSpecialForm(resultType, classType);
|
14618
|
+
if (isTypeFormSupported(errorNode)) {
|
14619
|
+
resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
|
14620
|
+
}
|
14331
14621
|
return { type: resultType };
|
14332
14622
|
}
|
14333
14623
|
case 'Optional': {
|
@@ -14340,6 +14630,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14340
14630
|
if ((0, types_1.isInstantiableClass)(typeType)) {
|
14341
14631
|
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
14342
14632
|
}
|
14633
|
+
if (isTypeFormSupported(errorNode)) {
|
14634
|
+
typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
|
14635
|
+
}
|
14343
14636
|
return { type: typeType };
|
14344
14637
|
}
|
14345
14638
|
case 'ClassVar': {
|
@@ -14364,12 +14657,14 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14364
14657
|
if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
|
14365
14658
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typedDictNotAllowed(), errorNode);
|
14366
14659
|
}
|
14660
|
+
isValidTypeForm = false;
|
14367
14661
|
break;
|
14368
14662
|
}
|
14369
14663
|
case 'Literal': {
|
14370
14664
|
if ((flags & (67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */)) !== 0) {
|
14371
14665
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalNotAllowed(), errorNode);
|
14372
14666
|
}
|
14667
|
+
isValidTypeForm = false;
|
14373
14668
|
break;
|
14374
14669
|
}
|
14375
14670
|
case 'Tuple': {
|
@@ -14442,16 +14737,22 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14442
14737
|
if ((0, types_1.isInstantiableClass)(typeType)) {
|
14443
14738
|
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
14444
14739
|
}
|
14740
|
+
if (isTypeFormSupported(errorNode)) {
|
14741
|
+
typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
|
14742
|
+
}
|
14445
14743
|
return { type: typeType };
|
14446
14744
|
}
|
14447
14745
|
}
|
14448
14746
|
// Handle "tuple" specially, since it needs to act like "Tuple"
|
14449
14747
|
// in Python 3.9 and newer.
|
14450
14748
|
if ((0, typeUtils_1.isTupleClass)(classType)) {
|
14451
|
-
|
14749
|
+
let specializedClass = createSpecialType(classType, typeArgs,
|
14452
14750
|
/* paramLimit */ undefined,
|
14453
14751
|
/* allowParamSpec */ undefined,
|
14454
14752
|
/* isSpecialForm */ false);
|
14753
|
+
if (isTypeFormSupported(errorNode)) {
|
14754
|
+
specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, (0, typeUtils_1.convertToInstance)(specializedClass));
|
14755
|
+
}
|
14455
14756
|
return { type: specializedClass };
|
14456
14757
|
}
|
14457
14758
|
}
|
@@ -14473,11 +14774,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14473
14774
|
if (typeArgCount > typeParams.length) {
|
14474
14775
|
if (!types_1.ClassType.isPartiallyEvaluated(classType) && !types_1.ClassType.isTupleClass(classType)) {
|
14475
14776
|
if (typeParams.length === 0) {
|
14777
|
+
isValidTypeForm = false;
|
14476
14778
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsExpectingNone().format({
|
14477
14779
|
name: classType.priv.aliasName || classType.shared.name,
|
14478
14780
|
}), typeArgs[typeParams.length].node);
|
14479
14781
|
}
|
14480
14782
|
else if (typeParams.length !== 1 || !(0, types_1.isParamSpec)(typeParams[0])) {
|
14783
|
+
isValidTypeForm = false;
|
14481
14784
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooMany().format({
|
14482
14785
|
name: classType.priv.aliasName || classType.shared.name,
|
14483
14786
|
expected: typeParams.length,
|
@@ -14488,6 +14791,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14488
14791
|
}
|
14489
14792
|
}
|
14490
14793
|
else if (typeArgCount < minTypeArgCount) {
|
14794
|
+
isValidTypeForm = false;
|
14491
14795
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooFew().format({
|
14492
14796
|
name: classType.priv.aliasName || classType.shared.name,
|
14493
14797
|
expected: minTypeArgCount,
|
@@ -14495,6 +14799,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14495
14799
|
}), typeArgs.length > 0 ? typeArgs[0].node.parent : errorNode);
|
14496
14800
|
}
|
14497
14801
|
typeArgs.forEach((typeArg, index) => {
|
14802
|
+
var _a;
|
14803
|
+
if (!((_a = typeArg.type.props) === null || _a === void 0 ? void 0 : _a.typeForm)) {
|
14804
|
+
isValidTypeForm = false;
|
14805
|
+
}
|
14498
14806
|
if (index === variadicTypeParamIndex) {
|
14499
14807
|
// The types that make up the tuple that maps to the
|
14500
14808
|
// TypeVarTuple have already been validated when the tuple
|
@@ -14503,16 +14811,20 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14503
14811
|
return;
|
14504
14812
|
}
|
14505
14813
|
if ((0, types_1.isTypeVarTuple)(typeArg.type)) {
|
14506
|
-
validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node)
|
14814
|
+
if (!validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node)) {
|
14815
|
+
isValidTypeForm = false;
|
14816
|
+
}
|
14507
14817
|
return;
|
14508
14818
|
}
|
14509
14819
|
}
|
14510
14820
|
const typeParam = index < typeParams.length ? typeParams[index] : undefined;
|
14511
14821
|
const isParamSpecTarget = typeParam && (0, types_1.isParamSpec)(typeParam);
|
14512
|
-
validateTypeArg(typeArg, {
|
14822
|
+
if (!validateTypeArg(typeArg, {
|
14513
14823
|
allowParamSpec: true,
|
14514
14824
|
allowTypeArgList: isParamSpecTarget,
|
14515
|
-
})
|
14825
|
+
})) {
|
14826
|
+
isValidTypeForm = false;
|
14827
|
+
}
|
14516
14828
|
});
|
14517
14829
|
}
|
14518
14830
|
// Handle ParamSpec arguments and fill in any missing type arguments with Unknown.
|
@@ -14542,10 +14854,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14542
14854
|
else if (typeArgs.length > 1) {
|
14543
14855
|
const paramSpecTypeArg = typeArgs.find((typeArg) => (0, types_1.isParamSpec)(typeArg.type));
|
14544
14856
|
if (paramSpecTypeArg) {
|
14857
|
+
isValidTypeForm = false;
|
14545
14858
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), paramSpecTypeArg.node);
|
14546
14859
|
}
|
14547
14860
|
const listTypeArg = typeArgs.find((typeArg) => !!typeArg.typeList);
|
14548
14861
|
if (listTypeArg) {
|
14862
|
+
isValidTypeForm = false;
|
14549
14863
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), listTypeArg.node);
|
14550
14864
|
}
|
14551
14865
|
}
|
@@ -14633,6 +14947,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14633
14947
|
// Avoid emitting this error for a partially-constructed class.
|
14634
14948
|
if (!(0, types_1.isClassInstance)(typeArgType) || !types_1.ClassType.isPartiallyEvaluated(typeArgType)) {
|
14635
14949
|
(0, debug_1.assert)(typeArgs !== undefined);
|
14950
|
+
isValidTypeForm = false;
|
14636
14951
|
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeVarAssignmentMismatch().format({
|
14637
14952
|
type: printType(typeArgType),
|
14638
14953
|
name: types_1.TypeVarType.getReadableName(typeParams[index]),
|
@@ -14648,7 +14963,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14648
14963
|
if (typeArgs && classType.shared.typeParams.length === 0 && types_1.ClassType.isPartiallyEvaluated(classType)) {
|
14649
14964
|
typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
|
14650
14965
|
}
|
14651
|
-
|
14966
|
+
let specializedClass = types_1.ClassType.specialize(classType, typeArgTypes, typeArgs !== undefined);
|
14967
|
+
if (isTypeFormSupported(errorNode)) {
|
14968
|
+
specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, isValidTypeForm ? (0, typeUtils_1.convertToInstance)(specializedClass) : undefined);
|
14969
|
+
}
|
14652
14970
|
return { type: specializedClass };
|
14653
14971
|
}
|
14654
14972
|
function getTypeOfArg(arg, inferenceContext) {
|
@@ -14687,7 +15005,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14687
15005
|
flags |= 8192 /* EvalFlags.TypeVarGetsCurScope */;
|
14688
15006
|
}
|
14689
15007
|
if (options === null || options === void 0 ? void 0 : options.enforceClassTypeVarScope) {
|
14690
|
-
flags |=
|
15008
|
+
flags |= -2147483648 /* EvalFlags.EnforceClassTypeVarScope */;
|
14691
15009
|
}
|
14692
15010
|
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
14693
15011
|
if ((0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(fileInfo) || (options === null || options === void 0 ? void 0 : options.forwardRefs)) {
|
@@ -14732,6 +15050,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14732
15050
|
if (options === null || options === void 0 ? void 0 : options.notParsed) {
|
14733
15051
|
flags |= 524288 /* EvalFlags.NotParsed */;
|
14734
15052
|
}
|
15053
|
+
if (options === null || options === void 0 ? void 0 : options.typeFormArg) {
|
15054
|
+
flags |= 1073741824 /* EvalFlags.TypeFormArg */;
|
15055
|
+
}
|
14735
15056
|
return getTypeOfExpression(node, flags);
|
14736
15057
|
}
|
14737
15058
|
function getBuiltInType(node, name) {
|
@@ -14988,7 +15309,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
14988
15309
|
// In general, string nodes don't have any declarations associated with them, but
|
14989
15310
|
// we need to handle the special case of string literals used as keys within a
|
14990
15311
|
// dictionary expression where those keys are associated with a known TypedDict.
|
14991
|
-
function
|
15312
|
+
function getDeclInfoForStringNode(node) {
|
14992
15313
|
var _a;
|
14993
15314
|
const declarations = [];
|
14994
15315
|
const expectedType = (_a = getExpectedType(node)) === null || _a === void 0 ? void 0 : _a.type;
|
@@ -15010,7 +15331,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15010
15331
|
}
|
15011
15332
|
});
|
15012
15333
|
}
|
15013
|
-
return declarations.length === 0 ? undefined : declarations;
|
15334
|
+
return declarations.length === 0 ? undefined : { decls: declarations, synthesizedTypes: [] };
|
15014
15335
|
}
|
15015
15336
|
function getAliasFromImport(node) {
|
15016
15337
|
if (node.parent &&
|
@@ -15021,12 +15342,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15021
15342
|
}
|
15022
15343
|
return undefined;
|
15023
15344
|
}
|
15024
|
-
function
|
15345
|
+
function getDeclInfoForNameNode(node, skipUnreachableCode = true) {
|
15025
15346
|
var _a, _b, _c;
|
15026
15347
|
if (skipUnreachableCode && AnalyzerNodeInfo.isCodeUnreachable(node)) {
|
15027
15348
|
return undefined;
|
15028
15349
|
}
|
15029
15350
|
const declarations = [];
|
15351
|
+
const synthesizedTypes = [];
|
15030
15352
|
// If the node is part of a "from X import Y as Z" statement and the node
|
15031
15353
|
// is the "Y" (non-aliased) name, we need to look up the alias symbol
|
15032
15354
|
// since the non-aliased name is not in the symbol table.
|
@@ -15096,7 +15418,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15096
15418
|
(0, collectionUtils_1.appendArray)(declarations, typedDecls);
|
15097
15419
|
}
|
15098
15420
|
else {
|
15099
|
-
|
15421
|
+
const synthesizedType = symbol.getSynthesizedType();
|
15422
|
+
if (synthesizedType) {
|
15423
|
+
synthesizedTypes.push(synthesizedType);
|
15424
|
+
}
|
15425
|
+
else {
|
15426
|
+
(0, collectionUtils_1.appendArray)(declarations, symbol.getDeclarations());
|
15427
|
+
}
|
15100
15428
|
}
|
15101
15429
|
}
|
15102
15430
|
});
|
@@ -15178,7 +15506,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15178
15506
|
(0, collectionUtils_1.appendArray)(declarations, symbolWithScope.symbol.getDeclarations());
|
15179
15507
|
}
|
15180
15508
|
}
|
15181
|
-
return declarations;
|
15509
|
+
return { decls: declarations, synthesizedTypes };
|
15182
15510
|
}
|
15183
15511
|
function getTypeForDeclaration(declaration) {
|
15184
15512
|
var _a, _b;
|
@@ -15724,6 +16052,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15724
16052
|
return true;
|
15725
16053
|
}
|
15726
16054
|
}
|
16055
|
+
if (decl.type === 2 /* DeclarationType.Param */) {
|
16056
|
+
return true;
|
16057
|
+
}
|
15727
16058
|
return false;
|
15728
16059
|
});
|
15729
16060
|
}
|
@@ -15880,9 +16211,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
15880
16211
|
isConstant = true;
|
15881
16212
|
}
|
15882
16213
|
// If the symbol is constant, we can retain the literal
|
15883
|
-
// value. Otherwise, strip literal values
|
16214
|
+
// value and TypeForm types. Otherwise, strip literal values
|
16215
|
+
// and TypeForm types to widen.
|
15884
16216
|
if (types_1.TypeBase.isInstance(type) && !isConstant && !isExplicitTypeAliasDeclaration(decl)) {
|
15885
|
-
type = stripLiteralValue(type);
|
16217
|
+
type = (0, typeUtils_1.stripTypeForm)(stripLiteralValue(type));
|
15886
16218
|
}
|
15887
16219
|
}
|
15888
16220
|
typesToCombine.push(type);
|
@@ -16240,7 +16572,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16240
16572
|
paramType = types_1.UnknownType.create();
|
16241
16573
|
}
|
16242
16574
|
if (stripLiteralArgTypes) {
|
16243
|
-
paramType = stripLiteralValue(paramType);
|
16575
|
+
paramType = (0, typeUtils_1.stripTypeForm)(convertSpecialFormToRuntimeValue(stripLiteralValue(paramType), 0 /* EvalFlags.None */));
|
16244
16576
|
}
|
16245
16577
|
paramTypes.push(paramType);
|
16246
16578
|
writeTypeCache(param.d.name, { type: paramType }, 0 /* EvalFlags.None */);
|
@@ -16520,8 +16852,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16520
16852
|
let flags = 0 /* AssignTypeFlags.Default */;
|
16521
16853
|
if ((primaryDecl === null || primaryDecl === void 0 ? void 0 : primaryDecl.type) === 1 /* DeclarationType.Variable */ &&
|
16522
16854
|
!isFinalVariableDeclaration(primaryDecl) &&
|
16523
|
-
!
|
16524
|
-
!types_1.ClassType.isDataClassFrozen(destType)) {
|
16855
|
+
!(0, typeUtils_1.isMemberReadOnly)(destType, name)) {
|
16525
16856
|
// Class and instance variables that are mutable need to
|
16526
16857
|
// enforce invariance. We will exempt variables that are
|
16527
16858
|
// private or protected, since these are presumably
|
@@ -16729,6 +17060,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16729
17060
|
// why two types are not the same.
|
16730
17061
|
includeDiagAddendum = false;
|
16731
17062
|
}
|
17063
|
+
// Special-case TypeForm to retain literals when solving TypeVars.
|
17064
|
+
if (types_1.ClassType.isBuiltIn(destType, 'TypeForm')) {
|
17065
|
+
effectiveFlags |= 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */;
|
17066
|
+
}
|
16732
17067
|
if (!assignType(variance === 4 /* Variance.Contravariant */ ? srcTypeArg : destTypeArg, variance === 4 /* Variance.Contravariant */ ? destTypeArg : srcTypeArg, assignmentDiag, constraints, effectiveFlags, recursionCount)) {
|
16733
17068
|
// Don't report errors with type variables in "pseudo-random"
|
16734
17069
|
// classes since these type variables are not real.
|
@@ -16772,7 +17107,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16772
17107
|
// in the dest type is not in the type map already, it is assigned a type
|
16773
17108
|
// and added to the map.
|
16774
17109
|
function assignType(destType, srcType, diag, constraints, flags = 0 /* AssignTypeFlags.Default */, recursionCount = 0) {
|
16775
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
17110
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
16776
17111
|
// Handle the case where the dest and src types are the same object.
|
16777
17112
|
// We can normally shortcut this and say that they are compatible,
|
16778
17113
|
// but if the type includes TypeVars, we need to go through
|
@@ -16793,7 +17128,12 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16793
17128
|
}
|
16794
17129
|
}
|
16795
17130
|
if (!isSpecialFormExempt) {
|
16796
|
-
srcType = specialForm
|
17131
|
+
if (((_b = srcType.props) === null || _b === void 0 ? void 0 : _b.typeForm) && !((_c = specialForm.props) === null || _c === void 0 ? void 0 : _c.typeForm)) {
|
17132
|
+
srcType = types_1.TypeBase.cloneWithTypeForm(specialForm, srcType.props.typeForm);
|
17133
|
+
}
|
17134
|
+
else {
|
17135
|
+
srcType = specialForm;
|
17136
|
+
}
|
16797
17137
|
}
|
16798
17138
|
}
|
16799
17139
|
if (recursionCount > types_1.maxTypeRecursionCount) {
|
@@ -16806,8 +17146,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16806
17146
|
destType.shared.recursiveAlias &&
|
16807
17147
|
(0, types_1.isTypeVar)(srcType) &&
|
16808
17148
|
srcType.shared.recursiveAlias) {
|
16809
|
-
const destAliasInfo = (
|
16810
|
-
const srcAliasInfo = (
|
17149
|
+
const destAliasInfo = (_d = destType.props) === null || _d === void 0 ? void 0 : _d.typeAliasInfo;
|
17150
|
+
const srcAliasInfo = (_e = srcType.props) === null || _e === void 0 ? void 0 : _e.typeAliasInfo;
|
16811
17151
|
// Do the source and dest refer to the same recursive type alias?
|
16812
17152
|
if ((destAliasInfo === null || destAliasInfo === void 0 ? void 0 : destAliasInfo.typeArgs) &&
|
16813
17153
|
(srcAliasInfo === null || srcAliasInfo === void 0 ? void 0 : srcAliasInfo.typeArgs) &&
|
@@ -16832,6 +17172,13 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16832
17172
|
flags |= 4 /* AssignTypeFlags.SkipRecursiveTypeCheck */;
|
16833
17173
|
}
|
16834
17174
|
}
|
17175
|
+
// If one or both of the types has an instantiable depth greater than
|
17176
|
+
// zero, convert both to instances first.
|
17177
|
+
if (types_1.TypeBase.isInstantiable(destType) && types_1.TypeBase.isInstantiable(srcType)) {
|
17178
|
+
if (types_1.TypeBase.getInstantiableDepth(destType) > 0 || types_1.TypeBase.getInstantiableDepth(srcType) > 0) {
|
17179
|
+
return assignType((0, typeUtils_1.convertToInstance)(destType), (0, typeUtils_1.convertToInstance)(srcType), diag, constraints, flags, recursionCount);
|
17180
|
+
}
|
17181
|
+
}
|
16835
17182
|
// Transform recursive type aliases if necessary.
|
16836
17183
|
const transformedDestType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(destType);
|
16837
17184
|
const transformedSrcType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(srcType);
|
@@ -16857,8 +17204,6 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16857
17204
|
if ((0, types_1.isUnbound)(destType) || (0, types_1.isUnbound)(srcType)) {
|
16858
17205
|
return true;
|
16859
17206
|
}
|
16860
|
-
// Before performing any other checks, see if the dest type is a
|
16861
|
-
// TypeVar that we are attempting to match.
|
16862
17207
|
if ((0, types_1.isTypeVar)(destType)) {
|
16863
17208
|
if ((0, typeUtils_1.isTypeVarSame)(destType, srcType)) {
|
16864
17209
|
return true;
|
@@ -16873,7 +17218,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16873
17218
|
// and the bound TypeVar matches the condition, the types are compatible.
|
16874
17219
|
const destTypeVar = destType;
|
16875
17220
|
if (types_1.TypeBase.isInstantiable(destType) === types_1.TypeBase.isInstantiable(srcType) &&
|
16876
|
-
((
|
17221
|
+
((_f = srcType.props) === null || _f === void 0 ? void 0 : _f.condition) &&
|
16877
17222
|
srcType.props.condition.some((cond) => {
|
16878
17223
|
return (!types_1.TypeVarType.hasConstraints(cond.typeVar) &&
|
16879
17224
|
cond.typeVar.priv.nameWithScope === destTypeVar.priv.nameWithScope);
|
@@ -16974,7 +17319,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
16974
17319
|
if ((0, types_1.isAnyOrUnknown)(destType)) {
|
16975
17320
|
return true;
|
16976
17321
|
}
|
16977
|
-
if ((0, types_1.isAnyOrUnknown)(srcType) && !((
|
17322
|
+
if ((0, types_1.isAnyOrUnknown)(srcType) && !((_g = srcType.props) === null || _g === void 0 ? void 0 : _g.specialForm)) {
|
16978
17323
|
if (constraints) {
|
16979
17324
|
// If it's an ellipsis type, convert it to a regular "Any"
|
16980
17325
|
// type. These are functionally equivalent, but "Any" looks
|
@@ -17003,7 +17348,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17003
17348
|
// If both the source and dest are unions, use assignFromUnionType which has
|
17004
17349
|
// special-case logic to handle this case.
|
17005
17350
|
if ((0, types_1.isUnion)(srcType)) {
|
17006
|
-
return assignFromUnionType(destType, srcType,
|
17351
|
+
return assignFromUnionType(destType, srcType, diag, constraints, flags, recursionCount);
|
17007
17352
|
}
|
17008
17353
|
const clonedConstraints = constraints === null || constraints === void 0 ? void 0 : constraints.clone();
|
17009
17354
|
if (assignToUnionType(destType, srcType, /* diag */ undefined, clonedConstraints, flags, recursionCount)) {
|
@@ -17048,7 +17393,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17048
17393
|
// PEP 544 says that if the dest type is a type[Proto] class,
|
17049
17394
|
// the source must be a "concrete" (non-protocol) class.
|
17050
17395
|
if (types_1.ClassType.isProtocolClass(destType)) {
|
17051
|
-
if (
|
17396
|
+
if ((flags & 65536 /* AssignTypeFlags.AllowProtocolClassSource */) === 0 &&
|
17397
|
+
types_1.ClassType.isProtocolClass(expandedSrcType) &&
|
17052
17398
|
(0, types_1.isInstantiableClass)(srcType) &&
|
17053
17399
|
!srcType.priv.includeSubclasses) {
|
17054
17400
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.protocolSourceIsNotConcrete().format({
|
@@ -17058,12 +17404,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17058
17404
|
return false;
|
17059
17405
|
}
|
17060
17406
|
}
|
17061
|
-
if (types_1.ClassType.isBuiltIn(destType, 'type') && ((
|
17407
|
+
if (types_1.ClassType.isBuiltIn(destType, 'type') && ((_j = (_h = srcType.props) === null || _h === void 0 ? void 0 : _h.instantiableDepth) !== null && _j !== void 0 ? _j : 0) > 0) {
|
17062
17408
|
return true;
|
17063
17409
|
}
|
17064
17410
|
if (isSpecialFormClass(expandedSrcType, flags)) {
|
17065
|
-
|
17066
|
-
|
17411
|
+
// Special form classes are compatible only with other special form
|
17412
|
+
// classes, not with 'object' or 'type'.
|
17413
|
+
const destSpecialForm = (_l = (_k = destType.props) === null || _k === void 0 ? void 0 : _k.specialForm) !== null && _l !== void 0 ? _l : destType;
|
17414
|
+
if (isSpecialFormClass(destSpecialForm, flags)) {
|
17415
|
+
return assignType(destSpecialForm, expandedSrcType, diag, constraints, flags, recursionCount);
|
17067
17416
|
}
|
17068
17417
|
}
|
17069
17418
|
else if (assignClass(destType, expandedSrcType, diag, constraints, flags, recursionCount,
|
@@ -17089,7 +17438,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17089
17438
|
}
|
17090
17439
|
if ((0, types_1.isClassInstance)(destType)) {
|
17091
17440
|
if (types_1.ClassType.isBuiltIn(destType, 'type')) {
|
17092
|
-
if ((0, types_1.isInstantiableClass)(srcType) &&
|
17441
|
+
if ((0, types_1.isInstantiableClass)(srcType) &&
|
17442
|
+
isSpecialFormClass(srcType, flags) &&
|
17443
|
+
types_1.TypeBase.getInstantiableDepth(srcType) === 0) {
|
17093
17444
|
return false;
|
17094
17445
|
}
|
17095
17446
|
if ((0, types_1.isAnyOrUnknown)(srcType) && (flags & 16 /* AssignTypeFlags.OverloadOverlap */) !== 0) {
|
@@ -17197,7 +17548,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17197
17548
|
}
|
17198
17549
|
}
|
17199
17550
|
}
|
17200
|
-
else if ((0, types_1.isAnyOrUnknown)(concreteSrcType) && !((
|
17551
|
+
else if ((0, types_1.isAnyOrUnknown)(concreteSrcType) && !((_m = concreteSrcType.props) === null || _m === void 0 ? void 0 : _m.specialForm)) {
|
17201
17552
|
return (flags & 16 /* AssignTypeFlags.OverloadOverlap */) === 0;
|
17202
17553
|
}
|
17203
17554
|
else if ((0, types_1.isUnion)(concreteSrcType)) {
|
@@ -17325,6 +17676,55 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17325
17676
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(printSrcDestTypes(srcType, destType)));
|
17326
17677
|
return false;
|
17327
17678
|
}
|
17679
|
+
// If the expected type is an explicit TypeForm type, see if the source
|
17680
|
+
// type has an implicit TypeForm type that can be assigned to it. If so,
|
17681
|
+
// convert to an explicit TypeForm type.
|
17682
|
+
function convertToTypeFormType(expectedType, srcType) {
|
17683
|
+
var _a, _b, _c;
|
17684
|
+
// Is the source is a TypeForm type?
|
17685
|
+
if (!((_a = srcType.props) === null || _a === void 0 ? void 0 : _a.typeForm)) {
|
17686
|
+
return srcType;
|
17687
|
+
}
|
17688
|
+
let srcTypeFormType;
|
17689
|
+
// Is the source is a TypeForm type?
|
17690
|
+
if ((_b = srcType.props) === null || _b === void 0 ? void 0 : _b.typeForm) {
|
17691
|
+
srcTypeFormType = srcType.props.typeForm;
|
17692
|
+
}
|
17693
|
+
else if ((0, types_1.isClass)(srcType)) {
|
17694
|
+
if (types_1.TypeBase.isInstantiable(srcType)) {
|
17695
|
+
if (!types_1.ClassType.isSpecialBuiltIn(srcType)) {
|
17696
|
+
srcTypeFormType = types_1.ClassType.cloneAsInstance(srcType);
|
17697
|
+
}
|
17698
|
+
}
|
17699
|
+
else if (types_1.ClassType.isBuiltIn(srcType, 'type')) {
|
17700
|
+
srcTypeFormType =
|
17701
|
+
((_c = srcType.priv.typeArgs) === null || _c === void 0 ? void 0 : _c.length) && srcType.priv.typeArgs.length > 0
|
17702
|
+
? srcType.priv.typeArgs[0]
|
17703
|
+
: types_1.UnknownType.create();
|
17704
|
+
}
|
17705
|
+
}
|
17706
|
+
else if ((0, types_1.isTypeVar)(srcType) && types_1.TypeBase.isInstantiable(srcType)) {
|
17707
|
+
if (!(0, types_1.isTypeVarTuple)(srcType) || !srcType.priv.isInUnion) {
|
17708
|
+
srcTypeFormType = (0, typeUtils_1.convertToInstance)(srcType);
|
17709
|
+
}
|
17710
|
+
}
|
17711
|
+
if (!srcTypeFormType) {
|
17712
|
+
return srcType;
|
17713
|
+
}
|
17714
|
+
let resultType;
|
17715
|
+
(0, typeUtils_1.doForEachSubtype)(expectedType, (subtype) => {
|
17716
|
+
if (resultType || !(0, types_1.isClassInstance)(subtype) || !types_1.ClassType.isBuiltIn(subtype, 'TypeForm')) {
|
17717
|
+
return;
|
17718
|
+
}
|
17719
|
+
const destTypeFormType = subtype.priv.typeArgs && subtype.priv.typeArgs.length > 0
|
17720
|
+
? subtype.priv.typeArgs[0]
|
17721
|
+
: types_1.UnknownType.create();
|
17722
|
+
if (assignType(destTypeFormType, srcTypeFormType)) {
|
17723
|
+
resultType = types_1.ClassType.specialize(subtype, [srcTypeFormType]);
|
17724
|
+
}
|
17725
|
+
});
|
17726
|
+
return resultType !== null && resultType !== void 0 ? resultType : srcType;
|
17727
|
+
}
|
17328
17728
|
function assignFromUnionType(destType, srcType, diag, constraints, flags, recursionCount) {
|
17329
17729
|
// Start by checking for an exact match. This is needed to handle unions
|
17330
17730
|
// that contain recursive type aliases.
|
@@ -17464,8 +17864,10 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17464
17864
|
}
|
17465
17865
|
else {
|
17466
17866
|
// Try to assign a union of the remaining source types to
|
17467
|
-
// the first destination TypeVar.
|
17468
|
-
|
17867
|
+
// the first destination TypeVar. If this is a contravariant
|
17868
|
+
// context, use the full dest type rather than the remaining
|
17869
|
+
// dest subtypes to keep the lower bound as wide as possible.
|
17870
|
+
if (!assignType(isContra ? destType : remainingDestSubtypes[0], isContra ? remainingSrcSubtypes[0] : (0, types_1.combineTypes)(remainingSrcSubtypes), diag === null || diag === void 0 ? void 0 : diag.createAddendum(), constraints, flags, recursionCount)) {
|
17469
17871
|
canUseFastPath = false;
|
17470
17872
|
}
|
17471
17873
|
}
|
@@ -17644,7 +18046,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
17644
18046
|
// in case the destType is a union with hundreds of literals.
|
17645
18047
|
if ((0, types_1.isClassInstance)(srcType) &&
|
17646
18048
|
(0, typeUtils_1.isLiteralType)(srcType) &&
|
17647
|
-
types_1.UnionType.containsType(destType, srcType,
|
18049
|
+
types_1.UnionType.containsType(destType, srcType,
|
18050
|
+
/* options */ undefined,
|
18051
|
+
/* exclusionSet */ undefined, recursionCount)) {
|
17648
18052
|
return true;
|
17649
18053
|
}
|
17650
18054
|
(0, typeUtils_1.doForEachSubtype)(destType, (subtype) => {
|
@@ -18321,9 +18725,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
18321
18725
|
// is the first remaining param.
|
18322
18726
|
}
|
18323
18727
|
else {
|
18324
|
-
remainingParams.push(types_1.FunctionParam.create(p.category, types_1.FunctionType.getParamType(effectiveSrcType, index), p.flags, p.name, types_1.FunctionType.getParamDefaultType(effectiveSrcType, index)
|
18325
|
-
? types_1.AnyType.create(/* isEllipsis */ true)
|
18326
|
-
: undefined, p.defaultExpr));
|
18728
|
+
remainingParams.push(types_1.FunctionParam.create(p.category, types_1.FunctionType.getParamType(effectiveSrcType, index), p.flags, p.name, types_1.FunctionType.getParamDefaultType(effectiveSrcType, index), p.defaultExpr));
|
18327
18729
|
}
|
18328
18730
|
});
|
18329
18731
|
// If there are remaining parameters and the source and dest do not contain
|
@@ -19183,9 +19585,9 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19183
19585
|
}
|
19184
19586
|
// Perform a sanity check on the RHS expression. Some expression
|
19185
19587
|
// forms should never be considered legitimate for type aliases.
|
19186
|
-
return isLegalTypeAliasExpressionForm(decl.node.parent.d.rightExpr);
|
19588
|
+
return isLegalTypeAliasExpressionForm(decl.node.parent.d.rightExpr, /* allowStrLiteral */ false);
|
19187
19589
|
}
|
19188
|
-
function isLegalTypeAliasExpressionForm(node) {
|
19590
|
+
function isLegalTypeAliasExpressionForm(node, allowStrLiteral) {
|
19189
19591
|
switch (node.nodeType) {
|
19190
19592
|
case 0 /* ParseNodeType.Error */:
|
19191
19593
|
case 55 /* ParseNodeType.UnaryOperation */:
|
@@ -19206,14 +19608,19 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19206
19608
|
case 34 /* ParseNodeType.List */:
|
19207
19609
|
case 45 /* ParseNodeType.Set */:
|
19208
19610
|
return false;
|
19611
|
+
case 48 /* ParseNodeType.StringList */:
|
19612
|
+
case 49 /* ParseNodeType.String */:
|
19613
|
+
return allowStrLiteral;
|
19614
|
+
case 14 /* ParseNodeType.Constant */:
|
19615
|
+
return node.d.constType === 26 /* KeywordType.None */;
|
19209
19616
|
case 7 /* ParseNodeType.BinaryOperation */:
|
19210
19617
|
return (node.d.operator === 6 /* OperatorType.BitwiseOr */ &&
|
19211
|
-
isLegalTypeAliasExpressionForm(node.d.leftExpr) &&
|
19212
|
-
isLegalTypeAliasExpressionForm(node.d.rightExpr));
|
19618
|
+
isLegalTypeAliasExpressionForm(node.d.leftExpr, /* allowStrLiteral */ true) &&
|
19619
|
+
isLegalTypeAliasExpressionForm(node.d.rightExpr, /* allowStrLiteral */ true));
|
19213
19620
|
case 27 /* ParseNodeType.Index */:
|
19214
|
-
return isLegalTypeAliasExpressionForm(node.d.leftExpr);
|
19621
|
+
return isLegalTypeAliasExpressionForm(node.d.leftExpr, allowStrLiteral);
|
19215
19622
|
case 35 /* ParseNodeType.MemberAccess */:
|
19216
|
-
return isLegalTypeAliasExpressionForm(node.d.leftExpr);
|
19623
|
+
return isLegalTypeAliasExpressionForm(node.d.leftExpr, allowStrLiteral);
|
19217
19624
|
}
|
19218
19625
|
return true;
|
19219
19626
|
}
|
@@ -19292,6 +19699,11 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19292
19699
|
}
|
19293
19700
|
return { sourceType: simpleSrcType, destType: simpleDestType };
|
19294
19701
|
}
|
19702
|
+
function isTypeFormSupported(node) {
|
19703
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
19704
|
+
// For now, enable only if enableExperimentalFeatures is true.
|
19705
|
+
return fileInfo.diagnosticRuleSet.enableExperimentalFeatures;
|
19706
|
+
}
|
19295
19707
|
function printType(type, options) {
|
19296
19708
|
let flags = evaluatorOptions.printTypeFlags;
|
19297
19709
|
if (options === null || options === void 0 ? void 0 : options.expandTypeAlias) {
|
@@ -19345,12 +19757,15 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19345
19757
|
const parseResults = parser.parseTextExpression(dummyFileContents, valueOffset, textValue.length, parseOptions, 0 /* ParseTextMode.Expression */,
|
19346
19758
|
/* initialParenDepth */ undefined, fileInfo.typingSymbolAliases);
|
19347
19759
|
if (parseResults.parseTree) {
|
19348
|
-
|
19349
|
-
|
19350
|
-
|
19351
|
-
|
19352
|
-
});
|
19760
|
+
// If there are errors but we are not reporting them, return
|
19761
|
+
// undefined to indicate that the parse failed.
|
19762
|
+
if (!reportErrors && parseResults.diagnostics.length > 0) {
|
19763
|
+
return undefined;
|
19353
19764
|
}
|
19765
|
+
const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
|
19766
|
+
parseResults.diagnostics.forEach((diag) => {
|
19767
|
+
fileInfo.diagnosticSink.addDiagnosticWithTextRange('error', diag.message, node);
|
19768
|
+
});
|
19354
19769
|
parseResults.parseTree.parent = node;
|
19355
19770
|
// Optionally add the new subtree to the parse tree so it can
|
19356
19771
|
// participate in language server operations like find and replace.
|
@@ -19420,8 +19835,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19420
19835
|
isAsymmetricAccessorAssignment,
|
19421
19836
|
suppressDiagnostics,
|
19422
19837
|
isSpecialFormClass,
|
19423
|
-
|
19424
|
-
|
19838
|
+
getDeclInfoForStringNode,
|
19839
|
+
getDeclInfoForNameNode,
|
19425
19840
|
getTypeForDeclaration,
|
19426
19841
|
resolveAliasDeclaration,
|
19427
19842
|
resolveAliasDeclarationWithInfo,
|
@@ -19449,6 +19864,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19449
19864
|
getBoundMagicMethod,
|
19450
19865
|
getTypeOfMagicMethodCall,
|
19451
19866
|
bindFunctionToClassOrObject,
|
19867
|
+
getCallbackProtocolType,
|
19452
19868
|
getCallSignatureInfo,
|
19453
19869
|
getAbstractSymbols,
|
19454
19870
|
narrowConstrainedTypeVar,
|
@@ -19460,6 +19876,8 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
|
|
19460
19876
|
assignClassToSelf,
|
19461
19877
|
getTypedDictClassType,
|
19462
19878
|
getTupleClassType,
|
19879
|
+
getDictClassType,
|
19880
|
+
getStrClassType,
|
19463
19881
|
getObjectType,
|
19464
19882
|
getNoneType,
|
19465
19883
|
getUnionClassType,
|