@zzzen/pyright-internal 1.2.0-dev.20240721 → 1.2.0-dev.20240804
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 +15 -12
- 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 +5 -3
- package/dist/analyzer/checker.js +238 -193
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.d.ts +2 -2
- package/dist/analyzer/codeFlowEngine.js +53 -46
- 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/constraintSolution.d.ts +21 -0
- package/dist/analyzer/constraintSolution.js +71 -0
- package/dist/analyzer/constraintSolution.js.map +1 -0
- package/dist/analyzer/constraintSolver.d.ts +10 -6
- package/dist/analyzer/constraintSolver.js +520 -462
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constraintTracker.d.ts +44 -0
- package/dist/analyzer/constraintTracker.js +230 -0
- package/dist/analyzer/constraintTracker.js.map +1 -0
- package/dist/analyzer/constructorTransform.d.ts +2 -2
- package/dist/analyzer/constructorTransform.js +25 -25
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.d.ts +3 -3
- package/dist/analyzer/constructors.js +80 -64
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.d.ts +2 -2
- package/dist/analyzer/dataClasses.js +87 -61
- 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 -5
- 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 +5 -5
- 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.d.ts +1 -2
- package/dist/analyzer/operations.js +105 -82
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +16 -12
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.d.ts +12 -12
- package/dist/analyzer/parameterUtils.js +67 -69
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.d.ts +6 -6
- package/dist/analyzer/parseTreeUtils.js +22 -22
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +122 -101
- 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.d.ts +3 -2
- package/dist/analyzer/properties.js +27 -27
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.d.ts +3 -3
- package/dist/analyzer/protocols.js +58 -57
- 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/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 +253 -0
- package/dist/analyzer/tuples.js.map +1 -0
- package/dist/analyzer/typeComplexity.d.ts +2 -0
- package/dist/analyzer/typeComplexity.js +94 -0
- package/dist/analyzer/typeComplexity.js.map +1 -0
- package/dist/analyzer/typeEvaluator.js +1758 -1979
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +69 -45
- package/dist/analyzer/typeEvaluatorTypes.js +7 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +157 -121
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +1 -1
- package/dist/analyzer/typePrinter.js +64 -61
- 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 +45 -41
- package/dist/analyzer/typeUtils.js +511 -748
- package/dist/analyzer/typeUtils.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 +4 -4
- package/dist/analyzer/typedDicts.js +73 -77
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +97 -72
- package/dist/analyzer/types.js +217 -208
- package/dist/analyzer/types.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +11 -11
- 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 +5 -4
- package/dist/languageServerBase.js +19 -12
- 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 +39 -38
- 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 +3 -3
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.js +2 -2
- 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 +5 -0
- package/dist/localization/localize.js +6 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.cs.json +5 -1
- package/dist/localization/package.nls.de.json +5 -1
- package/dist/localization/package.nls.en-us.json +5 -0
- package/dist/localization/package.nls.es.json +5 -1
- package/dist/localization/package.nls.fr.json +5 -1
- package/dist/localization/package.nls.it.json +5 -1
- package/dist/localization/package.nls.ja.json +5 -1
- package/dist/localization/package.nls.ko.json +5 -1
- package/dist/localization/package.nls.pl.json +5 -1
- package/dist/localization/package.nls.pt-br.json +5 -1
- package/dist/localization/package.nls.qps-ploc.json +5 -1
- package/dist/localization/package.nls.ru.json +5 -1
- package/dist/localization/package.nls.tr.json +5 -1
- package/dist/localization/package.nls.zh-cn.json +5 -1
- package/dist/localization/package.nls.zh-tw.json +5 -1
- package/dist/parser/parseNodes.d.ts +9 -9
- package/dist/parser/parseNodes.js +20 -20
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.js +30 -30
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/checker.test.js +12 -4
- package/dist/tests/checker.test.js.map +1 -1
- 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 +1 -1
- package/dist/tests/typeEvaluator2.test.js +12 -0
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +5 -1
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +10 -2
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +9 -3
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/typeEvaluator6.test.js +72 -60
- package/dist/tests/typeEvaluator6.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +6 -2
- package/dist/tests/typeEvaluator7.test.js.map +1 -1
- package/dist/tests/typeEvaluator8.test.js +4 -0
- package/dist/tests/typeEvaluator8.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
- package/dist/analyzer/typeVarContext.d.ts +0 -60
- package/dist/analyzer/typeVarContext.js +0 -392
- package/dist/analyzer/typeVarContext.js.map +0 -1
@@ -17,12 +17,12 @@ const diagnostic_1 = require("../common/diagnostic");
|
|
17
17
|
const diagnosticRules_1 = require("../common/diagnosticRules");
|
18
18
|
const localize_1 = require("../localization/localize");
|
19
19
|
const constraintSolver_1 = require("./constraintSolver");
|
20
|
+
const constraintTracker_1 = require("./constraintTracker");
|
20
21
|
const parseTreeUtils_1 = require("./parseTreeUtils");
|
21
|
-
const typeGuards_1 = require("./typeGuards");
|
22
|
-
const typeUtils_1 = require("./typeUtils");
|
23
|
-
const typeVarContext_1 = require("./typeVarContext");
|
24
22
|
const typedDicts_1 = require("./typedDicts");
|
23
|
+
const typeGuards_1 = require("./typeGuards");
|
25
24
|
const types_1 = require("./types");
|
25
|
+
const typeUtils_1 = require("./typeUtils");
|
26
26
|
// PEP 634 indicates that several built-in classes are handled differently
|
27
27
|
// when used with class pattern matching.
|
28
28
|
const classPatternSpecialCases = [
|
@@ -110,6 +110,7 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
110
110
|
let isPlausibleMatch = true;
|
111
111
|
let isDefiniteMatch = true;
|
112
112
|
const narrowedEntryTypes = [];
|
113
|
+
const unnarrowedEntryTypes = [];
|
113
114
|
let canNarrowTuple = entry.isTuple;
|
114
115
|
// Don't attempt to narrow tuples in the negative case if the subject
|
115
116
|
// contains indeterminate-length entries or the tuple is of indeterminate
|
@@ -118,8 +119,8 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
118
119
|
if (entry.isIndeterminateLength) {
|
119
120
|
canNarrowTuple = false;
|
120
121
|
}
|
121
|
-
if ((0, types_1.isClassInstance)(entry.subtype) && entry.subtype.priv.
|
122
|
-
const unboundedIndex = entry.subtype.priv.
|
122
|
+
if ((0, types_1.isClassInstance)(entry.subtype) && entry.subtype.priv.tupleTypeArgs) {
|
123
|
+
const unboundedIndex = entry.subtype.priv.tupleTypeArgs.findIndex((typeArg) => typeArg.isUnbounded);
|
123
124
|
if (unboundedIndex >= 0) {
|
124
125
|
// If the pattern includes a "star" entry that aligns exactly with
|
125
126
|
// the corresponding unbounded entry in the tuple, we can narrow
|
@@ -138,18 +139,19 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
138
139
|
isDefiniteMatch = false;
|
139
140
|
}
|
140
141
|
}
|
141
|
-
|
142
|
+
const negativeNarrowedDims = [];
|
142
143
|
pattern.d.entries.forEach((sequenceEntry, index) => {
|
143
144
|
const entryType = getTypeOfPatternSequenceEntry(evaluator, pattern, entry, index, pattern.d.entries.length, pattern.d.starEntryIndex,
|
144
145
|
/* unpackStarEntry */ true);
|
146
|
+
unnarrowedEntryTypes.push(entryType);
|
145
147
|
const narrowedEntryType = narrowTypeBasedOnPattern(evaluator, entryType, sequenceEntry, isPositiveTest);
|
146
148
|
if (isPositiveTest) {
|
147
149
|
if (index === pattern.d.starEntryIndex) {
|
148
150
|
if ((0, types_1.isClassInstance)(narrowedEntryType) &&
|
149
|
-
narrowedEntryType.priv.
|
151
|
+
narrowedEntryType.priv.tupleTypeArgs &&
|
150
152
|
!(0, typeUtils_1.isUnboundedTupleClass)(narrowedEntryType) &&
|
151
|
-
narrowedEntryType.priv.
|
152
|
-
(0, collectionUtils_1.appendArray)(narrowedEntryTypes, narrowedEntryType.priv.
|
153
|
+
narrowedEntryType.priv.tupleTypeArgs) {
|
154
|
+
(0, collectionUtils_1.appendArray)(narrowedEntryTypes, narrowedEntryType.priv.tupleTypeArgs.map((t) => t.type));
|
153
155
|
}
|
154
156
|
else {
|
155
157
|
narrowedEntryTypes.push(narrowedEntryType);
|
@@ -169,10 +171,11 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
169
171
|
}
|
170
172
|
if (!(0, types_1.isNever)(narrowedEntryType)) {
|
171
173
|
isDefiniteMatch = false;
|
172
|
-
// Record
|
173
|
-
//
|
174
|
-
//
|
175
|
-
|
174
|
+
// Record which entries were narrowed in the negative case
|
175
|
+
// by storing their indexes. If more than one is narrowed,
|
176
|
+
// we need to perform tuple expansion to represent the
|
177
|
+
// resulting narrowed type.
|
178
|
+
negativeNarrowedDims.push(index);
|
176
179
|
narrowedEntryTypes.push(narrowedEntryType);
|
177
180
|
}
|
178
181
|
else {
|
@@ -199,12 +202,22 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
199
202
|
return false;
|
200
203
|
}
|
201
204
|
// Can we narrow a tuple?
|
202
|
-
if (canNarrowTuple &&
|
205
|
+
if (canNarrowTuple && negativeNarrowedDims.length > 0) {
|
203
206
|
const tupleClassType = evaluator.getBuiltInType(pattern, 'tuple');
|
204
207
|
if (tupleClassType && (0, types_1.isInstantiableClass)(tupleClassType)) {
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
+
// Expand the tuple in the dimensions that were narrowed.
|
209
|
+
// Start with the fully-narrowed set of entries.
|
210
|
+
const expandedEntryTypes = [];
|
211
|
+
for (const dim of negativeNarrowedDims) {
|
212
|
+
const newEntryTypes = [...unnarrowedEntryTypes];
|
213
|
+
newEntryTypes[dim] = narrowedEntryTypes[dim];
|
214
|
+
expandedEntryTypes.push(newEntryTypes);
|
215
|
+
}
|
216
|
+
entry.subtype = (0, types_1.combineTypes)(expandedEntryTypes.map((entryTypes) => {
|
217
|
+
return types_1.ClassType.cloneAsInstance((0, typeUtils_1.specializeTupleClass)(tupleClassType, entryTypes.map((t) => {
|
218
|
+
return { type: t, isUnbounded: false };
|
219
|
+
})));
|
220
|
+
}));
|
208
221
|
}
|
209
222
|
}
|
210
223
|
return true;
|
@@ -229,7 +242,7 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
229
242
|
// If the type is a union that contains Any or Unknown, remove the other types
|
230
243
|
// before wrapping it in a Sequence.
|
231
244
|
typeArgType = (_a = (0, typeUtils_1.containsAnyOrUnknown)(typeArgType, /* recurse */ false)) !== null && _a !== void 0 ? _a : typeArgType;
|
232
|
-
entry.subtype = types_1.ClassType.cloneAsInstance(types_1.ClassType.
|
245
|
+
entry.subtype = types_1.ClassType.cloneAsInstance(types_1.ClassType.specialize(sequenceType, [typeArgType]));
|
233
246
|
}
|
234
247
|
}
|
235
248
|
}
|
@@ -380,8 +393,8 @@ function getPositionalMatchArgNames(evaluator, type) {
|
|
380
393
|
if ((0, types_1.isClassInstance)(matchArgsType) &&
|
381
394
|
(0, typeUtils_1.isTupleClass)(matchArgsType) &&
|
382
395
|
!(0, typeUtils_1.isUnboundedTupleClass)(matchArgsType) &&
|
383
|
-
matchArgsType.priv.
|
384
|
-
const tupleArgs = matchArgsType.priv.
|
396
|
+
matchArgsType.priv.tupleTypeArgs) {
|
397
|
+
const tupleArgs = matchArgsType.priv.tupleTypeArgs;
|
385
398
|
// Are all the args string literals?
|
386
399
|
if (tupleArgs.every((arg) => (0, types_1.isClassInstance)(arg.type) && types_1.ClassType.isBuiltIn(arg.type, 'str') && (0, typeUtils_1.isLiteralType)(arg.type))) {
|
387
400
|
return tupleArgs.map((arg) => arg.type.priv.literalValue);
|
@@ -455,10 +468,8 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
455
468
|
return type;
|
456
469
|
}
|
457
470
|
let classType = exprType;
|
458
|
-
if (classType.shared.
|
459
|
-
classType = types_1.ClassType.
|
460
|
-
/* typeArguments */ undefined,
|
461
|
-
/* isTypeArgumentExplicit */ false);
|
471
|
+
if (classType.shared.typeParams.length > 0) {
|
472
|
+
classType = types_1.ClassType.specialize(classType, /* typeArgs */ undefined);
|
462
473
|
}
|
463
474
|
const classInstance = (0, typeUtils_1.convertToInstance)(classType);
|
464
475
|
const isPatternMetaclass = (0, typeUtils_1.isMetaclassInstance)(classInstance);
|
@@ -515,7 +526,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
515
526
|
}
|
516
527
|
}
|
517
528
|
for (let index = 0; index < pattern.d.args.length; index++) {
|
518
|
-
const narrowedArgType =
|
529
|
+
const narrowedArgType = narrowTypeOfClassPatternArg(evaluator, pattern.d.args[index], index, positionalArgNames, subjectSubtypeExpanded, isPositiveTest);
|
519
530
|
if (!(0, types_1.isNever)(narrowedArgType)) {
|
520
531
|
return subjectSubtypeUnexpanded;
|
521
532
|
}
|
@@ -547,7 +558,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
547
558
|
if ((0, types_1.isInstantiableClass)(expandedSubtype) && types_1.ClassType.isBuiltIn(expandedSubtype, 'Callable')) {
|
548
559
|
// Convert to an unknown callable type.
|
549
560
|
const unknownCallable = types_1.FunctionType.createSynthesizedInstance('', 32768 /* FunctionTypeFlags.GradualCallableForm */);
|
550
|
-
types_1.FunctionType.
|
561
|
+
types_1.FunctionType.addDefaultParams(unknownCallable,
|
551
562
|
/* useUnknown */ (0, types_1.isUnknown)(subjectSubtypeExpanded));
|
552
563
|
unknownCallable.shared.declaredReturnType = subjectSubtypeExpanded;
|
553
564
|
return unknownCallable;
|
@@ -592,18 +603,19 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
592
603
|
// Try to retain the type arguments for the pattern class type.
|
593
604
|
if ((0, types_1.isInstantiableClass)(unexpandedSubtype) && (0, types_1.isClassInstance)(subjectSubtypeExpanded)) {
|
594
605
|
if (types_1.ClassType.isSpecialBuiltIn(unexpandedSubtype) ||
|
595
|
-
unexpandedSubtype.shared.
|
596
|
-
const
|
597
|
-
const unspecializedMatchType = types_1.ClassType.
|
598
|
-
/*
|
599
|
-
/* isTypeArgumentExplicit */ false);
|
606
|
+
unexpandedSubtype.shared.typeParams.length > 0) {
|
607
|
+
const constraints = new constraintTracker_1.ConstraintTracker();
|
608
|
+
const unspecializedMatchType = types_1.ClassType.specialize(unexpandedSubtype,
|
609
|
+
/* typeArgs */ undefined);
|
600
610
|
const matchTypeInstance = types_1.ClassType.cloneAsInstance(unspecializedMatchType);
|
601
|
-
if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluator, matchTypeInstance, subjectSubtypeExpanded,
|
611
|
+
if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluator, matchTypeInstance, subjectSubtypeExpanded, constraints,
|
602
612
|
/* liveTypeVarScopes */ undefined,
|
603
613
|
/* usageOffset */ undefined)) {
|
604
|
-
resultType =
|
605
|
-
|
606
|
-
|
614
|
+
resultType = evaluator.solveAndApplyConstraints(matchTypeInstance, constraints, {
|
615
|
+
replaceUnsolved: {
|
616
|
+
scopeIds: (0, typeUtils_1.getTypeVarScopeIds)(unexpandedSubtype),
|
617
|
+
tupleClassType: evaluator.getTupleClassType(),
|
618
|
+
},
|
607
619
|
});
|
608
620
|
}
|
609
621
|
}
|
@@ -623,7 +635,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
623
635
|
// Narrow the arg pattern. It's possible that the actual type of the object
|
624
636
|
// being matched is a subtype of the resultType, so it might contain additional
|
625
637
|
// attributes that we don't know about.
|
626
|
-
const narrowedArgType =
|
638
|
+
const narrowedArgType = narrowTypeOfClassPatternArg(evaluator, arg, index, positionalArgNames, resultType, isPositiveTest);
|
627
639
|
if ((0, types_1.isNever)(narrowedArgType)) {
|
628
640
|
isMatchValid = false;
|
629
641
|
}
|
@@ -658,7 +670,7 @@ function isClassSpecialCaseForClassPattern(classType) {
|
|
658
670
|
return false;
|
659
671
|
}
|
660
672
|
// Narrows the pattern provided for a class pattern argument.
|
661
|
-
function
|
673
|
+
function narrowTypeOfClassPatternArg(evaluator, arg, argIndex, positionalArgNames, matchType, isPositiveTest) {
|
662
674
|
var _a;
|
663
675
|
let argName;
|
664
676
|
if (arg.d.name) {
|
@@ -802,44 +814,40 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
802
814
|
});
|
803
815
|
return;
|
804
816
|
}
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
if ((0, types_1.isInstantiableClass)(mroClass) && types_1.ClassType.isBuiltIn(mroClass, 'Mapping')) {
|
809
|
-
mroClassToSpecialize = mroClass;
|
810
|
-
break;
|
811
|
-
}
|
817
|
+
const mappingType = evaluator.getTypingType(node, 'Mapping');
|
818
|
+
if (!mappingType || !(0, types_1.isInstantiableClass)(mappingType)) {
|
819
|
+
return;
|
812
820
|
}
|
813
|
-
|
814
|
-
|
815
|
-
|
821
|
+
const mappingObject = types_1.ClassType.cloneAsInstance(mappingType);
|
822
|
+
// Is it a subtype of Mapping?
|
823
|
+
const constraints = new constraintTracker_1.ConstraintTracker();
|
824
|
+
if (evaluator.assignType(mappingObject, subtype, /* diag */ undefined, constraints)) {
|
825
|
+
const specializedMapping = evaluator.solveAndApplyConstraints(mappingObject, constraints);
|
826
|
+
if (specializedMapping.priv.typeArgs && specializedMapping.priv.typeArgs.length >= 2) {
|
816
827
|
mappingInfo.push({
|
817
828
|
subtype,
|
818
829
|
isDefinitelyMapping: true,
|
819
830
|
isDefinitelyNotMapping: false,
|
820
831
|
dictTypeArgs: {
|
821
|
-
key: specializedMapping.priv.
|
822
|
-
value: specializedMapping.priv.
|
832
|
+
key: specializedMapping.priv.typeArgs[0],
|
833
|
+
value: specializedMapping.priv.typeArgs[1],
|
823
834
|
},
|
824
835
|
});
|
825
836
|
}
|
826
837
|
return;
|
827
838
|
}
|
828
839
|
// Is it a superclass of Mapping?
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
},
|
841
|
-
});
|
842
|
-
}
|
840
|
+
if (evaluator.assignType(subtype, mappingObject)) {
|
841
|
+
mappingInfo.push({
|
842
|
+
subtype,
|
843
|
+
isDefinitelyMapping: false,
|
844
|
+
isDefinitelyNotMapping: false,
|
845
|
+
dictTypeArgs: {
|
846
|
+
key: types_1.UnknownType.create(),
|
847
|
+
value: types_1.UnknownType.create(),
|
848
|
+
},
|
849
|
+
});
|
850
|
+
return;
|
843
851
|
}
|
844
852
|
mappingInfo.push({
|
845
853
|
subtype,
|
@@ -861,7 +869,6 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
861
869
|
var _a;
|
862
870
|
const concreteSubtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
863
871
|
let mroClassToSpecialize;
|
864
|
-
let pushedEntry = false;
|
865
872
|
if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
866
873
|
for (const mroClass of concreteSubtype.shared.mro) {
|
867
874
|
if (!(0, types_1.isInstantiableClass)(mroClass)) {
|
@@ -892,10 +899,10 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
892
899
|
if (mroClassToSpecialize) {
|
893
900
|
const specializedSequence = (0, typeUtils_1.partiallySpecializeType)(mroClassToSpecialize, concreteSubtype, evaluator.getTypeClassType());
|
894
901
|
if ((0, typeUtils_1.isTupleClass)(specializedSequence)) {
|
895
|
-
const typeArgs = (_a = specializedSequence.priv.
|
902
|
+
const typeArgs = (_a = specializedSequence.priv.tupleTypeArgs) !== null && _a !== void 0 ? _a : [
|
896
903
|
{ type: types_1.UnknownType.create(), isUnbounded: true },
|
897
904
|
];
|
898
|
-
const tupleIndeterminateIndex = typeArgs.findIndex((t) => t.isUnbounded || (0, types_1.
|
905
|
+
const tupleIndeterminateIndex = typeArgs.findIndex((t) => t.isUnbounded || (0, types_1.isUnpackedTypeVarTuple)(t.type));
|
899
906
|
let tupleDeterminateEntryCount = typeArgs.length;
|
900
907
|
// If the tuple contains an indeterminate entry, expand or remove that
|
901
908
|
// entry to match the length of the pattern if possible.
|
@@ -917,7 +924,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
917
924
|
const removedEntries = typeArgs.splice(patternStarEntryIndex, entriesToCombine);
|
918
925
|
typeArgs.splice(patternStarEntryIndex, 0, {
|
919
926
|
type: (0, types_1.combineTypes)(removedEntries.map((t) => t.type)),
|
920
|
-
isUnbounded: removedEntries.every((t) => t.isUnbounded || (0, types_1.
|
927
|
+
isUnbounded: removedEntries.every((t) => t.isUnbounded || (0, types_1.isUnpackedTypeVarTuple)(t.type)),
|
921
928
|
});
|
922
929
|
}
|
923
930
|
if (typeArgs.length === patternEntryCount) {
|
@@ -949,7 +956,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
949
956
|
isDefiniteNoMatch,
|
950
957
|
isPotentialNoMatch,
|
951
958
|
});
|
952
|
-
|
959
|
+
return;
|
953
960
|
}
|
954
961
|
// If the pattern contains a star entry and the pattern associated with
|
955
962
|
// the star entry is unbounded, we can remove it completely under the
|
@@ -988,7 +995,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
988
995
|
isTuple: true,
|
989
996
|
isDefiniteNoMatch,
|
990
997
|
});
|
991
|
-
|
998
|
+
return;
|
992
999
|
}
|
993
1000
|
}
|
994
1001
|
}
|
@@ -996,28 +1003,44 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
996
1003
|
sequenceInfo.push({
|
997
1004
|
subtype,
|
998
1005
|
entryTypes: [
|
999
|
-
specializedSequence.priv.
|
1000
|
-
? specializedSequence.priv.
|
1006
|
+
specializedSequence.priv.typeArgs && specializedSequence.priv.typeArgs.length > 0
|
1007
|
+
? specializedSequence.priv.typeArgs[0]
|
1001
1008
|
: types_1.UnknownType.create(),
|
1002
1009
|
],
|
1003
1010
|
isIndeterminateLength: true,
|
1004
1011
|
isDefiniteNoMatch: false,
|
1005
1012
|
});
|
1006
|
-
|
1013
|
+
return;
|
1007
1014
|
}
|
1008
1015
|
}
|
1009
1016
|
}
|
1010
|
-
if (!
|
1011
|
-
// If it wasn't a subtype of Sequence, see if it's a supertype.
|
1017
|
+
if (!mroClassToSpecialize) {
|
1012
1018
|
const sequenceType = evaluator.getTypingType(pattern, 'Sequence');
|
1013
1019
|
if (sequenceType && (0, types_1.isInstantiableClass)(sequenceType)) {
|
1014
|
-
const
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1020
|
+
const sequenceObject = types_1.ClassType.cloneAsInstance(sequenceType);
|
1021
|
+
// Is it a subtype of Sequence?
|
1022
|
+
const constraints = new constraintTracker_1.ConstraintTracker();
|
1023
|
+
if (evaluator.assignType(sequenceObject, subtype, /* diag */ undefined, constraints)) {
|
1024
|
+
const specializedSequence = evaluator.solveAndApplyConstraints(sequenceObject, constraints);
|
1025
|
+
if (specializedSequence.priv.typeArgs && specializedSequence.priv.typeArgs.length > 0) {
|
1026
|
+
sequenceInfo.push({
|
1027
|
+
subtype,
|
1028
|
+
entryTypes: [specializedSequence.priv.typeArgs[0]],
|
1029
|
+
isIndeterminateLength: true,
|
1030
|
+
isDefiniteNoMatch: false,
|
1031
|
+
isPotentialNoMatch: false,
|
1032
|
+
});
|
1033
|
+
return;
|
1034
|
+
}
|
1035
|
+
}
|
1036
|
+
// If it wasn't a subtype of Sequence, see if it's a supertype.
|
1037
|
+
const sequenceConstraints = new constraintTracker_1.ConstraintTracker();
|
1038
|
+
if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluator, types_1.ClassType.cloneAsInstance(sequenceType), subtype, sequenceConstraints, (0, parseTreeUtils_1.getTypeVarScopesForNode)(pattern), pattern.start)) {
|
1039
|
+
const specializedSequence = evaluator.solveAndApplyConstraints(types_1.ClassType.cloneAsInstantiable(sequenceType), sequenceConstraints);
|
1040
|
+
if (specializedSequence.priv.typeArgs && specializedSequence.priv.typeArgs.length > 0) {
|
1018
1041
|
sequenceInfo.push({
|
1019
1042
|
subtype,
|
1020
|
-
entryTypes: [specializedSequence.priv.
|
1043
|
+
entryTypes: [specializedSequence.priv.typeArgs[0]],
|
1021
1044
|
isIndeterminateLength: true,
|
1022
1045
|
isDefiniteNoMatch: false,
|
1023
1046
|
isPotentialNoMatch: true,
|
@@ -1025,8 +1048,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
1025
1048
|
return;
|
1026
1049
|
}
|
1027
1050
|
}
|
1028
|
-
if (evaluator.assignType(subtype, types_1.ClassType.
|
1029
|
-
/* isTypeArgumentExplicit */ true))) {
|
1051
|
+
if (evaluator.assignType(subtype, types_1.ClassType.specialize(types_1.ClassType.cloneAsInstance(sequenceType), [types_1.UnknownType.create()]))) {
|
1030
1052
|
sequenceInfo.push({
|
1031
1053
|
subtype,
|
1032
1054
|
entryTypes: [types_1.UnknownType.create()],
|
@@ -1037,14 +1059,14 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
1037
1059
|
return;
|
1038
1060
|
}
|
1039
1061
|
}
|
1040
|
-
// Push an entry that indicates that this is definitely not a match.
|
1041
|
-
sequenceInfo.push({
|
1042
|
-
subtype,
|
1043
|
-
entryTypes: [],
|
1044
|
-
isIndeterminateLength: true,
|
1045
|
-
isDefiniteNoMatch: true,
|
1046
|
-
});
|
1047
1062
|
}
|
1063
|
+
// Push an entry that indicates that this is definitely not a match.
|
1064
|
+
sequenceInfo.push({
|
1065
|
+
subtype,
|
1066
|
+
entryTypes: [],
|
1067
|
+
isIndeterminateLength: true,
|
1068
|
+
isDefiniteNoMatch: true,
|
1069
|
+
});
|
1048
1070
|
});
|
1049
1071
|
return sequenceInfo;
|
1050
1072
|
}
|
@@ -1065,11 +1087,11 @@ function getTypeOfPatternSequenceEntry(evaluator, node, sequenceInfo, entryIndex
|
|
1065
1087
|
const starEntryTypes = sequenceInfo.entryTypes
|
1066
1088
|
.slice(starEntryIndex, starEntryIndex + sequenceInfo.entryTypes.length - entryCount + 1)
|
1067
1089
|
.map((type) => {
|
1068
|
-
// If this is a
|
1090
|
+
// If this is a TypeVarTuple, there's not much we can say about
|
1069
1091
|
// its type other than it's "Unknown". We could evaluate it as an
|
1070
1092
|
// "object", but that will cause problems given that this type will
|
1071
1093
|
// be wrapped in a "list" below, and lists are invariant.
|
1072
|
-
if ((0, types_1.
|
1094
|
+
if ((0, types_1.isTypeVarTuple)(type) && !type.priv.isInUnion) {
|
1073
1095
|
return types_1.UnknownType.create();
|
1074
1096
|
}
|
1075
1097
|
return evaluator.stripLiteralValue(type);
|
@@ -1189,8 +1211,7 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1189
1211
|
const dictClass = evaluator.getBuiltInType(pattern, 'dict');
|
1190
1212
|
const strType = evaluator.getBuiltInObject(pattern, 'str');
|
1191
1213
|
const dictType = dictClass && (0, types_1.isInstantiableClass)(dictClass) && (0, types_1.isClassInstance)(strType)
|
1192
|
-
? types_1.ClassType.cloneAsInstance(types_1.ClassType.
|
1193
|
-
/* isTypeArgumentExplicit */ true))
|
1214
|
+
? types_1.ClassType.cloneAsInstance(types_1.ClassType.specialize(dictClass, [keyType, valueType]))
|
1194
1215
|
: types_1.UnknownType.create();
|
1195
1216
|
evaluator.assignTypeToExpression(mappingEntry.d.target, { type: dictType, isIncomplete: isTypeIncomplete }, mappingEntry.d.target);
|
1196
1217
|
}
|
@@ -1216,7 +1237,7 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1216
1237
|
positionalArgNames = getPositionalMatchArgNames(evaluator, types_1.ClassType.cloneAsInstantiable(expandedSubtype));
|
1217
1238
|
}
|
1218
1239
|
pattern.d.args.forEach((arg, index) => {
|
1219
|
-
const narrowedArgType =
|
1240
|
+
const narrowedArgType = narrowTypeOfClassPatternArg(evaluator, arg, index, positionalArgNames, types_1.ClassType.cloneAsInstantiable(expandedSubtype),
|
1220
1241
|
/* isPositiveTest */ true);
|
1221
1242
|
argTypes[index].push(narrowedArgType);
|
1222
1243
|
});
|
@@ -1255,7 +1276,7 @@ function wrapTypeInList(evaluator, node, type) {
|
|
1255
1276
|
// If the type is a union that contains an Any or Unknown, eliminate the other
|
1256
1277
|
// types before wrapping it in a list.
|
1257
1278
|
type = (_a = (0, typeUtils_1.containsAnyOrUnknown)(type, /* recurse */ false)) !== null && _a !== void 0 ? _a : type;
|
1258
|
-
return types_1.ClassType.
|
1279
|
+
return types_1.ClassType.specialize(listObjectType, [type]);
|
1259
1280
|
}
|
1260
1281
|
return types_1.UnknownType.create();
|
1261
1282
|
}
|
@@ -1273,8 +1294,8 @@ function validateClassPattern(evaluator, pattern) {
|
|
1273
1294
|
// Check for certain uses of type aliases that generate runtime exceptions.
|
1274
1295
|
if (((_b = exprType.props) === null || _b === void 0 ? void 0 : _b.typeAliasInfo) &&
|
1275
1296
|
(0, types_1.isInstantiableClass)(exprType) &&
|
1276
|
-
exprType.priv.
|
1277
|
-
exprType.priv.
|
1297
|
+
exprType.priv.typeArgs &&
|
1298
|
+
exprType.priv.isTypeArgExplicit) {
|
1278
1299
|
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classPatternTypeAlias().format({ type: evaluator.printType(exprType) }), pattern.d.className);
|
1279
1300
|
}
|
1280
1301
|
else if (!(0, types_1.isInstantiableClass)(exprType)) {
|
@@ -1324,7 +1345,7 @@ function getPatternSubtypeNarrowingCallback(evaluator, reference, subjectExpress
|
|
1324
1345
|
if (subjectExpression.nodeType === 27 /* ParseNodeType.Index */ &&
|
1325
1346
|
subjectExpression.d.items.length === 1 &&
|
1326
1347
|
!subjectExpression.d.trailingComma &&
|
1327
|
-
subjectExpression.d.items[0].d.argCategory === 0 /*
|
1348
|
+
subjectExpression.d.items[0].d.argCategory === 0 /* ArgCategory.Simple */ &&
|
1328
1349
|
(0, parseTreeUtils_1.isMatchingExpression)(reference, subjectExpression.d.leftExpr)) {
|
1329
1350
|
const indexTypeResult = evaluator.getTypeOfExpression(subjectExpression.d.items[0].d.valueExpr);
|
1330
1351
|
const indexType = indexTypeResult.type;
|
@@ -1375,10 +1396,10 @@ function getPatternSubtypeNarrowingCallback(evaluator, reference, subjectExpress
|
|
1375
1396
|
(0, typeUtils_1.doForEachSubtype)(narrowedSubjectType, (subtype) => {
|
1376
1397
|
if ((0, types_1.isClassInstance)(subtype) &&
|
1377
1398
|
types_1.ClassType.isBuiltIn(subtype, 'tuple') &&
|
1378
|
-
subtype.priv.
|
1379
|
-
matchingEntryIndex < subtype.priv.
|
1380
|
-
subtype.priv.
|
1381
|
-
narrowedSubtypes.push(subtype.priv.
|
1399
|
+
subtype.priv.tupleTypeArgs &&
|
1400
|
+
matchingEntryIndex < subtype.priv.tupleTypeArgs.length &&
|
1401
|
+
subtype.priv.tupleTypeArgs.every((e) => !e.isUnbounded)) {
|
1402
|
+
narrowedSubtypes.push(subtype.priv.tupleTypeArgs[matchingEntryIndex].type);
|
1382
1403
|
}
|
1383
1404
|
else if ((0, types_1.isNever)(narrowedSubjectType)) {
|
1384
1405
|
narrowedSubtypes.push(narrowedSubjectType);
|