@zzzen/pyright-internal 1.2.0-dev.20240707 → 1.2.0-dev.20240714
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 +74 -46
- package/dist/analyzer/analyzerNodeInfo.js.map +1 -1
- package/dist/analyzer/binder.js +506 -512
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +649 -634
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.d.ts +5 -3
- package/dist/analyzer/codeFlowEngine.js +87 -101
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/codeFlowTypes.js +30 -28
- package/dist/analyzer/codeFlowTypes.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +68 -64
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructorTransform.js +22 -22
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.js +51 -48
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.js +109 -175
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/declarationUtils.js +14 -15
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/decorators.js +77 -77
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/enums.js +70 -56
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/functionTransform.js +8 -18
- package/dist/analyzer/functionTransform.js.map +1 -1
- package/dist/analyzer/importResolver.d.ts +2 -1
- package/dist/analyzer/importResolver.js +40 -29
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/importStatementUtils.js +27 -27
- package/dist/analyzer/importStatementUtils.js.map +1 -1
- package/dist/analyzer/namedTuples.js +33 -59
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +66 -66
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +66 -62
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.d.ts +4 -4
- package/dist/analyzer/parameterUtils.js +28 -46
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.js +268 -269
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/parseTreeWalker.js +76 -76
- package/dist/analyzer/parseTreeWalker.js.map +1 -1
- package/dist/analyzer/patternMatching.js +176 -143
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.js +1 -1
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +78 -147
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +21 -21
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/sourceFile.js +3 -3
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceMapper.js +20 -19
- package/dist/analyzer/sourceMapper.js.map +1 -1
- package/dist/analyzer/staticExpressions.js +83 -84
- package/dist/analyzer/staticExpressions.js.map +1 -1
- package/dist/analyzer/testWalker.js +2 -2
- package/dist/analyzer/testWalker.js.map +1 -1
- package/dist/analyzer/tracePrinter.js +20 -20
- package/dist/analyzer/tracePrinter.js.map +1 -1
- package/dist/analyzer/typeDocStringUtils.js +25 -25
- package/dist/analyzer/typeDocStringUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +1884 -1937
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +6 -5
- package/dist/analyzer/typeEvaluatorTypes.js +4 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +211 -211
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.js +118 -115
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeStubWriter.js +104 -103
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +14 -3
- package/dist/analyzer/typeUtils.js +432 -402
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.js +8 -8
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typeWalker.js +13 -10
- package/dist/analyzer/typeWalker.js.map +1 -1
- package/dist/analyzer/typedDicts.js +96 -198
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +144 -102
- package/dist/analyzer/types.js +662 -494
- package/dist/analyzer/types.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +33 -33
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/fileSystem.d.ts +1 -0
- package/dist/common/fileSystem.js.map +1 -1
- package/dist/common/fullAccessHost.js +6 -4
- package/dist/common/fullAccessHost.js.map +1 -1
- package/dist/common/realFileSystem.d.ts +1 -0
- package/dist/common/realFileSystem.js +4 -0
- package/dist/common/realFileSystem.js.map +1 -1
- package/dist/common/textEditTracker.js +14 -14
- package/dist/common/textEditTracker.js.map +1 -1
- package/dist/languageService/autoImporter.js +10 -10
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +23 -23
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.js +153 -151
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/definitionProvider.d.ts +1 -1
- package/dist/languageService/definitionProvider.js +3 -1
- package/dist/languageService/definitionProvider.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.js +19 -19
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/hoverProvider.js +25 -24
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/importSorter.js +8 -8
- package/dist/languageService/importSorter.js.map +1 -1
- package/dist/languageService/referencesProvider.js +8 -8
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/signatureHelpProvider.d.ts +1 -1
- package/dist/languageService/signatureHelpProvider.js +6 -6
- package/dist/languageService/signatureHelpProvider.js.map +1 -1
- package/dist/languageService/symbolIndexer.js +3 -3
- package/dist/languageService/symbolIndexer.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +13 -13
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/parser/parseNodes.d.ts +468 -401
- package/dist/parser/parseNodes.js +626 -355
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.d.ts +1 -0
- package/dist/parser/parser.js +316 -294
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/classDeclaration.test.js +14 -14
- package/dist/tests/classDeclaration.test.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.d.ts +3 -2
- package/dist/tests/harness/vfs/filesystem.js +6 -2
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/importResolver.test.js +4 -3
- package/dist/tests/importResolver.test.js.map +1 -1
- package/dist/tests/parseTreeUtils.test.js +5 -5
- package/dist/tests/parseTreeUtils.test.js.map +1 -1
- package/dist/tests/parser.test.js +8 -8
- package/dist/tests/parser.test.js.map +1 -1
- package/dist/tests/sourceMapperUtils.test.js +7 -7
- package/dist/tests/sourceMapperUtils.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +8 -3
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typePrinter.test.js +14 -39
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/package.json +1 -1
@@ -74,9 +74,9 @@ function checkForUnusedPattern(evaluator, pattern, subjectType) {
|
|
74
74
|
if ((0, types_1.isNever)(subjectType)) {
|
75
75
|
reportUnnecessaryPattern(evaluator, pattern, subjectType);
|
76
76
|
}
|
77
|
-
else if (pattern.nodeType === 66 /* ParseNodeType.PatternAs */ && pattern.orPatterns.length > 1) {
|
77
|
+
else if (pattern.nodeType === 66 /* ParseNodeType.PatternAs */ && pattern.d.orPatterns.length > 1) {
|
78
78
|
// Check each of the or patterns separately.
|
79
|
-
pattern.orPatterns.forEach((orPattern) => {
|
79
|
+
pattern.d.orPatterns.forEach((orPattern) => {
|
80
80
|
const subjectTypeMatch = narrowTypeBasedOnPattern(evaluator, subjectType, orPattern,
|
81
81
|
/* isPositiveTest */ true);
|
82
82
|
if ((0, types_1.isNever)(subjectTypeMatch)) {
|
@@ -118,13 +118,13 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
118
118
|
if (entry.isIndeterminateLength) {
|
119
119
|
canNarrowTuple = false;
|
120
120
|
}
|
121
|
-
if ((0, types_1.isClassInstance)(entry.subtype) && entry.subtype.tupleTypeArguments) {
|
122
|
-
const unboundedIndex = entry.subtype.tupleTypeArguments.findIndex((typeArg) => typeArg.isUnbounded);
|
121
|
+
if ((0, types_1.isClassInstance)(entry.subtype) && entry.subtype.priv.tupleTypeArguments) {
|
122
|
+
const unboundedIndex = entry.subtype.priv.tupleTypeArguments.findIndex((typeArg) => typeArg.isUnbounded);
|
123
123
|
if (unboundedIndex >= 0) {
|
124
124
|
// If the pattern includes a "star" entry that aligns exactly with
|
125
125
|
// the corresponding unbounded entry in the tuple, we can narrow
|
126
126
|
// the tuple type.
|
127
|
-
if (pattern.starEntryIndex === undefined || pattern.starEntryIndex !== unboundedIndex) {
|
127
|
+
if (pattern.d.starEntryIndex === undefined || pattern.d.starEntryIndex !== unboundedIndex) {
|
128
128
|
canNarrowTuple = false;
|
129
129
|
}
|
130
130
|
}
|
@@ -134,22 +134,22 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
134
134
|
// an arbitrary number of entries or accepts at least one non-star entry,
|
135
135
|
// we can't prove that it's a definite match.
|
136
136
|
if (entry.isIndeterminateLength) {
|
137
|
-
if (pattern.entries.length !== 1 || pattern.starEntryIndex !== 0) {
|
137
|
+
if (pattern.d.entries.length !== 1 || pattern.d.starEntryIndex !== 0) {
|
138
138
|
isDefiniteMatch = false;
|
139
139
|
}
|
140
140
|
}
|
141
141
|
let negativeEntriesNarrowed = 0;
|
142
|
-
pattern.entries.forEach((sequenceEntry, index) => {
|
143
|
-
const entryType = getTypeOfPatternSequenceEntry(evaluator, pattern, entry, index, pattern.entries.length, pattern.starEntryIndex,
|
142
|
+
pattern.d.entries.forEach((sequenceEntry, index) => {
|
143
|
+
const entryType = getTypeOfPatternSequenceEntry(evaluator, pattern, entry, index, pattern.d.entries.length, pattern.d.starEntryIndex,
|
144
144
|
/* unpackStarEntry */ true);
|
145
145
|
const narrowedEntryType = narrowTypeBasedOnPattern(evaluator, entryType, sequenceEntry, isPositiveTest);
|
146
146
|
if (isPositiveTest) {
|
147
|
-
if (index === pattern.starEntryIndex) {
|
147
|
+
if (index === pattern.d.starEntryIndex) {
|
148
148
|
if ((0, types_1.isClassInstance)(narrowedEntryType) &&
|
149
|
-
narrowedEntryType.tupleTypeArguments &&
|
149
|
+
narrowedEntryType.priv.tupleTypeArguments &&
|
150
150
|
!(0, typeUtils_1.isUnboundedTupleClass)(narrowedEntryType) &&
|
151
|
-
narrowedEntryType.tupleTypeArguments) {
|
152
|
-
(0, collectionUtils_1.appendArray)(narrowedEntryTypes, narrowedEntryType.tupleTypeArguments.map((t) => t.type));
|
151
|
+
narrowedEntryType.priv.tupleTypeArguments) {
|
152
|
+
(0, collectionUtils_1.appendArray)(narrowedEntryTypes, narrowedEntryType.priv.tupleTypeArguments.map((t) => t.type));
|
153
153
|
}
|
154
154
|
else {
|
155
155
|
narrowedEntryTypes.push(narrowedEntryType);
|
@@ -178,12 +178,12 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
178
178
|
else {
|
179
179
|
narrowedEntryTypes.push(entryType);
|
180
180
|
}
|
181
|
-
if (index === pattern.starEntryIndex) {
|
181
|
+
if (index === pattern.d.starEntryIndex) {
|
182
182
|
canNarrowTuple = false;
|
183
183
|
}
|
184
184
|
}
|
185
185
|
});
|
186
|
-
if (pattern.entries.length === 0) {
|
186
|
+
if (pattern.d.entries.length === 0) {
|
187
187
|
// If the pattern is an empty sequence, use the entry types.
|
188
188
|
if (entry.entryTypes.length > 0) {
|
189
189
|
narrowedEntryTypes.push((0, types_1.combineTypes)(entry.entryTypes));
|
@@ -240,12 +240,12 @@ function narrowTypeBasedOnSequencePattern(evaluator, type, pattern, isPositiveTe
|
|
240
240
|
function narrowTypeBasedOnAsPattern(evaluator, type, pattern, isPositiveTest) {
|
241
241
|
let remainingType = type;
|
242
242
|
if (!isPositiveTest) {
|
243
|
-
pattern.orPatterns.forEach((subpattern) => {
|
243
|
+
pattern.d.orPatterns.forEach((subpattern) => {
|
244
244
|
remainingType = narrowTypeBasedOnPattern(evaluator, remainingType, subpattern, /* isPositiveTest */ false);
|
245
245
|
});
|
246
246
|
return remainingType;
|
247
247
|
}
|
248
|
-
const narrowedTypes = pattern.orPatterns.map((subpattern) => {
|
248
|
+
const narrowedTypes = pattern.d.orPatterns.map((subpattern) => {
|
249
249
|
const narrowedSubtype = narrowTypeBasedOnPattern(evaluator, remainingType, subpattern,
|
250
250
|
/* isPositiveTest */ true);
|
251
251
|
remainingType = narrowTypeBasedOnPattern(evaluator, remainingType, subpattern, /* isPositiveTest */ false);
|
@@ -256,26 +256,33 @@ function narrowTypeBasedOnAsPattern(evaluator, type, pattern, isPositiveTest) {
|
|
256
256
|
function narrowTypeBasedOnMappingPattern(evaluator, type, pattern, isPositiveTest) {
|
257
257
|
type = (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(type);
|
258
258
|
if (!isPositiveTest) {
|
259
|
-
//
|
260
|
-
|
261
|
-
|
262
|
-
|
259
|
+
// Handle the case where the pattern consists only of a "**x" entry.
|
260
|
+
if (pattern.d.entries.length === 1 &&
|
261
|
+
pattern.d.entries[0].nodeType === 72 /* ParseNodeType.PatternMappingExpandEntry */) {
|
262
|
+
const mappingInfo = getMappingPatternInfo(evaluator, type, pattern);
|
263
|
+
return (0, types_1.combineTypes)(mappingInfo.filter((m) => !m.isDefinitelyMapping).map((m) => m.subtype));
|
264
|
+
}
|
265
|
+
if (pattern.d.entries.length !== 1 || pattern.d.entries[0].nodeType !== 71 /* ParseNodeType.PatternMappingKeyEntry */) {
|
263
266
|
return type;
|
264
267
|
}
|
265
|
-
|
266
|
-
|
268
|
+
// Handle the case where the type is a union that includes a TypedDict with
|
269
|
+
// a field discriminated by a literal.
|
270
|
+
const keyPattern = pattern.d.entries[0].d.keyPattern;
|
271
|
+
const valuePattern = pattern.d.entries[0].d.valuePattern;
|
267
272
|
if (keyPattern.nodeType !== 67 /* ParseNodeType.PatternLiteral */ ||
|
268
273
|
valuePattern.nodeType !== 66 /* ParseNodeType.PatternAs */ ||
|
269
|
-
!valuePattern.orPatterns.every((orPattern) => orPattern.nodeType === 67 /* ParseNodeType.PatternLiteral */)) {
|
274
|
+
!valuePattern.d.orPatterns.every((orPattern) => orPattern.nodeType === 67 /* ParseNodeType.PatternLiteral */)) {
|
270
275
|
return type;
|
271
276
|
}
|
272
|
-
const keyType = evaluator.getTypeOfExpression(keyPattern.
|
277
|
+
const keyType = evaluator.getTypeOfExpression(keyPattern.d.expr).type;
|
273
278
|
// The key type must be a str literal.
|
274
|
-
if (!(0, types_1.isClassInstance)(keyType) ||
|
279
|
+
if (!(0, types_1.isClassInstance)(keyType) ||
|
280
|
+
!types_1.ClassType.isBuiltIn(keyType, 'str') ||
|
281
|
+
keyType.priv.literalValue === undefined) {
|
275
282
|
return type;
|
276
283
|
}
|
277
|
-
const keyValue = keyType.literalValue;
|
278
|
-
const valueTypes = valuePattern.orPatterns.map((orPattern) => evaluator.getTypeOfExpression(orPattern.
|
284
|
+
const keyValue = keyType.priv.literalValue;
|
285
|
+
const valueTypes = valuePattern.d.orPatterns.map((orPattern) => evaluator.getTypeOfExpression(orPattern.d.expr).type);
|
279
286
|
return (0, typeUtils_1.mapSubtypes)(type, (subtype) => {
|
280
287
|
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
281
288
|
const typedDictMembers = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype, /* allowNarrowed */ true);
|
@@ -286,7 +293,7 @@ function narrowTypeBasedOnMappingPattern(evaluator, type, pattern, isPositiveTes
|
|
286
293
|
// the literal type of the member, we can eliminate this type.
|
287
294
|
if (valueTypes.some((valueType) => (0, types_1.isClassInstance)(valueType) &&
|
288
295
|
types_1.ClassType.isSameGenericClass(valueType, memberValueType) &&
|
289
|
-
valueType.literalValue === memberValueType.literalValue)) {
|
296
|
+
valueType.priv.literalValue === memberValueType.priv.literalValue)) {
|
290
297
|
return undefined;
|
291
298
|
}
|
292
299
|
}
|
@@ -297,11 +304,14 @@ function narrowTypeBasedOnMappingPattern(evaluator, type, pattern, isPositiveTes
|
|
297
304
|
let mappingInfo = getMappingPatternInfo(evaluator, type, pattern);
|
298
305
|
// Further narrow based on pattern entry types.
|
299
306
|
mappingInfo = mappingInfo.filter((mappingSubtypeInfo) => {
|
307
|
+
if (mappingSubtypeInfo.isDefinitelyNotMapping) {
|
308
|
+
return false;
|
309
|
+
}
|
300
310
|
let isPlausibleMatch = true;
|
301
|
-
pattern.entries.forEach((mappingEntry) => {
|
311
|
+
pattern.d.entries.forEach((mappingEntry) => {
|
302
312
|
if (mappingSubtypeInfo.typedDict) {
|
303
313
|
if (mappingEntry.nodeType === 71 /* ParseNodeType.PatternMappingKeyEntry */) {
|
304
|
-
const narrowedKeyType = narrowTypeBasedOnPattern(evaluator, evaluator.getBuiltInObject(pattern, 'str'), mappingEntry.keyPattern, isPositiveTest);
|
314
|
+
const narrowedKeyType = narrowTypeBasedOnPattern(evaluator, evaluator.getBuiltInObject(pattern, 'str'), mappingEntry.d.keyPattern, isPositiveTest);
|
305
315
|
if ((0, types_1.isNever)(narrowedKeyType)) {
|
306
316
|
isPlausibleMatch = false;
|
307
317
|
}
|
@@ -315,9 +325,9 @@ function narrowTypeBasedOnMappingPattern(evaluator, type, pattern, isPositiveTes
|
|
315
325
|
return types_1.UnknownType.create();
|
316
326
|
}
|
317
327
|
const tdEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, mappingSubtypeInfo.typedDict);
|
318
|
-
const valueEntry = tdEntries.knownItems.get(keySubtype.literalValue);
|
328
|
+
const valueEntry = tdEntries.knownItems.get(keySubtype.priv.literalValue);
|
319
329
|
if (valueEntry) {
|
320
|
-
const narrowedValueType = narrowTypeBasedOnPattern(evaluator, valueEntry.valueType, mappingEntry.valuePattern,
|
330
|
+
const narrowedValueType = narrowTypeBasedOnPattern(evaluator, valueEntry.valueType, mappingEntry.d.valuePattern,
|
321
331
|
/* isPositiveTest */ true);
|
322
332
|
if (!(0, types_1.isNever)(narrowedValueType)) {
|
323
333
|
// If this is a "NotRequired" entry that has not yet been demonstrated
|
@@ -325,8 +335,8 @@ function narrowTypeBasedOnMappingPattern(evaluator, type, pattern, isPositiveTes
|
|
325
335
|
if (!valueEntry.isRequired &&
|
326
336
|
!valueEntry.isProvided &&
|
327
337
|
(0, types_1.isTypeSame)(mappingSubtypeInfo.subtype, mappingSubtypeInfo.typedDict)) {
|
328
|
-
const newNarrowedEntriesMap = new Map((_a = mappingSubtypeInfo.typedDict.typedDictNarrowedEntries) !== null && _a !== void 0 ? _a : []);
|
329
|
-
newNarrowedEntriesMap.set(keySubtype.literalValue, {
|
338
|
+
const newNarrowedEntriesMap = new Map((_a = mappingSubtypeInfo.typedDict.priv.typedDictNarrowedEntries) !== null && _a !== void 0 ? _a : []);
|
339
|
+
newNarrowedEntriesMap.set(keySubtype.priv.literalValue, {
|
330
340
|
valueType: valueEntry.valueType,
|
331
341
|
isReadOnly: valueEntry.isReadOnly,
|
332
342
|
isRequired: false,
|
@@ -349,8 +359,8 @@ function narrowTypeBasedOnMappingPattern(evaluator, type, pattern, isPositiveTes
|
|
349
359
|
}
|
350
360
|
else if (mappingSubtypeInfo.dictTypeArgs) {
|
351
361
|
if (mappingEntry.nodeType === 71 /* ParseNodeType.PatternMappingKeyEntry */) {
|
352
|
-
const narrowedKeyType = narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.key, mappingEntry.keyPattern, isPositiveTest);
|
353
|
-
const narrowedValueType = narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.value, mappingEntry.valuePattern, isPositiveTest);
|
362
|
+
const narrowedKeyType = narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.key, mappingEntry.d.keyPattern, isPositiveTest);
|
363
|
+
const narrowedValueType = narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.value, mappingEntry.d.valuePattern, isPositiveTest);
|
354
364
|
if ((0, types_1.isNever)(narrowedKeyType) || (0, types_1.isNever)(narrowedValueType)) {
|
355
365
|
isPlausibleMatch = false;
|
356
366
|
}
|
@@ -370,18 +380,18 @@ function getPositionalMatchArgNames(evaluator, type) {
|
|
370
380
|
if ((0, types_1.isClassInstance)(matchArgsType) &&
|
371
381
|
(0, typeUtils_1.isTupleClass)(matchArgsType) &&
|
372
382
|
!(0, typeUtils_1.isUnboundedTupleClass)(matchArgsType) &&
|
373
|
-
matchArgsType.tupleTypeArguments) {
|
374
|
-
const tupleArgs = matchArgsType.tupleTypeArguments;
|
383
|
+
matchArgsType.priv.tupleTypeArguments) {
|
384
|
+
const tupleArgs = matchArgsType.priv.tupleTypeArguments;
|
375
385
|
// Are all the args string literals?
|
376
386
|
if (tupleArgs.every((arg) => (0, types_1.isClassInstance)(arg.type) && types_1.ClassType.isBuiltIn(arg.type, 'str') && (0, typeUtils_1.isLiteralType)(arg.type))) {
|
377
|
-
return tupleArgs.map((arg) => arg.type.literalValue);
|
387
|
+
return tupleArgs.map((arg) => arg.type.priv.literalValue);
|
378
388
|
}
|
379
389
|
}
|
380
390
|
}
|
381
391
|
return [];
|
382
392
|
}
|
383
393
|
function narrowTypeBasedOnLiteralPattern(evaluator, type, pattern, isPositiveTest) {
|
384
|
-
const literalType = evaluator.getTypeOfExpression(pattern.
|
394
|
+
const literalType = evaluator.getTypeOfExpression(pattern.d.expr).type;
|
385
395
|
if (!isPositiveTest) {
|
386
396
|
return evaluator.mapSubtypesExpandTypeVars(type,
|
387
397
|
/* options */ undefined, (expandedSubtype, unexpandedSubtype) => {
|
@@ -398,11 +408,11 @@ function narrowTypeBasedOnLiteralPattern(evaluator, type, pattern, isPositiveTes
|
|
398
408
|
// Narrow a non-literal bool based on a literal bool pattern.
|
399
409
|
if ((0, types_1.isClassInstance)(expandedSubtype) &&
|
400
410
|
types_1.ClassType.isBuiltIn(expandedSubtype, 'bool') &&
|
401
|
-
expandedSubtype.literalValue === undefined &&
|
411
|
+
expandedSubtype.priv.literalValue === undefined &&
|
402
412
|
(0, types_1.isClassInstance)(literalType) &&
|
403
413
|
types_1.ClassType.isBuiltIn(literalType, 'bool') &&
|
404
|
-
literalType.literalValue !== undefined) {
|
405
|
-
return types_1.ClassType.cloneWithLiteral(literalType, !literalType.literalValue);
|
414
|
+
literalType.priv.literalValue !== undefined) {
|
415
|
+
return types_1.ClassType.cloneWithLiteral(literalType, !literalType.priv.literalValue);
|
406
416
|
}
|
407
417
|
return expandedSubtype;
|
408
418
|
});
|
@@ -425,17 +435,18 @@ function narrowTypeBasedOnLiteralPattern(evaluator, type, pattern, isPositiveTes
|
|
425
435
|
});
|
426
436
|
}
|
427
437
|
function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest) {
|
428
|
-
|
438
|
+
var _a;
|
439
|
+
let exprType = evaluator.getTypeOfExpression(pattern.d.className, 2 /* EvalFlags.CallBaseDefaults */).type;
|
429
440
|
// If this is a class (but not a type alias that refers to a class),
|
430
441
|
// specialize it with Unknown type arguments.
|
431
|
-
if ((0, types_1.isClass)(exprType) && !exprType.typeAliasInfo) {
|
442
|
+
if ((0, types_1.isClass)(exprType) && !((_a = exprType.props) === null || _a === void 0 ? void 0 : _a.typeAliasInfo)) {
|
432
443
|
exprType = types_1.ClassType.cloneRemoveTypePromotions(exprType);
|
433
444
|
exprType = (0, typeUtils_1.specializeWithUnknownTypeArgs)(exprType, evaluator.getTupleClassType());
|
434
445
|
}
|
435
446
|
// Are there any positional arguments? If so, try to get the mappings for
|
436
447
|
// these arguments by fetching the __match_args__ symbol from the class.
|
437
448
|
let positionalArgNames = [];
|
438
|
-
if (pattern.
|
449
|
+
if (pattern.d.args.some((arg) => !arg.d.name) && (0, types_1.isInstantiableClass)(exprType)) {
|
439
450
|
positionalArgNames = getPositionalMatchArgNames(evaluator, exprType);
|
440
451
|
}
|
441
452
|
if (!isPositiveTest) {
|
@@ -444,7 +455,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
444
455
|
return type;
|
445
456
|
}
|
446
457
|
let classType = exprType;
|
447
|
-
if (classType.
|
458
|
+
if (classType.shared.typeParameters.length > 0) {
|
448
459
|
classType = types_1.ClassType.cloneForSpecialization(classType,
|
449
460
|
/* typeArguments */ undefined,
|
450
461
|
/* isTypeArgumentExplicit */ false);
|
@@ -458,7 +469,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
458
469
|
// Handle the case where the class pattern references type() or a subtype thereof
|
459
470
|
// and the subject type is an instantiable class itself.
|
460
471
|
if (isPatternMetaclass && (0, types_1.isInstantiableClass)(subjectSubtypeExpanded)) {
|
461
|
-
const metaclass = (_a = subjectSubtypeExpanded.
|
472
|
+
const metaclass = (_a = subjectSubtypeExpanded.shared.effectiveMetaclass) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
|
462
473
|
if ((0, types_1.isInstantiableClass)(classType) && evaluator.assignType(classType, metaclass)) {
|
463
474
|
return undefined;
|
464
475
|
}
|
@@ -484,7 +495,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
484
495
|
if (!evaluator.assignType(classInstance, subjectSubtypeExpanded)) {
|
485
496
|
return subjectSubtypeExpanded;
|
486
497
|
}
|
487
|
-
if (pattern.
|
498
|
+
if (pattern.d.args.length === 0) {
|
488
499
|
if ((0, types_1.isClass)(classInstance) && (0, types_1.isClass)(subjectSubtypeExpanded)) {
|
489
500
|
// We know that this match will always succeed, so we can
|
490
501
|
// eliminate this subtype.
|
@@ -503,8 +514,8 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
503
514
|
return subjectSubtypeExpanded;
|
504
515
|
}
|
505
516
|
}
|
506
|
-
for (let index = 0; index < pattern.
|
507
|
-
const narrowedArgType = narrowTypeOfClassPatternArgument(evaluator, pattern.
|
517
|
+
for (let index = 0; index < pattern.d.args.length; index++) {
|
518
|
+
const narrowedArgType = narrowTypeOfClassPatternArgument(evaluator, pattern.d.args[index], index, positionalArgNames, subjectSubtypeExpanded, isPositiveTest);
|
508
519
|
if (!(0, types_1.isNever)(narrowedArgType)) {
|
509
520
|
return subjectSubtypeUnexpanded;
|
510
521
|
}
|
@@ -514,13 +525,13 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
514
525
|
});
|
515
526
|
}
|
516
527
|
if (!types_1.TypeBase.isInstantiable(exprType) && !(0, types_1.isNever)(exprType)) {
|
517
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocAddendum.typeNotClass().format({ type: evaluator.printType(exprType) }), pattern.className);
|
528
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocAddendum.typeNotClass().format({ type: evaluator.printType(exprType) }), pattern.d.className);
|
518
529
|
return types_1.NeverType.createNever();
|
519
530
|
}
|
520
531
|
else if ((0, types_1.isInstantiableClass)(exprType) &&
|
521
532
|
types_1.ClassType.isProtocolClass(exprType) &&
|
522
533
|
!types_1.ClassType.isRuntimeCheckable(exprType)) {
|
523
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocAddendum.protocolRequiresRuntimeCheckable(), pattern.className);
|
534
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocAddendum.protocolRequiresRuntimeCheckable(), pattern.d.className);
|
524
535
|
}
|
525
536
|
return evaluator.mapSubtypesExpandTypeVars(exprType,
|
526
537
|
/* options */ undefined, (expandedSubtype, unexpandedSubtype) => {
|
@@ -538,7 +549,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
538
549
|
const unknownCallable = types_1.FunctionType.createSynthesizedInstance('', 32768 /* FunctionTypeFlags.GradualCallableForm */);
|
539
550
|
types_1.FunctionType.addDefaultParameters(unknownCallable,
|
540
551
|
/* useUnknown */ (0, types_1.isUnknown)(subjectSubtypeExpanded));
|
541
|
-
unknownCallable.
|
552
|
+
unknownCallable.shared.declaredReturnType = subjectSubtypeExpanded;
|
542
553
|
return unknownCallable;
|
543
554
|
}
|
544
555
|
return (0, typeUtils_1.convertToInstance)(unexpandedSubtype);
|
@@ -546,7 +557,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
546
557
|
// Handle the case where the class pattern references type() or a subtype thereof
|
547
558
|
// and the subject type is a class itself.
|
548
559
|
if (isPatternMetaclass && (0, types_1.isInstantiableClass)(subjectSubtypeExpanded)) {
|
549
|
-
const metaclass = (_a = subjectSubtypeExpanded.
|
560
|
+
const metaclass = (_a = subjectSubtypeExpanded.shared.effectiveMetaclass) !== null && _a !== void 0 ? _a : types_1.UnknownType.create();
|
550
561
|
if (evaluator.assignType(expandedSubtype, metaclass) ||
|
551
562
|
evaluator.assignType(metaclass, expandedSubtype)) {
|
552
563
|
return subjectSubtypeExpanded;
|
@@ -581,7 +592,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
581
592
|
// Try to retain the type arguments for the pattern class type.
|
582
593
|
if ((0, types_1.isInstantiableClass)(unexpandedSubtype) && (0, types_1.isClassInstance)(subjectSubtypeExpanded)) {
|
583
594
|
if (types_1.ClassType.isSpecialBuiltIn(unexpandedSubtype) ||
|
584
|
-
unexpandedSubtype.
|
595
|
+
unexpandedSubtype.shared.typeParameters.length > 0) {
|
585
596
|
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(unexpandedSubtype));
|
586
597
|
const unspecializedMatchType = types_1.ClassType.cloneForSpecialization(unexpandedSubtype,
|
587
598
|
/* typeArguments */ undefined,
|
@@ -604,11 +615,11 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
604
615
|
// Are there any positional arguments? If so, try to get the mappings for
|
605
616
|
// these arguments by fetching the __match_args__ symbol from the class.
|
606
617
|
let positionalArgNames = [];
|
607
|
-
if (pattern.
|
618
|
+
if (pattern.d.args.some((arg) => !arg.d.name)) {
|
608
619
|
positionalArgNames = getPositionalMatchArgNames(evaluator, expandedSubtype);
|
609
620
|
}
|
610
621
|
let isMatchValid = true;
|
611
|
-
pattern.
|
622
|
+
pattern.d.args.forEach((arg, index) => {
|
612
623
|
// Narrow the arg pattern. It's possible that the actual type of the object
|
613
624
|
// being matched is a subtype of the resultType, so it might contain additional
|
614
625
|
// attributes that we don't know about.
|
@@ -630,7 +641,7 @@ function narrowTypeBasedOnClassPattern(evaluator, type, pattern, isPositiveTest)
|
|
630
641
|
// Some built-in classes are treated as special cases for the class pattern
|
631
642
|
// if a positional argument is used.
|
632
643
|
function isClassSpecialCaseForClassPattern(classType) {
|
633
|
-
if (classPatternSpecialCases.some((className) => classType.
|
644
|
+
if (classPatternSpecialCases.some((className) => classType.shared.fullName === className)) {
|
634
645
|
return true;
|
635
646
|
}
|
636
647
|
// If the class supplies its own `__match_args__`, it's not a special case.
|
@@ -639,9 +650,8 @@ function isClassSpecialCaseForClassPattern(classType) {
|
|
639
650
|
return false;
|
640
651
|
}
|
641
652
|
// If the class derives from a built-in class, it is considered a special case.
|
642
|
-
for (const mroClass of classType.
|
643
|
-
if ((0, types_1.isClass)(mroClass) &&
|
644
|
-
classPatternSpecialCases.some((className) => mroClass.details.fullName === className)) {
|
653
|
+
for (const mroClass of classType.shared.mro) {
|
654
|
+
if ((0, types_1.isClass)(mroClass) && classPatternSpecialCases.some((className) => mroClass.shared.fullName === className)) {
|
645
655
|
return true;
|
646
656
|
}
|
647
657
|
}
|
@@ -651,8 +661,8 @@ function isClassSpecialCaseForClassPattern(classType) {
|
|
651
661
|
function narrowTypeOfClassPatternArgument(evaluator, arg, argIndex, positionalArgNames, matchType, isPositiveTest) {
|
652
662
|
var _a;
|
653
663
|
let argName;
|
654
|
-
if (arg.name) {
|
655
|
-
argName = arg.name.value;
|
664
|
+
if (arg.d.name) {
|
665
|
+
argName = arg.d.name.d.value;
|
656
666
|
}
|
657
667
|
else if (argIndex < positionalArgNames.length) {
|
658
668
|
argName = positionalArgNames[argIndex];
|
@@ -669,12 +679,12 @@ function narrowTypeOfClassPatternArgument(evaluator, arg, argIndex, positionalAr
|
|
669
679
|
// if the subclass doesn't define its own __match_args__.
|
670
680
|
let useSelfForPattern = false;
|
671
681
|
let selfForPatternType = matchType;
|
672
|
-
if (!arg.name && (0, types_1.isClass)(matchType) && argIndex === 0) {
|
682
|
+
if (!arg.d.name && (0, types_1.isClass)(matchType) && argIndex === 0) {
|
673
683
|
if (isClassSpecialCaseForClassPattern(matchType)) {
|
674
684
|
useSelfForPattern = true;
|
675
685
|
}
|
676
686
|
else if (positionalArgNames.length === 0) {
|
677
|
-
matchType.
|
687
|
+
matchType.shared.mro.forEach((mroClass) => {
|
678
688
|
if ((0, types_1.isClass)(mroClass) && isClassSpecialCaseForClassPattern(mroClass)) {
|
679
689
|
selfForPatternType = mroClass;
|
680
690
|
useSelfForPattern = true;
|
@@ -706,10 +716,10 @@ function narrowTypeOfClassPatternArgument(evaluator, arg, argIndex, positionalAr
|
|
706
716
|
argType = types_1.UnknownType.create();
|
707
717
|
}
|
708
718
|
}
|
709
|
-
return narrowTypeBasedOnPattern(evaluator, argType, arg.pattern, isPositiveTest);
|
719
|
+
return narrowTypeBasedOnPattern(evaluator, argType, arg.d.pattern, isPositiveTest);
|
710
720
|
}
|
711
721
|
function narrowTypeBasedOnValuePattern(evaluator, subjectType, pattern, isPositiveTest) {
|
712
|
-
const valueType = evaluator.getTypeOfExpression(pattern.
|
722
|
+
const valueType = evaluator.getTypeOfExpression(pattern.d.expr).type;
|
713
723
|
const narrowedSubtypes = [];
|
714
724
|
evaluator.mapSubtypesExpandTypeVars(valueType,
|
715
725
|
/* options */ undefined, (valueSubtypeExpanded, valueSubtypeUnexpanded) => {
|
@@ -755,7 +765,7 @@ function narrowTypeBasedOnValuePattern(evaluator, subjectType, pattern, isPositi
|
|
755
765
|
}
|
756
766
|
// Determine if assignment is supported for this combination of
|
757
767
|
// value subtype and matching subtype.
|
758
|
-
const returnType = evaluator.useSpeculativeMode(pattern.
|
768
|
+
const returnType = evaluator.useSpeculativeMode(pattern.d.expr, () => evaluator.getTypeOfMagicMethodCall(valueSubtypeExpanded, '__eq__', [{ type: subjectSubtypeExpanded }], pattern.d.expr,
|
759
769
|
/* expectedType */ undefined));
|
760
770
|
return returnType ? valueSubtypeUnexpanded : undefined;
|
761
771
|
}));
|
@@ -772,6 +782,8 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
772
782
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
773
783
|
mappingInfo.push({
|
774
784
|
subtype,
|
785
|
+
isDefinitelyMapping: false,
|
786
|
+
isDefinitelyNotMapping: false,
|
775
787
|
dictTypeArgs: {
|
776
788
|
key: concreteSubtype,
|
777
789
|
value: concreteSubtype,
|
@@ -784,13 +796,15 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
784
796
|
if (types_1.ClassType.isTypedDictClass(concreteSubtype)) {
|
785
797
|
mappingInfo.push({
|
786
798
|
subtype,
|
799
|
+
isDefinitelyMapping: true,
|
800
|
+
isDefinitelyNotMapping: false,
|
787
801
|
typedDict: concreteSubtype,
|
788
802
|
});
|
789
803
|
return;
|
790
804
|
}
|
791
805
|
// Is it a subclass of Mapping?
|
792
806
|
let mroClassToSpecialize;
|
793
|
-
for (const mroClass of concreteSubtype.
|
807
|
+
for (const mroClass of concreteSubtype.shared.mro) {
|
794
808
|
if ((0, types_1.isInstantiableClass)(mroClass) && types_1.ClassType.isBuiltIn(mroClass, 'Mapping')) {
|
795
809
|
mroClassToSpecialize = mroClass;
|
796
810
|
break;
|
@@ -798,12 +812,14 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
798
812
|
}
|
799
813
|
if (mroClassToSpecialize) {
|
800
814
|
const specializedMapping = (0, typeUtils_1.partiallySpecializeType)(mroClassToSpecialize, concreteSubtype);
|
801
|
-
if (specializedMapping.typeArguments && specializedMapping.typeArguments.length >= 2) {
|
815
|
+
if (specializedMapping.priv.typeArguments && specializedMapping.priv.typeArguments.length >= 2) {
|
802
816
|
mappingInfo.push({
|
803
817
|
subtype,
|
818
|
+
isDefinitelyMapping: true,
|
819
|
+
isDefinitelyNotMapping: false,
|
804
820
|
dictTypeArgs: {
|
805
|
-
key: specializedMapping.typeArguments[0],
|
806
|
-
value: specializedMapping.typeArguments[1],
|
821
|
+
key: specializedMapping.priv.typeArguments[0],
|
822
|
+
value: specializedMapping.priv.typeArguments[1],
|
807
823
|
},
|
808
824
|
});
|
809
825
|
}
|
@@ -813,9 +829,11 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
813
829
|
const mappingType = evaluator.getTypingType(node, 'Mapping');
|
814
830
|
if (mappingType && (0, types_1.isInstantiableClass)(mappingType)) {
|
815
831
|
const mappingObject = types_1.ClassType.cloneAsInstance(mappingType);
|
816
|
-
if (evaluator.assignType(
|
832
|
+
if (evaluator.assignType(subtype, mappingObject)) {
|
817
833
|
mappingInfo.push({
|
818
834
|
subtype,
|
835
|
+
isDefinitelyMapping: false,
|
836
|
+
isDefinitelyNotMapping: false,
|
819
837
|
dictTypeArgs: {
|
820
838
|
key: types_1.UnknownType.create(),
|
821
839
|
value: types_1.UnknownType.create(),
|
@@ -823,6 +841,11 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
823
841
|
});
|
824
842
|
}
|
825
843
|
}
|
844
|
+
mappingInfo.push({
|
845
|
+
subtype,
|
846
|
+
isDefinitelyMapping: false,
|
847
|
+
isDefinitelyNotMapping: true,
|
848
|
+
});
|
826
849
|
}
|
827
850
|
});
|
828
851
|
return mappingInfo;
|
@@ -831,8 +854,8 @@ function getMappingPatternInfo(evaluator, type, node) {
|
|
831
854
|
// specified in PEP 634. For types that are not sequences or sequences that are not of
|
832
855
|
// sufficient length, it sets definiteNoMatch to true.
|
833
856
|
function getSequencePatternInfo(evaluator, pattern, type) {
|
834
|
-
const patternEntryCount = pattern.entries.length;
|
835
|
-
const patternStarEntryIndex = pattern.starEntryIndex;
|
857
|
+
const patternEntryCount = pattern.d.entries.length;
|
858
|
+
const patternStarEntryIndex = pattern.d.starEntryIndex;
|
836
859
|
const sequenceInfo = [];
|
837
860
|
(0, typeUtils_1.doForEachSubtype)(type, (subtype) => {
|
838
861
|
var _a;
|
@@ -840,7 +863,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
840
863
|
let mroClassToSpecialize;
|
841
864
|
let pushedEntry = false;
|
842
865
|
if ((0, types_1.isClassInstance)(concreteSubtype)) {
|
843
|
-
for (const mroClass of concreteSubtype.
|
866
|
+
for (const mroClass of concreteSubtype.shared.mro) {
|
844
867
|
if (!(0, types_1.isInstantiableClass)(mroClass)) {
|
845
868
|
break;
|
846
869
|
}
|
@@ -869,7 +892,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
869
892
|
if (mroClassToSpecialize) {
|
870
893
|
const specializedSequence = (0, typeUtils_1.partiallySpecializeType)(mroClassToSpecialize, concreteSubtype);
|
871
894
|
if ((0, typeUtils_1.isTupleClass)(specializedSequence)) {
|
872
|
-
const typeArgs = (_a = specializedSequence.tupleTypeArguments) !== null && _a !== void 0 ? _a : [
|
895
|
+
const typeArgs = (_a = specializedSequence.priv.tupleTypeArguments) !== null && _a !== void 0 ? _a : [
|
873
896
|
{ type: types_1.UnknownType.create(), isUnbounded: true },
|
874
897
|
];
|
875
898
|
const tupleIndeterminateIndex = typeArgs.findIndex((t) => t.isUnbounded || (0, types_1.isUnpackedVariadicTypeVar)(t.type));
|
@@ -905,12 +928,12 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
905
928
|
// assume it will always match.
|
906
929
|
if (patternStarEntryIndex !== undefined &&
|
907
930
|
tupleIndeterminateIndex >= 0 &&
|
908
|
-
pattern.entries.length - 1 === tupleDeterminateEntryCount &&
|
931
|
+
pattern.d.entries.length - 1 === tupleDeterminateEntryCount &&
|
909
932
|
patternStarEntryIndex === tupleIndeterminateIndex) {
|
910
933
|
isPotentialNoMatch = false;
|
911
934
|
}
|
912
935
|
for (let i = 0; i < patternEntryCount; i++) {
|
913
|
-
const subPattern = pattern.entries[i];
|
936
|
+
const subPattern = pattern.d.entries[i];
|
914
937
|
const typeArg = typeArgs[i].type;
|
915
938
|
const narrowedType = narrowTypeBasedOnPattern(evaluator, typeArg, subPattern,
|
916
939
|
/* isPositiveTest */ true);
|
@@ -950,7 +973,7 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
950
973
|
if (i === patternStarEntryIndex) {
|
951
974
|
continue;
|
952
975
|
}
|
953
|
-
const subPattern = pattern.entries[i];
|
976
|
+
const subPattern = pattern.d.entries[i];
|
954
977
|
const typeArg = typeArgs[i].type;
|
955
978
|
const narrowedType = narrowTypeBasedOnPattern(evaluator, typeArg, subPattern,
|
956
979
|
/* isPositiveTest */ true);
|
@@ -973,8 +996,8 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
973
996
|
sequenceInfo.push({
|
974
997
|
subtype,
|
975
998
|
entryTypes: [
|
976
|
-
specializedSequence.typeArguments && specializedSequence.typeArguments.length > 0
|
977
|
-
? specializedSequence.typeArguments[0]
|
999
|
+
specializedSequence.priv.typeArguments && specializedSequence.priv.typeArguments.length > 0
|
1000
|
+
? specializedSequence.priv.typeArguments[0]
|
978
1001
|
: types_1.UnknownType.create(),
|
979
1002
|
],
|
980
1003
|
isIndeterminateLength: true,
|
@@ -991,10 +1014,10 @@ function getSequencePatternInfo(evaluator, pattern, type) {
|
|
991
1014
|
const sequenceTypeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(sequenceType));
|
992
1015
|
if ((0, constraintSolver_1.addConstraintsForExpectedType)(evaluator, types_1.ClassType.cloneAsInstance(sequenceType), subtype, sequenceTypeVarContext, (0, parseTreeUtils_1.getTypeVarScopesForNode)(pattern), pattern.start)) {
|
993
1016
|
const specializedSequence = (0, typeUtils_1.applySolvedTypeVars)(types_1.ClassType.cloneAsInstantiable(sequenceType), sequenceTypeVarContext);
|
994
|
-
if (specializedSequence.typeArguments && specializedSequence.typeArguments.length > 0) {
|
1017
|
+
if (specializedSequence.priv.typeArguments && specializedSequence.priv.typeArguments.length > 0) {
|
995
1018
|
sequenceInfo.push({
|
996
1019
|
subtype,
|
997
|
-
entryTypes: [specializedSequence.typeArguments[0]],
|
1020
|
+
entryTypes: [specializedSequence.priv.typeArguments[0]],
|
998
1021
|
isIndeterminateLength: true,
|
999
1022
|
isDefiniteNoMatch: false,
|
1000
1023
|
isPotentialNoMatch: true,
|
@@ -1041,7 +1064,16 @@ function getTypeOfPatternSequenceEntry(evaluator, node, sequenceInfo, entryIndex
|
|
1041
1064
|
// Note that we strip literal types here.
|
1042
1065
|
const starEntryTypes = sequenceInfo.entryTypes
|
1043
1066
|
.slice(starEntryIndex, starEntryIndex + sequenceInfo.entryTypes.length - entryCount + 1)
|
1044
|
-
.map((type) =>
|
1067
|
+
.map((type) => {
|
1068
|
+
// If this is a variadic TypeVar, there's not much we can say about
|
1069
|
+
// its type other than it's "Unknown". We could evaluate it as an
|
1070
|
+
// "object", but that will cause problems given that this type will
|
1071
|
+
// be wrapped in a "list" below, and lists are invariant.
|
1072
|
+
if ((0, types_1.isVariadicTypeVar)(type) && !type.priv.isVariadicInUnion) {
|
1073
|
+
return types_1.UnknownType.create();
|
1074
|
+
}
|
1075
|
+
return evaluator.stripLiteralValue(type);
|
1076
|
+
});
|
1045
1077
|
let entryType = (0, types_1.combineTypes)(starEntryTypes);
|
1046
1078
|
if (!unpackStarEntry) {
|
1047
1079
|
entryType = wrapTypeInList(evaluator, node, entryType);
|
@@ -1062,19 +1094,19 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1062
1094
|
switch (pattern.nodeType) {
|
1063
1095
|
case 65 /* ParseNodeType.PatternSequence */: {
|
1064
1096
|
const sequenceInfo = getSequencePatternInfo(evaluator, pattern, narrowedType).filter((seqInfo) => !seqInfo.isDefiniteNoMatch);
|
1065
|
-
pattern.entries.forEach((entry, index) => {
|
1066
|
-
const entryType = (0, types_1.combineTypes)(sequenceInfo.map((info) => getTypeOfPatternSequenceEntry(evaluator, pattern, info, index, pattern.entries.length, pattern.starEntryIndex,
|
1097
|
+
pattern.d.entries.forEach((entry, index) => {
|
1098
|
+
const entryType = (0, types_1.combineTypes)(sequenceInfo.map((info) => getTypeOfPatternSequenceEntry(evaluator, pattern, info, index, pattern.d.entries.length, pattern.d.starEntryIndex,
|
1067
1099
|
/* unpackStarEntry */ false)));
|
1068
1100
|
assignTypeToPatternTargets(evaluator, entryType, isTypeIncomplete, entry);
|
1069
1101
|
});
|
1070
1102
|
break;
|
1071
1103
|
}
|
1072
1104
|
case 66 /* ParseNodeType.PatternAs */: {
|
1073
|
-
if (pattern.target) {
|
1074
|
-
evaluator.assignTypeToExpression(pattern.target, { type: narrowedType, isIncomplete: isTypeIncomplete }, pattern.target);
|
1105
|
+
if (pattern.d.target) {
|
1106
|
+
evaluator.assignTypeToExpression(pattern.d.target, { type: narrowedType, isIncomplete: isTypeIncomplete }, pattern.d.target);
|
1075
1107
|
}
|
1076
1108
|
let runningNarrowedType = narrowedType;
|
1077
|
-
pattern.orPatterns.forEach((orPattern) => {
|
1109
|
+
pattern.d.orPatterns.forEach((orPattern) => {
|
1078
1110
|
assignTypeToPatternTargets(evaluator, runningNarrowedType, isTypeIncomplete, orPattern);
|
1079
1111
|
// OR patterns are evaluated left to right, so we can narrow
|
1080
1112
|
// the type as we go.
|
@@ -1084,10 +1116,10 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1084
1116
|
break;
|
1085
1117
|
}
|
1086
1118
|
case 69 /* ParseNodeType.PatternCapture */: {
|
1087
|
-
if (pattern.isWildcard) {
|
1119
|
+
if (pattern.d.isWildcard) {
|
1088
1120
|
if (!isTypeIncomplete) {
|
1089
1121
|
if ((0, types_1.isUnknown)(narrowedType)) {
|
1090
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportUnknownVariableType, localize_1.LocMessage.wildcardPatternTypeUnknown(), pattern.target);
|
1122
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportUnknownVariableType, localize_1.LocMessage.wildcardPatternTypeUnknown(), pattern.d.target);
|
1091
1123
|
}
|
1092
1124
|
else if ((0, typeUtils_1.isPartlyUnknown)(narrowedType)) {
|
1093
1125
|
const diagAddendum = new diagnostic_1.DiagnosticAddendum();
|
@@ -1095,24 +1127,24 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1095
1127
|
name: '_',
|
1096
1128
|
type: evaluator.printType(narrowedType, { expandTypeAlias: true }),
|
1097
1129
|
}));
|
1098
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportUnknownVariableType, localize_1.LocMessage.wildcardPatternTypePartiallyUnknown() + diagAddendum.getString(), pattern.target);
|
1130
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportUnknownVariableType, localize_1.LocMessage.wildcardPatternTypePartiallyUnknown() + diagAddendum.getString(), pattern.d.target);
|
1099
1131
|
}
|
1100
1132
|
}
|
1101
1133
|
}
|
1102
1134
|
else {
|
1103
|
-
evaluator.assignTypeToExpression(pattern.target, { type: narrowedType, isIncomplete: isTypeIncomplete }, pattern.target);
|
1135
|
+
evaluator.assignTypeToExpression(pattern.d.target, { type: narrowedType, isIncomplete: isTypeIncomplete }, pattern.d.target);
|
1104
1136
|
}
|
1105
1137
|
break;
|
1106
1138
|
}
|
1107
1139
|
case 70 /* ParseNodeType.PatternMapping */: {
|
1108
1140
|
const mappingInfo = getMappingPatternInfo(evaluator, narrowedType, pattern);
|
1109
|
-
pattern.entries.forEach((mappingEntry) => {
|
1141
|
+
pattern.d.entries.forEach((mappingEntry) => {
|
1110
1142
|
const keyTypes = [];
|
1111
1143
|
const valueTypes = [];
|
1112
1144
|
mappingInfo.forEach((mappingSubtypeInfo) => {
|
1113
1145
|
if (mappingSubtypeInfo.typedDict) {
|
1114
1146
|
if (mappingEntry.nodeType === 71 /* ParseNodeType.PatternMappingKeyEntry */) {
|
1115
|
-
const keyType = narrowTypeBasedOnPattern(evaluator, evaluator.getBuiltInObject(pattern, 'str'), mappingEntry.keyPattern,
|
1147
|
+
const keyType = narrowTypeBasedOnPattern(evaluator, evaluator.getBuiltInObject(pattern, 'str'), mappingEntry.d.keyPattern,
|
1116
1148
|
/* isPositiveTest */ true);
|
1117
1149
|
keyTypes.push(keyType);
|
1118
1150
|
(0, typeUtils_1.doForEachSubtype)(keyType, (keySubtype) => {
|
@@ -1120,7 +1152,7 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1120
1152
|
types_1.ClassType.isBuiltIn(keySubtype, 'str') &&
|
1121
1153
|
(0, typeUtils_1.isLiteralType)(keySubtype)) {
|
1122
1154
|
const tdEntries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, mappingSubtypeInfo.typedDict);
|
1123
|
-
const valueInfo = tdEntries.knownItems.get(keySubtype.literalValue);
|
1155
|
+
const valueInfo = tdEntries.knownItems.get(keySubtype.priv.literalValue);
|
1124
1156
|
valueTypes.push(valueInfo ? valueInfo.valueType : types_1.UnknownType.create());
|
1125
1157
|
}
|
1126
1158
|
else {
|
@@ -1130,15 +1162,15 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1130
1162
|
}
|
1131
1163
|
else if (mappingEntry.nodeType === 72 /* ParseNodeType.PatternMappingExpandEntry */) {
|
1132
1164
|
keyTypes.push(evaluator.getBuiltInObject(pattern, 'str'));
|
1133
|
-
valueTypes.push(
|
1165
|
+
valueTypes.push(evaluator.getObjectType());
|
1134
1166
|
}
|
1135
1167
|
}
|
1136
1168
|
else if (mappingSubtypeInfo.dictTypeArgs) {
|
1137
1169
|
if (mappingEntry.nodeType === 71 /* ParseNodeType.PatternMappingKeyEntry */) {
|
1138
|
-
const keyType = narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.key, mappingEntry.keyPattern,
|
1170
|
+
const keyType = narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.key, mappingEntry.d.keyPattern,
|
1139
1171
|
/* isPositiveTest */ true);
|
1140
1172
|
keyTypes.push(keyType);
|
1141
|
-
valueTypes.push(narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.value, mappingEntry.valuePattern,
|
1173
|
+
valueTypes.push(narrowTypeBasedOnPattern(evaluator, mappingSubtypeInfo.dictTypeArgs.value, mappingEntry.d.valuePattern,
|
1142
1174
|
/* isPositiveTest */ true));
|
1143
1175
|
}
|
1144
1176
|
else if (mappingEntry.nodeType === 72 /* ParseNodeType.PatternMappingExpandEntry */) {
|
@@ -1150,8 +1182,8 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1150
1182
|
const keyType = (0, types_1.combineTypes)(keyTypes);
|
1151
1183
|
const valueType = (0, types_1.combineTypes)(valueTypes);
|
1152
1184
|
if (mappingEntry.nodeType === 71 /* ParseNodeType.PatternMappingKeyEntry */) {
|
1153
|
-
assignTypeToPatternTargets(evaluator, keyType, isTypeIncomplete, mappingEntry.keyPattern);
|
1154
|
-
assignTypeToPatternTargets(evaluator, valueType, isTypeIncomplete, mappingEntry.valuePattern);
|
1185
|
+
assignTypeToPatternTargets(evaluator, keyType, isTypeIncomplete, mappingEntry.d.keyPattern);
|
1186
|
+
assignTypeToPatternTargets(evaluator, valueType, isTypeIncomplete, mappingEntry.d.valuePattern);
|
1155
1187
|
}
|
1156
1188
|
else if (mappingEntry.nodeType === 72 /* ParseNodeType.PatternMappingExpandEntry */) {
|
1157
1189
|
const dictClass = evaluator.getBuiltInType(pattern, 'dict');
|
@@ -1160,19 +1192,19 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1160
1192
|
? types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForSpecialization(dictClass, [keyType, valueType],
|
1161
1193
|
/* isTypeArgumentExplicit */ true))
|
1162
1194
|
: types_1.UnknownType.create();
|
1163
|
-
evaluator.assignTypeToExpression(mappingEntry.target, { type: dictType, isIncomplete: isTypeIncomplete }, mappingEntry.target);
|
1195
|
+
evaluator.assignTypeToExpression(mappingEntry.d.target, { type: dictType, isIncomplete: isTypeIncomplete }, mappingEntry.d.target);
|
1164
1196
|
}
|
1165
1197
|
});
|
1166
1198
|
break;
|
1167
1199
|
}
|
1168
1200
|
case 68 /* ParseNodeType.PatternClass */: {
|
1169
|
-
const argTypes = pattern.
|
1201
|
+
const argTypes = pattern.d.args.map((arg) => []);
|
1170
1202
|
evaluator.mapSubtypesExpandTypeVars(narrowedType, /* options */ undefined, (expandedSubtype) => {
|
1171
1203
|
if ((0, types_1.isClassInstance)(expandedSubtype)) {
|
1172
1204
|
(0, typeUtils_1.doForEachSubtype)(narrowedType, (subjectSubtype) => {
|
1173
1205
|
const concreteSubtype = evaluator.makeTopLevelTypeVarsConcrete(subjectSubtype);
|
1174
1206
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
1175
|
-
pattern.
|
1207
|
+
pattern.d.args.forEach((arg, index) => {
|
1176
1208
|
argTypes[index].push(concreteSubtype);
|
1177
1209
|
});
|
1178
1210
|
}
|
@@ -1180,10 +1212,10 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1180
1212
|
// Are there any positional arguments? If so, try to get the mappings for
|
1181
1213
|
// these arguments by fetching the __match_args__ symbol from the class.
|
1182
1214
|
let positionalArgNames = [];
|
1183
|
-
if (pattern.
|
1215
|
+
if (pattern.d.args.some((arg) => !arg.d.name)) {
|
1184
1216
|
positionalArgNames = getPositionalMatchArgNames(evaluator, types_1.ClassType.cloneAsInstantiable(expandedSubtype));
|
1185
1217
|
}
|
1186
|
-
pattern.
|
1218
|
+
pattern.d.args.forEach((arg, index) => {
|
1187
1219
|
const narrowedArgType = narrowTypeOfClassPatternArgument(evaluator, arg, index, positionalArgNames, types_1.ClassType.cloneAsInstantiable(expandedSubtype),
|
1188
1220
|
/* isPositiveTest */ true);
|
1189
1221
|
argTypes[index].push(narrowedArgType);
|
@@ -1192,14 +1224,14 @@ function assignTypeToPatternTargets(evaluator, type, isTypeIncomplete, pattern)
|
|
1192
1224
|
});
|
1193
1225
|
}
|
1194
1226
|
else {
|
1195
|
-
pattern.
|
1227
|
+
pattern.d.args.forEach((arg, index) => {
|
1196
1228
|
argTypes[index].push(types_1.UnknownType.create());
|
1197
1229
|
});
|
1198
1230
|
}
|
1199
1231
|
return undefined;
|
1200
1232
|
});
|
1201
|
-
pattern.
|
1202
|
-
assignTypeToPatternTargets(evaluator, (0, types_1.combineTypes)(argTypes[index]), isTypeIncomplete, arg.pattern);
|
1233
|
+
pattern.d.args.forEach((arg, index) => {
|
1234
|
+
assignTypeToPatternTargets(evaluator, (0, types_1.combineTypes)(argTypes[index]), isTypeIncomplete, arg.d.pattern);
|
1203
1235
|
});
|
1204
1236
|
break;
|
1205
1237
|
}
|
@@ -1228,55 +1260,56 @@ function wrapTypeInList(evaluator, node, type) {
|
|
1228
1260
|
return types_1.UnknownType.create();
|
1229
1261
|
}
|
1230
1262
|
function validateClassPattern(evaluator, pattern) {
|
1231
|
-
|
1263
|
+
var _a, _b;
|
1264
|
+
let exprType = evaluator.getTypeOfExpression(pattern.d.className, 2 /* EvalFlags.CallBaseDefaults */).type;
|
1232
1265
|
// If the expression is a type alias or other special form, treat it
|
1233
1266
|
// as the special form rather than the class.
|
1234
|
-
if (exprType.specialForm) {
|
1235
|
-
exprType = exprType.specialForm;
|
1267
|
+
if ((_a = exprType.props) === null || _a === void 0 ? void 0 : _a.specialForm) {
|
1268
|
+
exprType = exprType.props.specialForm;
|
1236
1269
|
}
|
1237
1270
|
if ((0, types_1.isAnyOrUnknown)(exprType)) {
|
1238
1271
|
return;
|
1239
1272
|
}
|
1240
1273
|
// Check for certain uses of type aliases that generate runtime exceptions.
|
1241
|
-
if (exprType.typeAliasInfo &&
|
1274
|
+
if (((_b = exprType.props) === null || _b === void 0 ? void 0 : _b.typeAliasInfo) &&
|
1242
1275
|
(0, types_1.isInstantiableClass)(exprType) &&
|
1243
|
-
exprType.typeArguments &&
|
1244
|
-
exprType.isTypeArgumentExplicit) {
|
1245
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classPatternTypeAlias().format({ type: evaluator.printType(exprType) }), pattern.className);
|
1276
|
+
exprType.priv.typeArguments &&
|
1277
|
+
exprType.priv.isTypeArgumentExplicit) {
|
1278
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classPatternTypeAlias().format({ type: evaluator.printType(exprType) }), pattern.d.className);
|
1246
1279
|
}
|
1247
1280
|
else if (!(0, types_1.isInstantiableClass)(exprType)) {
|
1248
1281
|
if (!(0, types_1.isNever)(exprType)) {
|
1249
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocAddendum.typeNotClass().format({ type: evaluator.printType(exprType) }), pattern.className);
|
1282
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocAddendum.typeNotClass().format({ type: evaluator.printType(exprType) }), pattern.d.className);
|
1250
1283
|
}
|
1251
1284
|
}
|
1252
1285
|
else {
|
1253
1286
|
const isBuiltIn = isClassSpecialCaseForClassPattern(exprType);
|
1254
1287
|
// If it's a special-case builtin class, only positional arguments are allowed.
|
1255
1288
|
if (isBuiltIn) {
|
1256
|
-
if (pattern.
|
1257
|
-
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classPatternBuiltInArgPositional(), pattern.
|
1289
|
+
if (pattern.d.args.length === 1 && pattern.d.args[0].d.name) {
|
1290
|
+
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classPatternBuiltInArgPositional(), pattern.d.args[0].d.name);
|
1258
1291
|
}
|
1259
1292
|
}
|
1260
1293
|
// Emits an error if the supplied number of positional patterns is less than
|
1261
1294
|
// expected for the given subject type.
|
1262
|
-
let positionalPatternCount = pattern.
|
1295
|
+
let positionalPatternCount = pattern.d.args.findIndex((arg) => arg.d.name !== undefined);
|
1263
1296
|
if (positionalPatternCount < 0) {
|
1264
|
-
positionalPatternCount = pattern.
|
1297
|
+
positionalPatternCount = pattern.d.args.length;
|
1265
1298
|
}
|
1266
1299
|
let expectedPatternCount = 1;
|
1267
1300
|
if (!isBuiltIn) {
|
1268
1301
|
let positionalArgNames = [];
|
1269
|
-
if (pattern.
|
1302
|
+
if (pattern.d.args.some((arg) => !arg.d.name)) {
|
1270
1303
|
positionalArgNames = getPositionalMatchArgNames(evaluator, exprType);
|
1271
1304
|
}
|
1272
1305
|
expectedPatternCount = positionalArgNames.length;
|
1273
1306
|
}
|
1274
1307
|
if (positionalPatternCount > expectedPatternCount) {
|
1275
1308
|
evaluator.addDiagnostic(diagnosticRules_1.DiagnosticRule.reportGeneralTypeIssues, localize_1.LocMessage.classPatternPositionalArgCount().format({
|
1276
|
-
type: exprType.
|
1309
|
+
type: exprType.shared.name,
|
1277
1310
|
expected: expectedPatternCount,
|
1278
1311
|
received: positionalPatternCount,
|
1279
|
-
}), pattern.
|
1312
|
+
}), pattern.d.args[expectedPatternCount]);
|
1280
1313
|
}
|
1281
1314
|
}
|
1282
1315
|
}
|
@@ -1289,22 +1322,22 @@ function getPatternSubtypeNarrowingCallback(evaluator, reference, subjectExpress
|
|
1289
1322
|
// <literal> is either a str (for TypedDict discrimination) or an int
|
1290
1323
|
// (for tuple discrimination).
|
1291
1324
|
if (subjectExpression.nodeType === 27 /* ParseNodeType.Index */ &&
|
1292
|
-
subjectExpression.items.length === 1 &&
|
1293
|
-
!subjectExpression.trailingComma &&
|
1294
|
-
subjectExpression.items[0].
|
1295
|
-
(0, parseTreeUtils_1.isMatchingExpression)(reference, subjectExpression.
|
1296
|
-
const indexTypeResult = evaluator.getTypeOfExpression(subjectExpression.items[0].
|
1325
|
+
subjectExpression.d.items.length === 1 &&
|
1326
|
+
!subjectExpression.d.trailingComma &&
|
1327
|
+
subjectExpression.d.items[0].d.argCategory === 0 /* ArgumentCategory.Simple */ &&
|
1328
|
+
(0, parseTreeUtils_1.isMatchingExpression)(reference, subjectExpression.d.leftExpr)) {
|
1329
|
+
const indexTypeResult = evaluator.getTypeOfExpression(subjectExpression.d.items[0].d.valueExpr);
|
1297
1330
|
const indexType = indexTypeResult.type;
|
1298
1331
|
if ((0, types_1.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
1299
1332
|
if (types_1.ClassType.isBuiltIn(indexType, ['int', 'str'])) {
|
1300
|
-
const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression(subjectExpression.
|
1333
|
+
const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression(subjectExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
1301
1334
|
const unnarrowedReferenceType = unnarrowedReferenceTypeResult.type;
|
1302
1335
|
return (narrowedSubjectType) => {
|
1303
1336
|
let canNarrow = true;
|
1304
1337
|
const typesToCombine = [];
|
1305
1338
|
(0, typeUtils_1.doForEachSubtype)(narrowedSubjectType, (subtype) => {
|
1306
1339
|
subtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1307
|
-
if ((0, types_1.isClassInstance)(subtype) && subtype.literalValue !== undefined) {
|
1340
|
+
if ((0, types_1.isClassInstance)(subtype) && subtype.priv.literalValue !== undefined) {
|
1308
1341
|
if (types_1.ClassType.isBuiltIn(indexType, 'str')) {
|
1309
1342
|
typesToCombine.push((0, typeGuards_1.narrowTypeForDiscriminatedDictEntryComparison)(evaluator, unnarrowedReferenceType, indexType, subtype,
|
1310
1343
|
/* isPositiveTest */ true));
|
@@ -1333,19 +1366,19 @@ function getPatternSubtypeNarrowingCallback(evaluator, reference, subjectExpress
|
|
1333
1366
|
// Look for a subject expression that contains the reference
|
1334
1367
|
// expression as an entry in a tuple.
|
1335
1368
|
if (subjectExpression.nodeType === 52 /* ParseNodeType.Tuple */) {
|
1336
|
-
const matchingEntryIndex = subjectExpression.
|
1369
|
+
const matchingEntryIndex = subjectExpression.d.items.findIndex((expr) => (0, parseTreeUtils_1.isMatchingExpression)(reference, expr));
|
1337
1370
|
if (matchingEntryIndex >= 0) {
|
1338
|
-
const typeResult = evaluator.getTypeOfExpression(subjectExpression.
|
1371
|
+
const typeResult = evaluator.getTypeOfExpression(subjectExpression.d.items[matchingEntryIndex]);
|
1339
1372
|
return (narrowedSubjectType) => {
|
1340
1373
|
let canNarrow = true;
|
1341
1374
|
const narrowedSubtypes = [];
|
1342
1375
|
(0, typeUtils_1.doForEachSubtype)(narrowedSubjectType, (subtype) => {
|
1343
1376
|
if ((0, types_1.isClassInstance)(subtype) &&
|
1344
1377
|
types_1.ClassType.isBuiltIn(subtype, 'tuple') &&
|
1345
|
-
subtype.tupleTypeArguments &&
|
1346
|
-
matchingEntryIndex < subtype.tupleTypeArguments.length &&
|
1347
|
-
subtype.tupleTypeArguments.every((e) => !e.isUnbounded)) {
|
1348
|
-
narrowedSubtypes.push(subtype.tupleTypeArguments[matchingEntryIndex].type);
|
1378
|
+
subtype.priv.tupleTypeArguments &&
|
1379
|
+
matchingEntryIndex < subtype.priv.tupleTypeArguments.length &&
|
1380
|
+
subtype.priv.tupleTypeArguments.every((e) => !e.isUnbounded)) {
|
1381
|
+
narrowedSubtypes.push(subtype.priv.tupleTypeArguments[matchingEntryIndex].type);
|
1349
1382
|
}
|
1350
1383
|
else if ((0, types_1.isNever)(narrowedSubjectType)) {
|
1351
1384
|
narrowedSubtypes.push(narrowedSubjectType);
|
@@ -1363,8 +1396,8 @@ function getPatternSubtypeNarrowingCallback(evaluator, reference, subjectExpress
|
|
1363
1396
|
// Look for a subject expression of the form "a.b" where "b" is an attribute
|
1364
1397
|
// that is annotated with a literal type.
|
1365
1398
|
if (subjectExpression.nodeType === 35 /* ParseNodeType.MemberAccess */ &&
|
1366
|
-
(0, parseTreeUtils_1.isMatchingExpression)(reference, subjectExpression.
|
1367
|
-
const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression(subjectExpression.
|
1399
|
+
(0, parseTreeUtils_1.isMatchingExpression)(reference, subjectExpression.d.leftExpr)) {
|
1400
|
+
const unnarrowedReferenceTypeResult = evaluator.getTypeOfExpression(subjectExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
1368
1401
|
const unnarrowedReferenceType = unnarrowedReferenceTypeResult.type;
|
1369
1402
|
return (narrowedSubjectType) => {
|
1370
1403
|
if ((0, types_1.isNever)(narrowedSubjectType)) {
|
@@ -1374,8 +1407,8 @@ function getPatternSubtypeNarrowingCallback(evaluator, reference, subjectExpress
|
|
1374
1407
|
return undefined;
|
1375
1408
|
}
|
1376
1409
|
const resultType = (0, typeUtils_1.mapSubtypes)(narrowedSubjectType, (literalSubtype) => {
|
1377
|
-
(0, debug_1.assert)((0, types_1.isClassInstance)(literalSubtype) && literalSubtype.literalValue !== undefined);
|
1378
|
-
return (0, typeGuards_1.narrowTypeForDiscriminatedLiteralFieldComparison)(evaluator, unnarrowedReferenceType, subjectExpression.
|
1410
|
+
(0, debug_1.assert)((0, types_1.isClassInstance)(literalSubtype) && literalSubtype.priv.literalValue !== undefined);
|
1411
|
+
return (0, typeGuards_1.narrowTypeForDiscriminatedLiteralFieldComparison)(evaluator, unnarrowedReferenceType, subjectExpression.d.member.d.value, literalSubtype,
|
1379
1412
|
/* isPositiveTest */ true);
|
1380
1413
|
});
|
1381
1414
|
return {
|