@zzzen/pyright-internal 1.2.0-dev.20230326 → 1.2.0-dev.20230402
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/cacheManager.js +1 -1
- package/dist/analyzer/checker.js +44 -24
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +4 -1
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructorTransform.js +4 -3
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/declarationUtils.d.ts +8 -0
- package/dist/analyzer/declarationUtils.js +147 -1
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/importStatementUtils.js +1 -1
- package/dist/analyzer/parameterUtils.d.ts +25 -1
- package/dist/analyzer/parameterUtils.js +182 -1
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +37 -2
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.js +32 -13
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +13 -13
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +3 -2
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/pythonPathUtils.js +1 -1
- package/dist/analyzer/regions.js +4 -4
- package/dist/analyzer/regions.js.map +1 -1
- package/dist/analyzer/service.js +13 -8
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.js +1 -1
- package/dist/analyzer/sourceFileInfoUtils.js +1 -1
- package/dist/analyzer/sourceMapper.js +1 -1
- package/dist/analyzer/sourceMapperUtils.d.ts +0 -8
- package/dist/analyzer/sourceMapperUtils.js +18 -25
- package/dist/analyzer/sourceMapperUtils.js.map +1 -1
- package/dist/analyzer/staticExpressions.js +2 -2
- package/dist/analyzer/symbolUtils.js +1 -1
- package/dist/analyzer/testWalker.js +3 -0
- package/dist/analyzer/testWalker.js.map +1 -1
- package/dist/analyzer/tracePrinter.js +1 -1
- package/dist/analyzer/typeEvaluator.js +84 -55
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +3 -3
- package/dist/analyzer/typeEvaluatorTypes.js +0 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeEvaluatorWithTracker.js +3 -5
- package/dist/analyzer/typeEvaluatorWithTracker.js.map +1 -1
- package/dist/analyzer/typeGuards.js +182 -159
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +2 -25
- package/dist/analyzer/typeUtils.js +39 -187
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.js +6 -2
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typedDicts.js +2 -1
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/common/diagnosticSink.js +1 -1
- package/dist/common/envVarUtils.js +7 -7
- package/dist/common/envVarUtils.js.map +1 -1
- package/dist/common/fileBasedCancellationUtils.js +1 -1
- package/dist/common/host.js +1 -1
- package/dist/common/lspUtils.js +1 -1
- package/dist/common/pathConsts.d.ts +1 -0
- package/dist/common/pathConsts.js +2 -1
- package/dist/common/pathConsts.js.map +1 -1
- package/dist/common/progressReporter.js +4 -1
- package/dist/common/progressReporter.js.map +1 -1
- package/dist/common/pythonVersion.js +1 -2
- package/dist/common/pythonVersion.js.map +1 -1
- package/dist/common/realFileSystem.js +1 -1
- package/dist/common/textEditTracker.js +1 -1
- package/dist/common/workspaceEditUtils.js +1 -1
- package/dist/languageService/analyzerServiceExecutor.js +2 -2
- package/dist/languageService/analyzerServiceExecutor.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +1 -1
- package/dist/languageService/autoImporter.js +6 -3
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.d.ts +7 -3
- package/dist/languageService/callHierarchyProvider.js +38 -6
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.d.ts +1 -0
- package/dist/languageService/completionProvider.js +29 -10
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/completionProviderUtils.js +2 -2
- package/dist/languageService/completionProviderUtils.js.map +1 -1
- package/dist/languageService/documentSymbolProvider.js +1 -2
- package/dist/languageService/documentSymbolProvider.js.map +1 -1
- package/dist/languageService/importSorter.js +1 -1
- package/dist/languageService/indentationUtils.js +2 -1
- package/dist/languageService/indentationUtils.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +2 -2
- package/dist/localization/localize.d.ts +0 -3
- package/dist/localization/localize.js +0 -1
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +0 -1
- package/dist/tests/checker.test.js +1 -1
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/completions.test.js +3 -2
- package/dist/tests/completions.test.js.map +1 -1
- package/dist/tests/fourslash/fourslash.d.ts +5 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.js +34 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.js.map +1 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.js +19 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.js.map +1 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.js +39 -0
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.js.map +1 -0
- package/dist/tests/harness/fourslash/testState.d.ts +5 -0
- package/dist/tests/harness/fourslash/testState.js +21 -0
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +4 -0
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +4 -0
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +4 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +43 -115
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +1 -5
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/workspaceFactory.d.ts +1 -0
- package/dist/workspaceFactory.js +20 -10
- package/dist/workspaceFactory.js.map +1 -1
- package/package.json +1 -1
- package/dist/analyzer/aliasDeclarationUtils.d.ts +0 -9
- package/dist/analyzer/aliasDeclarationUtils.js +0 -157
- package/dist/analyzer/aliasDeclarationUtils.js.map +0 -1
@@ -38,6 +38,7 @@ const scopeUtils_1 = require("./scopeUtils");
|
|
38
38
|
const symbol_1 = require("./symbol");
|
39
39
|
const typedDicts_1 = require("./typedDicts");
|
40
40
|
const types_1 = require("./types");
|
41
|
+
const types_2 = require("./types");
|
41
42
|
const typeUtils_1 = require("./typeUtils");
|
42
43
|
const typeVarContext_1 = require("./typeVarContext");
|
43
44
|
// Given a reference expression and a test expression, returns a callback that
|
@@ -45,7 +46,7 @@ const typeVarContext_1 = require("./typeVarContext");
|
|
45
46
|
// If the specified flow node is not associated with the test expression,
|
46
47
|
// it returns undefined.
|
47
48
|
function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositiveTest, recursionCount = 0) {
|
48
|
-
if (recursionCount >
|
49
|
+
if (recursionCount > types_2.maxTypeRecursionCount) {
|
49
50
|
return undefined;
|
50
51
|
}
|
51
52
|
recursionCount++;
|
@@ -119,10 +120,10 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
119
120
|
const arg0Expr = testExpression.leftExpression.arguments[0].valueExpression;
|
120
121
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
121
122
|
const callType = evaluator.getTypeOfExpression(testExpression.leftExpression.leftExpression, 2 /* DoNotSpecialize */).type;
|
122
|
-
if ((0,
|
123
|
+
if ((0, types_2.isInstantiableClass)(callType) && types_2.ClassType.isBuiltIn(callType, 'type')) {
|
123
124
|
const classTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
124
125
|
const classType = evaluator.makeTopLevelTypeVarsConcrete(classTypeResult.type);
|
125
|
-
if ((0,
|
126
|
+
if ((0, types_2.isInstantiableClass)(classType)) {
|
126
127
|
return (type) => {
|
127
128
|
return {
|
128
129
|
type: narrowTypeForTypeIs(evaluator, type, classType, adjIsPositiveTest),
|
@@ -139,8 +140,8 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
139
140
|
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression)) {
|
140
141
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
141
142
|
const rightType = rightTypeResult.type;
|
142
|
-
if ((0,
|
143
|
-
(
|
143
|
+
if ((0, types_2.isClassInstance)(rightType) &&
|
144
|
+
(types_2.ClassType.isEnumClass(rightType) || types_2.ClassType.isBuiltIn(rightType, 'bool')) &&
|
144
145
|
rightType.literalValue !== undefined) {
|
145
146
|
return (type) => {
|
146
147
|
return {
|
@@ -159,10 +160,10 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
159
160
|
ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression.baseExpression)) {
|
160
161
|
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression.items[0].valueExpression);
|
161
162
|
const indexType = indexTypeResult.type;
|
162
|
-
if ((0,
|
163
|
-
if (
|
163
|
+
if ((0, types_2.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
164
|
+
if (types_2.ClassType.isBuiltIn(indexType, 'str')) {
|
164
165
|
const rightType = evaluator.getTypeOfExpression(testExpression.rightExpression).type;
|
165
|
-
if ((0,
|
166
|
+
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
166
167
|
return (type) => {
|
167
168
|
return {
|
168
169
|
type: narrowTypeForDiscriminatedDictEntryComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
@@ -171,6 +172,28 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
171
172
|
};
|
172
173
|
}
|
173
174
|
}
|
175
|
+
else if (types_2.ClassType.isBuiltIn(indexType, 'int')) {
|
176
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
177
|
+
const rightType = rightTypeResult.type;
|
178
|
+
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
179
|
+
let canNarrow = false;
|
180
|
+
// Narrowing can be applied only for bool or enum literals.
|
181
|
+
if (types_2.ClassType.isBuiltIn(rightType, 'bool')) {
|
182
|
+
canNarrow = true;
|
183
|
+
}
|
184
|
+
else if (rightType.literalValue instanceof types_1.EnumLiteral) {
|
185
|
+
canNarrow = true;
|
186
|
+
}
|
187
|
+
if (canNarrow) {
|
188
|
+
return (type) => {
|
189
|
+
return {
|
190
|
+
type: narrowTypeForDiscriminatedTupleComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
191
|
+
isIncomplete: !!rightTypeResult.isIncomplete,
|
192
|
+
};
|
193
|
+
};
|
194
|
+
}
|
195
|
+
}
|
196
|
+
}
|
174
197
|
}
|
175
198
|
}
|
176
199
|
}
|
@@ -180,7 +203,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
180
203
|
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression)) {
|
181
204
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
182
205
|
const rightType = rightTypeResult.type;
|
183
|
-
if ((0,
|
206
|
+
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
184
207
|
return (type) => {
|
185
208
|
return {
|
186
209
|
type: narrowTypeForLiteralComparison(evaluator, type, rightType, adjIsPositiveTest,
|
@@ -198,10 +221,10 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
198
221
|
ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression.baseExpression)) {
|
199
222
|
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression.items[0].valueExpression);
|
200
223
|
const indexType = indexTypeResult.type;
|
201
|
-
if ((0,
|
202
|
-
if (
|
224
|
+
if ((0, types_2.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
225
|
+
if (types_2.ClassType.isBuiltIn(indexType, 'str')) {
|
203
226
|
const rightType = evaluator.getTypeOfExpression(testExpression.rightExpression).type;
|
204
|
-
if ((0,
|
227
|
+
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
205
228
|
return (type) => {
|
206
229
|
return {
|
207
230
|
type: narrowTypeForDiscriminatedDictEntryComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
@@ -210,10 +233,10 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
210
233
|
};
|
211
234
|
}
|
212
235
|
}
|
213
|
-
else if (
|
236
|
+
else if (types_2.ClassType.isBuiltIn(indexType, 'int')) {
|
214
237
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
215
238
|
const rightType = rightTypeResult.type;
|
216
|
-
if ((0,
|
239
|
+
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
217
240
|
return (type) => {
|
218
241
|
return {
|
219
242
|
type: narrowTypeForDiscriminatedTupleComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
@@ -235,7 +258,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
235
258
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
236
259
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression.leftExpression, 2 /* DoNotSpecialize */);
|
237
260
|
const callType = callTypeResult.type;
|
238
|
-
if ((0,
|
261
|
+
if ((0, types_2.isFunction)(callType) && callType.details.fullName === 'builtins.len') {
|
239
262
|
const tupleLength = testExpression.rightExpression.value;
|
240
263
|
if (typeof tupleLength === 'number') {
|
241
264
|
return (type) => {
|
@@ -255,7 +278,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
255
278
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
256
279
|
const rightType = rightTypeResult.type;
|
257
280
|
const memberName = testExpression.leftExpression.memberName;
|
258
|
-
if ((0,
|
281
|
+
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
259
282
|
return (type) => {
|
260
283
|
return {
|
261
284
|
type: narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, type, memberName.value, rightType, adjIsPositiveTest),
|
@@ -271,8 +294,8 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
271
294
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
272
295
|
const rightType = rightTypeResult.type;
|
273
296
|
const memberName = testExpression.leftExpression.memberName;
|
274
|
-
if ((0,
|
275
|
-
(
|
297
|
+
if ((0, types_2.isClassInstance)(rightType) &&
|
298
|
+
(types_2.ClassType.isEnumClass(rightType) || types_2.ClassType.isBuiltIn(rightType, 'bool')) &&
|
276
299
|
rightType.literalValue !== undefined) {
|
277
300
|
return (type) => {
|
278
301
|
return {
|
@@ -315,11 +338,11 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
315
338
|
// one or more TypedDicts.
|
316
339
|
const leftTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression);
|
317
340
|
const leftType = leftTypeResult.type;
|
318
|
-
if ((0,
|
341
|
+
if ((0, types_2.isClassInstance)(leftType) && types_2.ClassType.isBuiltIn(leftType, 'str') && (0, typeUtils_1.isLiteralType)(leftType)) {
|
319
342
|
const adjIsPositiveTest = testExpression.operator === 41 /* In */ ? isPositiveTest : !isPositiveTest;
|
320
343
|
return (type) => {
|
321
344
|
return {
|
322
|
-
type: narrowTypeForTypedDictKey(evaluator, type,
|
345
|
+
type: narrowTypeForTypedDictKey(evaluator, type, types_2.ClassType.cloneAsInstantiable(leftType), adjIsPositiveTest),
|
323
346
|
isIncomplete: !!leftTypeResult.isIncomplete,
|
324
347
|
};
|
325
348
|
};
|
@@ -338,7 +361,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
338
361
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
339
362
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
340
363
|
const callType = callTypeResult.type;
|
341
|
-
if ((0,
|
364
|
+
if ((0, types_2.isFunction)(callType) &&
|
342
365
|
(callType.details.builtInName === 'isinstance' || callType.details.builtInName === 'issubclass')) {
|
343
366
|
const isInstanceCheck = callType.details.builtInName === 'isinstance';
|
344
367
|
const arg1TypeResult = evaluator.getTypeOfExpression(arg1Expr, 8 /* EvaluateStringLiteralAsType */ |
|
@@ -351,7 +374,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
351
374
|
return (type) => {
|
352
375
|
const narrowedType = narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck, isPositiveTest,
|
353
376
|
/* allowIntersections */ false, testExpression);
|
354
|
-
if (!(0,
|
377
|
+
if (!(0, types_2.isNever)(narrowedType)) {
|
355
378
|
return {
|
356
379
|
type: narrowedType,
|
357
380
|
isIncomplete,
|
@@ -384,11 +407,11 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
384
407
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
385
408
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
386
409
|
const callType = callTypeResult.type;
|
387
|
-
if ((0,
|
410
|
+
if ((0, types_2.isFunction)(callType) && callType.details.builtInName === 'callable') {
|
388
411
|
return (type) => {
|
389
412
|
let narrowedType = narrowTypeForCallable(evaluator, type, isPositiveTest, testExpression,
|
390
413
|
/* allowIntersections */ false);
|
391
|
-
if (isPositiveTest && (0,
|
414
|
+
if (isPositiveTest && (0, types_2.isNever)(narrowedType)) {
|
392
415
|
// Try again with intersections allowed.
|
393
416
|
narrowedType = narrowTypeForCallable(evaluator, type, isPositiveTest, testExpression,
|
394
417
|
/* allowIntersections */ true);
|
@@ -403,7 +426,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
403
426
|
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.arguments[0].valueExpression)) {
|
404
427
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
405
428
|
const callType = callTypeResult.type;
|
406
|
-
if ((0,
|
429
|
+
if ((0, types_2.isInstantiableClass)(callType) && types_2.ClassType.isBuiltIn(callType, 'bool')) {
|
407
430
|
return (type) => {
|
408
431
|
return {
|
409
432
|
type: narrowTypeForTruthiness(evaluator, type, isPositiveTest),
|
@@ -421,27 +444,27 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
421
444
|
let isPossiblyTypeGuard = false;
|
422
445
|
const isFunctionReturnTypeGuard = (type) => {
|
423
446
|
return (type.details.declaredReturnType &&
|
424
|
-
(0,
|
425
|
-
|
447
|
+
(0, types_2.isClassInstance)(type.details.declaredReturnType) &&
|
448
|
+
types_2.ClassType.isBuiltIn(type.details.declaredReturnType, ['TypeGuard', 'StrictTypeGuard']));
|
426
449
|
};
|
427
450
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
428
451
|
const callType = callTypeResult.type;
|
429
|
-
if ((0,
|
452
|
+
if ((0, types_2.isFunction)(callType) && isFunctionReturnTypeGuard(callType)) {
|
430
453
|
isPossiblyTypeGuard = true;
|
431
454
|
}
|
432
|
-
else if ((0,
|
433
|
-
|
455
|
+
else if ((0, types_2.isOverloadedFunction)(callType) &&
|
456
|
+
types_2.OverloadedFunctionType.getOverloads(callType).some((o) => isFunctionReturnTypeGuard(o))) {
|
434
457
|
isPossiblyTypeGuard = true;
|
435
458
|
}
|
436
|
-
else if ((0,
|
459
|
+
else if ((0, types_2.isClassInstance)(callType)) {
|
437
460
|
isPossiblyTypeGuard = true;
|
438
461
|
}
|
439
462
|
if (isPossiblyTypeGuard) {
|
440
463
|
// Evaluate the type guard call expression.
|
441
464
|
const functionReturnTypeResult = evaluator.getTypeOfExpression(testExpression);
|
442
465
|
const functionReturnType = functionReturnTypeResult.type;
|
443
|
-
if ((0,
|
444
|
-
|
466
|
+
if ((0, types_2.isClassInstance)(functionReturnType) &&
|
467
|
+
types_2.ClassType.isBuiltIn(functionReturnType, 'bool') &&
|
445
468
|
functionReturnType.typeGuardType) {
|
446
469
|
const isStrictTypeGuard = !!functionReturnType.isStrictTypeGuard;
|
447
470
|
const typeGuardType = functionReturnType.typeGuardType;
|
@@ -625,12 +648,12 @@ function narrowTupleTypeForIsNone(evaluator, type, isPositiveTest, indexValue) {
|
|
625
648
|
}
|
626
649
|
const typeOfEntry = evaluator.makeTopLevelTypeVarsConcrete(tupleType.tupleTypeArguments[indexValue].type);
|
627
650
|
if (isPositiveTest) {
|
628
|
-
if (!evaluator.assignType(typeOfEntry,
|
651
|
+
if (!evaluator.assignType(typeOfEntry, types_2.NoneType.createInstance())) {
|
629
652
|
return undefined;
|
630
653
|
}
|
631
654
|
}
|
632
655
|
else {
|
633
|
-
if ((0,
|
656
|
+
if ((0, types_2.isNoneInstance)(typeOfEntry)) {
|
634
657
|
return undefined;
|
635
658
|
}
|
636
659
|
}
|
@@ -644,7 +667,7 @@ function narrowTypeForIsNone(evaluator, type, isPositiveTest) {
|
|
644
667
|
});
|
645
668
|
return evaluator.mapSubtypesExpandTypeVars(expandedType,
|
646
669
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
647
|
-
if ((0,
|
670
|
+
if ((0, types_2.isAnyOrUnknown)(subtype)) {
|
648
671
|
// We need to assume that "Any" is always both None and not None,
|
649
672
|
// so it matches regardless of whether the test is positive or negative.
|
650
673
|
return subtype;
|
@@ -652,17 +675,17 @@ function narrowTypeForIsNone(evaluator, type, isPositiveTest) {
|
|
652
675
|
// If this is a TypeVar that isn't constrained, use the unexpanded
|
653
676
|
// TypeVar. For all other cases (including constrained TypeVars),
|
654
677
|
// use the expanded subtype.
|
655
|
-
const adjustedSubtype = (0,
|
678
|
+
const adjustedSubtype = (0, types_2.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.details.constraints.length === 0
|
656
679
|
? unexpandedSubtype
|
657
680
|
: subtype;
|
658
681
|
// See if it's a match for object.
|
659
|
-
if ((0,
|
682
|
+
if ((0, types_2.isClassInstance)(subtype) && types_2.ClassType.isBuiltIn(subtype, 'object')) {
|
660
683
|
return isPositiveTest
|
661
|
-
? (0, typeUtils_1.addConditionToType)(
|
684
|
+
? (0, typeUtils_1.addConditionToType)(types_2.NoneType.createInstance(), subtype.condition)
|
662
685
|
: adjustedSubtype;
|
663
686
|
}
|
664
687
|
// See if it's a match for None.
|
665
|
-
if ((0,
|
688
|
+
if ((0, types_2.isNoneInstance)(subtype) === isPositiveTest) {
|
666
689
|
return subtype;
|
667
690
|
}
|
668
691
|
return undefined;
|
@@ -675,7 +698,7 @@ function narrowTypeForIsEllipsis(evaluator, type, isPositiveTest) {
|
|
675
698
|
});
|
676
699
|
return evaluator.mapSubtypesExpandTypeVars(expandedType,
|
677
700
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
678
|
-
if ((0,
|
701
|
+
if ((0, types_2.isAnyOrUnknown)(subtype)) {
|
679
702
|
// We need to assume that "Any" is always both None and not None,
|
680
703
|
// so it matches regardless of whether the test is positive or negative.
|
681
704
|
return subtype;
|
@@ -683,16 +706,16 @@ function narrowTypeForIsEllipsis(evaluator, type, isPositiveTest) {
|
|
683
706
|
// If this is a TypeVar that isn't constrained, use the unexpanded
|
684
707
|
// TypeVar. For all other cases (including constrained TypeVars),
|
685
708
|
// use the expanded subtype.
|
686
|
-
const adjustedSubtype = (0,
|
709
|
+
const adjustedSubtype = (0, types_2.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.details.constraints.length === 0
|
687
710
|
? unexpandedSubtype
|
688
711
|
: subtype;
|
689
712
|
// See if it's a match for object.
|
690
|
-
if ((0,
|
713
|
+
if ((0, types_2.isClassInstance)(subtype) && types_2.ClassType.isBuiltIn(subtype, 'object')) {
|
691
714
|
return isPositiveTest
|
692
|
-
? (0, typeUtils_1.addConditionToType)(
|
715
|
+
? (0, typeUtils_1.addConditionToType)(types_2.NoneType.createInstance(), subtype.condition)
|
693
716
|
: adjustedSubtype;
|
694
717
|
}
|
695
|
-
const isEllipsis = (0,
|
718
|
+
const isEllipsis = (0, types_2.isClassInstance)(subtype) && types_2.ClassType.isBuiltIn(subtype, 'ellipsis');
|
696
719
|
// See if it's a match for "...".
|
697
720
|
if (isEllipsis === isPositiveTest) {
|
698
721
|
return subtype;
|
@@ -711,13 +734,13 @@ function getIsInstanceClassTypes(argType) {
|
|
711
734
|
// undefined if any of the types are not valid.
|
712
735
|
const addClassTypesToList = (types) => {
|
713
736
|
types.forEach((subtype) => {
|
714
|
-
if ((0,
|
737
|
+
if ((0, types_2.isInstantiableClass)(subtype) || ((0, types_2.isTypeVar)(subtype) && types_2.TypeBase.isInstantiable(subtype))) {
|
715
738
|
classTypeList.push(subtype);
|
716
739
|
}
|
717
|
-
else if ((0,
|
740
|
+
else if ((0, types_2.isNoneTypeClass)(subtype)) {
|
718
741
|
classTypeList.push(subtype);
|
719
742
|
}
|
720
|
-
else if ((0,
|
743
|
+
else if ((0, types_2.isFunction)(subtype) &&
|
721
744
|
subtype.details.parameters.length === 2 &&
|
722
745
|
subtype.details.parameters[0].category === 1 /* VarArgList */ &&
|
723
746
|
subtype.details.parameters[1].category === 2 /* VarArgDictionary */) {
|
@@ -729,10 +752,10 @@ function getIsInstanceClassTypes(argType) {
|
|
729
752
|
});
|
730
753
|
};
|
731
754
|
const addClassTypesRecursive = (subtype, recursionCount = 0) => {
|
732
|
-
if (recursionCount >
|
755
|
+
if (recursionCount > types_2.maxTypeRecursionCount) {
|
733
756
|
return;
|
734
757
|
}
|
735
|
-
if ((0,
|
758
|
+
if ((0, types_2.isClass)(subtype) && types_2.TypeBase.isInstance(subtype) && (0, typeUtils_1.isTupleClass)(subtype)) {
|
736
759
|
if (subtype.tupleTypeArguments) {
|
737
760
|
subtype.tupleTypeArguments.forEach((tupleEntry) => {
|
738
761
|
addClassTypesRecursive(tupleEntry.type, recursionCount + 1);
|
@@ -755,32 +778,32 @@ function isIsinstanceFilterSuperclass(evaluator, varType, filterType, concreteFi
|
|
755
778
|
if (concreteFilterType.includeSubclasses) {
|
756
779
|
return false;
|
757
780
|
}
|
758
|
-
if ((0,
|
781
|
+
if ((0, types_2.isTypeVar)(filterType)) {
|
759
782
|
return false;
|
760
783
|
}
|
761
|
-
if (
|
784
|
+
if (types_2.ClassType.isDerivedFrom(varType, concreteFilterType)) {
|
762
785
|
return true;
|
763
786
|
}
|
764
787
|
if (isInstanceCheck) {
|
765
|
-
if (
|
788
|
+
if (types_2.ClassType.isProtocolClass(concreteFilterType) && evaluator.assignType(concreteFilterType, varType)) {
|
766
789
|
return true;
|
767
790
|
}
|
768
791
|
}
|
769
792
|
// Handle the special case where the variable type is a TypedDict and
|
770
793
|
// we're filtering against 'dict'. TypedDict isn't derived from dict,
|
771
794
|
// but at runtime, isinstance returns True.
|
772
|
-
if (
|
795
|
+
if (types_2.ClassType.isBuiltIn(concreteFilterType, 'dict') && types_2.ClassType.isTypedDictClass(varType)) {
|
773
796
|
return true;
|
774
797
|
}
|
775
798
|
return false;
|
776
799
|
}
|
777
800
|
exports.isIsinstanceFilterSuperclass = isIsinstanceFilterSuperclass;
|
778
801
|
function isIsinstanceFilterSubclass(evaluator, varType, filterType, concreteFilterType, isInstanceCheck) {
|
779
|
-
if (
|
802
|
+
if (types_2.ClassType.isDerivedFrom(concreteFilterType, varType)) {
|
780
803
|
return true;
|
781
804
|
}
|
782
805
|
if (isInstanceCheck) {
|
783
|
-
if (
|
806
|
+
if (types_2.ClassType.isProtocolClass(varType) && evaluator.assignType(varType, concreteFilterType)) {
|
784
807
|
return true;
|
785
808
|
}
|
786
809
|
}
|
@@ -805,7 +828,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
805
828
|
let isClassRelationshipIndeterminate = false;
|
806
829
|
for (const filterType of classTypeList) {
|
807
830
|
const concreteFilterType = evaluator.makeTopLevelTypeVarsConcrete(filterType);
|
808
|
-
if ((0,
|
831
|
+
if ((0, types_2.isInstantiableClass)(concreteFilterType)) {
|
809
832
|
const filterIsSuperclass = isIsinstanceFilterSuperclass(evaluator, varType, filterType, concreteFilterType, isInstanceCheck);
|
810
833
|
const filterIsSubclass = isIsinstanceFilterSubclass(evaluator, varType, filterType, concreteFilterType, isInstanceCheck);
|
811
834
|
if (filterIsSuperclass) {
|
@@ -818,7 +841,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
818
841
|
// will generate false positives.
|
819
842
|
if (filterIsSubclass &&
|
820
843
|
filterIsSuperclass &&
|
821
|
-
!
|
844
|
+
!types_2.ClassType.isSameGenericClass(varType, concreteFilterType)) {
|
822
845
|
isClassRelationshipIndeterminate = true;
|
823
846
|
}
|
824
847
|
if (isPositiveTest) {
|
@@ -834,11 +857,11 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
834
857
|
// Try to retain the type arguments for the filter type. This is
|
835
858
|
// important because a specialized version of the filter cannot
|
836
859
|
// be passed to isinstance or issubclass.
|
837
|
-
if ((0,
|
838
|
-
if (
|
860
|
+
if ((0, types_2.isClass)(filterType)) {
|
861
|
+
if (types_2.ClassType.isSpecialBuiltIn(filterType) ||
|
839
862
|
filterType.details.typeParameters.length > 0) {
|
840
863
|
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(filterType));
|
841
|
-
const unspecializedFilterType =
|
864
|
+
const unspecializedFilterType = types_2.ClassType.cloneForSpecialization(filterType,
|
842
865
|
/* typeArguments */ undefined,
|
843
866
|
/* isTypeArgumentExplicit */ false);
|
844
867
|
if ((0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluator, unspecializedFilterType, varType, typeVarContext,
|
@@ -857,38 +880,38 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
857
880
|
// the two types.
|
858
881
|
const className = `<subclass of ${varType.details.name} and ${concreteFilterType.details.name}>`;
|
859
882
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
860
|
-
let newClassType =
|
883
|
+
let newClassType = types_2.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 0 /* None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
861
884
|
/* declaredMetaclass */ undefined, varType.details.effectiveMetaclass, varType.details.docString);
|
862
|
-
newClassType.details.baseClasses = [
|
885
|
+
newClassType.details.baseClasses = [types_2.ClassType.cloneAsInstantiable(varType), concreteFilterType];
|
863
886
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
864
887
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, concreteFilterType.condition);
|
865
|
-
if ((0,
|
888
|
+
if ((0, types_2.isTypeVar)(unexpandedType) &&
|
866
889
|
!unexpandedType.details.isParamSpec &&
|
867
890
|
unexpandedType.details.constraints.length === 0) {
|
868
891
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, [
|
869
892
|
{
|
870
|
-
typeVarName:
|
893
|
+
typeVarName: types_2.TypeVarType.getNameWithScope(unexpandedType),
|
871
894
|
constraintIndex: 0,
|
872
895
|
isConstrainedTypeVar: false,
|
873
896
|
},
|
874
897
|
]);
|
875
898
|
}
|
876
|
-
let newClassInstanceType =
|
899
|
+
let newClassInstanceType = types_2.ClassType.cloneAsInstance(newClassType);
|
877
900
|
if (varType.condition) {
|
878
901
|
newClassInstanceType = (0, typeUtils_1.addConditionToType)(newClassInstanceType, varType.condition);
|
879
902
|
}
|
880
903
|
// If this is a issubclass check, we do a double conversion from instantiable
|
881
904
|
// to instance back to instantiable to make sure that the includeSubclasses flag
|
882
905
|
// gets cleared.
|
883
|
-
filteredTypes.push(isInstanceCheck ? newClassInstanceType :
|
906
|
+
filteredTypes.push(isInstanceCheck ? newClassInstanceType : types_2.ClassType.cloneAsInstantiable(newClassInstanceType));
|
884
907
|
}
|
885
908
|
}
|
886
909
|
}
|
887
|
-
else if ((0,
|
910
|
+
else if ((0, types_2.isTypeVar)(filterType) && types_2.TypeBase.isInstantiable(filterType)) {
|
888
911
|
// Handle the case where the filter type is Type[T] and the unexpanded
|
889
912
|
// subtype is some instance type, possibly T.
|
890
|
-
if (isInstanceCheck &&
|
891
|
-
if ((0,
|
913
|
+
if (isInstanceCheck && types_2.TypeBase.isInstance(unexpandedType)) {
|
914
|
+
if ((0, types_2.isTypeVar)(unexpandedType) && (0, types_2.isTypeSame)((0, typeUtils_1.convertToInstance)(filterType), unexpandedType)) {
|
892
915
|
// If the unexpanded subtype is T, we can definitively filter
|
893
916
|
// in both the positive and negative cases.
|
894
917
|
if (isPositiveTest) {
|
@@ -907,8 +930,8 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
907
930
|
}
|
908
931
|
}
|
909
932
|
}
|
910
|
-
else if (!isInstanceCheck &&
|
911
|
-
if ((0,
|
933
|
+
else if (!isInstanceCheck && types_2.TypeBase.isInstantiable(unexpandedType)) {
|
934
|
+
if ((0, types_2.isTypeVar)(unexpandedType) && (0, types_2.isTypeSame)(filterType, unexpandedType)) {
|
912
935
|
if (isPositiveTest) {
|
913
936
|
filteredTypes.push(unexpandedType);
|
914
937
|
}
|
@@ -924,12 +947,12 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
924
947
|
}
|
925
948
|
}
|
926
949
|
}
|
927
|
-
else if ((0,
|
950
|
+
else if ((0, types_2.isFunction)(filterType)) {
|
928
951
|
// Handle an isinstance check against Callable.
|
929
952
|
if (isInstanceCheck) {
|
930
953
|
let isCallable = false;
|
931
|
-
if ((0,
|
932
|
-
if (
|
954
|
+
if ((0, types_2.isClass)(varType)) {
|
955
|
+
if (types_2.TypeBase.isInstantiable(unexpandedType)) {
|
933
956
|
isCallable = true;
|
934
957
|
}
|
935
958
|
else {
|
@@ -961,7 +984,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
961
984
|
// We perform a double conversion from instance to instantiable
|
962
985
|
// here to make sure that the includeSubclasses flag is cleared
|
963
986
|
// if it's a class.
|
964
|
-
return filteredTypes.map((t) => ((0,
|
987
|
+
return filteredTypes.map((t) => ((0, types_2.isInstantiableClass)(t) ? (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.convertToInstance)(t)) : t));
|
965
988
|
}
|
966
989
|
return filteredTypes.map((t) => (0, typeUtils_1.convertToInstance)(t));
|
967
990
|
};
|
@@ -973,7 +996,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
973
996
|
if (evaluator.assignType(varType, (0, typeUtils_1.convertToInstance)(concreteFilterType))) {
|
974
997
|
// If the filter type is a Callable, use the original type. If the
|
975
998
|
// filter type is a callback protocol, use the filter type.
|
976
|
-
if ((0,
|
999
|
+
if ((0, types_2.isFunction)(filterType)) {
|
977
1000
|
filteredTypes.push(unexpandedType);
|
978
1001
|
}
|
979
1002
|
else {
|
@@ -986,7 +1009,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
986
1009
|
// If the filter type is a runtime checkable protocol class, it can
|
987
1010
|
// be used in an instance check.
|
988
1011
|
const concreteFilterType = evaluator.makeTopLevelTypeVarsConcrete(filterType);
|
989
|
-
if ((0,
|
1012
|
+
if ((0, types_2.isClass)(concreteFilterType) && !types_2.ClassType.isProtocolClass(concreteFilterType)) {
|
990
1013
|
return false;
|
991
1014
|
}
|
992
1015
|
return evaluator.assignType(varType, (0, typeUtils_1.convertToInstance)(concreteFilterType));
|
@@ -1005,30 +1028,30 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1005
1028
|
// on a constrained TypeVar that they want to filter based on its constrained
|
1006
1029
|
// parts.
|
1007
1030
|
const negativeFallback = (0, typeUtils_1.getTypeCondition)(subtype) ? subtype : unexpandedSubtype;
|
1008
|
-
const isSubtypeTypeObject = (0,
|
1009
|
-
if (isPositiveTest && (0,
|
1031
|
+
const isSubtypeTypeObject = (0, types_2.isClassInstance)(subtype) && types_2.ClassType.isBuiltIn(subtype, 'type');
|
1032
|
+
if (isPositiveTest && (0, types_2.isAnyOrUnknown)(subtype)) {
|
1010
1033
|
// If this is a positive test and the effective type is Any or
|
1011
1034
|
// Unknown, we can assume that the type matches one of the
|
1012
1035
|
// specified types.
|
1013
1036
|
if (isInstanceCheck) {
|
1014
|
-
anyOrUnknownSubstitutions.push((0,
|
1037
|
+
anyOrUnknownSubstitutions.push((0, types_2.combineTypes)(classTypeList.map((classType) => (0, typeUtils_1.convertToInstance)(classType))));
|
1015
1038
|
}
|
1016
1039
|
else {
|
1017
1040
|
// We perform a double conversion from instance to instantiable
|
1018
1041
|
// here to make sure that the includeSubclasses flag is cleared
|
1019
1042
|
// if it's a class.
|
1020
|
-
anyOrUnknownSubstitutions.push((0,
|
1043
|
+
anyOrUnknownSubstitutions.push((0, types_2.combineTypes)(classTypeList.map((classType) => (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.convertToInstance)(classType)))));
|
1021
1044
|
}
|
1022
1045
|
anyOrUnknown.push(subtype);
|
1023
1046
|
return undefined;
|
1024
1047
|
}
|
1025
1048
|
if (isInstanceCheck) {
|
1026
|
-
if ((0,
|
1049
|
+
if ((0, types_2.isNoneInstance)(subtype)) {
|
1027
1050
|
const containsNoneType = classTypeList.some((t) => {
|
1028
|
-
if ((0,
|
1051
|
+
if ((0, types_2.isNoneTypeClass)(t)) {
|
1029
1052
|
return true;
|
1030
1053
|
}
|
1031
|
-
return (0,
|
1054
|
+
return (0, types_2.isInstantiableClass)(t) && types_2.ClassType.isBuiltIn(t, 'NoneType');
|
1032
1055
|
});
|
1033
1056
|
if (isPositiveTest) {
|
1034
1057
|
return containsNoneType ? subtype : undefined;
|
@@ -1037,28 +1060,28 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1037
1060
|
return containsNoneType ? undefined : subtype;
|
1038
1061
|
}
|
1039
1062
|
}
|
1040
|
-
if ((0,
|
1063
|
+
if ((0, types_2.isModule)(subtype) || ((0, types_2.isClassInstance)(subtype) && types_2.ClassType.isBuiltIn(subtype, 'ModuleType'))) {
|
1041
1064
|
// Handle type narrowing for runtime-checkable protocols
|
1042
1065
|
// when applied to modules.
|
1043
1066
|
if (isPositiveTest) {
|
1044
1067
|
const filteredTypes = classTypeList.filter((classType) => {
|
1045
1068
|
const concreteClassType = evaluator.makeTopLevelTypeVarsConcrete(classType);
|
1046
|
-
return ((0,
|
1069
|
+
return ((0, types_2.isInstantiableClass)(concreteClassType) && types_2.ClassType.isProtocolClass(concreteClassType));
|
1047
1070
|
});
|
1048
1071
|
if (filteredTypes.length > 0) {
|
1049
|
-
return (0, typeUtils_1.convertToInstance)((0,
|
1072
|
+
return (0, typeUtils_1.convertToInstance)((0, types_2.combineTypes)(filteredTypes));
|
1050
1073
|
}
|
1051
1074
|
}
|
1052
1075
|
}
|
1053
|
-
if ((0,
|
1054
|
-
return (0,
|
1076
|
+
if ((0, types_2.isClassInstance)(subtype) && !isSubtypeTypeObject) {
|
1077
|
+
return (0, types_2.combineTypes)(filterClassType(types_2.ClassType.cloneAsInstantiable(subtype), (0, typeUtils_1.convertToInstance)(unexpandedSubtype), (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1055
1078
|
}
|
1056
|
-
if (((0,
|
1057
|
-
return (0,
|
1079
|
+
if (((0, types_2.isFunction)(subtype) || (0, types_2.isOverloadedFunction)(subtype)) && isInstanceCheck) {
|
1080
|
+
return (0, types_2.combineTypes)(filterFunctionType(subtype, (0, typeUtils_1.convertToInstance)(unexpandedSubtype)));
|
1058
1081
|
}
|
1059
|
-
if ((0,
|
1082
|
+
if ((0, types_2.isInstantiableClass)(subtype) || isSubtypeTypeObject) {
|
1060
1083
|
// Handle the special case of isinstance(x, type).
|
1061
|
-
const includesTypeType = classTypeList.some((classType) => (0,
|
1084
|
+
const includesTypeType = classTypeList.some((classType) => (0, types_2.isInstantiableClass)(classType) && types_2.ClassType.isBuiltIn(classType, 'type'));
|
1062
1085
|
if (isPositiveTest) {
|
1063
1086
|
return includesTypeType ? negativeFallback : undefined;
|
1064
1087
|
}
|
@@ -1068,13 +1091,13 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1068
1091
|
}
|
1069
1092
|
}
|
1070
1093
|
else {
|
1071
|
-
if ((0,
|
1072
|
-
return (0,
|
1094
|
+
if ((0, types_2.isInstantiableClass)(subtype)) {
|
1095
|
+
return (0, types_2.combineTypes)(filterClassType(subtype, unexpandedSubtype, (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1073
1096
|
}
|
1074
1097
|
if (isSubtypeTypeObject) {
|
1075
1098
|
const objectType = evaluator.getBuiltInObject(errorNode, 'object');
|
1076
|
-
if (objectType && (0,
|
1077
|
-
return (0,
|
1099
|
+
if (objectType && (0, types_2.isClassInstance)(objectType)) {
|
1100
|
+
return (0, types_2.combineTypes)(filterClassType(types_2.ClassType.cloneAsInstantiable(objectType), (0, typeUtils_1.convertToInstantiable)(unexpandedSubtype), (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1078
1101
|
}
|
1079
1102
|
}
|
1080
1103
|
}
|
@@ -1086,11 +1109,11 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1086
1109
|
// Any/Unknown. For example, if the statement is "isinstance(x, list)"
|
1087
1110
|
// and the type of x is "List[str] | int | Any", the result should be
|
1088
1111
|
// "List[str]", not "List[str] | List[Unknown]".
|
1089
|
-
if ((0,
|
1090
|
-
return (0,
|
1112
|
+
if ((0, types_2.isNever)(filteredType) && anyOrUnknownSubstitutions.length > 0) {
|
1113
|
+
return (0, types_2.combineTypes)(anyOrUnknownSubstitutions);
|
1091
1114
|
}
|
1092
|
-
if ((0,
|
1093
|
-
return (0,
|
1115
|
+
if ((0, types_2.isNever)(filteredType) && anyOrUnknown.length > 0) {
|
1116
|
+
return (0, types_2.combineTypes)(anyOrUnknown);
|
1094
1117
|
}
|
1095
1118
|
return filteredType;
|
1096
1119
|
}
|
@@ -1099,7 +1122,7 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1099
1122
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1100
1123
|
const concreteSubtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1101
1124
|
// If it's not a tuple, we can't narrow it.
|
1102
|
-
if (!(0,
|
1125
|
+
if (!(0, types_2.isClassInstance)(concreteSubtype) ||
|
1103
1126
|
!(0, typeUtils_1.isTupleClass)(concreteSubtype) ||
|
1104
1127
|
(0, typeUtils_1.isUnboundedTupleClass)(concreteSubtype) ||
|
1105
1128
|
!concreteSubtype.tupleTypeArguments) {
|
@@ -1120,8 +1143,8 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1120
1143
|
}
|
1121
1144
|
// Narrowing in the negative case is possible only with tuples
|
1122
1145
|
// with a known length.
|
1123
|
-
if (!(0,
|
1124
|
-
!
|
1146
|
+
if (!(0, types_2.isClassInstance)(containerType) ||
|
1147
|
+
!types_2.ClassType.isBuiltIn(containerType, 'tuple') ||
|
1125
1148
|
!containerType.tupleTypeArguments) {
|
1126
1149
|
return referenceType;
|
1127
1150
|
}
|
@@ -1130,10 +1153,10 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1130
1153
|
const typesToEliminate = [];
|
1131
1154
|
containerType.tupleTypeArguments.forEach((tupleEntry) => {
|
1132
1155
|
if (!tupleEntry.isUnbounded) {
|
1133
|
-
if ((0,
|
1156
|
+
if ((0, types_2.isNoneInstance)(tupleEntry.type)) {
|
1134
1157
|
typesToEliminate.push(tupleEntry.type);
|
1135
1158
|
}
|
1136
|
-
else if ((0,
|
1159
|
+
else if ((0, types_2.isClassInstance)(tupleEntry.type) && (0, typeUtils_1.isLiteralType)(tupleEntry.type)) {
|
1137
1160
|
typesToEliminate.push(tupleEntry.type);
|
1138
1161
|
}
|
1139
1162
|
}
|
@@ -1143,15 +1166,15 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1143
1166
|
}
|
1144
1167
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1145
1168
|
referenceSubtype = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1146
|
-
if ((0,
|
1169
|
+
if ((0, types_2.isClassInstance)(referenceSubtype) && referenceSubtype.literalValue === undefined) {
|
1147
1170
|
// If we're able to enumerate all possible literal values
|
1148
1171
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1149
1172
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, referenceSubtype);
|
1150
1173
|
if (allLiteralTypes && allLiteralTypes.length > 0) {
|
1151
|
-
return (0,
|
1174
|
+
return (0, types_2.combineTypes)(allLiteralTypes.filter((type) => !typesToEliminate.some((t) => (0, types_2.isTypeSame)(t, type))));
|
1152
1175
|
}
|
1153
1176
|
}
|
1154
|
-
if (typesToEliminate.some((t) => (0,
|
1177
|
+
if (typesToEliminate.some((t) => (0, types_2.isTypeSame)(t, referenceSubtype))) {
|
1155
1178
|
return undefined;
|
1156
1179
|
}
|
1157
1180
|
return referenceSubtype;
|
@@ -1160,7 +1183,7 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1160
1183
|
function getElementTypeForContainerNarrowing(containerType) {
|
1161
1184
|
// We support contains narrowing only for certain built-in types that have been specialized.
|
1162
1185
|
const supportedContainers = ['list', 'set', 'frozenset', 'deque', 'tuple', 'dict', 'defaultdict', 'OrderedDict'];
|
1163
|
-
if (!(0,
|
1186
|
+
if (!(0, types_2.isClassInstance)(containerType) || !types_2.ClassType.isBuiltIn(containerType, supportedContainers)) {
|
1164
1187
|
return undefined;
|
1165
1188
|
}
|
1166
1189
|
if (!containerType.typeArguments || containerType.typeArguments.length < 1) {
|
@@ -1168,7 +1191,7 @@ function getElementTypeForContainerNarrowing(containerType) {
|
|
1168
1191
|
}
|
1169
1192
|
let elementType = containerType.typeArguments[0];
|
1170
1193
|
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.tupleTypeArguments) {
|
1171
|
-
elementType = (0,
|
1194
|
+
elementType = (0, types_2.combineTypes)(containerType.tupleTypeArguments.map((t) => t.type));
|
1172
1195
|
}
|
1173
1196
|
return elementType;
|
1174
1197
|
}
|
@@ -1182,12 +1205,12 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1182
1205
|
// narrow the reference type to "float" because it is a supertype of "int".
|
1183
1206
|
const narrowedSupertypes = (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1184
1207
|
const concreteReferenceType = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1185
|
-
if ((0,
|
1208
|
+
if ((0, types_2.isAnyOrUnknown)(concreteReferenceType)) {
|
1186
1209
|
canNarrow = false;
|
1187
1210
|
return referenceSubtype;
|
1188
1211
|
}
|
1189
1212
|
// Handle "type" specially.
|
1190
|
-
if ((0,
|
1213
|
+
if ((0, types_2.isClassInstance)(concreteReferenceType) && types_2.ClassType.isBuiltIn(concreteReferenceType, 'type')) {
|
1191
1214
|
canNarrow = false;
|
1192
1215
|
return referenceSubtype;
|
1193
1216
|
}
|
@@ -1196,7 +1219,7 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1196
1219
|
}
|
1197
1220
|
if (evaluator.assignType(elementTypeWithoutLiteral, concreteReferenceType)) {
|
1198
1221
|
return (0, typeUtils_1.mapSubtypes)(elementType, (elementSubtype) => {
|
1199
|
-
if ((0,
|
1222
|
+
if ((0, types_2.isClassInstance)(elementSubtype) && (0, types_2.isSameWithoutLiteralValue)(referenceSubtype, elementSubtype)) {
|
1200
1223
|
return elementSubtype;
|
1201
1224
|
}
|
1202
1225
|
return undefined;
|
@@ -1211,7 +1234,7 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1211
1234
|
// subtypes of "object".
|
1212
1235
|
const narrowedSubtypes = (0, typeUtils_1.mapSubtypes)(elementType, (elementSubtype) => {
|
1213
1236
|
const concreteElementType = evaluator.makeTopLevelTypeVarsConcrete(elementSubtype);
|
1214
|
-
if ((0,
|
1237
|
+
if ((0, types_2.isAnyOrUnknown)(concreteElementType)) {
|
1215
1238
|
canNarrow = false;
|
1216
1239
|
return referenceType;
|
1217
1240
|
}
|
@@ -1220,14 +1243,14 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1220
1243
|
}
|
1221
1244
|
return undefined;
|
1222
1245
|
});
|
1223
|
-
return canNarrow ? (0,
|
1246
|
+
return canNarrow ? (0, types_2.combineTypes)([narrowedSupertypes, narrowedSubtypes]) : referenceType;
|
1224
1247
|
}
|
1225
1248
|
exports.narrowTypeForContainerElementType = narrowTypeForContainerElementType;
|
1226
1249
|
// Attempts to narrow a type based on whether it is a TypedDict with
|
1227
1250
|
// a literal key value.
|
1228
1251
|
function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPositiveTest) {
|
1229
1252
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1230
|
-
if ((0,
|
1253
|
+
if ((0, types_2.isClassInstance)(subtype) && types_2.ClassType.isTypedDictClass(subtype)) {
|
1231
1254
|
const entries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype, /* allowNarrowed */ true);
|
1232
1255
|
const tdEntry = entries.get(literalKey.literalValue);
|
1233
1256
|
if (isPositiveTest) {
|
@@ -1236,7 +1259,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1236
1259
|
// the TypedDict doesn't define this entry, it is not this type.
|
1237
1260
|
// If it's not final, we can't say this because it could be a
|
1238
1261
|
// subclass of this TypedDict that adds more fields.
|
1239
|
-
return
|
1262
|
+
return types_2.ClassType.isFinal(subtype) ? undefined : subtype;
|
1240
1263
|
}
|
1241
1264
|
// If the entry is currently not required and not marked provided, we can mark
|
1242
1265
|
// it as provided after this guard expression confirms it is.
|
@@ -1258,7 +1281,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1258
1281
|
isProvided: true,
|
1259
1282
|
});
|
1260
1283
|
// Clone the TypedDict object with the new entries.
|
1261
|
-
return
|
1284
|
+
return types_2.ClassType.cloneAsInstance(types_2.ClassType.cloneForNarrowedTypedDictEntries(types_2.ClassType.cloneAsInstantiable(subtype), newNarrowedEntriesMap));
|
1262
1285
|
}
|
1263
1286
|
else {
|
1264
1287
|
return tdEntry !== undefined && (tdEntry.isRequired || tdEntry.isProvided) ? undefined : subtype;
|
@@ -1274,7 +1297,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1274
1297
|
function narrowTypeForDiscriminatedDictEntryComparison(evaluator, referenceType, indexLiteralType, literalType, isPositiveTest) {
|
1275
1298
|
let canNarrow = true;
|
1276
1299
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1277
|
-
if ((0,
|
1300
|
+
if ((0, types_2.isClassInstance)(subtype) && types_2.ClassType.isTypedDictClass(subtype)) {
|
1278
1301
|
const symbolMap = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype);
|
1279
1302
|
const tdEntry = symbolMap.get(indexLiteralType.literalValue);
|
1280
1303
|
if (tdEntry && (0, typeUtils_1.isLiteralTypeOrUnion)(tdEntry.valueType)) {
|
@@ -1295,8 +1318,8 @@ function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, ind
|
|
1295
1318
|
let canNarrow = true;
|
1296
1319
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1297
1320
|
var _a;
|
1298
|
-
if ((0,
|
1299
|
-
|
1321
|
+
if ((0, types_2.isClassInstance)(subtype) &&
|
1322
|
+
types_2.ClassType.isTupleClass(subtype) &&
|
1300
1323
|
!(0, typeUtils_1.isUnboundedTupleClass)(subtype) &&
|
1301
1324
|
typeof indexLiteralType.literalValue === 'number') {
|
1302
1325
|
const indexValue = indexLiteralType.literalValue;
|
@@ -1323,22 +1346,22 @@ function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, ind
|
|
1323
1346
|
function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceType, memberName, literalType, isPositiveTest) {
|
1324
1347
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1325
1348
|
let memberInfo;
|
1326
|
-
if ((0,
|
1349
|
+
if ((0, types_2.isClassInstance)(subtype)) {
|
1327
1350
|
memberInfo = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
|
1328
1351
|
}
|
1329
|
-
else if ((0,
|
1352
|
+
else if ((0, types_2.isInstantiableClass)(subtype)) {
|
1330
1353
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
|
1331
1354
|
}
|
1332
1355
|
if (memberInfo && memberInfo.isTypeDeclared) {
|
1333
1356
|
let memberType = evaluator.getTypeOfMember(memberInfo);
|
1334
1357
|
// Handle the case where the field is a property
|
1335
1358
|
// that has a declared literal return type for its getter.
|
1336
|
-
if ((0,
|
1359
|
+
if ((0, types_2.isClassInstance)(subtype) && (0, typeUtils_1.isProperty)(memberType)) {
|
1337
1360
|
const getterInfo = (0, typeUtils_1.lookUpObjectMember)(memberType, 'fget');
|
1338
1361
|
if (getterInfo && getterInfo.isTypeDeclared) {
|
1339
1362
|
const getterType = evaluator.getTypeOfMember(getterInfo);
|
1340
|
-
if ((0,
|
1341
|
-
const getterReturnType =
|
1363
|
+
if ((0, types_2.isFunction)(getterType) && getterType.details.declaredReturnType) {
|
1364
|
+
const getterReturnType = types_2.FunctionType.getSpecializedReturnType(getterType);
|
1342
1365
|
if (getterReturnType) {
|
1343
1366
|
memberType = getterReturnType;
|
1344
1367
|
}
|
@@ -1364,10 +1387,10 @@ function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceTy
|
|
1364
1387
|
function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType, memberName, isPositiveTest) {
|
1365
1388
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1366
1389
|
let memberInfo;
|
1367
|
-
if ((0,
|
1390
|
+
if ((0, types_2.isClassInstance)(subtype)) {
|
1368
1391
|
memberInfo = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
|
1369
1392
|
}
|
1370
|
-
else if ((0,
|
1393
|
+
else if ((0, types_2.isInstantiableClass)(subtype)) {
|
1371
1394
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
|
1372
1395
|
}
|
1373
1396
|
if (memberInfo && memberInfo.isTypeDeclared) {
|
@@ -1380,13 +1403,13 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1380
1403
|
if ((0, typeUtils_1.isProperty)(memberSubtype) || (0, typeUtils_1.isMaybeDescriptorInstance)(memberSubtype)) {
|
1381
1404
|
canNarrow = false;
|
1382
1405
|
}
|
1383
|
-
if ((0,
|
1406
|
+
if ((0, types_2.isAnyOrUnknown)(memberSubtype) || (0, types_2.isNoneInstance)(memberSubtype) || (0, types_2.isNever)(memberSubtype)) {
|
1384
1407
|
canNarrow = false;
|
1385
1408
|
}
|
1386
1409
|
});
|
1387
1410
|
}
|
1388
1411
|
else {
|
1389
|
-
canNarrow = (0,
|
1412
|
+
canNarrow = (0, types_2.isNoneInstance)(memberType);
|
1390
1413
|
}
|
1391
1414
|
if (canNarrow) {
|
1392
1415
|
return undefined;
|
@@ -1399,21 +1422,21 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1399
1422
|
function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
1400
1423
|
return evaluator.mapSubtypesExpandTypeVars(type,
|
1401
1424
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
1402
|
-
if ((0,
|
1403
|
-
const matches =
|
1425
|
+
if ((0, types_2.isClassInstance)(subtype)) {
|
1426
|
+
const matches = types_2.ClassType.isDerivedFrom(classType, types_2.ClassType.cloneAsInstantiable(subtype));
|
1404
1427
|
if (isPositiveTest) {
|
1405
1428
|
if (matches) {
|
1406
|
-
if (
|
1429
|
+
if (types_2.ClassType.isSameGenericClass(subtype, classType)) {
|
1407
1430
|
return subtype;
|
1408
1431
|
}
|
1409
|
-
return (0, typeUtils_1.addConditionToType)(
|
1432
|
+
return (0, typeUtils_1.addConditionToType)(types_2.ClassType.cloneAsInstance(classType), subtype.condition);
|
1410
1433
|
}
|
1411
1434
|
return undefined;
|
1412
1435
|
}
|
1413
1436
|
else {
|
1414
1437
|
// If the class if marked final and it matches, then
|
1415
1438
|
// we can eliminate it in the negative case.
|
1416
|
-
if (matches &&
|
1439
|
+
if (matches && types_2.ClassType.isFinal(subtype)) {
|
1417
1440
|
return undefined;
|
1418
1441
|
}
|
1419
1442
|
// We can't eliminate the subtype in the negative
|
@@ -1422,11 +1445,11 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1422
1445
|
return subtype;
|
1423
1446
|
}
|
1424
1447
|
}
|
1425
|
-
else if ((0,
|
1448
|
+
else if ((0, types_2.isNoneInstance)(subtype)) {
|
1426
1449
|
return isPositiveTest ? undefined : subtype;
|
1427
1450
|
}
|
1428
|
-
else if ((0,
|
1429
|
-
return isPositiveTest ?
|
1451
|
+
else if ((0, types_2.isAnyOrUnknown)(subtype)) {
|
1452
|
+
return isPositiveTest ? types_2.ClassType.cloneAsInstance(classType) : subtype;
|
1430
1453
|
}
|
1431
1454
|
return unexpandedSubtype;
|
1432
1455
|
});
|
@@ -1437,9 +1460,9 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1437
1460
|
function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, isPositiveTest, isIsOperator) {
|
1438
1461
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1439
1462
|
subtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1440
|
-
if ((0,
|
1463
|
+
if ((0, types_2.isClassInstance)(subtype) && types_2.ClassType.isSameGenericClass(literalType, subtype)) {
|
1441
1464
|
if (subtype.literalValue !== undefined) {
|
1442
|
-
const literalValueMatches =
|
1465
|
+
const literalValueMatches = types_2.ClassType.isLiteralValueSame(subtype, literalType);
|
1443
1466
|
if ((literalValueMatches && !isPositiveTest) || (!literalValueMatches && isPositiveTest)) {
|
1444
1467
|
return undefined;
|
1445
1468
|
}
|
@@ -1453,12 +1476,12 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1453
1476
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1454
1477
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, subtype);
|
1455
1478
|
if (allLiteralTypes && allLiteralTypes.length > 0) {
|
1456
|
-
return (0,
|
1479
|
+
return (0, types_2.combineTypes)(allLiteralTypes.filter((type) => !types_2.ClassType.isLiteralValueSame(type, literalType)));
|
1457
1480
|
}
|
1458
1481
|
}
|
1459
1482
|
}
|
1460
1483
|
else if (isPositiveTest) {
|
1461
|
-
if (isIsOperator || (0,
|
1484
|
+
if (isIsOperator || (0, types_2.isNoneInstance)(subtype)) {
|
1462
1485
|
return undefined;
|
1463
1486
|
}
|
1464
1487
|
}
|
@@ -1466,22 +1489,22 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1466
1489
|
});
|
1467
1490
|
}
|
1468
1491
|
function enumerateLiteralsForType(evaluator, type) {
|
1469
|
-
if (
|
1492
|
+
if (types_2.ClassType.isBuiltIn(type, 'bool')) {
|
1470
1493
|
// Booleans have only two types: True and False.
|
1471
1494
|
return [
|
1472
|
-
|
1473
|
-
|
1495
|
+
types_2.ClassType.cloneWithLiteral(type, /* value */ true),
|
1496
|
+
types_2.ClassType.cloneWithLiteral(type, /* value */ false),
|
1474
1497
|
];
|
1475
1498
|
}
|
1476
|
-
if (
|
1499
|
+
if (types_2.ClassType.isEnumClass(type)) {
|
1477
1500
|
// Enumerate all of the values in this enumeration.
|
1478
1501
|
const enumList = [];
|
1479
1502
|
const fields = type.details.fields;
|
1480
1503
|
fields.forEach((symbol) => {
|
1481
1504
|
if (!symbol.isIgnoredForProtocolMatch()) {
|
1482
1505
|
const symbolType = evaluator.getEffectiveTypeOfSymbol(symbol);
|
1483
|
-
if ((0,
|
1484
|
-
|
1506
|
+
if ((0, types_2.isClassInstance)(symbolType) &&
|
1507
|
+
types_2.ClassType.isSameGenericClass(type, symbolType) &&
|
1485
1508
|
symbolType.literalValue !== undefined) {
|
1486
1509
|
enumList.push(symbolType);
|
1487
1510
|
}
|
@@ -1508,7 +1531,7 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1508
1531
|
return isPositiveTest ? undefined : subtype;
|
1509
1532
|
}
|
1510
1533
|
case 7 /* Class */: {
|
1511
|
-
if (
|
1534
|
+
if (types_2.TypeBase.isInstantiable(subtype)) {
|
1512
1535
|
return isPositiveTest ? subtype : undefined;
|
1513
1536
|
}
|
1514
1537
|
// See if the object is callable.
|
@@ -1523,24 +1546,24 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1523
1546
|
// new intersection type.
|
1524
1547
|
const className = `<callable subtype of ${subtype.details.name}>`;
|
1525
1548
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
1526
|
-
let newClassType =
|
1549
|
+
let newClassType = types_2.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 0 /* None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
1527
1550
|
/* declaredMetaclass */ undefined, subtype.details.effectiveMetaclass, subtype.details.docString);
|
1528
|
-
newClassType.details.baseClasses = [
|
1551
|
+
newClassType.details.baseClasses = [types_2.ClassType.cloneAsInstantiable(subtype)];
|
1529
1552
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
1530
1553
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, subtype.condition);
|
1531
1554
|
// Add a __call__ method to the new class.
|
1532
|
-
const callMethod =
|
1555
|
+
const callMethod = types_2.FunctionType.createSynthesizedInstance('__call__');
|
1533
1556
|
const selfParam = {
|
1534
1557
|
category: 0 /* Simple */,
|
1535
1558
|
name: 'self',
|
1536
|
-
type:
|
1559
|
+
type: types_2.ClassType.cloneAsInstance(newClassType),
|
1537
1560
|
hasDeclaredType: true,
|
1538
1561
|
};
|
1539
|
-
|
1540
|
-
|
1541
|
-
callMethod.details.declaredReturnType =
|
1562
|
+
types_2.FunctionType.addParameter(callMethod, selfParam);
|
1563
|
+
types_2.FunctionType.addDefaultParameters(callMethod);
|
1564
|
+
callMethod.details.declaredReturnType = types_2.UnknownType.create();
|
1542
1565
|
newClassType.details.fields.set('__call__', symbol_1.Symbol.createWithType(4 /* ClassMember */, callMethod));
|
1543
|
-
return
|
1566
|
+
return types_2.ClassType.cloneAsInstance(newClassType);
|
1544
1567
|
}
|
1545
1568
|
return undefined;
|
1546
1569
|
}
|