@zzzen/pyright-internal 1.2.0-dev.20261018 → 1.2.0-dev.20261108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/binder.js +29 -12
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +4 -0
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +3 -3
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/constructorTransform.js +12 -0
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.d.ts +1 -1
- package/dist/analyzer/constructors.js +1 -1
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.js +14 -5
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/decorators.d.ts +2 -2
- package/dist/analyzer/decorators.js +22 -2
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/functionTransform.d.ts +5 -0
- package/dist/analyzer/functionTransform.js +6 -3
- package/dist/analyzer/functionTransform.js.map +1 -1
- package/dist/analyzer/missingBindingMetadata.d.ts +6 -0
- package/dist/analyzer/missingBindingMetadata.js +22 -0
- package/dist/analyzer/missingBindingMetadata.js.map +1 -0
- package/dist/analyzer/namedTuples.d.ts +5 -1
- package/dist/analyzer/namedTuples.js +64 -35
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +45 -1
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.d.ts +2 -0
- package/dist/analyzer/parseTreeUtils.js +43 -0
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.d.ts +5 -1
- package/dist/analyzer/patternMatching.js +6 -1
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +3 -0
- package/dist/analyzer/program.js +71 -1
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/sentinel.js +7 -1
- package/dist/analyzer/sentinel.js.map +1 -1
- package/dist/analyzer/sourceFile.js +13 -6
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/tupleComparison.d.ts +6 -0
- package/dist/analyzer/tupleComparison.js +731 -0
- package/dist/analyzer/tupleComparison.js.map +1 -0
- package/dist/analyzer/tuples.d.ts +1 -1
- package/dist/analyzer/tuples.js +35 -4
- package/dist/analyzer/tuples.js.map +1 -1
- package/dist/analyzer/typeCacheUtils.d.ts +1 -0
- package/dist/analyzer/typeCacheUtils.js +10 -0
- package/dist/analyzer/typeCacheUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator/boundMethodSpecialization.d.ts +15 -0
- package/dist/analyzer/typeEvaluator/boundMethodSpecialization.js +98 -0
- package/dist/analyzer/typeEvaluator/boundMethodSpecialization.js.map +1 -0
- package/dist/analyzer/typeEvaluator/classConstruction.d.ts +25 -0
- package/dist/analyzer/typeEvaluator/classConstruction.js +251 -0
- package/dist/analyzer/typeEvaluator/classConstruction.js.map +1 -0
- package/dist/analyzer/typeEvaluator/collectionInference.d.ts +41 -0
- package/dist/analyzer/typeEvaluator/collectionInference.js +695 -0
- package/dist/analyzer/typeEvaluator/collectionInference.js.map +1 -0
- package/dist/analyzer/typeEvaluator/diagnostics.d.ts +33 -0
- package/dist/analyzer/typeEvaluator/diagnostics.js +198 -0
- package/dist/analyzer/typeEvaluator/diagnostics.js.map +1 -0
- package/dist/analyzer/typeEvaluator/evaluatorCore.d.ts +80 -0
- package/dist/analyzer/typeEvaluator/evaluatorCore.js +1126 -0
- package/dist/analyzer/typeEvaluator/evaluatorCore.js.map +1 -0
- package/dist/analyzer/typeEvaluator/evaluatorNarrowing.d.ts +17 -0
- package/dist/analyzer/typeEvaluator/evaluatorNarrowing.js +370 -0
- package/dist/analyzer/typeEvaluator/evaluatorNarrowing.js.map +1 -0
- package/dist/analyzer/typeEvaluator/expressionEvaluation.d.ts +81 -0
- package/dist/analyzer/typeEvaluator/expressionEvaluation.js +1802 -0
- package/dist/analyzer/typeEvaluator/expressionEvaluation.js.map +1 -0
- package/dist/analyzer/typeEvaluator/flowAnalysis.d.ts +33 -0
- package/dist/analyzer/typeEvaluator/flowAnalysis.js +217 -0
- package/dist/analyzer/typeEvaluator/flowAnalysis.js.map +1 -0
- package/dist/analyzer/typeEvaluator/internals.d.ts +137 -0
- package/dist/analyzer/typeEvaluator/internals.js +10 -0
- package/dist/analyzer/typeEvaluator/internals.js.map +1 -0
- package/dist/analyzer/typeEvaluator/literalTypes.d.ts +3 -0
- package/dist/analyzer/typeEvaluator/literalTypes.js +52 -0
- package/dist/analyzer/typeEvaluator/literalTypes.js.map +1 -0
- package/dist/analyzer/typeEvaluator/memberResolution.d.ts +79 -0
- package/dist/analyzer/typeEvaluator/memberResolution.js +1007 -0
- package/dist/analyzer/typeEvaluator/memberResolution.js.map +1 -0
- package/dist/analyzer/typeEvaluator/overrideValidation.d.ts +15 -0
- package/dist/analyzer/typeEvaluator/overrideValidation.js +422 -0
- package/dist/analyzer/typeEvaluator/overrideValidation.js.map +1 -0
- package/dist/analyzer/typeEvaluator/returnTypeInference.d.ts +33 -0
- package/dist/analyzer/typeEvaluator/returnTypeInference.js +226 -0
- package/dist/analyzer/typeEvaluator/returnTypeInference.js.map +1 -0
- package/dist/analyzer/typeEvaluator/specialFormCreation.d.ts +39 -0
- package/dist/analyzer/typeEvaluator/specialFormCreation.js +1728 -0
- package/dist/analyzer/typeEvaluator/specialFormCreation.js.map +1 -0
- package/dist/analyzer/typeEvaluator/tupleUnpacking.d.ts +2 -0
- package/dist/analyzer/typeEvaluator/tupleUnpacking.js +36 -0
- package/dist/analyzer/typeEvaluator/tupleUnpacking.js.map +1 -0
- package/dist/analyzer/typeEvaluator/typeCompatibility.d.ts +36 -0
- package/dist/analyzer/typeEvaluator/typeCompatibility.js +1320 -0
- package/dist/analyzer/typeEvaluator/typeCompatibility.js.map +1 -0
- package/dist/analyzer/typeEvaluator/typeGuardNarrowing.d.ts +186 -0
- package/dist/analyzer/typeEvaluator/typeGuardNarrowing.js +1683 -0
- package/dist/analyzer/typeEvaluator/typeGuardNarrowing.js.map +1 -0
- package/dist/analyzer/typeEvaluator/typeVarHandling.d.ts +48 -0
- package/dist/analyzer/typeEvaluator/typeVarHandling.js +756 -0
- package/dist/analyzer/typeEvaluator/typeVarHandling.js.map +1 -0
- package/dist/analyzer/typeEvaluator.d.ts +3 -31
- package/dist/analyzer/typeEvaluator.js +5140 -13656
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +1 -1
- package/dist/analyzer/typeEvaluatorTypes.js +2 -3
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.d.ts +3 -2
- package/dist/analyzer/typeGuards.js +236 -1375
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeUtils.js +9 -2
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typedDicts.js +111 -0
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +14 -0
- package/dist/analyzer/types.js +215 -10
- package/dist/analyzer/types.js.map +1 -1
- package/dist/browser/browserConnection.d.ts +12 -0
- package/dist/browser/browserConnection.js +23 -0
- package/dist/browser/browserConnection.js.map +1 -0
- package/dist/browser/browserFileSystem.d.ts +73 -0
- package/dist/browser/browserFileSystem.js +578 -0
- package/dist/browser/browserFileSystem.js.map +1 -0
- package/dist/browser/browserHost.d.ts +4 -0
- package/dist/browser/browserHost.js +10 -0
- package/dist/browser/browserHost.js.map +1 -0
- package/dist/browser/browserProcess.d.ts +1 -0
- package/dist/browser/browserProcess.js +17 -0
- package/dist/browser/browserProcess.js.map +1 -0
- package/dist/browser/browserServer.d.ts +8 -0
- package/dist/browser/browserServer.js +32 -0
- package/dist/browser/browserServer.js.map +1 -0
- package/dist/browser/cancellation/common.d.ts +10 -0
- package/dist/browser/cancellation/common.js +114 -0
- package/dist/browser/cancellation/common.js.map +1 -0
- package/dist/browser/cancellation/objectURLReceiverStrategy.d.ts +5 -0
- package/dist/browser/cancellation/objectURLReceiverStrategy.js +37 -0
- package/dist/browser/cancellation/objectURLReceiverStrategy.js.map +1 -0
- package/dist/browser/cancellation/objectURLSenderStrategy.d.ts +7 -0
- package/dist/browser/cancellation/objectURLSenderStrategy.js +32 -0
- package/dist/browser/cancellation/objectURLSenderStrategy.js.map +1 -0
- package/dist/browser/worker.d.ts +1 -0
- package/dist/browser/worker.js +33 -0
- package/dist/browser/worker.js.map +1 -0
- package/dist/common/processUtils.d.ts +6 -0
- package/dist/common/processUtils.js +56 -1
- package/dist/common/processUtils.js.map +1 -1
- package/dist/common/textEditTracker.js +1 -1
- package/dist/common/textEditTracker.js.map +1 -1
- package/dist/common/windowsProcessTree.d.ts +2 -0
- package/dist/common/windowsProcessTree.js +23 -0
- package/dist/common/windowsProcessTree.js.map +1 -1
- package/dist/common/windowsProcessTree.json +23 -3
- package/dist/languageServerBase.d.ts +3 -0
- package/dist/languageServerBase.js +44 -12
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +6 -6
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.js +3 -3
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/renameProvider.js +1 -1
- package/dist/languageService/renameProvider.js.map +1 -1
- package/dist/languageService/symbolIndexer.js +2 -2
- package/dist/languageService/symbolIndexer.js.map +1 -1
- package/dist/localization/localize.d.ts +2 -0
- package/dist/localization/localize.js +2 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +8 -0
- package/dist/parser/parseTreeTokenReuse.d.ts +34 -0
- package/dist/parser/parseTreeTokenReuse.js +897 -0
- package/dist/parser/parseTreeTokenReuse.js.map +1 -0
- package/dist/parser/tokenizer.d.ts +10 -1
- package/dist/parser/tokenizer.js +184 -130
- package/dist/parser/tokenizer.js.map +1 -1
- package/dist/pyrightServer.d.ts +34 -0
- package/dist/pyrightServer.js +239 -0
- package/dist/pyrightServer.js.map +1 -0
- package/dist/server.d.ts +3 -23
- package/dist/server.js +12 -232
- package/dist/server.js.map +1 -1
- package/dist/tests/benchmarks/typeInferenceComparison.d.ts +1 -0
- package/dist/tests/benchmarks/typeInferenceComparison.js +1235 -0
- package/dist/tests/benchmarks/typeInferenceComparison.js.map +1 -0
- package/dist/tests/benchmarks/typeInferencePyrightEntry.d.ts +1 -0
- package/dist/tests/benchmarks/typeInferencePyrightEntry.js +53 -0
- package/dist/tests/benchmarks/typeInferencePyrightEntry.js.map +1 -0
- package/dist/tests/browserCancellation.test.d.ts +1 -0
- package/dist/tests/browserCancellation.test.js +63 -0
- package/dist/tests/browserCancellation.test.js.map +1 -0
- package/dist/tests/browserServer.test.d.ts +1 -0
- package/dist/tests/browserServer.test.js +177 -0
- package/dist/tests/browserServer.test.js.map +1 -0
- package/dist/tests/checker.test.js +8 -0
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/constraintSolver.test.js +45 -0
- package/dist/tests/constraintSolver.test.js.map +1 -1
- package/dist/tests/fourSlashParser.test.js +35 -0
- package/dist/tests/fourSlashParser.test.js.map +1 -1
- package/dist/tests/harness/fourslash/fourSlashParser.js +14 -4
- package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
- package/dist/tests/languageServer.test.js +15 -0
- package/dist/tests/languageServer.test.js.map +1 -1
- package/dist/tests/lsp/languageServer.js +6 -6
- package/dist/tests/lsp/languageServer.js.map +1 -1
- package/dist/tests/mypyPrimerAnalysis.test.js +148 -1
- package/dist/tests/mypyPrimerAnalysis.test.js.map +1 -1
- package/dist/tests/typeCacheUtils.test.js +46 -0
- package/dist/tests/typeCacheUtils.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +78 -1
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +54 -1
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +4 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +42 -3
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator6.test.js +35 -0
- package/dist/tests/typeEvaluator6.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +414 -0
- package/dist/tests/typeEvaluator7.test.js.map +1 -1
- package/dist/tests/typeEvaluator8.test.js +1291 -1
- package/dist/tests/typeEvaluator8.test.js.map +1 -1
- package/dist/tests/typeUtils.test.js +28 -0
- package/dist/tests/typeUtils.test.js.map +1 -1
- package/dist/tests/types.test.js +55 -0
- package/dist/tests/types.test.js.map +1 -1
- package/dist/typeServer/programWrapper.js +8 -2
- package/dist/typeServer/programWrapper.js.map +1 -1
- package/dist/typeServer/server.d.ts +12 -2
- package/dist/typeServer/server.js +97 -0
- package/dist/typeServer/server.js.map +1 -1
- package/dist/typeServer/stubGenerator.js +136 -3
- package/dist/typeServer/stubGenerator.js.map +1 -1
- package/dist/typeServer/typeServerConversionUtils.js +13 -0
- package/dist/typeServer/typeServerConversionUtils.js.map +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,1728 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* specialFormCreation.ts
|
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
* Licensed under the MIT license.
|
|
6
|
+
*
|
|
7
|
+
* specialFormCreation implementation, using evaluator-owned dependencies and state.
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
26
|
+
var ownKeys = function(o) {
|
|
27
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
28
|
+
var ar = [];
|
|
29
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
30
|
+
return ar;
|
|
31
|
+
};
|
|
32
|
+
return ownKeys(o);
|
|
33
|
+
};
|
|
34
|
+
return function (mod) {
|
|
35
|
+
if (mod && mod.__esModule) return mod;
|
|
36
|
+
var result = {};
|
|
37
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
38
|
+
__setModuleDefault(result, mod);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.createSpecialFormCreation = createSpecialFormCreation;
|
|
44
|
+
const collectionUtils_1 = require("../../common/collectionUtils");
|
|
45
|
+
const debug_1 = require("../../common/debug");
|
|
46
|
+
const diagnostic_1 = require("../../common/diagnostic");
|
|
47
|
+
const diagnosticRules_1 = require("../../common/diagnosticRules");
|
|
48
|
+
const pythonVersion_1 = require("../../common/pythonVersion");
|
|
49
|
+
const localize_1 = require("../../localization/localize");
|
|
50
|
+
const analyzerFileInfo_1 = require("../analyzerFileInfo");
|
|
51
|
+
const constraintTracker_1 = require("../constraintTracker");
|
|
52
|
+
const decorators_1 = require("../decorators");
|
|
53
|
+
const ParseTreeUtils = __importStar(require("../parseTreeUtils"));
|
|
54
|
+
const tuples_1 = require("../tuples");
|
|
55
|
+
const types_1 = require("../types");
|
|
56
|
+
const typeUtils_1 = require("../typeUtils");
|
|
57
|
+
// Synthesizes typing special forms and validates their arguments through evaluator callbacks.
|
|
58
|
+
function createSpecialFormCreation(context) {
|
|
59
|
+
const { addDiagnostic, applyUnpackToTupleLike, getScopeIdForNode, getTypeOfClass, getTupleClassType, getTypeVarScopesForNode, getTypingType, nodeInfo, validateTypeMetadata, assignType, convertTypeArgToInstance, getTypeOfAnnotation, makeTopLevelTypeVarsConcrete, printType, getBuiltInType, solveAndApplyConstraints, getTypeOfExpression, } = context;
|
|
60
|
+
function reportUseOfTypeCheckOnly(type, node) {
|
|
61
|
+
let isTypeCheckingOnly = false;
|
|
62
|
+
let name = '';
|
|
63
|
+
if ((0, types_1.isInstantiableClass)(type) && !type.priv.includeSubclasses) {
|
|
64
|
+
isTypeCheckingOnly = types_1.ClassType.isTypeCheckOnly(type);
|
|
65
|
+
name = type.shared.name;
|
|
66
|
+
}
|
|
67
|
+
else if ((0, types_1.isFunction)(type)) {
|
|
68
|
+
isTypeCheckingOnly = types_1.FunctionType.isTypeCheckOnly(type);
|
|
69
|
+
name = type.shared.name;
|
|
70
|
+
}
|
|
71
|
+
if (isTypeCheckingOnly) {
|
|
72
|
+
const fileInfo = nodeInfo.getFileInfo(node);
|
|
73
|
+
if (!fileInfo.isStubFile) {
|
|
74
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeCheckOnly().format({ name }), node);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function enforceClassTypeVarScope(node, type) {
|
|
79
|
+
const scopeId = type.priv.freeTypeVar?.priv.scopeId ?? type.priv.scopeId;
|
|
80
|
+
if (!scopeId) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
const enclosingClass = ParseTreeUtils.getEnclosingClass(node);
|
|
84
|
+
if (enclosingClass) {
|
|
85
|
+
const liveTypeVarScopeIds = getTypeVarScopesForNode(enclosingClass);
|
|
86
|
+
if (!liveTypeVarScopeIds.includes(scopeId)) {
|
|
87
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarInvalidForMemberVariable().format({
|
|
88
|
+
name: types_1.TypeVarType.getReadableName(type),
|
|
89
|
+
}), node);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
function adjustTypeArgsForTypeVarTuple(typeArgs, typeParams, errorNode) {
|
|
96
|
+
const variadicIndex = typeParams.findIndex((param) => (0, types_1.isTypeVarTuple)(param));
|
|
97
|
+
// Is there a *tuple[T, ...] somewhere in the type arguments that we can expand if needed?
|
|
98
|
+
let srcUnboundedTupleType;
|
|
99
|
+
const findUnboundedTupleIndex = (startArgIndex) => {
|
|
100
|
+
return typeArgs.findIndex((arg, index) => {
|
|
101
|
+
if (index < startArgIndex) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if ((0, types_1.isUnpackedClass)(arg.type) &&
|
|
105
|
+
arg.type.priv.tupleTypeArgs &&
|
|
106
|
+
arg.type.priv.tupleTypeArgs.length === 1 &&
|
|
107
|
+
arg.type.priv.tupleTypeArgs[0].isUnbounded) {
|
|
108
|
+
srcUnboundedTupleType = arg.type.priv.tupleTypeArgs[0].type;
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
let srcUnboundedTupleIndex = findUnboundedTupleIndex(0);
|
|
115
|
+
// Allow only one unpacked tuple that maps to a TypeVarTuple.
|
|
116
|
+
if (srcUnboundedTupleIndex >= 0) {
|
|
117
|
+
const secondUnboundedTupleIndex = findUnboundedTupleIndex(srcUnboundedTupleIndex + 1);
|
|
118
|
+
if (secondUnboundedTupleIndex >= 0) {
|
|
119
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), typeArgs[secondUnboundedTupleIndex].node);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (srcUnboundedTupleType &&
|
|
123
|
+
srcUnboundedTupleIndex >= 0 &&
|
|
124
|
+
variadicIndex >= 0 &&
|
|
125
|
+
typeArgs.length < typeParams.length) {
|
|
126
|
+
// "Smear" the tuple type across type argument slots prior to the TypeVarTuple.
|
|
127
|
+
while (variadicIndex > srcUnboundedTupleIndex) {
|
|
128
|
+
typeArgs = [
|
|
129
|
+
...typeArgs.slice(0, srcUnboundedTupleIndex),
|
|
130
|
+
{ node: typeArgs[srcUnboundedTupleIndex].node, type: srcUnboundedTupleType },
|
|
131
|
+
...typeArgs.slice(srcUnboundedTupleIndex),
|
|
132
|
+
];
|
|
133
|
+
srcUnboundedTupleIndex++;
|
|
134
|
+
}
|
|
135
|
+
// "Smear" the tuple type across type argument slots following the TypeVarTuple.
|
|
136
|
+
while (typeArgs.length < typeParams.length) {
|
|
137
|
+
typeArgs = [
|
|
138
|
+
...typeArgs.slice(0, srcUnboundedTupleIndex + 1),
|
|
139
|
+
{ node: typeArgs[srcUnboundedTupleIndex].node, type: srcUnboundedTupleType },
|
|
140
|
+
...typeArgs.slice(srcUnboundedTupleIndex + 1),
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// Do we need to adjust the type arguments to map to a variadic type
|
|
145
|
+
// param somewhere in the list?
|
|
146
|
+
if (variadicIndex >= 0) {
|
|
147
|
+
const variadicTypeVar = typeParams[variadicIndex];
|
|
148
|
+
// If the type param list ends with a ParamSpec with a default value,
|
|
149
|
+
// we can ignore it for purposes of finding type args that map to the
|
|
150
|
+
// TypeVarTuple.
|
|
151
|
+
let typeParamCount = typeParams.length;
|
|
152
|
+
while (typeParamCount > 0) {
|
|
153
|
+
const lastTypeParam = typeParams[typeParamCount - 1];
|
|
154
|
+
if (!(0, types_1.isParamSpec)(lastTypeParam) || !lastTypeParam.shared.isDefaultExplicit) {
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
typeParamCount--;
|
|
158
|
+
}
|
|
159
|
+
if (variadicIndex < typeArgs.length) {
|
|
160
|
+
// If there are typeArg lists at the end, these should map to ParamSpecs rather
|
|
161
|
+
// than the TypeVarTuple, so exclude them.
|
|
162
|
+
let variadicEndIndex = variadicIndex + 1 + typeArgs.length - typeParamCount;
|
|
163
|
+
while (variadicEndIndex > variadicIndex) {
|
|
164
|
+
if (!typeArgs[variadicEndIndex - 1].typeList) {
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
variadicEndIndex--;
|
|
168
|
+
}
|
|
169
|
+
const variadicTypeResults = typeArgs.slice(variadicIndex, variadicEndIndex);
|
|
170
|
+
// If the type args consist of a lone TypeVarTuple, don't wrap it in a tuple.
|
|
171
|
+
if (variadicTypeResults.length === 1 && (0, types_1.isTypeVarTuple)(variadicTypeResults[0].type)) {
|
|
172
|
+
validateTypeVarTupleIsUnpacked(variadicTypeResults[0].type, variadicTypeResults[0].node);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
variadicTypeResults.forEach((arg, index) => {
|
|
176
|
+
validateTypeArg(arg, {
|
|
177
|
+
allowEmptyTuple: index === 0,
|
|
178
|
+
allowTypeVarTuple: true,
|
|
179
|
+
allowUnpackedTuples: true,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
const variadicTypes = [];
|
|
183
|
+
if (variadicTypeResults.length !== 1 || !variadicTypeResults[0].isEmptyTupleShorthand) {
|
|
184
|
+
variadicTypeResults.forEach((typeResult) => {
|
|
185
|
+
if ((0, types_1.isUnpackedClass)(typeResult.type) && typeResult.type.priv.tupleTypeArgs) {
|
|
186
|
+
(0, collectionUtils_1.appendArray)(variadicTypes, typeResult.type.priv.tupleTypeArgs);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
variadicTypes.push({
|
|
190
|
+
type: (0, typeUtils_1.convertToInstance)(typeResult.type),
|
|
191
|
+
isUnbounded: false,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const tupleObject = (0, tuples_1.makeTupleObject)(context, variadicTypes, /* isUnpacked */ true);
|
|
197
|
+
typeArgs = [
|
|
198
|
+
...typeArgs.slice(0, variadicIndex),
|
|
199
|
+
{ node: typeArgs[variadicIndex].node, type: tupleObject },
|
|
200
|
+
...typeArgs.slice(variadicEndIndex, typeArgs.length),
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (!variadicTypeVar.shared.isDefaultExplicit) {
|
|
205
|
+
// Add an empty tuple that maps to the TypeVarTuple type parameter.
|
|
206
|
+
typeArgs.push({
|
|
207
|
+
node: errorNode,
|
|
208
|
+
type: (0, tuples_1.makeTupleObject)(context, [], /* isUnpacked */ true),
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return typeArgs;
|
|
213
|
+
}
|
|
214
|
+
function validateTypeVarTupleIsUnpacked(type, node) {
|
|
215
|
+
if (!type.priv.isUnpacked) {
|
|
216
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedTypeVarTupleExpected().format({
|
|
217
|
+
name1: type.shared.name,
|
|
218
|
+
name2: type.shared.name,
|
|
219
|
+
}), node);
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
function validateTypeArg(argResult, options) {
|
|
225
|
+
if (argResult.typeList) {
|
|
226
|
+
if (!options?.allowTypeArgList) {
|
|
227
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), argResult.node);
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
argResult.typeList.forEach((typeArg) => {
|
|
232
|
+
validateTypeArg(typeArg);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if ((0, typeUtils_1.isEllipsisType)(argResult.type)) {
|
|
237
|
+
if (!options?.allowTypeArgList) {
|
|
238
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), argResult.node);
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if ((0, types_1.isModule)(argResult.type)) {
|
|
243
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.moduleAsType(), argResult.node);
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if ((0, types_1.isParamSpec)(argResult.type)) {
|
|
247
|
+
if (!options?.allowParamSpec) {
|
|
248
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), argResult.node);
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if ((0, types_1.isTypeVarTuple)(argResult.type) && !argResult.type.priv.isInUnion) {
|
|
253
|
+
if (!options?.allowTypeVarTuple) {
|
|
254
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), argResult.node);
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
validateTypeVarTupleIsUnpacked(argResult.type, argResult.node);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!options?.allowEmptyTuple && argResult.isEmptyTupleShorthand) {
|
|
262
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.zeroLengthTupleNotAllowed(), argResult.node);
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
if ((0, types_1.isUnpackedClass)(argResult.type)) {
|
|
266
|
+
if (!options?.allowUnpackedTuples) {
|
|
267
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedArgInTypeArgument(), argResult.node);
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
function createCallableType(classType, typeArgs, errorNode) {
|
|
274
|
+
let functionType = types_1.FunctionType.createInstantiable(0 /* FunctionTypeFlags.None */);
|
|
275
|
+
let paramSpec;
|
|
276
|
+
let isValidTypeForm = true;
|
|
277
|
+
types_1.TypeBase.setSpecialForm(functionType, types_1.ClassType.cloneAsInstance(classType));
|
|
278
|
+
functionType.shared.declaredReturnType = types_1.UnknownType.create();
|
|
279
|
+
functionType.shared.typeVarScopeId = getScopeIdForNode(errorNode);
|
|
280
|
+
if (typeArgs && typeArgs.length > 0) {
|
|
281
|
+
functionType.priv.isCallableWithTypeArgs = true;
|
|
282
|
+
if (typeArgs[0].typeList) {
|
|
283
|
+
const typeList = typeArgs[0].typeList;
|
|
284
|
+
let sawUnpacked = false;
|
|
285
|
+
let reportedUnpackedError = false;
|
|
286
|
+
const noteSawUnpacked = (entry) => {
|
|
287
|
+
// Make sure we have at most one unpacked TypeVarTuple.
|
|
288
|
+
if (sawUnpacked) {
|
|
289
|
+
if (!reportedUnpackedError) {
|
|
290
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), entry.node);
|
|
291
|
+
reportedUnpackedError = true;
|
|
292
|
+
isValidTypeForm = false;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
sawUnpacked = true;
|
|
296
|
+
};
|
|
297
|
+
typeList.forEach((entry, index) => {
|
|
298
|
+
let entryType = entry.type;
|
|
299
|
+
let paramCategory = 0 /* ParamCategory.Simple */;
|
|
300
|
+
const paramName = `__p${index.toString()}`;
|
|
301
|
+
if ((0, types_1.isTypeVarTuple)(entryType)) {
|
|
302
|
+
validateTypeVarTupleIsUnpacked(entryType, entry.node);
|
|
303
|
+
paramCategory = 1 /* ParamCategory.ArgsList */;
|
|
304
|
+
noteSawUnpacked(entry);
|
|
305
|
+
}
|
|
306
|
+
else if (validateTypeArg(entry, { allowUnpackedTuples: true })) {
|
|
307
|
+
if ((0, types_1.isUnpackedClass)(entryType)) {
|
|
308
|
+
paramCategory = 1 /* ParamCategory.ArgsList */;
|
|
309
|
+
if (entryType.priv.tupleTypeArgs?.some((typeArg) => (0, types_1.isTypeVarTuple)(typeArg.type) || typeArg.isUnbounded)) {
|
|
310
|
+
noteSawUnpacked(entry);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
entryType = types_1.UnknownType.create();
|
|
316
|
+
}
|
|
317
|
+
types_1.FunctionType.addParam(functionType, types_1.FunctionParam.create(paramCategory, (0, typeUtils_1.convertToInstance)(entryType), types_1.FunctionParamFlags.NameSynthesized | types_1.FunctionParamFlags.TypeDeclared, paramName));
|
|
318
|
+
});
|
|
319
|
+
if (typeList.length > 0) {
|
|
320
|
+
types_1.FunctionType.addPositionOnlyParamSeparator(functionType);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else if ((0, typeUtils_1.isEllipsisType)(typeArgs[0].type)) {
|
|
324
|
+
types_1.FunctionType.addDefaultParams(functionType);
|
|
325
|
+
functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
|
326
|
+
}
|
|
327
|
+
else if ((0, types_1.isParamSpec)(typeArgs[0].type)) {
|
|
328
|
+
paramSpec = typeArgs[0].type;
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
if ((0, types_1.isInstantiableClass)(typeArgs[0].type) && types_1.ClassType.isBuiltIn(typeArgs[0].type, 'Concatenate')) {
|
|
332
|
+
const concatTypeArgs = typeArgs[0].type.priv.typeArgs;
|
|
333
|
+
if (concatTypeArgs && concatTypeArgs.length > 0) {
|
|
334
|
+
concatTypeArgs.forEach((typeArg, index) => {
|
|
335
|
+
if (index === concatTypeArgs.length - 1) {
|
|
336
|
+
types_1.FunctionType.addPositionOnlyParamSeparator(functionType);
|
|
337
|
+
if ((0, types_1.isParamSpec)(typeArg)) {
|
|
338
|
+
paramSpec = typeArg;
|
|
339
|
+
}
|
|
340
|
+
else if ((0, typeUtils_1.isEllipsisType)(typeArg)) {
|
|
341
|
+
types_1.FunctionType.addDefaultParams(functionType);
|
|
342
|
+
functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
types_1.FunctionType.addParam(functionType, types_1.FunctionParam.create(0 /* ParamCategory.Simple */, typeArg, types_1.FunctionParamFlags.NameSynthesized | types_1.FunctionParamFlags.TypeDeclared, `__p${index}`));
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableFirstArg(), typeArgs[0].node);
|
|
353
|
+
isValidTypeForm = false;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (typeArgs.length > 1) {
|
|
357
|
+
let typeArg1Type = typeArgs[1].type;
|
|
358
|
+
if (!validateTypeArg(typeArgs[1])) {
|
|
359
|
+
typeArg1Type = types_1.UnknownType.create();
|
|
360
|
+
}
|
|
361
|
+
functionType.shared.declaredReturnType = (0, typeUtils_1.convertToInstance)(typeArg1Type);
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportMissingTypeArgument, localize_1.LocMessage.callableSecondArg(), errorNode);
|
|
365
|
+
functionType.shared.declaredReturnType = types_1.UnknownType.create();
|
|
366
|
+
isValidTypeForm = false;
|
|
367
|
+
}
|
|
368
|
+
if (typeArgs.length > 2) {
|
|
369
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.callableExtraArgs(), typeArgs[2].node);
|
|
370
|
+
isValidTypeForm = false;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
types_1.FunctionType.addDefaultParams(functionType, /* useUnknown */ true);
|
|
375
|
+
functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
|
376
|
+
if (typeArgs && typeArgs.length === 0) {
|
|
377
|
+
isValidTypeForm = false;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (paramSpec) {
|
|
381
|
+
types_1.FunctionType.addParamSpecVariadics(functionType, (0, typeUtils_1.convertToInstance)(paramSpec));
|
|
382
|
+
}
|
|
383
|
+
if (isValidTypeForm) {
|
|
384
|
+
functionType = types_1.TypeBase.cloneWithTypeForm(functionType, (0, typeUtils_1.convertToInstance)(functionType));
|
|
385
|
+
}
|
|
386
|
+
return functionType;
|
|
387
|
+
}
|
|
388
|
+
function createOptionalType(classType, errorNode, typeArgs, flags) {
|
|
389
|
+
if (!typeArgs) {
|
|
390
|
+
// If no type arguments are provided, the resulting type
|
|
391
|
+
// depends on whether we're evaluating a type annotation or
|
|
392
|
+
// we're in some other context.
|
|
393
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
394
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.optionalExtraArgs(), errorNode);
|
|
395
|
+
return types_1.UnknownType.create();
|
|
396
|
+
}
|
|
397
|
+
return classType;
|
|
398
|
+
}
|
|
399
|
+
if (typeArgs.length !== 1) {
|
|
400
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.optionalExtraArgs(), errorNode);
|
|
401
|
+
return types_1.UnknownType.create();
|
|
402
|
+
}
|
|
403
|
+
let typeArg0Type = typeArgs[0].type;
|
|
404
|
+
if (!validateTypeArg(typeArgs[0])) {
|
|
405
|
+
typeArg0Type = types_1.UnknownType.create();
|
|
406
|
+
}
|
|
407
|
+
let optionalType = (0, types_1.combineTypes)([typeArg0Type, context.prefetched?.noneTypeClass ?? types_1.UnknownType.create()]);
|
|
408
|
+
if (context.prefetched?.unionTypeClass && (0, types_1.isInstantiableClass)(context.prefetched.unionTypeClass)) {
|
|
409
|
+
optionalType = types_1.TypeBase.cloneAsSpecialForm(optionalType, types_1.ClassType.cloneAsInstance(context.prefetched.unionTypeClass));
|
|
410
|
+
}
|
|
411
|
+
if (typeArg0Type.props?.typeForm) {
|
|
412
|
+
const typeFormType = (0, types_1.combineTypes)([
|
|
413
|
+
typeArg0Type.props.typeForm,
|
|
414
|
+
(0, typeUtils_1.convertToInstance)(context.prefetched?.noneTypeClass ?? types_1.UnknownType.create()),
|
|
415
|
+
]);
|
|
416
|
+
optionalType = types_1.TypeBase.cloneWithTypeForm(optionalType, typeFormType);
|
|
417
|
+
}
|
|
418
|
+
return optionalType;
|
|
419
|
+
}
|
|
420
|
+
function createClassVarType(classType, errorNode, typeArgs, flags) {
|
|
421
|
+
if (flags & (131072 /* EvalFlags.NoClassVar */ | 1073741824 /* EvalFlags.TypeFormArg */)) {
|
|
422
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarNotAllowed(), errorNode);
|
|
423
|
+
return (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : types_1.AnyType.create();
|
|
424
|
+
}
|
|
425
|
+
if (!typeArgs) {
|
|
426
|
+
return classType;
|
|
427
|
+
}
|
|
428
|
+
else if (typeArgs.length === 0) {
|
|
429
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarFirstArgMissing(), errorNode);
|
|
430
|
+
return types_1.UnknownType.create();
|
|
431
|
+
}
|
|
432
|
+
else if (typeArgs.length > 1) {
|
|
433
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.classVarTooManyArgs(), typeArgs[1].node);
|
|
434
|
+
return types_1.UnknownType.create();
|
|
435
|
+
}
|
|
436
|
+
const type = typeArgs[0].type;
|
|
437
|
+
// A ClassVar should not allow TypeVars or generic types parameterized
|
|
438
|
+
// by TypeVars.
|
|
439
|
+
if ((0, typeUtils_1.requiresSpecialization)(type, { ignorePseudoGeneric: true, ignoreSelf: true })) {
|
|
440
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classVarWithTypeVar(), typeArgs[0].node ?? errorNode);
|
|
441
|
+
}
|
|
442
|
+
return type;
|
|
443
|
+
}
|
|
444
|
+
function createTypeFormType(classType, errorNode, typeArgs) {
|
|
445
|
+
if (!typeArgs) {
|
|
446
|
+
const specializedType = types_1.ClassType.specialize(classType, [types_1.AnyType.create()]);
|
|
447
|
+
return types_1.TypeBase.cloneWithTypeForm(specializedType, types_1.ClassType.cloneAsInstance(specializedType));
|
|
448
|
+
}
|
|
449
|
+
if (typeArgs.length === 0) {
|
|
450
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooFew().format({
|
|
451
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
452
|
+
expected: 1,
|
|
453
|
+
received: 0,
|
|
454
|
+
}), errorNode);
|
|
455
|
+
return types_1.UnknownType.create();
|
|
456
|
+
}
|
|
457
|
+
if (typeArgs.length > 1) {
|
|
458
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
|
|
459
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
460
|
+
expected: 1,
|
|
461
|
+
received: typeArgs.length,
|
|
462
|
+
}), typeArgs[1].node);
|
|
463
|
+
return types_1.UnknownType.create();
|
|
464
|
+
}
|
|
465
|
+
const convertedTypeArgs = typeArgs.map((typeArg) => {
|
|
466
|
+
return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
|
|
467
|
+
});
|
|
468
|
+
let resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
|
|
469
|
+
resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
|
|
470
|
+
return resultType;
|
|
471
|
+
}
|
|
472
|
+
function createTypeGuardType(classType, errorNode, typeArgs, flags) {
|
|
473
|
+
// If no type arguments are provided, the resulting type
|
|
474
|
+
// depends on whether we're evaluating a type annotation or
|
|
475
|
+
// we're in some other context.
|
|
476
|
+
if (!typeArgs) {
|
|
477
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
478
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeGuardArgCount(), errorNode);
|
|
479
|
+
}
|
|
480
|
+
return (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType;
|
|
481
|
+
}
|
|
482
|
+
else if (typeArgs.length !== 1) {
|
|
483
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeGuardArgCount(), errorNode);
|
|
484
|
+
return types_1.UnknownType.create();
|
|
485
|
+
}
|
|
486
|
+
const convertedTypeArgs = typeArgs.map((typeArg) => {
|
|
487
|
+
return (0, typeUtils_1.convertToInstance)(validateTypeArg(typeArg) ? typeArg.type : types_1.UnknownType.create());
|
|
488
|
+
});
|
|
489
|
+
let resultType = types_1.ClassType.specialize(classType, convertedTypeArgs);
|
|
490
|
+
resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
|
|
491
|
+
return resultType;
|
|
492
|
+
}
|
|
493
|
+
function createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags) {
|
|
494
|
+
// If no type arguments are provided, the resulting type
|
|
495
|
+
// depends on whether we're evaluating a type annotation or
|
|
496
|
+
// we're in some other context.
|
|
497
|
+
const typeExpressionFlags = 256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */;
|
|
498
|
+
if (!typeArgs && (flags & typeExpressionFlags) === 0) {
|
|
499
|
+
return { type: classType };
|
|
500
|
+
}
|
|
501
|
+
if (!typeArgs || typeArgs.length !== 1) {
|
|
502
|
+
if ((flags & typeExpressionFlags) !== 0) {
|
|
503
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
|
|
504
|
+
? localize_1.LocMessage.readOnlyArgCount()
|
|
505
|
+
: classType.shared.name === 'Required'
|
|
506
|
+
? localize_1.LocMessage.requiredArgCount()
|
|
507
|
+
: localize_1.LocMessage.notRequiredArgCount(), errorNode);
|
|
508
|
+
}
|
|
509
|
+
return { type: (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType };
|
|
510
|
+
}
|
|
511
|
+
const typeArgType = typeArgs[0].type;
|
|
512
|
+
// Make sure this is used only in a dataclass.
|
|
513
|
+
const containingClassNode = ParseTreeUtils.getEnclosingClass(errorNode, /* stopAtFunction */ true);
|
|
514
|
+
const classTypeInfo = containingClassNode ? getTypeOfClass(containingClassNode) : undefined;
|
|
515
|
+
let isUsageLegal = false;
|
|
516
|
+
if (classTypeInfo &&
|
|
517
|
+
(0, types_1.isInstantiableClass)(classTypeInfo.classType) &&
|
|
518
|
+
types_1.ClassType.isTypedDictClass(classTypeInfo.classType)) {
|
|
519
|
+
// The only legal usage is when used in a type annotation statement.
|
|
520
|
+
if (ParseTreeUtils.isNodeContainedWithinNodeType(errorNode, 54 /* ParseNodeType.TypeAnnotation */)) {
|
|
521
|
+
isUsageLegal = true;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
let isReadOnly = typeArgs[0].isReadOnly;
|
|
525
|
+
let isRequired = typeArgs[0].isRequired;
|
|
526
|
+
let isNotRequired = typeArgs[0].isNotRequired;
|
|
527
|
+
if (classType.shared.name === 'ReadOnly') {
|
|
528
|
+
if ((flags & 2097152 /* EvalFlags.AllowReadOnly */) !== 0) {
|
|
529
|
+
isUsageLegal = true;
|
|
530
|
+
}
|
|
531
|
+
// Nested ReadOnly are not allowed.
|
|
532
|
+
if (typeArgs[0].isReadOnly) {
|
|
533
|
+
isUsageLegal = false;
|
|
534
|
+
}
|
|
535
|
+
isReadOnly = true;
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
if ((flags & 1048576 /* EvalFlags.AllowRequired */) !== 0) {
|
|
539
|
+
isUsageLegal = true;
|
|
540
|
+
}
|
|
541
|
+
// Nested Required/NotRequired are not allowed.
|
|
542
|
+
if (typeArgs[0].isRequired || typeArgs[0].isNotRequired) {
|
|
543
|
+
isUsageLegal = false;
|
|
544
|
+
}
|
|
545
|
+
isRequired = classType.shared.name === 'Required';
|
|
546
|
+
isNotRequired = classType.shared.name === 'NotRequired';
|
|
547
|
+
}
|
|
548
|
+
if (!isUsageLegal) {
|
|
549
|
+
if ((flags & typeExpressionFlags) !== 0) {
|
|
550
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, classType.shared.name === 'ReadOnly'
|
|
551
|
+
? localize_1.LocMessage.readOnlyNotInTypedDict()
|
|
552
|
+
: classType.shared.name === 'Required'
|
|
553
|
+
? localize_1.LocMessage.requiredNotInTypedDict()
|
|
554
|
+
: localize_1.LocMessage.notRequiredNotInTypedDict(), errorNode);
|
|
555
|
+
}
|
|
556
|
+
return { type: (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType };
|
|
557
|
+
}
|
|
558
|
+
return { type: typeArgType, isReadOnly, isRequired, isNotRequired };
|
|
559
|
+
}
|
|
560
|
+
function createUnpackType(classType, errorNode, typeArgs, flags) {
|
|
561
|
+
if (!typeArgs || typeArgs.length !== 1) {
|
|
562
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
563
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackArgCount(), errorNode);
|
|
564
|
+
}
|
|
565
|
+
return (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType;
|
|
566
|
+
}
|
|
567
|
+
const typeArgType = typeArgs[0].type;
|
|
568
|
+
if ((flags & 4194304 /* EvalFlags.AllowUnpackedTuple */) !== 0) {
|
|
569
|
+
const unpackedType = applyUnpackToTupleLike(typeArgType);
|
|
570
|
+
if (unpackedType) {
|
|
571
|
+
return unpackedType;
|
|
572
|
+
}
|
|
573
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) === 0) {
|
|
574
|
+
return classType;
|
|
575
|
+
}
|
|
576
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackExpectedTypeVarTuple(), errorNode);
|
|
577
|
+
return types_1.UnknownType.create();
|
|
578
|
+
}
|
|
579
|
+
if ((flags & 8388608 /* EvalFlags.AllowUnpackedTypedDict */) !== 0) {
|
|
580
|
+
if ((0, types_1.isInstantiableClass)(typeArgType) && types_1.ClassType.isTypedDictClass(typeArgType)) {
|
|
581
|
+
return types_1.ClassType.cloneForUnpacked(typeArgType);
|
|
582
|
+
}
|
|
583
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) === 0) {
|
|
584
|
+
return classType;
|
|
585
|
+
}
|
|
586
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackExpectedTypedDict(), errorNode);
|
|
587
|
+
return types_1.UnknownType.create();
|
|
588
|
+
}
|
|
589
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) === 0) {
|
|
590
|
+
return classType;
|
|
591
|
+
}
|
|
592
|
+
if ((flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0) {
|
|
593
|
+
if ((0, types_1.isUnknown)(typeArgType)) {
|
|
594
|
+
return typeArgType;
|
|
595
|
+
}
|
|
596
|
+
if ((0, types_1.isTypeVar)(typeArgType) && !typeArgType.priv.scopeId) {
|
|
597
|
+
return types_1.UnknownType.create();
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unpackNotAllowed(), errorNode);
|
|
601
|
+
return types_1.UnknownType.create();
|
|
602
|
+
}
|
|
603
|
+
function createFinalType(classType, errorNode, typeArgs, flags) {
|
|
604
|
+
if (flags & (16 /* EvalFlags.NoFinal */ | 1073741824 /* EvalFlags.TypeFormArg */)) {
|
|
605
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
606
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalContext(), errorNode);
|
|
607
|
+
}
|
|
608
|
+
return (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType;
|
|
609
|
+
}
|
|
610
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) === 0 || !typeArgs || typeArgs.length === 0) {
|
|
611
|
+
return classType;
|
|
612
|
+
}
|
|
613
|
+
if (typeArgs.length > 1) {
|
|
614
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.finalTooManyArgs(), errorNode);
|
|
615
|
+
}
|
|
616
|
+
return types_1.TypeBase.cloneAsSpecialForm(typeArgs[0].type, classType);
|
|
617
|
+
}
|
|
618
|
+
function createConcatenateType(classType, errorNode, typeArgs, flags) {
|
|
619
|
+
if ((flags & 134217728 /* EvalFlags.AllowConcatenate */) === 0) {
|
|
620
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
621
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), errorNode);
|
|
622
|
+
}
|
|
623
|
+
return (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType;
|
|
624
|
+
}
|
|
625
|
+
if (!typeArgs || typeArgs.length === 0) {
|
|
626
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateTypeArgsMissing(), errorNode);
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
typeArgs.forEach((typeArg, index) => {
|
|
630
|
+
if (index === typeArgs.length - 1) {
|
|
631
|
+
if (!(0, types_1.isParamSpec)(typeArg.type) && !(0, typeUtils_1.isEllipsisType)(typeArg.type)) {
|
|
632
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateParamSpecMissing(), typeArg.node);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
if ((0, types_1.isParamSpec)(typeArg.type)) {
|
|
637
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), typeArg.node);
|
|
638
|
+
}
|
|
639
|
+
else if ((0, types_1.isUnpackedTypeVarTuple)(typeArg.type)) {
|
|
640
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), typeArg.node);
|
|
641
|
+
}
|
|
642
|
+
else if ((0, types_1.isUnpackedClass)(typeArg.type)) {
|
|
643
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedArgInTypeArgument(), typeArg.node);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
|
|
649
|
+
}
|
|
650
|
+
function createAnnotatedType(classType, errorNode, typeArgs, flags) {
|
|
651
|
+
let type;
|
|
652
|
+
const typeExprFlags = 256 /* EvalFlags.TypeExpression */ | 33554432 /* EvalFlags.NoConvertSpecialForm */;
|
|
653
|
+
if ((flags & typeExprFlags) === 0) {
|
|
654
|
+
type = types_1.ClassType.cloneAsInstance(classType);
|
|
655
|
+
if (typeArgs && typeArgs.length >= 1 && typeArgs[0].type.props?.typeForm) {
|
|
656
|
+
type = types_1.TypeBase.cloneWithTypeForm(type, typeArgs[0].type.props.typeForm);
|
|
657
|
+
}
|
|
658
|
+
return { type };
|
|
659
|
+
}
|
|
660
|
+
if (typeArgs && typeArgs.length > 0) {
|
|
661
|
+
type = typeArgs[0].type;
|
|
662
|
+
if (typeArgs.length < 2) {
|
|
663
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.annotatedTypeArgMissing(), errorNode);
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
type = validateAnnotatedMetadata(errorNode, typeArgs[0].type, typeArgs.slice(1));
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
if (!type || !typeArgs || typeArgs.length === 0) {
|
|
670
|
+
return { type: types_1.AnyType.create() };
|
|
671
|
+
}
|
|
672
|
+
if (typeArgs[0].typeList) {
|
|
673
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArgs[0].node);
|
|
674
|
+
}
|
|
675
|
+
return {
|
|
676
|
+
type: types_1.TypeBase.cloneAsSpecialForm(type, types_1.ClassType.cloneAsInstance(classType)),
|
|
677
|
+
isReadOnly: typeArgs[0].isReadOnly,
|
|
678
|
+
isRequired: typeArgs[0].isRequired,
|
|
679
|
+
isNotRequired: typeArgs[0].isNotRequired,
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
function validateAnnotatedMetadata(errorNode, baseType, metaArgs) {
|
|
683
|
+
for (const metaArg of metaArgs) {
|
|
684
|
+
validateTypeMetadata(errorNode, baseType, metaArg);
|
|
685
|
+
}
|
|
686
|
+
return baseType;
|
|
687
|
+
}
|
|
688
|
+
function createSpecialType(classType, typeArgs, paramLimit, allowParamSpec = false, isSpecialForm = true) {
|
|
689
|
+
const isTupleTypeParam = types_1.ClassType.isTupleClass(classType);
|
|
690
|
+
if (typeArgs) {
|
|
691
|
+
if (isTupleTypeParam && typeArgs.length === 1 && typeArgs[0].isEmptyTupleShorthand) {
|
|
692
|
+
typeArgs = [];
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
let sawUnpacked = false;
|
|
696
|
+
const noteSawUnpacked = (typeArg) => {
|
|
697
|
+
if (sawUnpacked) {
|
|
698
|
+
if (!reportedUnpackedError) {
|
|
699
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeArgsTooMany(), typeArg.node);
|
|
700
|
+
reportedUnpackedError = true;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
sawUnpacked = true;
|
|
704
|
+
};
|
|
705
|
+
let reportedUnpackedError = false;
|
|
706
|
+
// Verify that we didn't receive any inappropriate types.
|
|
707
|
+
typeArgs.forEach((typeArg, index) => {
|
|
708
|
+
(0, debug_1.assert)(typeArgs !== undefined);
|
|
709
|
+
if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
|
|
710
|
+
if (!isTupleTypeParam) {
|
|
711
|
+
if (!allowParamSpec) {
|
|
712
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), typeArg.node);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
else if (typeArgs.length !== 2 || index !== 1) {
|
|
716
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisSecondArg(), typeArg.node);
|
|
717
|
+
}
|
|
718
|
+
else {
|
|
719
|
+
if ((0, types_1.isTypeVarTuple)(typeArgs[0].type) && !typeArgs[0].type.priv.isInUnion) {
|
|
720
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeVarTupleContext(), typeArgs[0].node);
|
|
721
|
+
}
|
|
722
|
+
else if ((0, types_1.isUnpackedClass)(typeArgs[0].type)) {
|
|
723
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisAfterUnpacked(), typeArg.node);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
else if ((0, types_1.isParamSpec)(typeArg.type) && allowParamSpec) {
|
|
728
|
+
// Nothing to do - this is allowed.
|
|
729
|
+
}
|
|
730
|
+
else if (paramLimit === undefined && (0, types_1.isTypeVarTuple)(typeArg.type)) {
|
|
731
|
+
if (!typeArg.type.priv.isInUnion) {
|
|
732
|
+
noteSawUnpacked(typeArg);
|
|
733
|
+
}
|
|
734
|
+
validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node);
|
|
735
|
+
}
|
|
736
|
+
else if (paramLimit === undefined && (0, types_1.isUnpackedClass)(typeArg.type)) {
|
|
737
|
+
if ((0, typeUtils_1.isUnboundedTupleClass)(typeArg.type)) {
|
|
738
|
+
noteSawUnpacked(typeArg);
|
|
739
|
+
}
|
|
740
|
+
validateTypeArg(typeArg, { allowUnpackedTuples: true });
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
validateTypeArg(typeArg);
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
let typeArgTypes = typeArgs ? typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type)) : [];
|
|
749
|
+
// Make sure the argument list count is correct.
|
|
750
|
+
if (paramLimit !== undefined) {
|
|
751
|
+
if (typeArgs && typeArgTypes.length > paramLimit) {
|
|
752
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsTooMany().format({
|
|
753
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
754
|
+
expected: paramLimit,
|
|
755
|
+
received: typeArgTypes.length,
|
|
756
|
+
}), typeArgs[paramLimit].node);
|
|
757
|
+
typeArgTypes = typeArgTypes.slice(0, paramLimit);
|
|
758
|
+
}
|
|
759
|
+
else if (typeArgTypes.length < paramLimit) {
|
|
760
|
+
// Fill up the remainder of the slots with unknown types.
|
|
761
|
+
while (typeArgTypes.length < paramLimit) {
|
|
762
|
+
typeArgTypes.push(types_1.UnknownType.create());
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
// Handle tuple type params as a special case.
|
|
767
|
+
let returnType;
|
|
768
|
+
if (isTupleTypeParam) {
|
|
769
|
+
const tupleTypeArgTypes = [];
|
|
770
|
+
// If no type args are provided and it's a tuple, default to [Unknown, ...].
|
|
771
|
+
if (!typeArgs) {
|
|
772
|
+
tupleTypeArgTypes.push({ type: types_1.UnknownType.create(), isUnbounded: true });
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
typeArgs.forEach((typeArg, index) => {
|
|
776
|
+
if (index === 1 && (0, typeUtils_1.isEllipsisType)(typeArgTypes[index])) {
|
|
777
|
+
if (tupleTypeArgTypes.length === 1 && !tupleTypeArgTypes[0].isUnbounded) {
|
|
778
|
+
tupleTypeArgTypes[0] = { type: tupleTypeArgTypes[0].type, isUnbounded: true };
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
else if ((0, types_1.isUnpackedClass)(typeArg.type) && typeArg.type.priv.tupleTypeArgs) {
|
|
782
|
+
(0, collectionUtils_1.appendArray)(tupleTypeArgTypes, typeArg.type.priv.tupleTypeArgs);
|
|
783
|
+
}
|
|
784
|
+
else {
|
|
785
|
+
tupleTypeArgTypes.push({ type: typeArgTypes[index], isUnbounded: false });
|
|
786
|
+
}
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
returnType = (0, typeUtils_1.specializeTupleClass)(classType, tupleTypeArgTypes, typeArgs !== undefined);
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
returnType = types_1.ClassType.specialize(classType, typeArgTypes, typeArgs !== undefined);
|
|
793
|
+
}
|
|
794
|
+
if (isSpecialForm) {
|
|
795
|
+
returnType = types_1.TypeBase.cloneAsSpecialForm(returnType, classType);
|
|
796
|
+
}
|
|
797
|
+
return returnType;
|
|
798
|
+
}
|
|
799
|
+
function createUnionType(classType, errorNode, typeArgs, flags) {
|
|
800
|
+
const fileInfo = nodeInfo.getFileInfo(errorNode);
|
|
801
|
+
const types = [];
|
|
802
|
+
let allowSingleTypeArg = false;
|
|
803
|
+
let isValidTypeForm = true;
|
|
804
|
+
if (!typeArgs) {
|
|
805
|
+
// If no type arguments are provided, the resulting type
|
|
806
|
+
// depends on whether we're evaluating a type annotation or
|
|
807
|
+
// we're in some other context.
|
|
808
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
809
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unionTypeArgCount(), errorNode);
|
|
810
|
+
return types_1.NeverType.createNever();
|
|
811
|
+
}
|
|
812
|
+
return classType;
|
|
813
|
+
}
|
|
814
|
+
for (const typeArg of typeArgs) {
|
|
815
|
+
let typeArgType = typeArg.type;
|
|
816
|
+
// This is an experimental feature because Unions of unpacked TypeVarTuples are not officially supported.
|
|
817
|
+
if (!validateTypeArg(typeArg, {
|
|
818
|
+
allowTypeVarTuple: fileInfo.diagnosticRuleSet.enableExperimentalFeatures,
|
|
819
|
+
})) {
|
|
820
|
+
typeArgType = types_1.UnknownType.create();
|
|
821
|
+
}
|
|
822
|
+
if ((0, types_1.isTypeVar)(typeArgType) && (0, types_1.isUnpackedTypeVarTuple)(typeArgType)) {
|
|
823
|
+
// This is an experimental feature because Unions of unpacked TypeVarTuples are not officially supported.
|
|
824
|
+
if (fileInfo.diagnosticRuleSet.enableExperimentalFeatures) {
|
|
825
|
+
// If this is an unpacked TypeVar, note that it is in a union so we can
|
|
826
|
+
// differentiate between Unpack[Vs] and Union[Unpack[Vs]].
|
|
827
|
+
typeArgType = types_1.TypeVarType.cloneForUnpacked(typeArgType, /* isInUnion */ true);
|
|
828
|
+
allowSingleTypeArg = true;
|
|
829
|
+
}
|
|
830
|
+
else {
|
|
831
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.unionUnpackedTypeVarTuple(), errorNode);
|
|
832
|
+
typeArgType = types_1.UnknownType.create();
|
|
833
|
+
isValidTypeForm = false;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
types.push(typeArgType);
|
|
837
|
+
}
|
|
838
|
+
// Validate that we received at least two type arguments. One type argument
|
|
839
|
+
// is allowed if it's an unpacked TypeVarTuple or tuple. None is also allowed
|
|
840
|
+
// since it is used to define NoReturn in typeshed stubs). No type arguments,
|
|
841
|
+
// as in "Union[()]", is a runtime error.
|
|
842
|
+
if (types.length === 0 || (types.length === 1 && !allowSingleTypeArg && !(0, typeUtils_1.isNoneInstance)(types[0]))) {
|
|
843
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
844
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.unionTypeArgCount(), errorNode);
|
|
845
|
+
}
|
|
846
|
+
isValidTypeForm = false;
|
|
847
|
+
}
|
|
848
|
+
let unionType = (0, types_1.combineTypes)(types, { skipElideRedundantLiterals: true });
|
|
849
|
+
if (context.prefetched?.unionTypeClass && (0, types_1.isInstantiableClass)(context.prefetched.unionTypeClass)) {
|
|
850
|
+
unionType = types_1.TypeBase.cloneAsSpecialForm(unionType, types_1.ClassType.cloneAsInstance(context.prefetched.unionTypeClass));
|
|
851
|
+
}
|
|
852
|
+
if (!isValidTypeForm || types.some((t) => !t.props?.typeForm)) {
|
|
853
|
+
if (unionType.props?.typeForm) {
|
|
854
|
+
unionType = types_1.TypeBase.cloneWithTypeForm(unionType, undefined);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
else {
|
|
858
|
+
const typeFormType = (0, types_1.combineTypes)(types.map((t) => t.props.typeForm));
|
|
859
|
+
unionType = types_1.TypeBase.cloneWithTypeForm(unionType, typeFormType);
|
|
860
|
+
}
|
|
861
|
+
return unionType;
|
|
862
|
+
}
|
|
863
|
+
function createGenericType(classType, errorNode, typeArgs, flags) {
|
|
864
|
+
if (!typeArgs) {
|
|
865
|
+
// If no type arguments are provided, the resulting type
|
|
866
|
+
// depends on whether we're evaluating a type annotation or
|
|
867
|
+
// we're in some other context.
|
|
868
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 262144 /* EvalFlags.NoNakedGeneric */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
869
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgMissing(), errorNode);
|
|
870
|
+
}
|
|
871
|
+
return (flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0 ? types_1.UnknownType.create() : classType;
|
|
872
|
+
}
|
|
873
|
+
if ((flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0) {
|
|
874
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericNotAllowed(), errorNode);
|
|
875
|
+
return types_1.UnknownType.create();
|
|
876
|
+
}
|
|
877
|
+
const uniqueTypeVars = [];
|
|
878
|
+
if (typeArgs) {
|
|
879
|
+
// Make sure there's at least one type arg.
|
|
880
|
+
if (typeArgs.length === 0) {
|
|
881
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgMissing(), errorNode);
|
|
882
|
+
}
|
|
883
|
+
// Make sure that all of the type args are typeVars and are unique.
|
|
884
|
+
typeArgs.forEach((typeArg) => {
|
|
885
|
+
if (!(0, types_1.isTypeVar)(typeArg.type)) {
|
|
886
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgTypeVar(), typeArg.node);
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
if (uniqueTypeVars.some((t) => (0, types_1.isTypeSame)(t, typeArg.type))) {
|
|
890
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeArgUnique(), typeArg.node);
|
|
891
|
+
}
|
|
892
|
+
uniqueTypeVars.push(typeArg.type);
|
|
893
|
+
}
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
return createSpecialType(classType, typeArgs, /* paramLimit */ undefined, /* allowParamSpec */ true);
|
|
897
|
+
}
|
|
898
|
+
function transformTypeForTypeAlias(type, errorNode, typeAliasPlaceholder, isPep695TypeVarType, typeParamNodes) {
|
|
899
|
+
// If this is a recursive type alias that hasn't yet been fully resolved
|
|
900
|
+
// (i.e. there is no boundType associated with it), don't apply the transform.
|
|
901
|
+
if ((0, typeUtils_1.isTypeAliasPlaceholder)(type)) {
|
|
902
|
+
return type;
|
|
903
|
+
}
|
|
904
|
+
const sharedInfo = typeAliasPlaceholder.shared.recursiveAlias;
|
|
905
|
+
(0, debug_1.assert)(sharedInfo !== undefined);
|
|
906
|
+
let typeParams = sharedInfo.typeParams;
|
|
907
|
+
if (!typeParams) {
|
|
908
|
+
// Determine if there are any generic type parameters associated
|
|
909
|
+
// with this type alias.
|
|
910
|
+
typeParams = [];
|
|
911
|
+
(0, typeUtils_1.addTypeVarsToListIfUnique)(typeParams, (0, typeUtils_1.getTypeVarArgsRecursive)(type));
|
|
912
|
+
// Don't include any synthesized type variables.
|
|
913
|
+
typeParams = typeParams.filter((typeVar) => !typeVar.shared.isSynthesized);
|
|
914
|
+
}
|
|
915
|
+
// Convert all type variables to instances.
|
|
916
|
+
typeParams = typeParams.map((typeVar) => {
|
|
917
|
+
if (types_1.TypeBase.isInstance(typeVar)) {
|
|
918
|
+
return typeVar;
|
|
919
|
+
}
|
|
920
|
+
return (0, typeUtils_1.convertToInstance)(typeVar);
|
|
921
|
+
});
|
|
922
|
+
// See if the type alias includes a TypeVarTuple followed by a TypeVar
|
|
923
|
+
// with a default value. This isn't allowed.
|
|
924
|
+
const firstTypeVarTupleIndex = typeParams.findIndex((typeVar) => (0, types_1.isTypeVarTuple)(typeVar));
|
|
925
|
+
if (firstTypeVarTupleIndex >= 0) {
|
|
926
|
+
const typeVarWithDefaultIndex = typeParams.findIndex((typeVar, index) => index > firstTypeVarTupleIndex && !(0, types_1.isParamSpec)(typeVar) && typeVar.shared.isDefaultExplicit);
|
|
927
|
+
if (typeVarWithDefaultIndex >= 0) {
|
|
928
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarWithDefaultFollowsVariadic().format({
|
|
929
|
+
typeVarName: typeParams[typeVarWithDefaultIndex].shared.name,
|
|
930
|
+
variadicName: typeParams[firstTypeVarTupleIndex].shared.name,
|
|
931
|
+
}), typeParamNodes ? typeParamNodes[typeVarWithDefaultIndex].d.name : errorNode);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
// Validate the default types for all type parameters.
|
|
935
|
+
typeParams.forEach((typeParam, index) => {
|
|
936
|
+
(0, debug_1.assert)(typeParams !== undefined);
|
|
937
|
+
let bestErrorNode = errorNode;
|
|
938
|
+
if (typeParamNodes && index < typeParamNodes.length) {
|
|
939
|
+
bestErrorNode = typeParamNodes[index].d.defaultExpr ?? typeParamNodes[index].d.name;
|
|
940
|
+
}
|
|
941
|
+
validateTypeParamDefault(bestErrorNode, typeParam, typeParams.slice(0, index), sharedInfo.typeVarScopeId);
|
|
942
|
+
});
|
|
943
|
+
// Verify that we have at most one TypeVarTuple.
|
|
944
|
+
const variadics = typeParams.filter((param) => (0, types_1.isTypeVarTuple)(param));
|
|
945
|
+
if (variadics.length > 1) {
|
|
946
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.variadicTypeParamTooManyAlias().format({
|
|
947
|
+
names: variadics.map((v) => `"${v.shared.name}"`).join(', '),
|
|
948
|
+
}), errorNode);
|
|
949
|
+
}
|
|
950
|
+
if (!sharedInfo.isTypeAliasType && !isPep695TypeVarType) {
|
|
951
|
+
const boundTypeVars = typeParams.filter((typeVar) => typeVar.priv.scopeId !== sharedInfo.typeVarScopeId &&
|
|
952
|
+
typeVar.priv.scopeType === 0 /* TypeVarScopeType.Class */);
|
|
953
|
+
if (boundTypeVars.length > 0) {
|
|
954
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.genericTypeAliasBoundTypeVar().format({
|
|
955
|
+
names: boundTypeVars.map((t) => `${t.shared.name}`).join(', '),
|
|
956
|
+
}), errorNode);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
if (!types_1.TypeBase.isInstantiable(type)) {
|
|
960
|
+
return type;
|
|
961
|
+
}
|
|
962
|
+
sharedInfo.typeParams = typeParams.length > 0 ? typeParams : undefined;
|
|
963
|
+
let typeAlias = types_1.TypeBase.cloneForTypeAlias(type, {
|
|
964
|
+
shared: sharedInfo,
|
|
965
|
+
typeArgs: undefined,
|
|
966
|
+
});
|
|
967
|
+
// All PEP 695 type aliases are special forms because they are
|
|
968
|
+
// TypeAliasType objects at runtime.
|
|
969
|
+
if (sharedInfo.isTypeAliasType || isPep695TypeVarType) {
|
|
970
|
+
const typeAliasTypeClass = getTypingType(errorNode, 'TypeAliasType');
|
|
971
|
+
if (typeAliasTypeClass && (0, types_1.isInstantiableClass)(typeAliasTypeClass)) {
|
|
972
|
+
typeAlias = types_1.TypeBase.cloneAsSpecialForm(typeAlias, types_1.ClassType.cloneAsInstance(typeAliasTypeClass));
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
// Delete the TypeForm info. The type alias serves as its own TypeForm info.
|
|
976
|
+
if (typeAlias.props?.typeForm) {
|
|
977
|
+
typeAlias = types_1.TypeBase.cloneWithTypeForm(typeAlias, undefined);
|
|
978
|
+
}
|
|
979
|
+
return typeAlias;
|
|
980
|
+
}
|
|
981
|
+
function validateTypeParamDefault(errorNode, typeParam, otherLiveTypeParams, scopeId) {
|
|
982
|
+
if (!typeParam.shared.isDefaultExplicit && !typeParam.shared.isSynthesized && !types_1.TypeVarType.isSelf(typeParam)) {
|
|
983
|
+
const typeVarWithDefault = otherLiveTypeParams.find((param) => param.shared.isDefaultExplicit && param.priv.scopeId === scopeId);
|
|
984
|
+
if (typeVarWithDefault) {
|
|
985
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarWithoutDefault().format({
|
|
986
|
+
name: typeParam.shared.name,
|
|
987
|
+
other: typeVarWithDefault.shared.name,
|
|
988
|
+
}), errorNode);
|
|
989
|
+
}
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
const invalidTypeVars = new Set();
|
|
993
|
+
(0, typeUtils_1.validateTypeVarDefault)(typeParam, otherLiveTypeParams, invalidTypeVars);
|
|
994
|
+
// If we found one or more unapplied type variable, report an error.
|
|
995
|
+
if (invalidTypeVars.size > 0) {
|
|
996
|
+
const diag = new diagnostic_1.DiagnosticAddendum();
|
|
997
|
+
invalidTypeVars.forEach((name) => {
|
|
998
|
+
diag.addMessage(localize_1.LocAddendum.typeVarDefaultOutOfScope().format({ name }));
|
|
999
|
+
});
|
|
1000
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarDefaultInvalidTypeVar().format({
|
|
1001
|
+
name: typeParam.shared.name,
|
|
1002
|
+
}) + diag.getString(), errorNode);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
function verifyGenericTypeParams(errorNode, typeVars, genericTypeVars) {
|
|
1006
|
+
const missingFromGeneric = typeVars.filter((typeVar) => {
|
|
1007
|
+
return !genericTypeVars.some((genericTypeVar) => genericTypeVar.shared.name === typeVar.shared.name);
|
|
1008
|
+
});
|
|
1009
|
+
if (missingFromGeneric.length > 0) {
|
|
1010
|
+
const diag = new diagnostic_1.DiagnosticAddendum();
|
|
1011
|
+
diag.addMessage(localize_1.LocAddendum.typeVarsMissing().format({
|
|
1012
|
+
names: missingFromGeneric.map((typeVar) => `"${typeVar.shared.name}"`).join(', '),
|
|
1013
|
+
}));
|
|
1014
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.typeVarsNotInGenericOrProtocol() + diag.getString(), errorNode);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
function transformTypeArgsForParamSpec(typeParams, typeArgs, errorNode) {
|
|
1018
|
+
if (typeParams.length !== 1 || !(0, types_1.isParamSpec)(typeParams[0]) || !typeArgs) {
|
|
1019
|
+
return typeArgs;
|
|
1020
|
+
}
|
|
1021
|
+
if (typeArgs.length > 1) {
|
|
1022
|
+
for (const typeArg of typeArgs) {
|
|
1023
|
+
if ((0, types_1.isParamSpec)(typeArg.type)) {
|
|
1024
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.paramSpecContext(), typeArg.node);
|
|
1025
|
+
return undefined;
|
|
1026
|
+
}
|
|
1027
|
+
if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
|
|
1028
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.ellipsisContext(), typeArg.node);
|
|
1029
|
+
return undefined;
|
|
1030
|
+
}
|
|
1031
|
+
if ((0, types_1.isInstantiableClass)(typeArg.type) && types_1.ClassType.isBuiltIn(typeArg.type, 'Concatenate')) {
|
|
1032
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.concatenateContext(), typeArg.node);
|
|
1033
|
+
return undefined;
|
|
1034
|
+
}
|
|
1035
|
+
if (typeArg.typeList) {
|
|
1036
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgListNotAllowed(), typeArg.node);
|
|
1037
|
+
return undefined;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
if (typeArgs.length === 1) {
|
|
1042
|
+
// Don't transform a type list.
|
|
1043
|
+
if (typeArgs[0].typeList) {
|
|
1044
|
+
return typeArgs;
|
|
1045
|
+
}
|
|
1046
|
+
const typeArgType = typeArgs[0].type;
|
|
1047
|
+
// Don't transform a single ParamSpec or ellipsis.
|
|
1048
|
+
if ((0, types_1.isParamSpec)(typeArgType) || (0, typeUtils_1.isEllipsisType)(typeArgType)) {
|
|
1049
|
+
return typeArgs;
|
|
1050
|
+
}
|
|
1051
|
+
// Don't transform a Concatenate.
|
|
1052
|
+
if ((0, types_1.isInstantiableClass)(typeArgType) && types_1.ClassType.isBuiltIn(typeArgType, 'Concatenate')) {
|
|
1053
|
+
return typeArgs;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
// Package up the type arguments into a type list.
|
|
1057
|
+
return [
|
|
1058
|
+
{
|
|
1059
|
+
type: types_1.UnknownType.create(),
|
|
1060
|
+
node: typeArgs.length > 0 ? typeArgs[0].node : errorNode,
|
|
1061
|
+
typeList: typeArgs,
|
|
1062
|
+
},
|
|
1063
|
+
];
|
|
1064
|
+
}
|
|
1065
|
+
function createSpecializedClassType(classType, typeArgs, flags, errorNode) {
|
|
1066
|
+
let isValidTypeForm = true;
|
|
1067
|
+
// Handle the special-case classes that are not defined
|
|
1068
|
+
// in the type stubs.
|
|
1069
|
+
if (types_1.ClassType.isSpecialBuiltIn(classType)) {
|
|
1070
|
+
const aliasedName = classType.priv.aliasName || classType.shared.name;
|
|
1071
|
+
switch (aliasedName) {
|
|
1072
|
+
case 'Callable': {
|
|
1073
|
+
return { type: createCallableType(classType, typeArgs, errorNode) };
|
|
1074
|
+
}
|
|
1075
|
+
case 'Never':
|
|
1076
|
+
case 'NoReturn': {
|
|
1077
|
+
if (typeArgs && typeArgs.length > 0) {
|
|
1078
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeArgsExpectingNone().format({ name: aliasedName }), typeArgs[0].node);
|
|
1079
|
+
}
|
|
1080
|
+
let resultType = aliasedName === 'Never' ? types_1.NeverType.createNever() : types_1.NeverType.createNoReturn();
|
|
1081
|
+
resultType = types_1.TypeBase.cloneAsSpecialForm(resultType, classType);
|
|
1082
|
+
resultType = types_1.TypeBase.cloneWithTypeForm(resultType, (0, typeUtils_1.convertToInstance)(resultType));
|
|
1083
|
+
return { type: resultType };
|
|
1084
|
+
}
|
|
1085
|
+
case 'Optional': {
|
|
1086
|
+
return { type: createOptionalType(classType, errorNode, typeArgs, flags) };
|
|
1087
|
+
}
|
|
1088
|
+
case 'Type': {
|
|
1089
|
+
let typeType = createSpecialType(classType, typeArgs, 1,
|
|
1090
|
+
/* allowParamSpec */ undefined,
|
|
1091
|
+
/* isSpecialForm */ false);
|
|
1092
|
+
if ((0, types_1.isInstantiableClass)(typeType)) {
|
|
1093
|
+
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
|
1094
|
+
}
|
|
1095
|
+
typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
|
|
1096
|
+
return { type: typeType };
|
|
1097
|
+
}
|
|
1098
|
+
case 'ClassVar': {
|
|
1099
|
+
return { type: createClassVarType(classType, errorNode, typeArgs, flags) };
|
|
1100
|
+
}
|
|
1101
|
+
case 'Protocol': {
|
|
1102
|
+
if ((flags &
|
|
1103
|
+
(67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !==
|
|
1104
|
+
0) {
|
|
1105
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.protocolNotAllowed(), errorNode);
|
|
1106
|
+
}
|
|
1107
|
+
typeArgs?.forEach((typeArg) => {
|
|
1108
|
+
if (typeArg.typeList || !(0, types_1.isTypeVar)(typeArg.type)) {
|
|
1109
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.protocolTypeArgMustBeTypeParam(), typeArg.node);
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
return {
|
|
1113
|
+
type: createSpecialType(classType, typeArgs,
|
|
1114
|
+
/* paramLimit */ undefined,
|
|
1115
|
+
/* allowParamSpec */ true),
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
case 'TypedDict': {
|
|
1119
|
+
if ((flags &
|
|
1120
|
+
(67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !==
|
|
1121
|
+
0) {
|
|
1122
|
+
const isInlinedTypedDict = nodeInfo.getFileInfo(errorNode).diagnosticRuleSet.enableExperimentalFeatures && !!typeArgs;
|
|
1123
|
+
if (!isInlinedTypedDict) {
|
|
1124
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typedDictNotAllowed(), errorNode);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
isValidTypeForm = false;
|
|
1128
|
+
break;
|
|
1129
|
+
}
|
|
1130
|
+
case 'Literal': {
|
|
1131
|
+
if ((flags &
|
|
1132
|
+
(67108864 /* EvalFlags.NoNonTypeSpecialForms */ | 256 /* EvalFlags.TypeExpression */ | 1073741824 /* EvalFlags.TypeFormArg */)) !==
|
|
1133
|
+
0) {
|
|
1134
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalNotAllowed(), errorNode);
|
|
1135
|
+
}
|
|
1136
|
+
isValidTypeForm = false;
|
|
1137
|
+
break;
|
|
1138
|
+
}
|
|
1139
|
+
case 'TypeAlias': {
|
|
1140
|
+
if ((flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0) {
|
|
1141
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeAnnotationVariable(), errorNode);
|
|
1142
|
+
}
|
|
1143
|
+
isValidTypeForm = false;
|
|
1144
|
+
break;
|
|
1145
|
+
}
|
|
1146
|
+
case 'Tuple': {
|
|
1147
|
+
return {
|
|
1148
|
+
type: createSpecialType(classType, typeArgs,
|
|
1149
|
+
/* paramLimit */ undefined,
|
|
1150
|
+
/* allowParamSpec */ false,
|
|
1151
|
+
/* isSpecialForm */ false),
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
case 'Union': {
|
|
1155
|
+
return { type: createUnionType(classType, errorNode, typeArgs, flags) };
|
|
1156
|
+
}
|
|
1157
|
+
case 'Generic': {
|
|
1158
|
+
return { type: createGenericType(classType, errorNode, typeArgs, flags) };
|
|
1159
|
+
}
|
|
1160
|
+
case 'Final': {
|
|
1161
|
+
return { type: createFinalType(classType, errorNode, typeArgs, flags) };
|
|
1162
|
+
}
|
|
1163
|
+
case 'Annotated': {
|
|
1164
|
+
return createAnnotatedType(classType, errorNode, typeArgs, flags);
|
|
1165
|
+
}
|
|
1166
|
+
case 'Concatenate': {
|
|
1167
|
+
return { type: createConcatenateType(classType, errorNode, typeArgs, flags) };
|
|
1168
|
+
}
|
|
1169
|
+
case 'TypeGuard':
|
|
1170
|
+
case 'TypeIs': {
|
|
1171
|
+
return { type: createTypeGuardType(classType, errorNode, typeArgs, flags) };
|
|
1172
|
+
}
|
|
1173
|
+
case 'Unpack': {
|
|
1174
|
+
return { type: createUnpackType(classType, errorNode, typeArgs, flags) };
|
|
1175
|
+
}
|
|
1176
|
+
case 'Required':
|
|
1177
|
+
case 'NotRequired': {
|
|
1178
|
+
return createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags);
|
|
1179
|
+
}
|
|
1180
|
+
case 'ReadOnly': {
|
|
1181
|
+
return createRequiredOrReadOnlyType(classType, errorNode, typeArgs, flags);
|
|
1182
|
+
}
|
|
1183
|
+
case 'Self': {
|
|
1184
|
+
return { type: createSelfType(classType, errorNode, typeArgs, flags) };
|
|
1185
|
+
}
|
|
1186
|
+
case 'LiteralString': {
|
|
1187
|
+
return { type: createSpecialType(classType, typeArgs, 0) };
|
|
1188
|
+
}
|
|
1189
|
+
case 'TypeForm': {
|
|
1190
|
+
return { type: createTypeFormType(classType, errorNode, typeArgs) };
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
const fileInfo = nodeInfo.getFileInfo(errorNode);
|
|
1195
|
+
if (fileInfo.isStubFile ||
|
|
1196
|
+
pythonVersion_1.PythonVersion.isGreaterOrEqualTo(fileInfo.executionEnvironment.pythonVersion, pythonVersion_1.pythonVersion3_9) ||
|
|
1197
|
+
(0, analyzerFileInfo_1.isAnnotationEvaluationPostponed)(nodeInfo.getFileInfo(errorNode)) ||
|
|
1198
|
+
(flags & 4 /* EvalFlags.ForwardRefs */) !== 0) {
|
|
1199
|
+
// Handle "type" specially, since it needs to act like "Type"
|
|
1200
|
+
// in Python 3.9 and newer.
|
|
1201
|
+
if (types_1.ClassType.isBuiltIn(classType, 'type') && typeArgs) {
|
|
1202
|
+
if (typeArgs.length >= 1) {
|
|
1203
|
+
// Treat type[function] as illegal.
|
|
1204
|
+
if ((0, types_1.isFunctionOrOverloaded)(typeArgs[0].type)) {
|
|
1205
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.typeAnnotationWithCallable(), typeArgs[0].node);
|
|
1206
|
+
return { type: types_1.UnknownType.create() };
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
if (context.prefetched?.typeClass && (0, types_1.isInstantiableClass)(context.prefetched.typeClass)) {
|
|
1210
|
+
let typeType = createSpecialType(context.prefetched.typeClass, typeArgs, 1,
|
|
1211
|
+
/* allowParamSpec */ undefined,
|
|
1212
|
+
/* isSpecialForm */ false);
|
|
1213
|
+
if ((0, types_1.isInstantiableClass)(typeType)) {
|
|
1214
|
+
typeType = (0, typeUtils_1.explodeGenericClass)(typeType);
|
|
1215
|
+
}
|
|
1216
|
+
typeType = types_1.TypeBase.cloneWithTypeForm(typeType, (0, typeUtils_1.convertToInstance)(typeType));
|
|
1217
|
+
return { type: typeType };
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
// Handle "tuple" specially, since it needs to act like "Tuple"
|
|
1221
|
+
// in Python 3.9 and newer.
|
|
1222
|
+
if ((0, typeUtils_1.isTupleClass)(classType)) {
|
|
1223
|
+
let specializedClass = createSpecialType(classType, typeArgs,
|
|
1224
|
+
/* paramLimit */ undefined,
|
|
1225
|
+
/* allowParamSpec */ undefined,
|
|
1226
|
+
/* isSpecialForm */ false);
|
|
1227
|
+
specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, (0, typeUtils_1.convertToInstance)(specializedClass));
|
|
1228
|
+
return { type: specializedClass };
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
let typeArgCount = typeArgs ? typeArgs.length : 0;
|
|
1232
|
+
// Make sure the argument list count is correct.
|
|
1233
|
+
const typeParams = types_1.ClassType.isPseudoGenericClass(classType) ? [] : types_1.ClassType.getTypeParams(classType);
|
|
1234
|
+
// If there are no type parameters or args, the class is already specialized.
|
|
1235
|
+
// No need to do any more work.
|
|
1236
|
+
if (typeParams.length === 0 && typeArgCount === 0) {
|
|
1237
|
+
return { type: classType };
|
|
1238
|
+
}
|
|
1239
|
+
const variadicTypeParamIndex = typeParams.findIndex((param) => (0, types_1.isTypeVarTuple)(param));
|
|
1240
|
+
if (typeArgs) {
|
|
1241
|
+
let minTypeArgCount = typeParams.length;
|
|
1242
|
+
const firstDefaultParamIndex = typeParams.findIndex((param) => !!param.shared.isDefaultExplicit);
|
|
1243
|
+
if (firstDefaultParamIndex >= 0) {
|
|
1244
|
+
minTypeArgCount = firstDefaultParamIndex;
|
|
1245
|
+
}
|
|
1246
|
+
// Classes that accept inlined type dict type args allow only one.
|
|
1247
|
+
if (typeArgs.length > 0 && typeArgs[0].inlinedTypeDict) {
|
|
1248
|
+
if (typeArgs.length > 1) {
|
|
1249
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooMany().format({
|
|
1250
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
1251
|
+
expected: 1,
|
|
1252
|
+
received: typeArgCount,
|
|
1253
|
+
}), typeArgs[1].node);
|
|
1254
|
+
}
|
|
1255
|
+
let inlinedTypeDict = typeArgs[0].inlinedTypeDict;
|
|
1256
|
+
if ((flags & 1073741824 /* EvalFlags.TypeFormArg */) !== 0) {
|
|
1257
|
+
inlinedTypeDict = types_1.TypeBase.cloneWithTypeForm(inlinedTypeDict, (0, typeUtils_1.convertToInstance)(inlinedTypeDict));
|
|
1258
|
+
}
|
|
1259
|
+
return { type: inlinedTypeDict };
|
|
1260
|
+
}
|
|
1261
|
+
else if (typeArgCount > typeParams.length) {
|
|
1262
|
+
if (!types_1.ClassType.isPartiallyEvaluated(classType) && !types_1.ClassType.isTupleClass(classType)) {
|
|
1263
|
+
if (typeParams.length === 0) {
|
|
1264
|
+
isValidTypeForm = false;
|
|
1265
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsExpectingNone().format({
|
|
1266
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
1267
|
+
}), typeArgs[typeParams.length].node);
|
|
1268
|
+
}
|
|
1269
|
+
else if (typeParams.length !== 1 || !(0, types_1.isParamSpec)(typeParams[0])) {
|
|
1270
|
+
isValidTypeForm = false;
|
|
1271
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooMany().format({
|
|
1272
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
1273
|
+
expected: typeParams.length,
|
|
1274
|
+
received: typeArgCount,
|
|
1275
|
+
}), typeArgs[typeParams.length].node);
|
|
1276
|
+
}
|
|
1277
|
+
typeArgCount = typeParams.length;
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
else if (typeArgCount < minTypeArgCount) {
|
|
1281
|
+
isValidTypeForm = false;
|
|
1282
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsTooFew().format({
|
|
1283
|
+
name: classType.priv.aliasName || classType.shared.name,
|
|
1284
|
+
expected: minTypeArgCount,
|
|
1285
|
+
received: typeArgCount,
|
|
1286
|
+
}), typeArgs.length > 0 ? typeArgs[0].node.parent : errorNode);
|
|
1287
|
+
}
|
|
1288
|
+
typeArgs.forEach((typeArg, index) => {
|
|
1289
|
+
if (!typeArg.type.props?.typeForm) {
|
|
1290
|
+
isValidTypeForm = false;
|
|
1291
|
+
}
|
|
1292
|
+
if (index === variadicTypeParamIndex) {
|
|
1293
|
+
// The types that make up the tuple that maps to the
|
|
1294
|
+
// TypeVarTuple have already been validated when the tuple
|
|
1295
|
+
// object was created in adjustTypeArgsForTypeVarTuple.
|
|
1296
|
+
if ((0, types_1.isClassInstance)(typeArg.type) && (0, typeUtils_1.isTupleClass)(typeArg.type)) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
if ((0, types_1.isTypeVarTuple)(typeArg.type)) {
|
|
1300
|
+
if (!validateTypeVarTupleIsUnpacked(typeArg.type, typeArg.node)) {
|
|
1301
|
+
isValidTypeForm = false;
|
|
1302
|
+
}
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
const typeParam = index < typeParams.length ? typeParams[index] : undefined;
|
|
1307
|
+
const isParamSpecTarget = typeParam && (0, types_1.isParamSpec)(typeParam);
|
|
1308
|
+
if (!validateTypeArg(typeArg, {
|
|
1309
|
+
allowParamSpec: true,
|
|
1310
|
+
allowTypeArgList: isParamSpecTarget,
|
|
1311
|
+
})) {
|
|
1312
|
+
isValidTypeForm = false;
|
|
1313
|
+
}
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
// Handle ParamSpec arguments and fill in any missing type arguments with Unknown.
|
|
1317
|
+
const fullTypeParams = types_1.ClassType.getTypeParams(classType);
|
|
1318
|
+
typeArgs = transformTypeArgsForParamSpec(fullTypeParams, typeArgs, errorNode);
|
|
1319
|
+
if (!typeArgs) {
|
|
1320
|
+
isValidTypeForm = false;
|
|
1321
|
+
}
|
|
1322
|
+
let typeArgTypes = resolveClassTypeArguments(classType, fullTypeParams, typeArgs, flags);
|
|
1323
|
+
typeArgTypes = typeArgTypes.map((typeArgType, index) => {
|
|
1324
|
+
if (index < typeArgCount) {
|
|
1325
|
+
const diag = new diagnostic_1.DiagnosticAddendum();
|
|
1326
|
+
let adjustedTypeArgType = applyTypeArgToTypeVar(typeParams[index], typeArgType, diag);
|
|
1327
|
+
// Determine if the variance must match.
|
|
1328
|
+
if (adjustedTypeArgType && (flags & 16384 /* EvalFlags.EnforceVarianceConsistency */) !== 0) {
|
|
1329
|
+
const destType = typeParams[index];
|
|
1330
|
+
const declaredVariance = destType.shared.declaredVariance;
|
|
1331
|
+
if (!(0, typeUtils_1.isVarianceOfTypeArgCompatible)(adjustedTypeArgType, declaredVariance)) {
|
|
1332
|
+
diag.addMessage(localize_1.LocAddendum.varianceMismatchForClass().format({
|
|
1333
|
+
typeVarName: printType(adjustedTypeArgType),
|
|
1334
|
+
className: classType.shared.name,
|
|
1335
|
+
}));
|
|
1336
|
+
adjustedTypeArgType = undefined;
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
if (adjustedTypeArgType) {
|
|
1340
|
+
typeArgType = adjustedTypeArgType;
|
|
1341
|
+
}
|
|
1342
|
+
else {
|
|
1343
|
+
// Avoid emitting this error for a partially-constructed class.
|
|
1344
|
+
if (!(0, types_1.isClassInstance)(typeArgType) || !types_1.ClassType.isPartiallyEvaluated(typeArgType)) {
|
|
1345
|
+
(0, debug_1.assert)(typeArgs !== undefined);
|
|
1346
|
+
isValidTypeForm = false;
|
|
1347
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeVarAssignmentMismatch().format({
|
|
1348
|
+
type: printType(typeArgType),
|
|
1349
|
+
name: types_1.TypeVarType.getReadableName(typeParams[index]),
|
|
1350
|
+
}) + diag.getString(), typeArgs[index].node);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
return typeArgType;
|
|
1355
|
+
});
|
|
1356
|
+
// If the class is partially constructed and doesn't yet have
|
|
1357
|
+
// type parameters, assume that the number and types of supplied type
|
|
1358
|
+
// arguments are correct.
|
|
1359
|
+
if (typeArgs && classType.shared.typeParams.length === 0 && types_1.ClassType.isPartiallyEvaluated(classType)) {
|
|
1360
|
+
typeArgTypes = typeArgs.map((t) => (0, typeUtils_1.convertToInstance)(t.type));
|
|
1361
|
+
}
|
|
1362
|
+
let specializedClass = types_1.ClassType.specialize(classType, typeArgTypes, typeArgs !== undefined);
|
|
1363
|
+
specializedClass = types_1.TypeBase.cloneWithTypeForm(specializedClass, isValidTypeForm ? (0, typeUtils_1.convertToInstance)(specializedClass) : undefined);
|
|
1364
|
+
return { type: specializedClass };
|
|
1365
|
+
}
|
|
1366
|
+
function resolveClassTypeArguments(classType, typeParams, typeArgs, flags) {
|
|
1367
|
+
const typeArgTypes = [];
|
|
1368
|
+
const constraints = new constraintTracker_1.ConstraintTracker();
|
|
1369
|
+
typeParams.forEach((typeParam, index) => {
|
|
1370
|
+
if (typeArgs && index < typeArgs.length) {
|
|
1371
|
+
if ((0, types_1.isParamSpec)(typeParam)) {
|
|
1372
|
+
const typeArg = typeArgs[index];
|
|
1373
|
+
const functionType = types_1.FunctionType.createSynthesizedInstance('', 65536 /* FunctionTypeFlags.ParamSpecValue */);
|
|
1374
|
+
if ((0, typeUtils_1.isEllipsisType)(typeArg.type)) {
|
|
1375
|
+
types_1.FunctionType.addDefaultParams(functionType);
|
|
1376
|
+
functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
|
1377
|
+
typeArgTypes.push(functionType);
|
|
1378
|
+
constraints.setBounds(typeParam, functionType);
|
|
1379
|
+
return;
|
|
1380
|
+
}
|
|
1381
|
+
if (typeArg.typeList) {
|
|
1382
|
+
typeArg.typeList.forEach((paramType, paramIndex) => {
|
|
1383
|
+
types_1.FunctionType.addParam(functionType, types_1.FunctionParam.create(0 /* ParamCategory.Simple */, (0, typeUtils_1.convertToInstance)(paramType.type), types_1.FunctionParamFlags.NameSynthesized | types_1.FunctionParamFlags.TypeDeclared, `__p${paramIndex}`));
|
|
1384
|
+
});
|
|
1385
|
+
if (typeArg.typeList.length > 0) {
|
|
1386
|
+
types_1.FunctionType.addPositionOnlyParamSeparator(functionType);
|
|
1387
|
+
}
|
|
1388
|
+
typeArgTypes.push(functionType);
|
|
1389
|
+
constraints.setBounds(typeParam, functionType);
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
if ((0, types_1.isInstantiableClass)(typeArg.type) && types_1.ClassType.isBuiltIn(typeArg.type, 'Concatenate')) {
|
|
1393
|
+
const concatTypeArgs = typeArg.type.priv.typeArgs;
|
|
1394
|
+
if (concatTypeArgs && concatTypeArgs.length > 0) {
|
|
1395
|
+
concatTypeArgs.forEach((typeArg, index) => {
|
|
1396
|
+
if (index === concatTypeArgs.length - 1) {
|
|
1397
|
+
if ((0, types_1.isParamSpec)(typeArg)) {
|
|
1398
|
+
types_1.FunctionType.addParamSpecVariadics(functionType, typeArg);
|
|
1399
|
+
}
|
|
1400
|
+
else if ((0, typeUtils_1.isEllipsisType)(typeArg)) {
|
|
1401
|
+
types_1.FunctionType.addDefaultParams(functionType);
|
|
1402
|
+
functionType.shared.flags |= 32768 /* FunctionTypeFlags.GradualCallableForm */;
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
else {
|
|
1406
|
+
types_1.FunctionType.addParam(functionType, types_1.FunctionParam.create(0 /* ParamCategory.Simple */, typeArg, types_1.FunctionParamFlags.NameSynthesized | types_1.FunctionParamFlags.TypeDeclared, `__p${index}`));
|
|
1407
|
+
}
|
|
1408
|
+
});
|
|
1409
|
+
}
|
|
1410
|
+
typeArgTypes.push(functionType);
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
const typeArgType = convertTypeArgToInstance(typeArgs[index].type, flags);
|
|
1415
|
+
typeArgTypes.push(typeArgType);
|
|
1416
|
+
constraints.setBounds(typeParam, typeArgType);
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
const solvedDefaultType = solveAndApplyConstraints(typeParam, constraints, {
|
|
1420
|
+
replaceUnsolved: {
|
|
1421
|
+
scopeIds: (0, typeUtils_1.getTypeVarScopeIds)(classType),
|
|
1422
|
+
tupleClassType: getTupleClassType(),
|
|
1423
|
+
},
|
|
1424
|
+
});
|
|
1425
|
+
typeArgTypes.push(solvedDefaultType);
|
|
1426
|
+
constraints.setBounds(typeParam, solvedDefaultType);
|
|
1427
|
+
});
|
|
1428
|
+
return typeArgTypes;
|
|
1429
|
+
}
|
|
1430
|
+
function applyTypeArgToTypeVar(destType, srcType, diag) {
|
|
1431
|
+
if ((0, types_1.isAnyOrUnknown)(srcType)) {
|
|
1432
|
+
return srcType;
|
|
1433
|
+
}
|
|
1434
|
+
let effectiveSrcType = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(srcType);
|
|
1435
|
+
if ((0, types_1.isTypeVar)(srcType)) {
|
|
1436
|
+
if ((0, types_1.isTypeSame)(srcType, destType)) {
|
|
1437
|
+
return srcType;
|
|
1438
|
+
}
|
|
1439
|
+
effectiveSrcType = makeTopLevelTypeVarsConcrete(srcType);
|
|
1440
|
+
}
|
|
1441
|
+
// If this is a partially-evaluated class, don't perform any further
|
|
1442
|
+
// checks. Assume in this case that the type is compatible with the
|
|
1443
|
+
// bound or constraint.
|
|
1444
|
+
if ((0, types_1.isClass)(effectiveSrcType) && types_1.ClassType.isPartiallyEvaluated(effectiveSrcType)) {
|
|
1445
|
+
return srcType;
|
|
1446
|
+
}
|
|
1447
|
+
// If there's a bound type, make sure the source is derived from it.
|
|
1448
|
+
if (destType.shared.boundType && !(0, typeUtils_1.isTypeAliasPlaceholder)(effectiveSrcType)) {
|
|
1449
|
+
if (!assignType(destType.shared.boundType, effectiveSrcType, diag.createAddendum(),
|
|
1450
|
+
/* constraints */ undefined)) {
|
|
1451
|
+
// Avoid adding a message that will confuse users if the TypeVar was
|
|
1452
|
+
// synthesized for internal purposes.
|
|
1453
|
+
if (!destType.shared.isSynthesized) {
|
|
1454
|
+
diag.addMessage(localize_1.LocAddendum.typeBound().format({
|
|
1455
|
+
sourceType: printType(effectiveSrcType),
|
|
1456
|
+
destType: printType(destType.shared.boundType),
|
|
1457
|
+
name: types_1.TypeVarType.getReadableName(destType),
|
|
1458
|
+
}));
|
|
1459
|
+
}
|
|
1460
|
+
return undefined;
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
if ((0, types_1.isParamSpec)(destType)) {
|
|
1464
|
+
if ((0, types_1.isParamSpec)(srcType)) {
|
|
1465
|
+
return srcType;
|
|
1466
|
+
}
|
|
1467
|
+
if ((0, types_1.isFunction)(srcType) && types_1.FunctionType.isParamSpecValue(srcType)) {
|
|
1468
|
+
return srcType;
|
|
1469
|
+
}
|
|
1470
|
+
if ((0, types_1.isClassInstance)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'Concatenate')) {
|
|
1471
|
+
return srcType;
|
|
1472
|
+
}
|
|
1473
|
+
diag.addMessage(localize_1.LocAddendum.typeParamSpec().format({
|
|
1474
|
+
type: printType(srcType),
|
|
1475
|
+
name: types_1.TypeVarType.getReadableName(destType),
|
|
1476
|
+
}));
|
|
1477
|
+
return undefined;
|
|
1478
|
+
}
|
|
1479
|
+
if ((0, types_1.isParamSpec)(srcType)) {
|
|
1480
|
+
diag.addMessage(localize_1.LocMessage.paramSpecContext());
|
|
1481
|
+
return undefined;
|
|
1482
|
+
}
|
|
1483
|
+
// If there are no constraints, we're done.
|
|
1484
|
+
const constraints = destType.shared.constraints;
|
|
1485
|
+
if (constraints.length === 0) {
|
|
1486
|
+
return srcType;
|
|
1487
|
+
}
|
|
1488
|
+
if ((0, typeUtils_1.isTypeAliasPlaceholder)(srcType)) {
|
|
1489
|
+
return srcType;
|
|
1490
|
+
}
|
|
1491
|
+
if ((0, types_1.isTypeVar)(srcType) && types_1.TypeVarType.hasConstraints(srcType)) {
|
|
1492
|
+
// Make sure all the source constraint types map to constraint types in the dest.
|
|
1493
|
+
if (srcType.shared.constraints.every((sourceConstraint) => {
|
|
1494
|
+
return constraints.some((destConstraint) => assignType(destConstraint, sourceConstraint));
|
|
1495
|
+
})) {
|
|
1496
|
+
return srcType;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
else {
|
|
1500
|
+
let bestConstraintSoFar;
|
|
1501
|
+
// Try to find the best (narrowest) match among the constraints.
|
|
1502
|
+
for (const constraint of constraints) {
|
|
1503
|
+
if (assignType(constraint, effectiveSrcType)) {
|
|
1504
|
+
// Don't allow Never to match unless the constraint is also explicitly Never.
|
|
1505
|
+
if (!(0, types_1.isNever)(effectiveSrcType) || (0, types_1.isNever)(constraint)) {
|
|
1506
|
+
if (!bestConstraintSoFar || assignType(bestConstraintSoFar, constraint)) {
|
|
1507
|
+
bestConstraintSoFar = constraint;
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
if (bestConstraintSoFar) {
|
|
1513
|
+
return bestConstraintSoFar;
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
diag.addMessage(localize_1.LocAddendum.typeConstrainedTypeVar().format({
|
|
1517
|
+
type: printType(srcType),
|
|
1518
|
+
name: types_1.TypeVarType.getReadableName(destType),
|
|
1519
|
+
}));
|
|
1520
|
+
return undefined;
|
|
1521
|
+
}
|
|
1522
|
+
function createSelfType(classType, errorNode, typeArgs, flags) {
|
|
1523
|
+
// Self doesn't support any type arguments.
|
|
1524
|
+
if (typeArgs && typeArgs.length > 0) {
|
|
1525
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeArguments, localize_1.LocMessage.typeArgsExpectingNone().format({
|
|
1526
|
+
name: classType.shared.name,
|
|
1527
|
+
}), typeArgs[0].node ?? errorNode);
|
|
1528
|
+
}
|
|
1529
|
+
let enclosingClass = ParseTreeUtils.getEnclosingClass(errorNode);
|
|
1530
|
+
// If `Self` appears anywhere outside of the class body (e.g. a decorator,
|
|
1531
|
+
// base class list, metaclass argument, type parameter list), it is
|
|
1532
|
+
// considered illegal.
|
|
1533
|
+
if (enclosingClass && !ParseTreeUtils.isNodeContainedWithin(errorNode, enclosingClass.d.suite)) {
|
|
1534
|
+
enclosingClass = undefined;
|
|
1535
|
+
}
|
|
1536
|
+
const enclosingClassTypeResult = enclosingClass ? getTypeOfClass(enclosingClass) : undefined;
|
|
1537
|
+
if (!enclosingClassTypeResult) {
|
|
1538
|
+
if ((flags & (256 /* EvalFlags.TypeExpression */ | 128 /* EvalFlags.InstantiableType */ | 1073741824 /* EvalFlags.TypeFormArg */)) !== 0) {
|
|
1539
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.selfTypeContext(), errorNode);
|
|
1540
|
+
}
|
|
1541
|
+
return types_1.UnknownType.create();
|
|
1542
|
+
}
|
|
1543
|
+
else if ((0, typeUtils_1.isInstantiableMetaclass)(enclosingClassTypeResult.classType)) {
|
|
1544
|
+
// If `Self` appears within a metaclass, it is considered illegal.
|
|
1545
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.selfTypeMetaclass(), errorNode);
|
|
1546
|
+
return types_1.UnknownType.create();
|
|
1547
|
+
}
|
|
1548
|
+
const enclosingFunction = ParseTreeUtils.getEnclosingFunction(errorNode);
|
|
1549
|
+
if (enclosingFunction) {
|
|
1550
|
+
const functionInfo = (0, decorators_1.getFunctionInfoFromDecorators)(context, enclosingFunction,
|
|
1551
|
+
/* isInClass */ true, nodeInfo);
|
|
1552
|
+
const isInnerFunction = !!ParseTreeUtils.getEnclosingFunction(enclosingFunction);
|
|
1553
|
+
if (!isInnerFunction) {
|
|
1554
|
+
// Check for static methods.
|
|
1555
|
+
if (functionInfo.flags & 4 /* FunctionTypeFlags.StaticMethod */) {
|
|
1556
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.selfTypeContext(), errorNode);
|
|
1557
|
+
return types_1.UnknownType.create();
|
|
1558
|
+
}
|
|
1559
|
+
if (enclosingFunction.d.params.length > 0) {
|
|
1560
|
+
const firstParamTypeAnnotation = ParseTreeUtils.getTypeAnnotationForParam(enclosingFunction, 0);
|
|
1561
|
+
if (firstParamTypeAnnotation &&
|
|
1562
|
+
!ParseTreeUtils.isNodeContainedWithin(errorNode, firstParamTypeAnnotation)) {
|
|
1563
|
+
const annotationType = getTypeOfAnnotation(firstParamTypeAnnotation, {
|
|
1564
|
+
typeVarGetsCurScope: true,
|
|
1565
|
+
});
|
|
1566
|
+
if (!(0, types_1.isTypeVar)(annotationType) || !types_1.TypeVarType.isSelf(annotationType)) {
|
|
1567
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.selfTypeWithTypedSelfOrCls(), errorNode);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
let result = (0, typeUtils_1.synthesizeTypeVarForSelfCls)(enclosingClassTypeResult.classType, /* isClsParam */ true);
|
|
1574
|
+
if (enclosingClass) {
|
|
1575
|
+
// If "Self" is used as a type expression within a function suite, it needs
|
|
1576
|
+
// to be marked as bound.
|
|
1577
|
+
const enclosingSuite = ParseTreeUtils.getEnclosingClassOrFunctionSuite(errorNode);
|
|
1578
|
+
if (enclosingSuite && ParseTreeUtils.isNodeContainedWithin(enclosingSuite, enclosingClass)) {
|
|
1579
|
+
if (enclosingClass.d.suite !== enclosingSuite) {
|
|
1580
|
+
result = types_1.TypeVarType.cloneAsBound(result);
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
return result;
|
|
1585
|
+
}
|
|
1586
|
+
function cloneBuiltinClassWithLiteral(node, literalClassType, builtInName, value) {
|
|
1587
|
+
const type = getBuiltInType(node, builtInName);
|
|
1588
|
+
if ((0, types_1.isInstantiableClass)(type)) {
|
|
1589
|
+
const literalType = types_1.ClassType.cloneWithLiteral(type, value);
|
|
1590
|
+
types_1.TypeBase.setSpecialForm(literalType, literalClassType);
|
|
1591
|
+
return literalType;
|
|
1592
|
+
}
|
|
1593
|
+
return types_1.UnknownType.create();
|
|
1594
|
+
}
|
|
1595
|
+
function createLiteralType(classType, node, flags) {
|
|
1596
|
+
if (node.d.items.length === 0) {
|
|
1597
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalEmptyArgs(), node.d.leftExpr);
|
|
1598
|
+
return types_1.UnknownType.create();
|
|
1599
|
+
}
|
|
1600
|
+
// As per the specification, we support None, int, bool, str, bytes literals
|
|
1601
|
+
// plus enum values.
|
|
1602
|
+
const literalTypes = [];
|
|
1603
|
+
let isValidTypeForm = true;
|
|
1604
|
+
for (const item of node.d.items) {
|
|
1605
|
+
let type;
|
|
1606
|
+
const itemExpr = item.d.valueExpr;
|
|
1607
|
+
if (item.d.argCategory !== 0 /* ArgCategory.Simple */) {
|
|
1608
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
|
1609
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.unpackedArgInTypeArgument(), itemExpr);
|
|
1610
|
+
type = types_1.UnknownType.create();
|
|
1611
|
+
isValidTypeForm = false;
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
else if (item.d.name) {
|
|
1615
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
|
1616
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.keywordArgInTypeArgument(), itemExpr);
|
|
1617
|
+
type = types_1.UnknownType.create();
|
|
1618
|
+
isValidTypeForm = false;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
else if (itemExpr.nodeType === 48 /* ParseNodeType.StringList */ &&
|
|
1622
|
+
itemExpr.d.strings.every((stringNode) => stringNode.nodeType === 49 /* ParseNodeType.String */)) {
|
|
1623
|
+
const isBytes = (itemExpr.d.strings[0].d.token.flags & 32 /* StringTokenFlags.Bytes */) !== 0;
|
|
1624
|
+
const value = itemExpr.d.strings.map((s) => s.d.value).join('');
|
|
1625
|
+
if (isBytes) {
|
|
1626
|
+
type = cloneBuiltinClassWithLiteral(node, classType, 'bytes', value);
|
|
1627
|
+
}
|
|
1628
|
+
else {
|
|
1629
|
+
type = cloneBuiltinClassWithLiteral(node, classType, 'str', value);
|
|
1630
|
+
}
|
|
1631
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
|
1632
|
+
itemExpr.d.strings.forEach((stringNode) => {
|
|
1633
|
+
if ((stringNode.d.token.flags & 1024 /* StringTokenFlags.NamedUnicodeEscape */) !== 0) {
|
|
1634
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalNamedUnicodeEscape(), stringNode);
|
|
1635
|
+
isValidTypeForm = false;
|
|
1636
|
+
}
|
|
1637
|
+
});
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
else if (itemExpr.nodeType === 40 /* ParseNodeType.Number */) {
|
|
1641
|
+
if (!itemExpr.d.isImaginary && itemExpr.d.isInteger) {
|
|
1642
|
+
type = cloneBuiltinClassWithLiteral(node, classType, 'int', itemExpr.d.value);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
else if (itemExpr.nodeType === 14 /* ParseNodeType.Constant */) {
|
|
1646
|
+
if (itemExpr.d.constType === 34 /* KeywordType.True */) {
|
|
1647
|
+
type = cloneBuiltinClassWithLiteral(node, classType, 'bool', true);
|
|
1648
|
+
}
|
|
1649
|
+
else if (itemExpr.d.constType === 15 /* KeywordType.False */) {
|
|
1650
|
+
type = cloneBuiltinClassWithLiteral(node, classType, 'bool', false);
|
|
1651
|
+
}
|
|
1652
|
+
else if (itemExpr.d.constType === 27 /* KeywordType.None */) {
|
|
1653
|
+
type = context.prefetched?.noneTypeClass ?? types_1.UnknownType.create();
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
else if (itemExpr.nodeType === 55 /* ParseNodeType.UnaryOperation */) {
|
|
1657
|
+
if (itemExpr.d.operator === 33 /* OperatorType.Subtract */ || itemExpr.d.operator === 0 /* OperatorType.Add */) {
|
|
1658
|
+
if (itemExpr.d.expr.nodeType === 40 /* ParseNodeType.Number */) {
|
|
1659
|
+
if (!itemExpr.d.expr.d.isImaginary && itemExpr.d.expr.d.isInteger) {
|
|
1660
|
+
type = cloneBuiltinClassWithLiteral(node, classType, 'int', itemExpr.d.operator === 33 /* OperatorType.Subtract */
|
|
1661
|
+
? -itemExpr.d.expr.d.value
|
|
1662
|
+
: itemExpr.d.expr.d.value);
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
if (!type) {
|
|
1668
|
+
const exprType = getTypeOfExpression(itemExpr, (flags & (4 /* EvalFlags.ForwardRefs */ | 256 /* EvalFlags.TypeExpression */)) | 33554432 /* EvalFlags.NoConvertSpecialForm */);
|
|
1669
|
+
// Is this an enum type?
|
|
1670
|
+
if ((0, types_1.isClassInstance)(exprType.type) &&
|
|
1671
|
+
types_1.ClassType.isEnumClass(exprType.type) &&
|
|
1672
|
+
exprType.type.priv.literalValue !== undefined) {
|
|
1673
|
+
type = types_1.ClassType.cloneAsInstantiable(exprType.type);
|
|
1674
|
+
}
|
|
1675
|
+
else {
|
|
1676
|
+
// Is this a type alias to an existing literal type?
|
|
1677
|
+
let isLiteralType = true;
|
|
1678
|
+
(0, typeUtils_1.doForEachSubtype)(exprType.type, (subtype) => {
|
|
1679
|
+
if (!(0, types_1.isInstantiableClass)(subtype) || subtype.priv.literalValue === undefined) {
|
|
1680
|
+
if (!(0, typeUtils_1.isNoneTypeClass)(subtype)) {
|
|
1681
|
+
isLiteralType = false;
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
});
|
|
1685
|
+
if (isLiteralType) {
|
|
1686
|
+
type = exprType.type;
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
if (!type) {
|
|
1691
|
+
if ((flags & 256 /* EvalFlags.TypeExpression */) !== 0) {
|
|
1692
|
+
addDiagnostic(diagnosticRules_1.DiagnosticRule.reportInvalidTypeForm, localize_1.LocMessage.literalUnsupportedType(), item);
|
|
1693
|
+
type = types_1.UnknownType.create();
|
|
1694
|
+
isValidTypeForm = false;
|
|
1695
|
+
}
|
|
1696
|
+
else {
|
|
1697
|
+
return types_1.ClassType.cloneAsInstance(classType);
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
literalTypes.push(type);
|
|
1701
|
+
}
|
|
1702
|
+
let result = (0, types_1.combineTypes)(literalTypes, { skipElideRedundantLiterals: true });
|
|
1703
|
+
if ((0, types_1.isUnion)(result) &&
|
|
1704
|
+
context.prefetched?.unionTypeClass &&
|
|
1705
|
+
(0, types_1.isInstantiableClass)(context.prefetched.unionTypeClass)) {
|
|
1706
|
+
result = types_1.TypeBase.cloneAsSpecialForm(result, types_1.ClassType.cloneAsInstance(context.prefetched.unionTypeClass));
|
|
1707
|
+
}
|
|
1708
|
+
if (isValidTypeForm) {
|
|
1709
|
+
result = types_1.TypeBase.cloneWithTypeForm(result, (0, typeUtils_1.convertToInstance)(result));
|
|
1710
|
+
}
|
|
1711
|
+
return result;
|
|
1712
|
+
}
|
|
1713
|
+
return {
|
|
1714
|
+
reportUseOfTypeCheckOnly,
|
|
1715
|
+
enforceClassTypeVarScope,
|
|
1716
|
+
adjustTypeArgsForTypeVarTuple,
|
|
1717
|
+
validateTypeArg,
|
|
1718
|
+
createTypeFormType,
|
|
1719
|
+
transformTypeForTypeAlias,
|
|
1720
|
+
validateTypeParamDefault,
|
|
1721
|
+
verifyGenericTypeParams,
|
|
1722
|
+
transformTypeArgsForParamSpec,
|
|
1723
|
+
createSpecializedClassType,
|
|
1724
|
+
createSelfType,
|
|
1725
|
+
createLiteralType,
|
|
1726
|
+
};
|
|
1727
|
+
}
|
|
1728
|
+
//# sourceMappingURL=specialFormCreation.js.map
|