@zzzen/pyright-internal 1.2.0-dev.20240714 → 1.2.0-dev.20240728
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/analyzerNodeInfo.js +2 -2
- package/dist/analyzer/analyzerNodeInfo.js.map +1 -1
- package/dist/analyzer/binder.js +8 -8
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/cacheManager.js +3 -0
- package/dist/analyzer/cacheManager.js.map +1 -1
- package/dist/analyzer/checker.d.ts +3 -2
- package/dist/analyzer/checker.js +137 -142
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.d.ts +2 -2
- package/dist/analyzer/codeFlowEngine.js +74 -44
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/codeFlowTypes.js +1 -1
- package/dist/analyzer/codeFlowTypes.js.map +1 -1
- package/dist/analyzer/constraintSolver.d.ts +2 -2
- package/dist/analyzer/constraintSolver.js +166 -206
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructorTransform.d.ts +2 -3
- package/dist/analyzer/constructorTransform.js +18 -19
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.d.ts +3 -3
- package/dist/analyzer/constructors.js +72 -175
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.d.ts +2 -2
- package/dist/analyzer/dataClasses.js +60 -40
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/declaration.d.ts +9 -9
- package/dist/analyzer/declaration.js +9 -9
- package/dist/analyzer/declaration.js.map +1 -1
- package/dist/analyzer/declarationUtils.d.ts +1 -1
- package/dist/analyzer/declarationUtils.js +6 -6
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/decorators.js +5 -6
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/docStringConversion.js +2 -2
- package/dist/analyzer/docStringConversion.js.map +1 -1
- package/dist/analyzer/enums.d.ts +2 -2
- package/dist/analyzer/enums.js +6 -6
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/functionTransform.d.ts +2 -2
- package/dist/analyzer/functionTransform.js +4 -4
- package/dist/analyzer/functionTransform.js.map +1 -1
- package/dist/analyzer/importResolver.js +5 -3
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/namedTuples.d.ts +3 -3
- package/dist/analyzer/namedTuples.js +21 -22
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +145 -96
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +8 -8
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.d.ts +10 -10
- package/dist/analyzer/parameterUtils.js +60 -59
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.d.ts +7 -6
- package/dist/analyzer/parseTreeUtils.js +47 -24
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +109 -90
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.js +11 -4
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +23 -19
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +23 -20
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/pythonPathUtils.js +1 -1
- package/dist/analyzer/pythonPathUtils.js.map +1 -1
- package/dist/analyzer/service.d.ts +3 -7
- package/dist/analyzer/service.js +28 -30
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/serviceUtils.d.ts +8 -0
- package/dist/analyzer/serviceUtils.js +31 -0
- package/dist/analyzer/serviceUtils.js.map +1 -0
- package/dist/analyzer/sourceFile.js +5 -19
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceMapper.d.ts +1 -1
- package/dist/analyzer/sourceMapper.js +4 -4
- package/dist/analyzer/sourceMapper.js.map +1 -1
- package/dist/analyzer/staticExpressions.js +1 -1
- package/dist/analyzer/staticExpressions.js.map +1 -1
- package/dist/analyzer/testWalker.js +13 -1
- package/dist/analyzer/testWalker.js.map +1 -1
- package/dist/analyzer/tracePrinter.js +4 -4
- package/dist/analyzer/tracePrinter.js.map +1 -1
- package/dist/analyzer/tuples.d.ts +9 -0
- package/dist/analyzer/tuples.js +252 -0
- package/dist/analyzer/tuples.js.map +1 -0
- package/dist/analyzer/typeComplexity.d.ts +2 -0
- package/dist/analyzer/typeComplexity.js +86 -0
- package/dist/analyzer/typeComplexity.js.map +1 -0
- package/dist/analyzer/typeEvaluator.js +1721 -1824
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +47 -40
- package/dist/analyzer/typeEvaluatorTypes.js +10 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +116 -90
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +1 -1
- package/dist/analyzer/typePrinter.js +54 -53
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeStubWriter.d.ts +3 -3
- package/dist/analyzer/typeStubWriter.js +15 -15
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +35 -29
- package/dist/analyzer/typeUtils.js +420 -357
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.d.ts +24 -36
- package/dist/analyzer/typeVarContext.js +89 -193
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typeWalker.js +4 -4
- package/dist/analyzer/typeWalker.js.map +1 -1
- package/dist/analyzer/typedDicts.d.ts +2 -2
- package/dist/analyzer/typedDicts.js +49 -53
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +89 -63
- package/dist/analyzer/types.js +163 -120
- package/dist/analyzer/types.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +12 -12
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/configOptions.d.ts +1 -0
- package/dist/common/configOptions.js +5 -0
- package/dist/common/configOptions.js.map +1 -1
- package/dist/common/diagnosticRules.d.ts +1 -0
- package/dist/common/diagnosticRules.js +1 -0
- package/dist/common/diagnosticRules.js.map +1 -1
- package/dist/common/fileSystem.d.ts +0 -1
- package/dist/common/fileSystem.js.map +1 -1
- package/dist/common/languageServerInterface.d.ts +13 -0
- package/dist/common/languageServerInterface.js +15 -1
- package/dist/common/languageServerInterface.js.map +1 -1
- package/dist/common/lspUtils.js +2 -2
- package/dist/common/memUtils.d.ts +4 -0
- package/dist/common/memUtils.js +33 -2
- package/dist/common/memUtils.js.map +1 -1
- package/dist/common/serviceKeys.d.ts +3 -0
- package/dist/common/serviceKeys.js +2 -0
- package/dist/common/serviceKeys.js.map +1 -1
- package/dist/common/serviceProviderExtensions.js +7 -0
- package/dist/common/serviceProviderExtensions.js.map +1 -1
- package/dist/languageServerBase.d.ts +4 -3
- package/dist/languageServerBase.js +14 -10
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +3 -2
- package/dist/languageService/autoImporter.js +5 -1
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/completionProvider.d.ts +5 -3
- package/dist/languageService/completionProvider.js +38 -37
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/completionProviderUtils.d.ts +1 -1
- package/dist/languageService/completionProviderUtils.js +3 -3
- package/dist/languageService/completionProviderUtils.js.map +1 -1
- package/dist/languageService/fileWatcherDynamicFeature.js +2 -2
- package/dist/languageService/fileWatcherDynamicFeature.js.map +1 -1
- package/dist/languageService/hoverProvider.js +14 -6
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.d.ts +10 -4
- package/dist/languageService/referencesProvider.js +24 -12
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/renameProvider.js +21 -4
- package/dist/languageService/renameProvider.js.map +1 -1
- package/dist/languageService/signatureHelpProvider.js +1 -1
- package/dist/languageService/signatureHelpProvider.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +3 -3
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/localization/localize.d.ts +9 -3
- package/dist/localization/localize.js +8 -1
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.cs.json +5 -2
- package/dist/localization/package.nls.de.json +5 -2
- package/dist/localization/package.nls.en-us.json +7 -1
- package/dist/localization/package.nls.es.json +5 -2
- package/dist/localization/package.nls.fr.json +5 -2
- package/dist/localization/package.nls.it.json +5 -2
- package/dist/localization/package.nls.ja.json +5 -2
- package/dist/localization/package.nls.ko.json +5 -2
- package/dist/localization/package.nls.pl.json +5 -2
- package/dist/localization/package.nls.pt-br.json +5 -2
- package/dist/localization/package.nls.qps-ploc.json +5 -2
- package/dist/localization/package.nls.ru.json +5 -2
- package/dist/localization/package.nls.tr.json +5 -2
- package/dist/localization/package.nls.zh-cn.json +5 -2
- package/dist/localization/package.nls.zh-tw.json +5 -2
- package/dist/parser/parseNodes.d.ts +12 -10
- package/dist/parser/parseNodes.js +23 -21
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.js +66 -43
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/checker.test.js +2 -2
- package/dist/tests/fourslash/rename.args.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/rename.args.fourslash.js +71 -0
- package/dist/tests/fourslash/rename.args.fourslash.js.map +1 -0
- package/dist/tests/fourslash/signature.builtinDocstrings.fourslash.js +4 -21
- package/dist/tests/fourslash/signature.builtinDocstrings.fourslash.js.map +1 -1
- package/dist/tests/fourslash/signature.complicated.fourslash.js +1 -1
- package/dist/tests/fourslash/signature.complicated.fourslash.js.map +1 -1
- package/dist/tests/harness/fourslash/fourSlashParser.js +1 -1
- package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
- package/dist/tests/harness/fourslash/testStateUtils.js +2 -2
- package/dist/tests/harness/fourslash/testStateUtils.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.d.ts +0 -1
- package/dist/tests/harness/vfs/filesystem.js +0 -4
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/importResolver.test.js +62 -15
- package/dist/tests/importResolver.test.js.map +1 -1
- package/dist/tests/lsp/languageServerTestUtils.js +1 -1
- package/dist/tests/lsp/languageServerTestUtils.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +17 -0
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +4 -0
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +1 -1
- package/dist/tests/typeEvaluator4.test.js +19 -7
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +8 -2
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/typeEvaluator6.test.js +76 -60
- package/dist/tests/typeEvaluator6.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +5 -1
- package/dist/tests/typeEvaluator7.test.js.map +1 -1
- package/dist/tests/typePrinter.test.js +13 -18
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/dist/workspaceFactory.d.ts +3 -2
- package/dist/workspaceFactory.js +15 -2
- package/dist/workspaceFactory.js.map +1 -1
- package/package.json +1 -1
@@ -17,10 +17,10 @@ const typeEvaluatorTypes_1 = require("./typeEvaluatorTypes");
|
|
17
17
|
const types_1 = require("./types");
|
18
18
|
const typeUtils_1 = require("./typeUtils");
|
19
19
|
const typeVarContext_1 = require("./typeVarContext");
|
20
|
-
// As we widen the
|
20
|
+
// As we widen the lower bound of a type variable, we may end up with
|
21
21
|
// many subtypes. For performance reasons, we need to cap this at some
|
22
22
|
// point. This constant determines the cap.
|
23
|
-
const
|
23
|
+
const maxSubtypeCountForTypeVarLowerBound = 64;
|
24
24
|
// This debugging switch enables logging of the TypeVarContext before and
|
25
25
|
// after it is updated by the constraint solver.
|
26
26
|
const logTypeVarContextUpdates = false;
|
@@ -39,7 +39,6 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
39
39
|
console.log(`${indent}srcType: ${evaluator.printType(srcType)}`);
|
40
40
|
console.log(`${indent}flags: ${flags}`);
|
41
41
|
console.log(`${indent}scopes: ${(typeVarContext.getSolveForScopes() || []).join(', ')}`);
|
42
|
-
console.log(`${indent}pre-call context #${typeVarContext.getId()}: `);
|
43
42
|
logTypeVarContext(evaluator, typeVarContext, indent);
|
44
43
|
}
|
45
44
|
let isTypeVarInScope = true;
|
@@ -66,16 +65,16 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
66
65
|
}
|
67
66
|
// Handle a type[Any] as a source.
|
68
67
|
if ((0, types_1.isClassInstance)(srcType) && types_1.ClassType.isBuiltIn(srcType, 'type')) {
|
69
|
-
if (!srcType.priv.
|
70
|
-
srcType.priv.
|
71
|
-
(0, types_1.isAnyOrUnknown)(srcType.priv.
|
68
|
+
if (!srcType.priv.typeArgs ||
|
69
|
+
srcType.priv.typeArgs.length < 1 ||
|
70
|
+
(0, types_1.isAnyOrUnknown)(srcType.priv.typeArgs[0])) {
|
72
71
|
if (types_1.TypeBase.isInstantiable(destType)) {
|
73
72
|
return true;
|
74
73
|
}
|
75
74
|
}
|
76
75
|
}
|
77
76
|
// Is this the equivalent of an "Unknown" for a ParamSpec?
|
78
|
-
if (
|
77
|
+
if ((0, types_1.isParamSpec)(destType) &&
|
79
78
|
(0, types_1.isFunction)(srcType) &&
|
80
79
|
types_1.FunctionType.isParamSpecValue(srcType) &&
|
81
80
|
types_1.FunctionType.isGradualCallableForm(srcType)) {
|
@@ -109,16 +108,16 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
109
108
|
/* destTypeVarContext */ undefined,
|
110
109
|
/* srcTypeVarContext */ undefined, flags, recursionCount);
|
111
110
|
}
|
112
|
-
if (
|
111
|
+
if ((0, types_1.isParamSpec)(destType)) {
|
113
112
|
return assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContext, recursionCount);
|
114
113
|
}
|
115
|
-
if (destType
|
114
|
+
if ((0, types_1.isTypeVarTuple)(destType) && !destType.priv.isVariadicInUnion) {
|
116
115
|
if (!(0, types_1.isUnpacked)(srcType)) {
|
117
116
|
const tupleClassType = evaluator.getTupleClassType();
|
118
117
|
if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
|
119
118
|
// Package up the type into a tuple.
|
120
119
|
srcType = (0, typeUtils_1.convertToInstance)((0, typeUtils_1.specializeTupleClass)(tupleClassType, [{ type: srcType, isUnbounded: false }],
|
121
|
-
/*
|
120
|
+
/* isTypeArgExplicit */ true,
|
122
121
|
/* isUnpackedTuple */ true));
|
123
122
|
}
|
124
123
|
else {
|
@@ -128,11 +127,10 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
128
127
|
}
|
129
128
|
// If we're assigning an unpacked TypeVarTuple to a regular TypeVar,
|
130
129
|
// we need to treat it as a union of the unpacked TypeVarTuple.
|
131
|
-
if ((0, types_1.
|
132
|
-
srcType.shared.isVariadic &&
|
130
|
+
if ((0, types_1.isTypeVarTuple)(srcType) &&
|
133
131
|
srcType.priv.isVariadicUnpacked &&
|
134
132
|
!srcType.priv.isVariadicInUnion &&
|
135
|
-
!
|
133
|
+
!(0, types_1.isTypeVarTuple)(destType)) {
|
136
134
|
srcType = types_1.TypeVarType.cloneForUnpacked(srcType, /* isInUnion */ true);
|
137
135
|
}
|
138
136
|
// Handle the constrained case. This case needs to be handled specially
|
@@ -143,19 +141,19 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
143
141
|
return assignTypeToConstrainedTypeVar(evaluator, destType, srcType, diag, typeVarContext, flags, isTypeVarInScope, recursionCount);
|
144
142
|
}
|
145
143
|
// Handle the unconstrained (but possibly bound) case.
|
146
|
-
const curEntry = typeVarContext.
|
147
|
-
let
|
148
|
-
if (!
|
149
|
-
|
150
|
-
}
|
151
|
-
let
|
152
|
-
let
|
153
|
-
let
|
144
|
+
const curEntry = typeVarContext.getMainSolutionSet().getTypeVar(destType);
|
145
|
+
let curUpperBound = curEntry === null || curEntry === void 0 ? void 0 : curEntry.upperBound;
|
146
|
+
if (!curUpperBound && !destType.shared.isSynthesizedSelf) {
|
147
|
+
curUpperBound = destType.shared.boundType;
|
148
|
+
}
|
149
|
+
let curLowerBound = curEntry === null || curEntry === void 0 ? void 0 : curEntry.lowerBound;
|
150
|
+
let newLowerBound = curLowerBound;
|
151
|
+
let newUpperTypeBound = curUpperBound;
|
154
152
|
const diagAddendum = diag ? new diagnostic_1.DiagnosticAddendum() : undefined;
|
155
153
|
let adjSrcType = srcType;
|
156
154
|
// If the source is a class that is missing type arguments, fill
|
157
155
|
// in missing type arguments with Unknown.
|
158
|
-
if ((flags & 8192 /* AssignTypeFlags.
|
156
|
+
if ((flags & 8192 /* AssignTypeFlags.AllowUnspecifiedTypeArgs */) === 0) {
|
159
157
|
if ((0, types_1.isClass)(adjSrcType) && adjSrcType.priv.includeSubclasses) {
|
160
158
|
adjSrcType = (0, typeUtils_1.specializeWithDefaultTypeArgs)(adjSrcType);
|
161
159
|
}
|
@@ -187,51 +185,51 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
187
185
|
return true;
|
188
186
|
}
|
189
187
|
// If we're populating the expected type, constrain either the
|
190
|
-
//
|
188
|
+
// lower type bound, upper type bound or both. Don't overwrite
|
191
189
|
// an existing entry.
|
192
190
|
if (!curEntry) {
|
193
191
|
if (isInvariant) {
|
194
|
-
|
195
|
-
|
192
|
+
newLowerBound = adjSrcType;
|
193
|
+
newUpperTypeBound = adjSrcType;
|
196
194
|
}
|
197
195
|
else if (isContravariant) {
|
198
|
-
|
196
|
+
newLowerBound = adjSrcType;
|
199
197
|
}
|
200
198
|
else {
|
201
|
-
|
199
|
+
newUpperTypeBound = adjSrcType;
|
202
200
|
}
|
203
201
|
}
|
204
202
|
}
|
205
203
|
else if (isContravariant) {
|
206
|
-
// Update the
|
207
|
-
if (!
|
208
|
-
|
204
|
+
// Update the upper bound.
|
205
|
+
if (!curUpperBound || (0, types_1.isTypeSame)(destType, curUpperBound)) {
|
206
|
+
newUpperTypeBound = adjSrcType;
|
209
207
|
}
|
210
|
-
else if (!(0, types_1.isTypeSame)(
|
211
|
-
if (evaluator.assignType(
|
208
|
+
else if (!(0, types_1.isTypeSame)(curUpperBound, adjSrcType, {}, recursionCount)) {
|
209
|
+
if (evaluator.assignType(curUpperBound, evaluator.makeTopLevelTypeVarsConcrete(adjSrcType), diagAddendum,
|
212
210
|
/* destTypeVarContext */ undefined,
|
213
211
|
/* srcTypeVarContext */ undefined, flags & 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
214
|
-
// The srcType is narrower than the current
|
215
|
-
|
212
|
+
// The srcType is narrower than the current upper bound, so replace it.
|
213
|
+
newUpperTypeBound = adjSrcType;
|
216
214
|
}
|
217
|
-
else if (!evaluator.assignType(adjSrcType,
|
215
|
+
else if (!evaluator.assignType(adjSrcType, curUpperBound, diagAddendum,
|
218
216
|
/* destTypeVarContext */ undefined,
|
219
217
|
/* srcTypeVarContext */ undefined, flags & 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
220
218
|
if (diag && diagAddendum) {
|
221
|
-
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(
|
219
|
+
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(curUpperBound, adjSrcType)));
|
222
220
|
diag.addAddendum(diagAddendum);
|
223
221
|
}
|
224
222
|
return false;
|
225
223
|
}
|
226
224
|
}
|
227
|
-
// Make sure we haven't narrowed it beyond the current
|
228
|
-
if (
|
229
|
-
if (!evaluator.assignType(
|
225
|
+
// Make sure we haven't narrowed it beyond the current lower bound.
|
226
|
+
if (curLowerBound) {
|
227
|
+
if (!evaluator.assignType(newUpperTypeBound, curLowerBound,
|
230
228
|
/* diag */ undefined,
|
231
229
|
/* destTypeVarContext */ undefined,
|
232
230
|
/* srcTypeVarContext */ undefined, flags & 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
233
231
|
if (diag && diagAddendum) {
|
234
|
-
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(
|
232
|
+
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(curLowerBound, newUpperTypeBound)));
|
235
233
|
diag.addAddendum(diagAddendum);
|
236
234
|
}
|
237
235
|
return false;
|
@@ -239,138 +237,132 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
239
237
|
}
|
240
238
|
}
|
241
239
|
else {
|
242
|
-
if (!
|
243
|
-
// There was previously no
|
244
|
-
|
240
|
+
if (!curLowerBound || (0, types_1.isTypeSame)(destType, curLowerBound)) {
|
241
|
+
// There was previously no lower bound. We've now established one.
|
242
|
+
newLowerBound = adjSrcType;
|
245
243
|
}
|
246
|
-
else if ((0, types_1.isTypeSame)(
|
247
|
-
// If this is an invariant context and there is currently no
|
248
|
-
// established, use the "no literals" version of the
|
244
|
+
else if ((0, types_1.isTypeSame)(curLowerBound, adjSrcType, {}, recursionCount)) {
|
245
|
+
// If this is an invariant context and there is currently no upper bound
|
246
|
+
// established, use the "no literals" version of the lower bound rather
|
249
247
|
// than a version that has literals.
|
250
|
-
if (!
|
251
|
-
|
248
|
+
if (!newUpperTypeBound && isInvariant && (curEntry === null || curEntry === void 0 ? void 0 : curEntry.lowerBoundNoLiterals)) {
|
249
|
+
newLowerBound = curEntry.lowerBoundNoLiterals;
|
252
250
|
}
|
253
251
|
}
|
254
252
|
else {
|
255
|
-
if ((
|
256
|
-
// Handle the tuple case specially. If Any or Unknown is assigned
|
257
|
-
// during the construction of a tuple, the resulting tuple type must
|
258
|
-
// be tuple[Any, ...], which is compatible with any tuple.
|
259
|
-
newNarrowTypeBound = adjSrcType;
|
260
|
-
}
|
261
|
-
else if (evaluator.assignType(curNarrowTypeBound, adjSrcType, diagAddendum, typeVarContext,
|
253
|
+
if (evaluator.assignType(curLowerBound, adjSrcType, diagAddendum, typeVarContext,
|
262
254
|
/* srcTypeVarContext */ undefined, flags, recursionCount)) {
|
263
255
|
// No need to widen. Stick with the existing type unless it's unknown
|
264
256
|
// or partly unknown, in which case we'll replace it with a known type
|
265
|
-
// as long as it doesn't violate the current
|
266
|
-
if ((0, typeUtils_1.isPartlyUnknown)(
|
257
|
+
// as long as it doesn't violate the current lower bound.
|
258
|
+
if ((0, typeUtils_1.isPartlyUnknown)(curLowerBound) &&
|
267
259
|
!(0, types_1.isUnknown)(adjSrcType) &&
|
268
|
-
evaluator.assignType(adjSrcType,
|
260
|
+
evaluator.assignType(adjSrcType, curLowerBound,
|
269
261
|
/* diag */ undefined, typeVarContext,
|
270
262
|
/* srcTypeVarContext */ undefined, flags & 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
271
|
-
|
263
|
+
newLowerBound = adjSrcType;
|
272
264
|
}
|
273
265
|
else {
|
274
|
-
|
266
|
+
newLowerBound = (0, typeUtils_1.applySolvedTypeVars)(curLowerBound, typeVarContext);
|
275
267
|
}
|
276
268
|
}
|
277
|
-
else if ((0, types_1.isTypeVar)(
|
269
|
+
else if ((0, types_1.isTypeVar)(curLowerBound) &&
|
278
270
|
!(0, types_1.isTypeVar)(adjSrcType) &&
|
279
|
-
evaluator.assignType(evaluator.makeTopLevelTypeVarsConcrete(
|
271
|
+
evaluator.assignType(evaluator.makeTopLevelTypeVarsConcrete(curLowerBound), adjSrcType, diagAddendum, typeVarContext,
|
280
272
|
/* srcTypeVarContext */ undefined, flags, recursionCount)) {
|
281
|
-
// If the existing
|
273
|
+
// If the existing lower bound was a TypeVar that is not
|
282
274
|
// part of the current context we can replace it with the new
|
283
275
|
// source type.
|
284
|
-
|
276
|
+
newLowerBound = adjSrcType;
|
285
277
|
}
|
286
278
|
else {
|
287
279
|
// We need to widen the type.
|
288
280
|
if (typeVarContext.isLocked()) {
|
289
|
-
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(adjSrcType,
|
281
|
+
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(adjSrcType, curLowerBound)));
|
290
282
|
return false;
|
291
283
|
}
|
292
|
-
if (evaluator.assignType(adjSrcType,
|
284
|
+
if (evaluator.assignType(adjSrcType, curLowerBound,
|
293
285
|
/* diag */ undefined, typeVarContext,
|
294
286
|
/* srcTypeVarContext */ undefined, flags & 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
295
|
-
|
287
|
+
newLowerBound = adjSrcType;
|
296
288
|
}
|
297
|
-
else if ((0, types_1.
|
298
|
-
const widenedType =
|
289
|
+
else if ((0, types_1.isTypeVarTuple)(destType)) {
|
290
|
+
const widenedType = widenTypeForTypeVarTuple(evaluator, curLowerBound, adjSrcType);
|
299
291
|
if (!widenedType) {
|
300
|
-
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(
|
292
|
+
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(curLowerBound, adjSrcType)));
|
301
293
|
return false;
|
302
294
|
}
|
303
|
-
|
295
|
+
newLowerBound = widenedType;
|
304
296
|
}
|
305
297
|
else {
|
306
298
|
const objectType = evaluator.getObjectType();
|
307
|
-
// If this is an invariant context and there is currently no
|
308
|
-
// established, use the "no literals" version of the
|
299
|
+
// If this is an invariant context and there is currently no upper bound
|
300
|
+
// established, use the "no literals" version of the lower bound rather
|
309
301
|
// than a version that has literals.
|
310
|
-
if (!
|
311
|
-
|
302
|
+
if (!newUpperTypeBound && isInvariant && (curEntry === null || curEntry === void 0 ? void 0 : curEntry.lowerBoundNoLiterals)) {
|
303
|
+
curLowerBound = curEntry.lowerBoundNoLiterals;
|
312
304
|
}
|
313
|
-
const
|
314
|
-
// In some extreme edge cases, the
|
305
|
+
const curSolvedLowerBound = (0, typeUtils_1.applySolvedTypeVars)(curLowerBound, typeVarContext);
|
306
|
+
// In some extreme edge cases, the lower bound can become
|
315
307
|
// a union with so many subtypes that performance grinds to a
|
316
308
|
// halt. We'll detect this case and widen the resulting type
|
317
309
|
// to an 'object' instead of making the union even bigger. This
|
318
310
|
// is still a valid solution to the TypeVar.
|
319
|
-
if ((0, types_1.isUnion)(
|
320
|
-
|
311
|
+
if ((0, types_1.isUnion)(curSolvedLowerBound) &&
|
312
|
+
curSolvedLowerBound.priv.subtypes.length > typeEvaluatorTypes_1.maxSubtypesForInferredType &&
|
321
313
|
destType.shared.boundType !== undefined &&
|
322
314
|
(0, types_1.isClassInstance)(objectType)) {
|
323
|
-
|
324
|
-
maxSubtypeCount:
|
315
|
+
newLowerBound = (0, types_1.combineTypes)([curSolvedLowerBound, objectType], {
|
316
|
+
maxSubtypeCount: maxSubtypeCountForTypeVarLowerBound,
|
325
317
|
});
|
326
318
|
}
|
327
319
|
else {
|
328
|
-
|
329
|
-
maxSubtypeCount:
|
320
|
+
newLowerBound = (0, types_1.combineTypes)([curSolvedLowerBound, adjSrcType], {
|
321
|
+
maxSubtypeCount: maxSubtypeCountForTypeVarLowerBound,
|
330
322
|
});
|
331
323
|
}
|
332
324
|
}
|
333
325
|
}
|
334
326
|
}
|
335
|
-
// If this is an invariant context, make sure the
|
327
|
+
// If this is an invariant context, make sure the lower bound
|
336
328
|
// isn't too wide.
|
337
|
-
if (isInvariant &&
|
338
|
-
if (!evaluator.assignType(adjSrcType,
|
329
|
+
if (isInvariant && newLowerBound) {
|
330
|
+
if (!evaluator.assignType(adjSrcType, newLowerBound, diag === null || diag === void 0 ? void 0 : diag.createAddendum(),
|
339
331
|
/* destTypeVarContext */ undefined,
|
340
332
|
/* srcTypeVarContext */ undefined, 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
341
333
|
if (diag && diagAddendum) {
|
342
|
-
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(
|
334
|
+
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(newLowerBound, adjSrcType)));
|
343
335
|
}
|
344
336
|
return false;
|
345
337
|
}
|
346
338
|
}
|
347
|
-
// Make sure we don't exceed the
|
348
|
-
if (
|
349
|
-
if (!(0, types_1.isTypeSame)(
|
350
|
-
let
|
339
|
+
// Make sure we don't exceed the upper bound.
|
340
|
+
if (curUpperBound && newLowerBound) {
|
341
|
+
if (!(0, types_1.isTypeSame)(curUpperBound, newLowerBound, {}, recursionCount)) {
|
342
|
+
let adjUpperBound = evaluator.makeTopLevelTypeVarsConcrete(curUpperBound,
|
351
343
|
/* makeParamSpecsConcrete */ true);
|
352
|
-
// Convert any remaining (non-top-level) TypeVars in the
|
344
|
+
// Convert any remaining (non-top-level) TypeVars in the upper
|
353
345
|
// bound to in-scope placeholders.
|
354
|
-
|
346
|
+
adjUpperBound = (0, typeUtils_1.transformExpectedType)(adjUpperBound,
|
355
347
|
/* liveTypeVarScopes */ [],
|
356
348
|
/* usageOffset */ undefined);
|
357
|
-
if (!evaluator.assignType(
|
349
|
+
if (!evaluator.assignType(adjUpperBound, newLowerBound, diag === null || diag === void 0 ? void 0 : diag.createAddendum(),
|
358
350
|
/* destTypeVarContext */ undefined,
|
359
351
|
/* srcTypeVarContext */ undefined, 1024 /* AssignTypeFlags.IgnoreTypeVarScope */, recursionCount)) {
|
360
352
|
if (diag && diagAddendum) {
|
361
|
-
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(
|
353
|
+
diag.addMessage(localize_1.LocAddendum.typeAssignmentMismatch().format(evaluator.printSrcDestTypes(newLowerBound, adjUpperBound)));
|
362
354
|
}
|
363
355
|
return false;
|
364
356
|
}
|
365
357
|
}
|
366
358
|
}
|
367
359
|
}
|
368
|
-
if (!
|
369
|
-
|
360
|
+
if (!newUpperTypeBound && isInvariant) {
|
361
|
+
newUpperTypeBound = newLowerBound;
|
370
362
|
}
|
371
363
|
// If there's a bound type, make sure the source is assignable to it.
|
372
364
|
if (destType.shared.boundType) {
|
373
|
-
const updatedType = (
|
365
|
+
const updatedType = (newLowerBound || newUpperTypeBound);
|
374
366
|
// If the dest is a Type[T] but the source is not a valid Type,
|
375
367
|
// skip the assignType check and the diagnostic addendum, which will
|
376
368
|
// be confusing and inaccurate.
|
@@ -397,56 +389,45 @@ function assignTypeToTypeVar(evaluator, destType, srcType, diag, typeVarContext,
|
|
397
389
|
return false;
|
398
390
|
}
|
399
391
|
}
|
400
|
-
// Update the tuple types based on the new type bounds. We need to
|
401
|
-
// switch to an unbounded tuple type since the length of the resulting
|
402
|
-
// tuple is indeterminate.
|
403
|
-
let newTupleTypes = curEntry === null || curEntry === void 0 ? void 0 : curEntry.tupleTypes;
|
404
|
-
if (newTupleTypes) {
|
405
|
-
const updatedType = newNarrowTypeBound !== null && newNarrowTypeBound !== void 0 ? newNarrowTypeBound : newWideTypeBound;
|
406
|
-
if (updatedType) {
|
407
|
-
newTupleTypes = [{ type: updatedType, isUnbounded: true }];
|
408
|
-
}
|
409
|
-
}
|
410
392
|
if (!typeVarContext.isLocked() && isTypeVarInScope) {
|
411
|
-
updateTypeVarType(evaluator, typeVarContext, destType,
|
393
|
+
updateTypeVarType(evaluator, typeVarContext, destType, newLowerBound, newUpperTypeBound, (flags & (2048 /* AssignTypeFlags.PopulatingExpectedType */ | 256 /* AssignTypeFlags.RetainLiteralsForTypeVar */)) !== 0);
|
412
394
|
}
|
413
395
|
if (logTypeVarContextUpdates) {
|
414
396
|
const indent = ' '.repeat(recursionCount * 2);
|
415
397
|
console.log(`${indent}`);
|
416
|
-
console.log(`${indent}post-call context #${typeVarContext.getId()}: `);
|
417
398
|
logTypeVarContext(evaluator, typeVarContext, indent);
|
418
399
|
}
|
419
400
|
return true;
|
420
401
|
}
|
421
402
|
exports.assignTypeToTypeVar = assignTypeToTypeVar;
|
422
|
-
// Updates the
|
423
|
-
//
|
403
|
+
// Updates the lower and upper bounds for a type variable. It also calculates the
|
404
|
+
// lowerBoundNoLiterals, which is a variant of the lower bound that has
|
424
405
|
// literals stripped. By default, the constraint solver always uses the "no literals"
|
425
406
|
// type in its solutions unless the version with literals is required to satisfy
|
426
|
-
// the
|
427
|
-
function updateTypeVarType(evaluator, typeVarContext, destType,
|
428
|
-
let
|
429
|
-
if (
|
430
|
-
const strippedLiteral = (0, types_1.
|
431
|
-
? stripLiteralValueForUnpackedTuple(evaluator,
|
432
|
-
: evaluator.stripLiteralValue(
|
433
|
-
// Strip the literals from the
|
434
|
-
// narrower than the
|
435
|
-
if (strippedLiteral !==
|
436
|
-
if (!
|
437
|
-
|
438
|
-
}
|
439
|
-
}
|
440
|
-
}
|
441
|
-
typeVarContext.setTypeVarType(destType,
|
407
|
+
// the upper bound.
|
408
|
+
function updateTypeVarType(evaluator, typeVarContext, destType, lowerBound, upperBound, forceRetainLiterals = false) {
|
409
|
+
let lowerBoundNoLiterals;
|
410
|
+
if (lowerBound && !forceRetainLiterals) {
|
411
|
+
const strippedLiteral = (0, types_1.isTypeVarTuple)(destType)
|
412
|
+
? stripLiteralValueForUnpackedTuple(evaluator, lowerBound)
|
413
|
+
: evaluator.stripLiteralValue(lowerBound);
|
414
|
+
// Strip the literals from the lower bound and see if it is still
|
415
|
+
// narrower than the upper bound.
|
416
|
+
if (strippedLiteral !== lowerBound) {
|
417
|
+
if (!upperBound || evaluator.assignType(upperBound, strippedLiteral)) {
|
418
|
+
lowerBoundNoLiterals = strippedLiteral;
|
419
|
+
}
|
420
|
+
}
|
421
|
+
}
|
422
|
+
typeVarContext.setTypeVarType(destType, lowerBound, lowerBoundNoLiterals, upperBound); //, tupleTypes);
|
442
423
|
}
|
443
424
|
exports.updateTypeVarType = updateTypeVarType;
|
444
425
|
function assignTypeToConstrainedTypeVar(evaluator, destType, srcType, diag, typeVarContext, flags, isTypeVarInScope, recursionCount) {
|
445
426
|
let constrainedType;
|
446
427
|
const concreteSrcType = evaluator.makeTopLevelTypeVarsConcrete(srcType);
|
447
|
-
const curEntry = typeVarContext.
|
448
|
-
const
|
449
|
-
const
|
428
|
+
const curEntry = typeVarContext.getMainSolutionSet().getTypeVar(destType);
|
429
|
+
const curUpperBound = curEntry === null || curEntry === void 0 ? void 0 : curEntry.upperBound;
|
430
|
+
const curLowerBound = curEntry === null || curEntry === void 0 ? void 0 : curEntry.lowerBound;
|
450
431
|
let forceRetainLiterals = false;
|
451
432
|
if ((0, types_1.isTypeVar)(srcType)) {
|
452
433
|
if (evaluator.assignType(destType, concreteSrcType,
|
@@ -542,26 +523,26 @@ function assignTypeToConstrainedTypeVar(evaluator, destType, srcType, diag, type
|
|
542
523
|
else if ((0, typeUtils_1.isLiteralTypeOrUnion)(constrainedType)) {
|
543
524
|
forceRetainLiterals = true;
|
544
525
|
}
|
545
|
-
if (
|
546
|
-
if (!evaluator.assignType(
|
526
|
+
if (curLowerBound && !(0, types_1.isAnyOrUnknown)(curLowerBound)) {
|
527
|
+
if (!evaluator.assignType(curLowerBound, constrainedType,
|
547
528
|
/* diag */ undefined,
|
548
529
|
/* destTypeVarContext */ undefined,
|
549
530
|
/* srcTypeVarContext */ undefined, 0 /* AssignTypeFlags.Default */, recursionCount)) {
|
550
531
|
// Handle the case where one of the constrained types is a wider
|
551
532
|
// version of another constrained type that was previously assigned
|
552
533
|
// to the type variable.
|
553
|
-
if (evaluator.assignType(constrainedType,
|
534
|
+
if (evaluator.assignType(constrainedType, curLowerBound,
|
554
535
|
/* diag */ undefined,
|
555
536
|
/* destTypeVarContext */ undefined,
|
556
537
|
/* srcTypeVarContext */ undefined, 0 /* AssignTypeFlags.Default */, recursionCount)) {
|
557
538
|
if (!typeVarContext.isLocked() && isTypeVarInScope) {
|
558
|
-
updateTypeVarType(evaluator, typeVarContext, destType, constrainedType,
|
539
|
+
updateTypeVarType(evaluator, typeVarContext, destType, constrainedType, curUpperBound);
|
559
540
|
}
|
560
541
|
}
|
561
542
|
else {
|
562
543
|
diag === null || diag === void 0 ? void 0 : diag.addMessage(localize_1.LocAddendum.typeConstrainedTypeVar().format({
|
563
544
|
type: evaluator.printType(constrainedType),
|
564
|
-
name: evaluator.printType(
|
545
|
+
name: evaluator.printType(curLowerBound),
|
565
546
|
}));
|
566
547
|
return false;
|
567
548
|
}
|
@@ -570,8 +551,7 @@ function assignTypeToConstrainedTypeVar(evaluator, destType, srcType, diag, type
|
|
570
551
|
else {
|
571
552
|
// Assign the type to the type var.
|
572
553
|
if (!typeVarContext.isLocked() && isTypeVarInScope) {
|
573
|
-
updateTypeVarType(evaluator, typeVarContext, destType, constrainedType,
|
574
|
-
/* tupleTypes */ undefined, forceRetainLiterals);
|
554
|
+
updateTypeVarType(evaluator, typeVarContext, destType, constrainedType, curUpperBound, forceRetainLiterals);
|
575
555
|
}
|
576
556
|
}
|
577
557
|
return true;
|
@@ -579,9 +559,9 @@ function assignTypeToConstrainedTypeVar(evaluator, destType, srcType, diag, type
|
|
579
559
|
function assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContext, recursionCount = 0) {
|
580
560
|
let isAssignable = true;
|
581
561
|
const adjSrcType = (0, types_1.isFunction)(srcType) ? (0, typeUtils_1.convertParamSpecValueToType)(srcType) : srcType;
|
582
|
-
typeVarContext.
|
583
|
-
if ((0, types_1.
|
584
|
-
const existingType =
|
562
|
+
typeVarContext.doForEachSolutionSet((solutionSet) => {
|
563
|
+
if ((0, types_1.isParamSpec)(adjSrcType)) {
|
564
|
+
const existingType = solutionSet.getTypeVarType(destType);
|
585
565
|
if (existingType) {
|
586
566
|
const existingTypeParamSpec = types_1.FunctionType.getParamSpecFromArgsKwargs(existingType);
|
587
567
|
const existingTypeWithoutArgsKwargs = types_1.FunctionType.cloneRemoveParamSpecArgsKwargs(existingType);
|
@@ -594,7 +574,7 @@ function assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContex
|
|
594
574
|
}
|
595
575
|
else {
|
596
576
|
if (!typeVarContext.isLocked() && typeVarContext.hasSolveForScope(destType.priv.scopeId)) {
|
597
|
-
|
577
|
+
solutionSet.setTypeVarType(destType, (0, typeUtils_1.convertTypeToParamSpecValue)(adjSrcType));
|
598
578
|
}
|
599
579
|
return;
|
600
580
|
}
|
@@ -602,7 +582,7 @@ function assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContex
|
|
602
582
|
else if ((0, types_1.isFunction)(adjSrcType)) {
|
603
583
|
const newFunction = adjSrcType;
|
604
584
|
let updateContextWithNewFunction = false;
|
605
|
-
const existingType =
|
585
|
+
const existingType = solutionSet.getTypeVarType(destType);
|
606
586
|
if (existingType) {
|
607
587
|
// Convert the remaining portion of the signature to a function
|
608
588
|
// for comparison purposes.
|
@@ -610,11 +590,11 @@ function assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContex
|
|
610
590
|
const isNewNarrower = evaluator.assignType(existingFunction, newFunction,
|
611
591
|
/* diag */ undefined,
|
612
592
|
/* destTypeVarContext */ undefined,
|
613
|
-
/* srcTypeVarContext */ undefined, 64 /* AssignTypeFlags.
|
593
|
+
/* srcTypeVarContext */ undefined, 64 /* AssignTypeFlags.SkipReturnTypeCheck */, recursionCount);
|
614
594
|
const isNewWider = evaluator.assignType(newFunction, existingFunction,
|
615
595
|
/* diag */ undefined,
|
616
596
|
/* destTypeVarContext */ undefined,
|
617
|
-
/* srcTypeVarContext */ undefined, 64 /* AssignTypeFlags.
|
597
|
+
/* srcTypeVarContext */ undefined, 64 /* AssignTypeFlags.SkipReturnTypeCheck */, recursionCount);
|
618
598
|
// Should we widen the type?
|
619
599
|
if (isNewNarrower && isNewWider) {
|
620
600
|
// The new type is both a supertype and a subtype of the existing type.
|
@@ -643,7 +623,7 @@ function assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContex
|
|
643
623
|
}
|
644
624
|
if (updateContextWithNewFunction) {
|
645
625
|
if (!typeVarContext.isLocked() && typeVarContext.hasSolveForScope(destType.priv.scopeId)) {
|
646
|
-
|
626
|
+
solutionSet.setTypeVarType(destType, newFunction);
|
647
627
|
}
|
648
628
|
return;
|
649
629
|
}
|
@@ -673,7 +653,7 @@ function assignTypeToParamSpec(evaluator, destType, srcType, diag, typeVarContex
|
|
673
653
|
// it returns false.
|
674
654
|
function addConstraintsForExpectedType(evaluator, type, expectedType, typeVarContext, liveTypeVarScopes, usageOffset = undefined) {
|
675
655
|
if ((0, types_1.isAny)(expectedType)) {
|
676
|
-
type.shared.
|
656
|
+
type.shared.typeParams.forEach((typeParam) => {
|
677
657
|
updateTypeVarType(evaluator, typeVarContext, typeParam, expectedType, expectedType);
|
678
658
|
});
|
679
659
|
return true;
|
@@ -685,84 +665,64 @@ function addConstraintsForExpectedType(evaluator, type, expectedType, typeVarCon
|
|
685
665
|
return false;
|
686
666
|
}
|
687
667
|
// If the expected type is generic (but not specialized), we can't proceed.
|
688
|
-
const expectedTypeArgs = expectedType.priv.
|
668
|
+
const expectedTypeArgs = expectedType.priv.typeArgs;
|
689
669
|
if (!expectedTypeArgs) {
|
690
670
|
return evaluator.assignType(type, expectedType,
|
691
671
|
/* diag */ undefined, typeVarContext,
|
692
672
|
/* srcTypeVarContext */ undefined, 2048 /* AssignTypeFlags.PopulatingExpectedType */);
|
693
673
|
}
|
694
|
-
evaluator.
|
674
|
+
evaluator.inferVarianceForClass(type);
|
695
675
|
// If the expected type is the same as the target type (commonly the case),
|
696
676
|
// we can use a faster method.
|
697
677
|
if (types_1.ClassType.isSameGenericClass(expectedType, type)) {
|
698
678
|
const sameClassTypeVarContext = (0, typeUtils_1.buildTypeVarContextFromSpecializedClass)(expectedType);
|
699
679
|
sameClassTypeVarContext
|
700
|
-
.
|
680
|
+
.getMainSolutionSet()
|
701
681
|
.getTypeVars()
|
702
682
|
.forEach((entry) => {
|
703
|
-
let typeArgValue = sameClassTypeVarContext.
|
683
|
+
let typeArgValue = sameClassTypeVarContext.getMainSolutionSet().getTypeVarType(entry.typeVar);
|
704
684
|
if (typeArgValue && liveTypeVarScopes) {
|
705
685
|
typeArgValue = (0, typeUtils_1.transformExpectedType)(typeArgValue, liveTypeVarScopes, usageOffset);
|
706
686
|
}
|
707
687
|
if (typeArgValue) {
|
708
688
|
const variance = types_1.TypeVarType.getVariance(entry.typeVar);
|
709
689
|
updateTypeVarType(evaluator, typeVarContext, entry.typeVar, variance === 3 /* Variance.Covariant */ ? undefined : typeArgValue, variance === 4 /* Variance.Contravariant */ ? undefined : typeArgValue);
|
710
|
-
if (entry.tupleTypes) {
|
711
|
-
typeVarContext.setTupleTypeVar(entry.typeVar, entry.tupleTypes.map((tupleEntry) => {
|
712
|
-
let tupleType = tupleEntry.type;
|
713
|
-
if (liveTypeVarScopes) {
|
714
|
-
tupleType = (0, typeUtils_1.transformExpectedType)(tupleEntry.type, liveTypeVarScopes, usageOffset);
|
715
|
-
}
|
716
|
-
return {
|
717
|
-
type: tupleType,
|
718
|
-
isUnbounded: tupleEntry.isUnbounded,
|
719
|
-
isOptional: tupleEntry.isOptional,
|
720
|
-
};
|
721
|
-
}));
|
722
|
-
}
|
723
690
|
}
|
724
691
|
});
|
725
692
|
return true;
|
726
693
|
}
|
727
694
|
// Create a generic version of the expected type.
|
728
695
|
const expectedTypeScopeId = (0, typeUtils_1.getTypeVarScopeId)(expectedType);
|
729
|
-
const synthExpectedTypeArgs = types_1.ClassType.
|
730
|
-
const typeVar = types_1.TypeVarType.createInstance(`__dest${index}
|
696
|
+
const synthExpectedTypeArgs = types_1.ClassType.getTypeParams(expectedType).map((typeParam, index) => {
|
697
|
+
const typeVar = types_1.TypeVarType.createInstance(`__dest${index}`, (0, types_1.isParamSpec)(typeParam) ? types_1.TypeVarKind.ParamSpec : types_1.TypeVarKind.TypeVar);
|
731
698
|
typeVar.shared.isSynthesized = true;
|
732
|
-
|
733
|
-
typeVar.shared.isParamSpec = true;
|
734
|
-
}
|
735
|
-
// Use invariance here so we set the narrow and wide values on the TypeVar.
|
699
|
+
// Use invariance here so we set the lower and upper bound on the TypeVar.
|
736
700
|
typeVar.shared.declaredVariance = 2 /* Variance.Invariant */;
|
737
701
|
typeVar.priv.scopeId = expectedTypeScopeId;
|
738
702
|
return typeVar;
|
739
703
|
});
|
740
|
-
const genericExpectedType = types_1.ClassType.
|
741
|
-
/* isTypeArgumentExplicit */ true);
|
704
|
+
const genericExpectedType = types_1.ClassType.specialize(expectedType, synthExpectedTypeArgs);
|
742
705
|
// For each type param in the target type, create a placeholder type variable.
|
743
|
-
const typeArgs = types_1.ClassType.
|
744
|
-
const typeVar = types_1.TypeVarType.createInstance(`__source${index}
|
706
|
+
const typeArgs = types_1.ClassType.getTypeParams(type).map((typeParam, index) => {
|
707
|
+
const typeVar = types_1.TypeVarType.createInstance(`__source${index}`, (0, types_1.isParamSpec)(typeParam) ? types_1.TypeVarKind.ParamSpec : types_1.TypeVarKind.TypeVar);
|
745
708
|
typeVar.shared.isSynthesized = true;
|
746
709
|
typeVar.shared.synthesizedIndex = index;
|
747
710
|
typeVar.shared.isExemptFromBoundCheck = true;
|
748
|
-
if (typeParam.shared.isParamSpec) {
|
749
|
-
typeVar.shared.isParamSpec = true;
|
750
|
-
}
|
751
711
|
return types_1.TypeVarType.cloneAsInScopePlaceholder(typeVar);
|
752
712
|
});
|
753
|
-
const specializedType = types_1.ClassType.
|
713
|
+
const specializedType = types_1.ClassType.specialize(type, typeArgs);
|
754
714
|
const syntheticTypeVarContext = new typeVarContext_1.TypeVarContext(expectedTypeScopeId);
|
755
715
|
if (evaluator.assignType(genericExpectedType, specializedType,
|
756
716
|
/* diag */ undefined, syntheticTypeVarContext,
|
757
717
|
/* srcTypeVarContext */ undefined, 2048 /* AssignTypeFlags.PopulatingExpectedType */)) {
|
758
718
|
let isResultValid = true;
|
759
719
|
synthExpectedTypeArgs.forEach((typeVar, index) => {
|
760
|
-
let synthTypeVar = syntheticTypeVarContext.
|
720
|
+
let synthTypeVar = syntheticTypeVarContext.getMainSolutionSet().getTypeVarType(typeVar);
|
761
721
|
const otherSubtypes = [];
|
762
722
|
// If the resulting type is a union, try to find a matching type var and move
|
763
723
|
// the remaining subtypes to the "otherSubtypes" array.
|
764
724
|
if (synthTypeVar) {
|
765
|
-
if (
|
725
|
+
if ((0, types_1.isParamSpec)(typeVar) && (0, types_1.isFunction)(synthTypeVar)) {
|
766
726
|
synthTypeVar = (0, typeUtils_1.convertParamSpecValueToType)(synthTypeVar);
|
767
727
|
}
|
768
728
|
if ((0, types_1.isUnion)(synthTypeVar)) {
|
@@ -789,7 +749,7 @@ function addConstraintsForExpectedType(evaluator, type, expectedType, typeVarCon
|
|
789
749
|
(0, types_1.isTypeVar)(synthTypeVar) &&
|
790
750
|
synthTypeVar.shared.isSynthesized &&
|
791
751
|
synthTypeVar.shared.synthesizedIndex !== undefined) {
|
792
|
-
const targetTypeVar = types_1.ClassType.
|
752
|
+
const targetTypeVar = types_1.ClassType.getTypeParams(specializedType)[synthTypeVar.shared.synthesizedIndex];
|
793
753
|
if (index < expectedTypeArgs.length) {
|
794
754
|
let typeArgValue = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(expectedTypeArgs[index]);
|
795
755
|
if (otherSubtypes.length > 0) {
|
@@ -803,7 +763,7 @@ function addConstraintsForExpectedType(evaluator, type, expectedType, typeVarCon
|
|
803
763
|
// If this type variable already has a type, don't overwrite it. This can
|
804
764
|
// happen if a single type variable in the derived class is used multiple times
|
805
765
|
// in the specialized base class type (e.g. Mapping[T, T]).
|
806
|
-
if (typeVarContext.
|
766
|
+
if (typeVarContext.getMainSolutionSet().getTypeVarType(targetTypeVar)) {
|
807
767
|
isResultValid = false;
|
808
768
|
typeArgValue = types_1.UnknownType.create();
|
809
769
|
}
|
@@ -823,7 +783,7 @@ exports.addConstraintsForExpectedType = addConstraintsForExpectedType;
|
|
823
783
|
// For normal TypeVars, the constraint solver can widen a type by combining
|
824
784
|
// two otherwise incompatible types into a union. For TypeVarTuples, we need
|
825
785
|
// to do the equivalent operation for unpacked tuples.
|
826
|
-
function
|
786
|
+
function widenTypeForTypeVarTuple(evaluator, type1, type2) {
|
827
787
|
// The typing spec indicates that the type should always be "exactly
|
828
788
|
// the same type" if a TypeVarTuple is used in multiple locations.
|
829
789
|
// This is problematic for a number of reasons, but in the interest
|
@@ -833,9 +793,9 @@ function widenTypeForVariadicTypeVar(evaluator, type1, type2) {
|
|
833
793
|
return undefined;
|
834
794
|
}
|
835
795
|
// If the two unpacked tuples are not the same length, we can't combine them.
|
836
|
-
if (!type1.priv.
|
837
|
-
!type2.priv.
|
838
|
-
type1.priv.
|
796
|
+
if (!type1.priv.tupleTypeArgs ||
|
797
|
+
!type2.priv.tupleTypeArgs ||
|
798
|
+
type1.priv.tupleTypeArgs.length !== type2.priv.tupleTypeArgs.length) {
|
839
799
|
return undefined;
|
840
800
|
}
|
841
801
|
const strippedType1 = stripLiteralValueForUnpackedTuple(evaluator, type1);
|
@@ -848,11 +808,11 @@ function widenTypeForVariadicTypeVar(evaluator, type1, type2) {
|
|
848
808
|
// If the provided type is an unpacked tuple, this function strips the
|
849
809
|
// literals from types of the corresponding elements.
|
850
810
|
function stripLiteralValueForUnpackedTuple(evaluator, type) {
|
851
|
-
if (!(0, types_1.isUnpackedClass)(type) || !type.priv.
|
811
|
+
if (!(0, types_1.isUnpackedClass)(type) || !type.priv.tupleTypeArgs) {
|
852
812
|
return type;
|
853
813
|
}
|
854
814
|
let strippedLiteral = false;
|
855
|
-
const tupleTypeArgs = type.priv.
|
815
|
+
const tupleTypeArgs = type.priv.tupleTypeArgs.map((arg) => {
|
856
816
|
const strippedType = evaluator.stripLiteralValue(arg.type);
|
857
817
|
if (strippedType !== arg.type) {
|
858
818
|
strippedLiteral = true;
|
@@ -866,44 +826,44 @@ function stripLiteralValueForUnpackedTuple(evaluator, type) {
|
|
866
826
|
if (!strippedLiteral) {
|
867
827
|
return type;
|
868
828
|
}
|
869
|
-
return (0, typeUtils_1.specializeTupleClass)(type, tupleTypeArgs, /*
|
829
|
+
return (0, typeUtils_1.specializeTupleClass)(type, tupleTypeArgs, /* isTypeArgExplicit */ true, /* isUnpackedTuple */ true);
|
870
830
|
}
|
871
831
|
// This function is used for debugging only. It dumps the current contents of
|
872
832
|
// the TypeVarContext to the console.
|
873
833
|
function logTypeVarContext(evaluator, typeVarContext, indent) {
|
874
|
-
const
|
875
|
-
if (
|
834
|
+
const solutionSetCount = typeVarContext.getSolutionSets().length;
|
835
|
+
if (solutionSetCount === 0) {
|
876
836
|
console.log(`${indent} no signatures`);
|
877
837
|
}
|
878
|
-
else if (
|
879
|
-
|
838
|
+
else if (solutionSetCount === 1) {
|
839
|
+
logTypeVarSolutionSet(evaluator, typeVarContext.getSolutionSets()[0], `${indent} `);
|
880
840
|
}
|
881
841
|
else {
|
882
|
-
typeVarContext.
|
883
|
-
console.log(`${indent} signature ${
|
884
|
-
|
842
|
+
typeVarContext.doForEachSolutionSet((solutionSet, index) => {
|
843
|
+
console.log(`${indent} signature ${index}`);
|
844
|
+
logTypeVarSolutionSet(evaluator, solutionSet, `${indent} `);
|
885
845
|
});
|
886
846
|
}
|
887
847
|
}
|
888
|
-
function
|
848
|
+
function logTypeVarSolutionSet(evaluator, context, indent) {
|
889
849
|
let loggedConstraint = false;
|
890
850
|
context.getTypeVars().forEach((entry) => {
|
891
851
|
var _a;
|
892
852
|
const typeVarName = `${indent}${entry.typeVar.shared.name}`;
|
893
|
-
const
|
894
|
-
const
|
895
|
-
// Log the
|
896
|
-
if (
|
897
|
-
console.log(`${typeVarName} = ${evaluator.printType(
|
853
|
+
const lowerBound = (_a = entry.lowerBoundNoLiterals) !== null && _a !== void 0 ? _a : entry.lowerBound;
|
854
|
+
const upperBound = entry.upperBound;
|
855
|
+
// Log the lower and upper bounds.
|
856
|
+
if (lowerBound && upperBound && (0, types_1.isTypeSame)(lowerBound, upperBound)) {
|
857
|
+
console.log(`${typeVarName} = ${evaluator.printType(lowerBound)}`);
|
898
858
|
loggedConstraint = true;
|
899
859
|
}
|
900
860
|
else {
|
901
|
-
if (
|
902
|
-
console.log(`${typeVarName} ≤ ${evaluator.printType(
|
861
|
+
if (lowerBound) {
|
862
|
+
console.log(`${typeVarName} ≤ ${evaluator.printType(lowerBound)}`);
|
903
863
|
loggedConstraint = true;
|
904
864
|
}
|
905
|
-
if (
|
906
|
-
console.log(`${typeVarName} ≥ ${evaluator.printType(
|
865
|
+
if (upperBound) {
|
866
|
+
console.log(`${typeVarName} ≥ ${evaluator.printType(upperBound)}`);
|
907
867
|
loggedConstraint = true;
|
908
868
|
}
|
909
869
|
}
|