@zzzen/pyright-internal 1.2.0-dev.20230409 → 1.2.0-dev.20230423
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/binder.js +1 -3
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +22 -0
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/commentUtils.d.ts +1 -1
- package/dist/analyzer/commentUtils.js +18 -3
- package/dist/analyzer/commentUtils.js.map +1 -1
- package/dist/analyzer/constructorTransform.js +112 -49
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/declarationUtils.js +2 -1
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/docStringConversion.js +2 -3
- package/dist/analyzer/docStringConversion.js.map +1 -1
- package/dist/analyzer/enums.d.ts +1 -1
- package/dist/analyzer/enums.js +114 -55
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.js +9 -4
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.d.ts +6 -2
- package/dist/analyzer/patternMatching.js +109 -2
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +9 -7
- package/dist/analyzer/program.js +106 -46
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/service.d.ts +2 -2
- package/dist/analyzer/service.js +6 -6
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.d.ts +1 -3
- package/dist/analyzer/sourceFile.js +1 -9
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceMapper.js +1 -1
- package/dist/analyzer/sourceMapper.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +147 -38
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +4 -3
- package/dist/analyzer/typeEvaluatorTypes.js +0 -2
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.d.ts +2 -0
- package/dist/analyzer/typeGuards.js +172 -175
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +1 -1
- package/dist/analyzer/typePrinter.js +21 -18
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.js +13 -13
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/types.d.ts +2 -1
- package/dist/analyzer/types.js +20 -4
- package/dist/analyzer/types.js.map +1 -1
- package/dist/common/configOptions.d.ts +1 -1
- package/dist/common/configOptions.js +3 -3
- package/dist/common/configOptions.js.map +1 -1
- package/dist/common/extensibility.d.ts +7 -4
- package/dist/common/extensibility.js.map +1 -1
- package/dist/common/fullAccessHost.d.ts +2 -1
- package/dist/common/fullAccessHost.js +11 -1
- package/dist/common/fullAccessHost.js.map +1 -1
- package/dist/common/host.d.ts +3 -2
- package/dist/common/host.js +1 -1
- package/dist/common/host.js.map +1 -1
- package/dist/languageServerBase.js +3 -2
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +1 -1
- package/dist/languageService/autoImporter.js +2 -2
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +17 -4
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.js +259 -93
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/completionProviderUtils.js +11 -3
- package/dist/languageService/completionProviderUtils.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.js +1 -1
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/hoverProvider.d.ts +28 -28
- package/dist/languageService/hoverProvider.js +151 -118
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/importAdder.d.ts +13 -2
- package/dist/languageService/importAdder.js +73 -26
- package/dist/languageService/importAdder.js.map +1 -1
- package/dist/languageService/indentationUtils.js +9 -4
- package/dist/languageService/indentationUtils.js.map +1 -1
- package/dist/languageService/insertionPointUtils.js +2 -2
- package/dist/languageService/insertionPointUtils.js.map +1 -1
- package/dist/localization/localize.d.ts +2 -0
- package/dist/localization/localize.js +2 -0
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +2 -0
- package/dist/tests/checker.test.js +1 -1
- package/dist/tests/completions.test.js +412 -0
- package/dist/tests/completions.test.js.map +1 -1
- package/dist/tests/docStringConversion.test.js +11 -1
- package/dist/tests/docStringConversion.test.js.map +1 -1
- package/dist/tests/fourslash/completions.inherited.function.docFromStub.fourslash.js +1 -1
- package/dist/tests/fourslash/completions.inherited.function.docFromStub.fourslash.js.map +1 -1
- package/dist/tests/fourslash/completions.override2.fourslash.js +16 -1
- package/dist/tests/fourslash/completions.override2.fourslash.js.map +1 -1
- package/dist/tests/fourslash/completions.variableDocStrings.fourslash.js +1 -1
- package/dist/tests/fourslash/completions.variableDocStrings.fourslash.js.map +1 -1
- package/dist/tests/fourslash/fourslash.d.ts +11 -1
- package/dist/tests/fourslash/hover.docstring.overloads.fourslash.js +2 -2
- package/dist/tests/fourslash/hover.docstring.overloads.fourslash.js.map +1 -1
- package/dist/tests/fourslash/hover.variable.docString.fourslash.js +1 -1
- package/dist/tests/fourslash/hover.variable.docString.fourslash.js.map +1 -1
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.js +5 -6
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.js.map +1 -1
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.js +1 -4
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.js.map +1 -1
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.js +10 -9
- package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.js.map +1 -1
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction1.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction1.fourslash.js +54 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction1.fourslash.js.map +1 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction2.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction2.fourslash.js +46 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction2.fourslash.js.map +1 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.function.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.function.fourslash.js +39 -0
- package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.function.fourslash.js.map +1 -0
- package/dist/tests/harness/fourslash/fourSlashParser.js +1 -1
- package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.d.ts +6 -1
- package/dist/tests/harness/fourslash/testState.js +38 -6
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/harness/testHost.js +1 -1
- package/dist/tests/harness/testHost.js.map +1 -1
- package/dist/tests/harness/vfs/factory.js +2 -2
- package/dist/tests/harness/vfs/factory.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.js +26 -25
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/hoverProvider.test.js +6 -0
- package/dist/tests/hoverProvider.test.js.map +1 -1
- package/dist/tests/importAdder.test.js +69 -1
- package/dist/tests/importAdder.test.js.map +1 -1
- package/dist/tests/indentationUtils.ptvs.test.js +2 -2
- package/dist/tests/indentationUtils.ptvs.test.js.map +1 -1
- package/dist/tests/indentationUtils.test.js +18 -0
- package/dist/tests/indentationUtils.test.js.map +1 -1
- package/dist/tests/moveSymbol.insertion.test.js +25 -4
- package/dist/tests/moveSymbol.insertion.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +8 -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 +10 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +5 -1
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typePrinter.test.js +5 -0
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/dist/workspaceFactory.js +1 -1
- package/dist/workspaceFactory.js.map +1 -1
- package/package.json +1 -1
@@ -29,7 +29,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
29
29
|
return result;
|
30
30
|
};
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
32
|
-
exports.enumerateLiteralsForType = exports.narrowTypeForContainerElementType = exports.getElementTypeForContainerNarrowing = exports.isIsinstanceFilterSubclass = exports.isIsinstanceFilterSuperclass = exports.getTypeNarrowingCallback = void 0;
|
32
|
+
exports.enumerateLiteralsForType = exports.narrowTypeForDiscriminatedTupleComparison = exports.narrowTypeForDiscriminatedDictEntryComparison = exports.narrowTypeForContainerElementType = exports.getElementTypeForContainerNarrowing = exports.isIsinstanceFilterSubclass = exports.isIsinstanceFilterSuperclass = exports.getTypeNarrowingCallback = void 0;
|
33
33
|
const parseNodes_1 = require("../parser/parseNodes");
|
34
34
|
const analyzerNodeInfo_1 = require("./analyzerNodeInfo");
|
35
35
|
const constraintSolver_1 = require("./constraintSolver");
|
@@ -38,7 +38,6 @@ 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");
|
42
41
|
const typeUtils_1 = require("./typeUtils");
|
43
42
|
const typeVarContext_1 = require("./typeVarContext");
|
44
43
|
// Given a reference expression and a test expression, returns a callback that
|
@@ -46,7 +45,7 @@ const typeVarContext_1 = require("./typeVarContext");
|
|
46
45
|
// If the specified flow node is not associated with the test expression,
|
47
46
|
// it returns undefined.
|
48
47
|
function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositiveTest, recursionCount = 0) {
|
49
|
-
if (recursionCount >
|
48
|
+
if (recursionCount > types_1.maxTypeRecursionCount) {
|
50
49
|
return undefined;
|
51
50
|
}
|
52
51
|
recursionCount++;
|
@@ -120,10 +119,10 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
120
119
|
const arg0Expr = testExpression.leftExpression.arguments[0].valueExpression;
|
121
120
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
122
121
|
const callType = evaluator.getTypeOfExpression(testExpression.leftExpression.leftExpression, 2 /* DoNotSpecialize */).type;
|
123
|
-
if ((0,
|
122
|
+
if ((0, types_1.isInstantiableClass)(callType) && types_1.ClassType.isBuiltIn(callType, 'type')) {
|
124
123
|
const classTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
125
124
|
const classType = evaluator.makeTopLevelTypeVarsConcrete(classTypeResult.type);
|
126
|
-
if ((0,
|
125
|
+
if ((0, types_1.isInstantiableClass)(classType)) {
|
127
126
|
return (type) => {
|
128
127
|
return {
|
129
128
|
type: narrowTypeForTypeIs(evaluator, type, classType, adjIsPositiveTest),
|
@@ -140,8 +139,8 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
140
139
|
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression)) {
|
141
140
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
142
141
|
const rightType = rightTypeResult.type;
|
143
|
-
if ((0,
|
144
|
-
(
|
142
|
+
if ((0, types_1.isClassInstance)(rightType) &&
|
143
|
+
(types_1.ClassType.isEnumClass(rightType) || types_1.ClassType.isBuiltIn(rightType, 'bool')) &&
|
145
144
|
rightType.literalValue !== undefined) {
|
146
145
|
return (type) => {
|
147
146
|
return {
|
@@ -160,10 +159,10 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
160
159
|
ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression.baseExpression)) {
|
161
160
|
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression.items[0].valueExpression);
|
162
161
|
const indexType = indexTypeResult.type;
|
163
|
-
if ((0,
|
164
|
-
if (
|
162
|
+
if ((0, types_1.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
163
|
+
if (types_1.ClassType.isBuiltIn(indexType, 'str')) {
|
165
164
|
const rightType = evaluator.getTypeOfExpression(testExpression.rightExpression).type;
|
166
|
-
if ((0,
|
165
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
167
166
|
return (type) => {
|
168
167
|
return {
|
169
168
|
type: narrowTypeForDiscriminatedDictEntryComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
@@ -172,13 +171,13 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
172
171
|
};
|
173
172
|
}
|
174
173
|
}
|
175
|
-
else if (
|
174
|
+
else if (types_1.ClassType.isBuiltIn(indexType, 'int')) {
|
176
175
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
177
176
|
const rightType = rightTypeResult.type;
|
178
|
-
if ((0,
|
177
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
179
178
|
let canNarrow = false;
|
180
179
|
// Narrowing can be applied only for bool or enum literals.
|
181
|
-
if (
|
180
|
+
if (types_1.ClassType.isBuiltIn(rightType, 'bool')) {
|
182
181
|
canNarrow = true;
|
183
182
|
}
|
184
183
|
else if (rightType.literalValue instanceof types_1.EnumLiteral) {
|
@@ -203,7 +202,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
203
202
|
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression)) {
|
204
203
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
205
204
|
const rightType = rightTypeResult.type;
|
206
|
-
if ((0,
|
205
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
207
206
|
return (type) => {
|
208
207
|
return {
|
209
208
|
type: narrowTypeForLiteralComparison(evaluator, type, rightType, adjIsPositiveTest,
|
@@ -221,26 +220,22 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
221
220
|
ParseTreeUtils.isMatchingExpression(reference, testExpression.leftExpression.baseExpression)) {
|
222
221
|
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression.items[0].valueExpression);
|
223
222
|
const indexType = indexTypeResult.type;
|
224
|
-
if ((0,
|
225
|
-
if (
|
226
|
-
const rightType = evaluator.getTypeOfExpression(testExpression.rightExpression).type;
|
227
|
-
if ((0, types_2.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
228
|
-
return (type) => {
|
229
|
-
return {
|
230
|
-
type: narrowTypeForDiscriminatedDictEntryComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
231
|
-
isIncomplete: !!indexTypeResult.isIncomplete,
|
232
|
-
};
|
233
|
-
};
|
234
|
-
}
|
235
|
-
}
|
236
|
-
else if (types_2.ClassType.isBuiltIn(indexType, 'int')) {
|
223
|
+
if ((0, types_1.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
224
|
+
if (types_1.ClassType.isBuiltIn(indexType, ['str', 'int'])) {
|
237
225
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
238
226
|
const rightType = rightTypeResult.type;
|
239
|
-
if ((0,
|
227
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
240
228
|
return (type) => {
|
229
|
+
let narrowedType;
|
230
|
+
if (types_1.ClassType.isBuiltIn(indexType, 'str')) {
|
231
|
+
narrowedType = narrowTypeForDiscriminatedDictEntryComparison(evaluator, type, indexType, rightType, adjIsPositiveTest);
|
232
|
+
}
|
233
|
+
else {
|
234
|
+
narrowedType = narrowTypeForDiscriminatedTupleComparison(evaluator, type, indexType, rightType, adjIsPositiveTest);
|
235
|
+
}
|
241
236
|
return {
|
242
|
-
type:
|
243
|
-
isIncomplete: !!rightTypeResult.isIncomplete,
|
237
|
+
type: narrowedType,
|
238
|
+
isIncomplete: !!indexTypeResult.isIncomplete || !!rightTypeResult.isIncomplete,
|
244
239
|
};
|
245
240
|
};
|
246
241
|
}
|
@@ -258,7 +253,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
258
253
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
259
254
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression.leftExpression, 2 /* DoNotSpecialize */);
|
260
255
|
const callType = callTypeResult.type;
|
261
|
-
if ((0,
|
256
|
+
if ((0, types_1.isFunction)(callType) && callType.details.fullName === 'builtins.len') {
|
262
257
|
const tupleLength = testExpression.rightExpression.value;
|
263
258
|
if (typeof tupleLength === 'number') {
|
264
259
|
return (type) => {
|
@@ -278,7 +273,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
278
273
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
279
274
|
const rightType = rightTypeResult.type;
|
280
275
|
const memberName = testExpression.leftExpression.memberName;
|
281
|
-
if ((0,
|
276
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
282
277
|
return (type) => {
|
283
278
|
return {
|
284
279
|
type: narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, type, memberName.value, rightType, adjIsPositiveTest),
|
@@ -294,8 +289,8 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
294
289
|
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.rightExpression);
|
295
290
|
const rightType = rightTypeResult.type;
|
296
291
|
const memberName = testExpression.leftExpression.memberName;
|
297
|
-
if ((0,
|
298
|
-
(
|
292
|
+
if ((0, types_1.isClassInstance)(rightType) &&
|
293
|
+
(types_1.ClassType.isEnumClass(rightType) || types_1.ClassType.isBuiltIn(rightType, 'bool')) &&
|
299
294
|
rightType.literalValue !== undefined) {
|
300
295
|
return (type) => {
|
301
296
|
return {
|
@@ -338,11 +333,11 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
338
333
|
// one or more TypedDicts.
|
339
334
|
const leftTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression);
|
340
335
|
const leftType = leftTypeResult.type;
|
341
|
-
if ((0,
|
336
|
+
if ((0, types_1.isClassInstance)(leftType) && types_1.ClassType.isBuiltIn(leftType, 'str') && (0, typeUtils_1.isLiteralType)(leftType)) {
|
342
337
|
const adjIsPositiveTest = testExpression.operator === 41 /* In */ ? isPositiveTest : !isPositiveTest;
|
343
338
|
return (type) => {
|
344
339
|
return {
|
345
|
-
type: narrowTypeForTypedDictKey(evaluator, type,
|
340
|
+
type: narrowTypeForTypedDictKey(evaluator, type, types_1.ClassType.cloneAsInstantiable(leftType), adjIsPositiveTest),
|
346
341
|
isIncomplete: !!leftTypeResult.isIncomplete,
|
347
342
|
};
|
348
343
|
};
|
@@ -361,7 +356,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
361
356
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
362
357
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
363
358
|
const callType = callTypeResult.type;
|
364
|
-
if ((0,
|
359
|
+
if ((0, types_1.isFunction)(callType) &&
|
365
360
|
(callType.details.builtInName === 'isinstance' || callType.details.builtInName === 'issubclass')) {
|
366
361
|
const isInstanceCheck = callType.details.builtInName === 'isinstance';
|
367
362
|
const arg1TypeResult = evaluator.getTypeOfExpression(arg1Expr, 8 /* EvaluateStringLiteralAsType */ |
|
@@ -374,7 +369,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
374
369
|
return (type) => {
|
375
370
|
const narrowedType = narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck, isPositiveTest,
|
376
371
|
/* allowIntersections */ false, testExpression);
|
377
|
-
if (!(0,
|
372
|
+
if (!(0, types_1.isNever)(narrowedType)) {
|
378
373
|
return {
|
379
374
|
type: narrowedType,
|
380
375
|
isIncomplete,
|
@@ -407,11 +402,11 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
407
402
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
408
403
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
409
404
|
const callType = callTypeResult.type;
|
410
|
-
if ((0,
|
405
|
+
if ((0, types_1.isFunction)(callType) && callType.details.builtInName === 'callable') {
|
411
406
|
return (type) => {
|
412
407
|
let narrowedType = narrowTypeForCallable(evaluator, type, isPositiveTest, testExpression,
|
413
408
|
/* allowIntersections */ false);
|
414
|
-
if (isPositiveTest && (0,
|
409
|
+
if (isPositiveTest && (0, types_1.isNever)(narrowedType)) {
|
415
410
|
// Try again with intersections allowed.
|
416
411
|
narrowedType = narrowTypeForCallable(evaluator, type, isPositiveTest, testExpression,
|
417
412
|
/* allowIntersections */ true);
|
@@ -426,7 +421,7 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
426
421
|
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.arguments[0].valueExpression)) {
|
427
422
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
428
423
|
const callType = callTypeResult.type;
|
429
|
-
if ((0,
|
424
|
+
if ((0, types_1.isInstantiableClass)(callType) && types_1.ClassType.isBuiltIn(callType, 'bool')) {
|
430
425
|
return (type) => {
|
431
426
|
return {
|
432
427
|
type: narrowTypeForTruthiness(evaluator, type, isPositiveTest),
|
@@ -444,27 +439,27 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
444
439
|
let isPossiblyTypeGuard = false;
|
445
440
|
const isFunctionReturnTypeGuard = (type) => {
|
446
441
|
return (type.details.declaredReturnType &&
|
447
|
-
(0,
|
448
|
-
|
442
|
+
(0, types_1.isClassInstance)(type.details.declaredReturnType) &&
|
443
|
+
types_1.ClassType.isBuiltIn(type.details.declaredReturnType, ['TypeGuard', 'StrictTypeGuard']));
|
449
444
|
};
|
450
445
|
const callTypeResult = evaluator.getTypeOfExpression(testExpression.leftExpression, 2 /* DoNotSpecialize */);
|
451
446
|
const callType = callTypeResult.type;
|
452
|
-
if ((0,
|
447
|
+
if ((0, types_1.isFunction)(callType) && isFunctionReturnTypeGuard(callType)) {
|
453
448
|
isPossiblyTypeGuard = true;
|
454
449
|
}
|
455
|
-
else if ((0,
|
456
|
-
|
450
|
+
else if ((0, types_1.isOverloadedFunction)(callType) &&
|
451
|
+
types_1.OverloadedFunctionType.getOverloads(callType).some((o) => isFunctionReturnTypeGuard(o))) {
|
457
452
|
isPossiblyTypeGuard = true;
|
458
453
|
}
|
459
|
-
else if ((0,
|
454
|
+
else if ((0, types_1.isClassInstance)(callType)) {
|
460
455
|
isPossiblyTypeGuard = true;
|
461
456
|
}
|
462
457
|
if (isPossiblyTypeGuard) {
|
463
458
|
// Evaluate the type guard call expression.
|
464
459
|
const functionReturnTypeResult = evaluator.getTypeOfExpression(testExpression);
|
465
460
|
const functionReturnType = functionReturnTypeResult.type;
|
466
|
-
if ((0,
|
467
|
-
|
461
|
+
if ((0, types_1.isClassInstance)(functionReturnType) &&
|
462
|
+
types_1.ClassType.isBuiltIn(functionReturnType, 'bool') &&
|
468
463
|
functionReturnType.typeGuardType) {
|
469
464
|
const isStrictTypeGuard = !!functionReturnType.isStrictTypeGuard;
|
470
465
|
const typeGuardType = functionReturnType.typeGuardType;
|
@@ -648,12 +643,12 @@ function narrowTupleTypeForIsNone(evaluator, type, isPositiveTest, indexValue) {
|
|
648
643
|
}
|
649
644
|
const typeOfEntry = evaluator.makeTopLevelTypeVarsConcrete(tupleType.tupleTypeArguments[indexValue].type);
|
650
645
|
if (isPositiveTest) {
|
651
|
-
if (!evaluator.assignType(typeOfEntry,
|
646
|
+
if (!evaluator.assignType(typeOfEntry, types_1.NoneType.createInstance())) {
|
652
647
|
return undefined;
|
653
648
|
}
|
654
649
|
}
|
655
650
|
else {
|
656
|
-
if ((0,
|
651
|
+
if ((0, types_1.isNoneInstance)(typeOfEntry)) {
|
657
652
|
return undefined;
|
658
653
|
}
|
659
654
|
}
|
@@ -667,7 +662,7 @@ function narrowTypeForIsNone(evaluator, type, isPositiveTest) {
|
|
667
662
|
});
|
668
663
|
return evaluator.mapSubtypesExpandTypeVars(expandedType,
|
669
664
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
670
|
-
if ((0,
|
665
|
+
if ((0, types_1.isAnyOrUnknown)(subtype)) {
|
671
666
|
// We need to assume that "Any" is always both None and not None,
|
672
667
|
// so it matches regardless of whether the test is positive or negative.
|
673
668
|
return subtype;
|
@@ -675,17 +670,17 @@ function narrowTypeForIsNone(evaluator, type, isPositiveTest) {
|
|
675
670
|
// If this is a TypeVar that isn't constrained, use the unexpanded
|
676
671
|
// TypeVar. For all other cases (including constrained TypeVars),
|
677
672
|
// use the expanded subtype.
|
678
|
-
const adjustedSubtype = (0,
|
673
|
+
const adjustedSubtype = (0, types_1.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.details.constraints.length === 0
|
679
674
|
? unexpandedSubtype
|
680
675
|
: subtype;
|
681
676
|
// See if it's a match for object.
|
682
|
-
if ((0,
|
677
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'object')) {
|
683
678
|
return isPositiveTest
|
684
|
-
? (0, typeUtils_1.addConditionToType)(
|
679
|
+
? (0, typeUtils_1.addConditionToType)(types_1.NoneType.createInstance(), subtype.condition)
|
685
680
|
: adjustedSubtype;
|
686
681
|
}
|
687
682
|
// See if it's a match for None.
|
688
|
-
if ((0,
|
683
|
+
if ((0, types_1.isNoneInstance)(subtype) === isPositiveTest) {
|
689
684
|
return subtype;
|
690
685
|
}
|
691
686
|
return undefined;
|
@@ -698,7 +693,7 @@ function narrowTypeForIsEllipsis(evaluator, type, isPositiveTest) {
|
|
698
693
|
});
|
699
694
|
return evaluator.mapSubtypesExpandTypeVars(expandedType,
|
700
695
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
701
|
-
if ((0,
|
696
|
+
if ((0, types_1.isAnyOrUnknown)(subtype)) {
|
702
697
|
// We need to assume that "Any" is always both None and not None,
|
703
698
|
// so it matches regardless of whether the test is positive or negative.
|
704
699
|
return subtype;
|
@@ -706,16 +701,16 @@ function narrowTypeForIsEllipsis(evaluator, type, isPositiveTest) {
|
|
706
701
|
// If this is a TypeVar that isn't constrained, use the unexpanded
|
707
702
|
// TypeVar. For all other cases (including constrained TypeVars),
|
708
703
|
// use the expanded subtype.
|
709
|
-
const adjustedSubtype = (0,
|
704
|
+
const adjustedSubtype = (0, types_1.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.details.constraints.length === 0
|
710
705
|
? unexpandedSubtype
|
711
706
|
: subtype;
|
712
707
|
// See if it's a match for object.
|
713
|
-
if ((0,
|
708
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'object')) {
|
714
709
|
return isPositiveTest
|
715
|
-
? (0, typeUtils_1.addConditionToType)(
|
710
|
+
? (0, typeUtils_1.addConditionToType)(types_1.NoneType.createInstance(), subtype.condition)
|
716
711
|
: adjustedSubtype;
|
717
712
|
}
|
718
|
-
const isEllipsis = (0,
|
713
|
+
const isEllipsis = (0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'ellipsis');
|
719
714
|
// See if it's a match for "...".
|
720
715
|
if (isEllipsis === isPositiveTest) {
|
721
716
|
return subtype;
|
@@ -734,13 +729,13 @@ function getIsInstanceClassTypes(argType) {
|
|
734
729
|
// undefined if any of the types are not valid.
|
735
730
|
const addClassTypesToList = (types) => {
|
736
731
|
types.forEach((subtype) => {
|
737
|
-
if ((0,
|
732
|
+
if ((0, types_1.isInstantiableClass)(subtype) || ((0, types_1.isTypeVar)(subtype) && types_1.TypeBase.isInstantiable(subtype))) {
|
738
733
|
classTypeList.push(subtype);
|
739
734
|
}
|
740
|
-
else if ((0,
|
735
|
+
else if ((0, types_1.isNoneTypeClass)(subtype)) {
|
741
736
|
classTypeList.push(subtype);
|
742
737
|
}
|
743
|
-
else if ((0,
|
738
|
+
else if ((0, types_1.isFunction)(subtype) &&
|
744
739
|
subtype.details.parameters.length === 2 &&
|
745
740
|
subtype.details.parameters[0].category === 1 /* VarArgList */ &&
|
746
741
|
subtype.details.parameters[1].category === 2 /* VarArgDictionary */) {
|
@@ -752,10 +747,10 @@ function getIsInstanceClassTypes(argType) {
|
|
752
747
|
});
|
753
748
|
};
|
754
749
|
const addClassTypesRecursive = (subtype, recursionCount = 0) => {
|
755
|
-
if (recursionCount >
|
750
|
+
if (recursionCount > types_1.maxTypeRecursionCount) {
|
756
751
|
return;
|
757
752
|
}
|
758
|
-
if ((0,
|
753
|
+
if ((0, types_1.isClass)(subtype) && types_1.TypeBase.isInstance(subtype) && (0, typeUtils_1.isTupleClass)(subtype)) {
|
759
754
|
if (subtype.tupleTypeArguments) {
|
760
755
|
subtype.tupleTypeArguments.forEach((tupleEntry) => {
|
761
756
|
addClassTypesRecursive(tupleEntry.type, recursionCount + 1);
|
@@ -778,32 +773,32 @@ function isIsinstanceFilterSuperclass(evaluator, varType, filterType, concreteFi
|
|
778
773
|
if (concreteFilterType.includeSubclasses) {
|
779
774
|
return false;
|
780
775
|
}
|
781
|
-
if ((0,
|
776
|
+
if ((0, types_1.isTypeVar)(filterType)) {
|
782
777
|
return false;
|
783
778
|
}
|
784
|
-
if (
|
779
|
+
if (types_1.ClassType.isDerivedFrom(varType, concreteFilterType)) {
|
785
780
|
return true;
|
786
781
|
}
|
787
782
|
if (isInstanceCheck) {
|
788
|
-
if (
|
783
|
+
if (types_1.ClassType.isProtocolClass(concreteFilterType) && evaluator.assignType(concreteFilterType, varType)) {
|
789
784
|
return true;
|
790
785
|
}
|
791
786
|
}
|
792
787
|
// Handle the special case where the variable type is a TypedDict and
|
793
788
|
// we're filtering against 'dict'. TypedDict isn't derived from dict,
|
794
789
|
// but at runtime, isinstance returns True.
|
795
|
-
if (
|
790
|
+
if (types_1.ClassType.isBuiltIn(concreteFilterType, 'dict') && types_1.ClassType.isTypedDictClass(varType)) {
|
796
791
|
return true;
|
797
792
|
}
|
798
793
|
return false;
|
799
794
|
}
|
800
795
|
exports.isIsinstanceFilterSuperclass = isIsinstanceFilterSuperclass;
|
801
796
|
function isIsinstanceFilterSubclass(evaluator, varType, filterType, concreteFilterType, isInstanceCheck) {
|
802
|
-
if (
|
797
|
+
if (types_1.ClassType.isDerivedFrom(concreteFilterType, varType)) {
|
803
798
|
return true;
|
804
799
|
}
|
805
800
|
if (isInstanceCheck) {
|
806
|
-
if (
|
801
|
+
if (types_1.ClassType.isProtocolClass(varType) && evaluator.assignType(varType, concreteFilterType)) {
|
807
802
|
return true;
|
808
803
|
}
|
809
804
|
}
|
@@ -828,7 +823,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
828
823
|
let isClassRelationshipIndeterminate = false;
|
829
824
|
for (const filterType of classTypeList) {
|
830
825
|
const concreteFilterType = evaluator.makeTopLevelTypeVarsConcrete(filterType);
|
831
|
-
if ((0,
|
826
|
+
if ((0, types_1.isInstantiableClass)(concreteFilterType)) {
|
832
827
|
const filterIsSuperclass = isIsinstanceFilterSuperclass(evaluator, varType, filterType, concreteFilterType, isInstanceCheck);
|
833
828
|
const filterIsSubclass = isIsinstanceFilterSubclass(evaluator, varType, filterType, concreteFilterType, isInstanceCheck);
|
834
829
|
if (filterIsSuperclass) {
|
@@ -841,7 +836,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
841
836
|
// will generate false positives.
|
842
837
|
if (filterIsSubclass &&
|
843
838
|
filterIsSuperclass &&
|
844
|
-
!
|
839
|
+
!types_1.ClassType.isSameGenericClass(varType, concreteFilterType)) {
|
845
840
|
isClassRelationshipIndeterminate = true;
|
846
841
|
}
|
847
842
|
if (isPositiveTest) {
|
@@ -857,11 +852,11 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
857
852
|
// Try to retain the type arguments for the filter type. This is
|
858
853
|
// important because a specialized version of the filter cannot
|
859
854
|
// be passed to isinstance or issubclass.
|
860
|
-
if ((0,
|
861
|
-
if (
|
855
|
+
if ((0, types_1.isClass)(filterType)) {
|
856
|
+
if (types_1.ClassType.isSpecialBuiltIn(filterType) ||
|
862
857
|
filterType.details.typeParameters.length > 0) {
|
863
858
|
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(filterType));
|
864
|
-
const unspecializedFilterType =
|
859
|
+
const unspecializedFilterType = types_1.ClassType.cloneForSpecialization(filterType,
|
865
860
|
/* typeArguments */ undefined,
|
866
861
|
/* isTypeArgumentExplicit */ false);
|
867
862
|
if ((0, constraintSolver_1.populateTypeVarContextBasedOnExpectedType)(evaluator, unspecializedFilterType, varType, typeVarContext,
|
@@ -880,38 +875,38 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
880
875
|
// the two types.
|
881
876
|
const className = `<subclass of ${varType.details.name} and ${concreteFilterType.details.name}>`;
|
882
877
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
883
|
-
let newClassType =
|
878
|
+
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 0 /* None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
884
879
|
/* declaredMetaclass */ undefined, varType.details.effectiveMetaclass, varType.details.docString);
|
885
|
-
newClassType.details.baseClasses = [
|
880
|
+
newClassType.details.baseClasses = [types_1.ClassType.cloneAsInstantiable(varType), concreteFilterType];
|
886
881
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
887
882
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, concreteFilterType.condition);
|
888
|
-
if ((0,
|
883
|
+
if ((0, types_1.isTypeVar)(unexpandedType) &&
|
889
884
|
!unexpandedType.details.isParamSpec &&
|
890
885
|
unexpandedType.details.constraints.length === 0) {
|
891
886
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, [
|
892
887
|
{
|
893
|
-
typeVarName:
|
888
|
+
typeVarName: types_1.TypeVarType.getNameWithScope(unexpandedType),
|
894
889
|
constraintIndex: 0,
|
895
890
|
isConstrainedTypeVar: false,
|
896
891
|
},
|
897
892
|
]);
|
898
893
|
}
|
899
|
-
let newClassInstanceType =
|
894
|
+
let newClassInstanceType = types_1.ClassType.cloneAsInstance(newClassType);
|
900
895
|
if (varType.condition) {
|
901
896
|
newClassInstanceType = (0, typeUtils_1.addConditionToType)(newClassInstanceType, varType.condition);
|
902
897
|
}
|
903
898
|
// If this is a issubclass check, we do a double conversion from instantiable
|
904
899
|
// to instance back to instantiable to make sure that the includeSubclasses flag
|
905
900
|
// gets cleared.
|
906
|
-
filteredTypes.push(isInstanceCheck ? newClassInstanceType :
|
901
|
+
filteredTypes.push(isInstanceCheck ? newClassInstanceType : types_1.ClassType.cloneAsInstantiable(newClassInstanceType));
|
907
902
|
}
|
908
903
|
}
|
909
904
|
}
|
910
|
-
else if ((0,
|
905
|
+
else if ((0, types_1.isTypeVar)(filterType) && types_1.TypeBase.isInstantiable(filterType)) {
|
911
906
|
// Handle the case where the filter type is Type[T] and the unexpanded
|
912
907
|
// subtype is some instance type, possibly T.
|
913
|
-
if (isInstanceCheck &&
|
914
|
-
if ((0,
|
908
|
+
if (isInstanceCheck && types_1.TypeBase.isInstance(unexpandedType)) {
|
909
|
+
if ((0, types_1.isTypeVar)(unexpandedType) && (0, types_1.isTypeSame)((0, typeUtils_1.convertToInstance)(filterType), unexpandedType)) {
|
915
910
|
// If the unexpanded subtype is T, we can definitively filter
|
916
911
|
// in both the positive and negative cases.
|
917
912
|
if (isPositiveTest) {
|
@@ -930,8 +925,8 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
930
925
|
}
|
931
926
|
}
|
932
927
|
}
|
933
|
-
else if (!isInstanceCheck &&
|
934
|
-
if ((0,
|
928
|
+
else if (!isInstanceCheck && types_1.TypeBase.isInstantiable(unexpandedType)) {
|
929
|
+
if ((0, types_1.isTypeVar)(unexpandedType) && (0, types_1.isTypeSame)(filterType, unexpandedType)) {
|
935
930
|
if (isPositiveTest) {
|
936
931
|
filteredTypes.push(unexpandedType);
|
937
932
|
}
|
@@ -947,12 +942,12 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
947
942
|
}
|
948
943
|
}
|
949
944
|
}
|
950
|
-
else if ((0,
|
945
|
+
else if ((0, types_1.isFunction)(filterType)) {
|
951
946
|
// Handle an isinstance check against Callable.
|
952
947
|
if (isInstanceCheck) {
|
953
948
|
let isCallable = false;
|
954
|
-
if ((0,
|
955
|
-
if (
|
949
|
+
if ((0, types_1.isClass)(varType)) {
|
950
|
+
if (types_1.TypeBase.isInstantiable(unexpandedType)) {
|
956
951
|
isCallable = true;
|
957
952
|
}
|
958
953
|
else {
|
@@ -984,7 +979,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
984
979
|
// We perform a double conversion from instance to instantiable
|
985
980
|
// here to make sure that the includeSubclasses flag is cleared
|
986
981
|
// if it's a class.
|
987
|
-
return filteredTypes.map((t) => ((0,
|
982
|
+
return filteredTypes.map((t) => ((0, types_1.isInstantiableClass)(t) ? (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.convertToInstance)(t)) : t));
|
988
983
|
}
|
989
984
|
return filteredTypes.map((t) => (0, typeUtils_1.convertToInstance)(t));
|
990
985
|
};
|
@@ -996,7 +991,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
996
991
|
if (evaluator.assignType(varType, (0, typeUtils_1.convertToInstance)(concreteFilterType))) {
|
997
992
|
// If the filter type is a Callable, use the original type. If the
|
998
993
|
// filter type is a callback protocol, use the filter type.
|
999
|
-
if ((0,
|
994
|
+
if ((0, types_1.isFunction)(filterType)) {
|
1000
995
|
filteredTypes.push(unexpandedType);
|
1001
996
|
}
|
1002
997
|
else {
|
@@ -1009,7 +1004,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1009
1004
|
// If the filter type is a runtime checkable protocol class, it can
|
1010
1005
|
// be used in an instance check.
|
1011
1006
|
const concreteFilterType = evaluator.makeTopLevelTypeVarsConcrete(filterType);
|
1012
|
-
if ((0,
|
1007
|
+
if ((0, types_1.isClass)(concreteFilterType) && !types_1.ClassType.isProtocolClass(concreteFilterType)) {
|
1013
1008
|
return false;
|
1014
1009
|
}
|
1015
1010
|
return evaluator.assignType(varType, (0, typeUtils_1.convertToInstance)(concreteFilterType));
|
@@ -1028,30 +1023,30 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1028
1023
|
// on a constrained TypeVar that they want to filter based on its constrained
|
1029
1024
|
// parts.
|
1030
1025
|
const negativeFallback = (0, typeUtils_1.getTypeCondition)(subtype) ? subtype : unexpandedSubtype;
|
1031
|
-
const isSubtypeTypeObject = (0,
|
1032
|
-
if (isPositiveTest && (0,
|
1026
|
+
const isSubtypeTypeObject = (0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'type');
|
1027
|
+
if (isPositiveTest && (0, types_1.isAnyOrUnknown)(subtype)) {
|
1033
1028
|
// If this is a positive test and the effective type is Any or
|
1034
1029
|
// Unknown, we can assume that the type matches one of the
|
1035
1030
|
// specified types.
|
1036
1031
|
if (isInstanceCheck) {
|
1037
|
-
anyOrUnknownSubstitutions.push((0,
|
1032
|
+
anyOrUnknownSubstitutions.push((0, types_1.combineTypes)(classTypeList.map((classType) => (0, typeUtils_1.convertToInstance)(classType))));
|
1038
1033
|
}
|
1039
1034
|
else {
|
1040
1035
|
// We perform a double conversion from instance to instantiable
|
1041
1036
|
// here to make sure that the includeSubclasses flag is cleared
|
1042
1037
|
// if it's a class.
|
1043
|
-
anyOrUnknownSubstitutions.push((0,
|
1038
|
+
anyOrUnknownSubstitutions.push((0, types_1.combineTypes)(classTypeList.map((classType) => (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.convertToInstance)(classType)))));
|
1044
1039
|
}
|
1045
1040
|
anyOrUnknown.push(subtype);
|
1046
1041
|
return undefined;
|
1047
1042
|
}
|
1048
1043
|
if (isInstanceCheck) {
|
1049
|
-
if ((0,
|
1044
|
+
if ((0, types_1.isNoneInstance)(subtype)) {
|
1050
1045
|
const containsNoneType = classTypeList.some((t) => {
|
1051
|
-
if ((0,
|
1046
|
+
if ((0, types_1.isNoneTypeClass)(t)) {
|
1052
1047
|
return true;
|
1053
1048
|
}
|
1054
|
-
return (0,
|
1049
|
+
return (0, types_1.isInstantiableClass)(t) && types_1.ClassType.isBuiltIn(t, 'NoneType');
|
1055
1050
|
});
|
1056
1051
|
if (isPositiveTest) {
|
1057
1052
|
return containsNoneType ? subtype : undefined;
|
@@ -1060,28 +1055,28 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1060
1055
|
return containsNoneType ? undefined : subtype;
|
1061
1056
|
}
|
1062
1057
|
}
|
1063
|
-
if ((0,
|
1058
|
+
if ((0, types_1.isModule)(subtype) || ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'ModuleType'))) {
|
1064
1059
|
// Handle type narrowing for runtime-checkable protocols
|
1065
1060
|
// when applied to modules.
|
1066
1061
|
if (isPositiveTest) {
|
1067
1062
|
const filteredTypes = classTypeList.filter((classType) => {
|
1068
1063
|
const concreteClassType = evaluator.makeTopLevelTypeVarsConcrete(classType);
|
1069
|
-
return ((0,
|
1064
|
+
return ((0, types_1.isInstantiableClass)(concreteClassType) && types_1.ClassType.isProtocolClass(concreteClassType));
|
1070
1065
|
});
|
1071
1066
|
if (filteredTypes.length > 0) {
|
1072
|
-
return (0, typeUtils_1.convertToInstance)((0,
|
1067
|
+
return (0, typeUtils_1.convertToInstance)((0, types_1.combineTypes)(filteredTypes));
|
1073
1068
|
}
|
1074
1069
|
}
|
1075
1070
|
}
|
1076
|
-
if ((0,
|
1077
|
-
return (0,
|
1071
|
+
if ((0, types_1.isClassInstance)(subtype) && !isSubtypeTypeObject) {
|
1072
|
+
return (0, types_1.combineTypes)(filterClassType(types_1.ClassType.cloneAsInstantiable(subtype), (0, typeUtils_1.convertToInstance)(unexpandedSubtype), (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1078
1073
|
}
|
1079
|
-
if (((0,
|
1080
|
-
return (0,
|
1074
|
+
if (((0, types_1.isFunction)(subtype) || (0, types_1.isOverloadedFunction)(subtype)) && isInstanceCheck) {
|
1075
|
+
return (0, types_1.combineTypes)(filterFunctionType(subtype, (0, typeUtils_1.convertToInstance)(unexpandedSubtype)));
|
1081
1076
|
}
|
1082
|
-
if ((0,
|
1077
|
+
if ((0, types_1.isInstantiableClass)(subtype) || isSubtypeTypeObject) {
|
1083
1078
|
// Handle the special case of isinstance(x, type).
|
1084
|
-
const includesTypeType = classTypeList.some((classType) => (0,
|
1079
|
+
const includesTypeType = classTypeList.some((classType) => (0, types_1.isInstantiableClass)(classType) && types_1.ClassType.isBuiltIn(classType, 'type'));
|
1085
1080
|
if (isPositiveTest) {
|
1086
1081
|
return includesTypeType ? negativeFallback : undefined;
|
1087
1082
|
}
|
@@ -1091,13 +1086,13 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1091
1086
|
}
|
1092
1087
|
}
|
1093
1088
|
else {
|
1094
|
-
if ((0,
|
1095
|
-
return (0,
|
1089
|
+
if ((0, types_1.isInstantiableClass)(subtype)) {
|
1090
|
+
return (0, types_1.combineTypes)(filterClassType(subtype, unexpandedSubtype, (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1096
1091
|
}
|
1097
1092
|
if (isSubtypeTypeObject) {
|
1098
1093
|
const objectType = evaluator.getBuiltInObject(errorNode, 'object');
|
1099
|
-
if (objectType && (0,
|
1100
|
-
return (0,
|
1094
|
+
if (objectType && (0, types_1.isClassInstance)(objectType)) {
|
1095
|
+
return (0, types_1.combineTypes)(filterClassType(types_1.ClassType.cloneAsInstantiable(objectType), (0, typeUtils_1.convertToInstantiable)(unexpandedSubtype), (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1101
1096
|
}
|
1102
1097
|
}
|
1103
1098
|
}
|
@@ -1109,11 +1104,11 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1109
1104
|
// Any/Unknown. For example, if the statement is "isinstance(x, list)"
|
1110
1105
|
// and the type of x is "List[str] | int | Any", the result should be
|
1111
1106
|
// "List[str]", not "List[str] | List[Unknown]".
|
1112
|
-
if ((0,
|
1113
|
-
return (0,
|
1107
|
+
if ((0, types_1.isNever)(filteredType) && anyOrUnknownSubstitutions.length > 0) {
|
1108
|
+
return (0, types_1.combineTypes)(anyOrUnknownSubstitutions);
|
1114
1109
|
}
|
1115
|
-
if ((0,
|
1116
|
-
return (0,
|
1110
|
+
if ((0, types_1.isNever)(filteredType) && anyOrUnknown.length > 0) {
|
1111
|
+
return (0, types_1.combineTypes)(anyOrUnknown);
|
1117
1112
|
}
|
1118
1113
|
return filteredType;
|
1119
1114
|
}
|
@@ -1122,7 +1117,7 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1122
1117
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1123
1118
|
const concreteSubtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1124
1119
|
// If it's not a tuple, we can't narrow it.
|
1125
|
-
if (!(0,
|
1120
|
+
if (!(0, types_1.isClassInstance)(concreteSubtype) ||
|
1126
1121
|
!(0, typeUtils_1.isTupleClass)(concreteSubtype) ||
|
1127
1122
|
(0, typeUtils_1.isUnboundedTupleClass)(concreteSubtype) ||
|
1128
1123
|
!concreteSubtype.tupleTypeArguments) {
|
@@ -1143,8 +1138,8 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1143
1138
|
}
|
1144
1139
|
// Narrowing in the negative case is possible only with tuples
|
1145
1140
|
// with a known length.
|
1146
|
-
if (!(0,
|
1147
|
-
!
|
1141
|
+
if (!(0, types_1.isClassInstance)(containerType) ||
|
1142
|
+
!types_1.ClassType.isBuiltIn(containerType, 'tuple') ||
|
1148
1143
|
!containerType.tupleTypeArguments) {
|
1149
1144
|
return referenceType;
|
1150
1145
|
}
|
@@ -1153,10 +1148,10 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1153
1148
|
const typesToEliminate = [];
|
1154
1149
|
containerType.tupleTypeArguments.forEach((tupleEntry) => {
|
1155
1150
|
if (!tupleEntry.isUnbounded) {
|
1156
|
-
if ((0,
|
1151
|
+
if ((0, types_1.isNoneInstance)(tupleEntry.type)) {
|
1157
1152
|
typesToEliminate.push(tupleEntry.type);
|
1158
1153
|
}
|
1159
|
-
else if ((0,
|
1154
|
+
else if ((0, types_1.isClassInstance)(tupleEntry.type) && (0, typeUtils_1.isLiteralType)(tupleEntry.type)) {
|
1160
1155
|
typesToEliminate.push(tupleEntry.type);
|
1161
1156
|
}
|
1162
1157
|
}
|
@@ -1166,15 +1161,15 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1166
1161
|
}
|
1167
1162
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1168
1163
|
referenceSubtype = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1169
|
-
if ((0,
|
1164
|
+
if ((0, types_1.isClassInstance)(referenceSubtype) && referenceSubtype.literalValue === undefined) {
|
1170
1165
|
// If we're able to enumerate all possible literal values
|
1171
1166
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1172
1167
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, referenceSubtype);
|
1173
1168
|
if (allLiteralTypes && allLiteralTypes.length > 0) {
|
1174
|
-
return (0,
|
1169
|
+
return (0, types_1.combineTypes)(allLiteralTypes.filter((type) => !typesToEliminate.some((t) => (0, types_1.isTypeSame)(t, type))));
|
1175
1170
|
}
|
1176
1171
|
}
|
1177
|
-
if (typesToEliminate.some((t) => (0,
|
1172
|
+
if (typesToEliminate.some((t) => (0, types_1.isTypeSame)(t, referenceSubtype))) {
|
1178
1173
|
return undefined;
|
1179
1174
|
}
|
1180
1175
|
return referenceSubtype;
|
@@ -1183,7 +1178,7 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1183
1178
|
function getElementTypeForContainerNarrowing(containerType) {
|
1184
1179
|
// We support contains narrowing only for certain built-in types that have been specialized.
|
1185
1180
|
const supportedContainers = ['list', 'set', 'frozenset', 'deque', 'tuple', 'dict', 'defaultdict', 'OrderedDict'];
|
1186
|
-
if (!(0,
|
1181
|
+
if (!(0, types_1.isClassInstance)(containerType) || !types_1.ClassType.isBuiltIn(containerType, supportedContainers)) {
|
1187
1182
|
return undefined;
|
1188
1183
|
}
|
1189
1184
|
if (!containerType.typeArguments || containerType.typeArguments.length < 1) {
|
@@ -1191,7 +1186,7 @@ function getElementTypeForContainerNarrowing(containerType) {
|
|
1191
1186
|
}
|
1192
1187
|
let elementType = containerType.typeArguments[0];
|
1193
1188
|
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.tupleTypeArguments) {
|
1194
|
-
elementType = (0,
|
1189
|
+
elementType = (0, types_1.combineTypes)(containerType.tupleTypeArguments.map((t) => t.type));
|
1195
1190
|
}
|
1196
1191
|
return elementType;
|
1197
1192
|
}
|
@@ -1205,12 +1200,12 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1205
1200
|
// narrow the reference type to "float" because it is a supertype of "int".
|
1206
1201
|
const narrowedSupertypes = (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1207
1202
|
const concreteReferenceType = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1208
|
-
if ((0,
|
1203
|
+
if ((0, types_1.isAnyOrUnknown)(concreteReferenceType)) {
|
1209
1204
|
canNarrow = false;
|
1210
1205
|
return referenceSubtype;
|
1211
1206
|
}
|
1212
1207
|
// Handle "type" specially.
|
1213
|
-
if ((0,
|
1208
|
+
if ((0, types_1.isClassInstance)(concreteReferenceType) && types_1.ClassType.isBuiltIn(concreteReferenceType, 'type')) {
|
1214
1209
|
canNarrow = false;
|
1215
1210
|
return referenceSubtype;
|
1216
1211
|
}
|
@@ -1219,7 +1214,7 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1219
1214
|
}
|
1220
1215
|
if (evaluator.assignType(elementTypeWithoutLiteral, concreteReferenceType)) {
|
1221
1216
|
return (0, typeUtils_1.mapSubtypes)(elementType, (elementSubtype) => {
|
1222
|
-
if ((0,
|
1217
|
+
if ((0, types_1.isClassInstance)(elementSubtype) && (0, types_1.isSameWithoutLiteralValue)(referenceSubtype, elementSubtype)) {
|
1223
1218
|
return elementSubtype;
|
1224
1219
|
}
|
1225
1220
|
return undefined;
|
@@ -1234,7 +1229,7 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1234
1229
|
// subtypes of "object".
|
1235
1230
|
const narrowedSubtypes = (0, typeUtils_1.mapSubtypes)(elementType, (elementSubtype) => {
|
1236
1231
|
const concreteElementType = evaluator.makeTopLevelTypeVarsConcrete(elementSubtype);
|
1237
|
-
if ((0,
|
1232
|
+
if ((0, types_1.isAnyOrUnknown)(concreteElementType)) {
|
1238
1233
|
canNarrow = false;
|
1239
1234
|
return referenceType;
|
1240
1235
|
}
|
@@ -1243,14 +1238,14 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1243
1238
|
}
|
1244
1239
|
return undefined;
|
1245
1240
|
});
|
1246
|
-
return canNarrow ? (0,
|
1241
|
+
return canNarrow ? (0, types_1.combineTypes)([narrowedSupertypes, narrowedSubtypes]) : referenceType;
|
1247
1242
|
}
|
1248
1243
|
exports.narrowTypeForContainerElementType = narrowTypeForContainerElementType;
|
1249
1244
|
// Attempts to narrow a type based on whether it is a TypedDict with
|
1250
1245
|
// a literal key value.
|
1251
1246
|
function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPositiveTest) {
|
1252
1247
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1253
|
-
if ((0,
|
1248
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
1254
1249
|
const entries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype, /* allowNarrowed */ true);
|
1255
1250
|
const tdEntry = entries.get(literalKey.literalValue);
|
1256
1251
|
if (isPositiveTest) {
|
@@ -1259,7 +1254,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1259
1254
|
// the TypedDict doesn't define this entry, it is not this type.
|
1260
1255
|
// If it's not final, we can't say this because it could be a
|
1261
1256
|
// subclass of this TypedDict that adds more fields.
|
1262
|
-
return
|
1257
|
+
return types_1.ClassType.isFinal(subtype) ? undefined : subtype;
|
1263
1258
|
}
|
1264
1259
|
// If the entry is currently not required and not marked provided, we can mark
|
1265
1260
|
// it as provided after this guard expression confirms it is.
|
@@ -1281,7 +1276,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1281
1276
|
isProvided: true,
|
1282
1277
|
});
|
1283
1278
|
// Clone the TypedDict object with the new entries.
|
1284
|
-
return
|
1279
|
+
return types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForNarrowedTypedDictEntries(types_1.ClassType.cloneAsInstantiable(subtype), newNarrowedEntriesMap));
|
1285
1280
|
}
|
1286
1281
|
else {
|
1287
1282
|
return tdEntry !== undefined && (tdEntry.isRequired || tdEntry.isProvided) ? undefined : subtype;
|
@@ -1297,7 +1292,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1297
1292
|
function narrowTypeForDiscriminatedDictEntryComparison(evaluator, referenceType, indexLiteralType, literalType, isPositiveTest) {
|
1298
1293
|
let canNarrow = true;
|
1299
1294
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1300
|
-
if ((0,
|
1295
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
1301
1296
|
const symbolMap = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype);
|
1302
1297
|
const tdEntry = symbolMap.get(indexLiteralType.literalValue);
|
1303
1298
|
if (tdEntry && (0, typeUtils_1.isLiteralTypeOrUnion)(tdEntry.valueType)) {
|
@@ -1314,12 +1309,13 @@ function narrowTypeForDiscriminatedDictEntryComparison(evaluator, referenceType,
|
|
1314
1309
|
});
|
1315
1310
|
return canNarrow ? narrowedType : referenceType;
|
1316
1311
|
}
|
1312
|
+
exports.narrowTypeForDiscriminatedDictEntryComparison = narrowTypeForDiscriminatedDictEntryComparison;
|
1317
1313
|
function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, indexLiteralType, literalType, isPositiveTest) {
|
1318
1314
|
let canNarrow = true;
|
1319
1315
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1320
1316
|
var _a;
|
1321
|
-
if ((0,
|
1322
|
-
|
1317
|
+
if ((0, types_1.isClassInstance)(subtype) &&
|
1318
|
+
types_1.ClassType.isTupleClass(subtype) &&
|
1323
1319
|
!(0, typeUtils_1.isUnboundedTupleClass)(subtype) &&
|
1324
1320
|
typeof indexLiteralType.literalValue === 'number') {
|
1325
1321
|
const indexValue = indexLiteralType.literalValue;
|
@@ -1340,28 +1336,29 @@ function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, ind
|
|
1340
1336
|
});
|
1341
1337
|
return canNarrow ? narrowedType : referenceType;
|
1342
1338
|
}
|
1339
|
+
exports.narrowTypeForDiscriminatedTupleComparison = narrowTypeForDiscriminatedTupleComparison;
|
1343
1340
|
// Attempts to narrow a type based on a comparison (equal or not equal)
|
1344
1341
|
// between a discriminating field that has a declared literal type to a
|
1345
1342
|
// literal value.
|
1346
1343
|
function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceType, memberName, literalType, isPositiveTest) {
|
1347
1344
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1348
1345
|
let memberInfo;
|
1349
|
-
if ((0,
|
1346
|
+
if ((0, types_1.isClassInstance)(subtype)) {
|
1350
1347
|
memberInfo = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
|
1351
1348
|
}
|
1352
|
-
else if ((0,
|
1349
|
+
else if ((0, types_1.isInstantiableClass)(subtype)) {
|
1353
1350
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
|
1354
1351
|
}
|
1355
1352
|
if (memberInfo && memberInfo.isTypeDeclared) {
|
1356
1353
|
let memberType = evaluator.getTypeOfMember(memberInfo);
|
1357
1354
|
// Handle the case where the field is a property
|
1358
1355
|
// that has a declared literal return type for its getter.
|
1359
|
-
if ((0,
|
1356
|
+
if ((0, types_1.isClassInstance)(subtype) && (0, typeUtils_1.isProperty)(memberType)) {
|
1360
1357
|
const getterInfo = (0, typeUtils_1.lookUpObjectMember)(memberType, 'fget');
|
1361
1358
|
if (getterInfo && getterInfo.isTypeDeclared) {
|
1362
1359
|
const getterType = evaluator.getTypeOfMember(getterInfo);
|
1363
|
-
if ((0,
|
1364
|
-
const getterReturnType =
|
1360
|
+
if ((0, types_1.isFunction)(getterType) && getterType.details.declaredReturnType) {
|
1361
|
+
const getterReturnType = types_1.FunctionType.getSpecializedReturnType(getterType);
|
1365
1362
|
if (getterReturnType) {
|
1366
1363
|
memberType = getterReturnType;
|
1367
1364
|
}
|
@@ -1387,10 +1384,10 @@ function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceTy
|
|
1387
1384
|
function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType, memberName, isPositiveTest) {
|
1388
1385
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1389
1386
|
let memberInfo;
|
1390
|
-
if ((0,
|
1387
|
+
if ((0, types_1.isClassInstance)(subtype)) {
|
1391
1388
|
memberInfo = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
|
1392
1389
|
}
|
1393
|
-
else if ((0,
|
1390
|
+
else if ((0, types_1.isInstantiableClass)(subtype)) {
|
1394
1391
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
|
1395
1392
|
}
|
1396
1393
|
if (memberInfo && memberInfo.isTypeDeclared) {
|
@@ -1403,13 +1400,13 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1403
1400
|
if ((0, typeUtils_1.isProperty)(memberSubtype) || (0, typeUtils_1.isMaybeDescriptorInstance)(memberSubtype)) {
|
1404
1401
|
canNarrow = false;
|
1405
1402
|
}
|
1406
|
-
if ((0,
|
1403
|
+
if ((0, types_1.isAnyOrUnknown)(memberSubtype) || (0, types_1.isNoneInstance)(memberSubtype) || (0, types_1.isNever)(memberSubtype)) {
|
1407
1404
|
canNarrow = false;
|
1408
1405
|
}
|
1409
1406
|
});
|
1410
1407
|
}
|
1411
1408
|
else {
|
1412
|
-
canNarrow = (0,
|
1409
|
+
canNarrow = (0, types_1.isNoneInstance)(memberType);
|
1413
1410
|
}
|
1414
1411
|
if (canNarrow) {
|
1415
1412
|
return undefined;
|
@@ -1422,21 +1419,21 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1422
1419
|
function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
1423
1420
|
return evaluator.mapSubtypesExpandTypeVars(type,
|
1424
1421
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
1425
|
-
if ((0,
|
1426
|
-
const matches =
|
1422
|
+
if ((0, types_1.isClassInstance)(subtype)) {
|
1423
|
+
const matches = types_1.ClassType.isDerivedFrom(classType, types_1.ClassType.cloneAsInstantiable(subtype));
|
1427
1424
|
if (isPositiveTest) {
|
1428
1425
|
if (matches) {
|
1429
|
-
if (
|
1426
|
+
if (types_1.ClassType.isSameGenericClass(subtype, classType)) {
|
1430
1427
|
return subtype;
|
1431
1428
|
}
|
1432
|
-
return (0, typeUtils_1.addConditionToType)(
|
1429
|
+
return (0, typeUtils_1.addConditionToType)(types_1.ClassType.cloneAsInstance(classType), subtype.condition);
|
1433
1430
|
}
|
1434
1431
|
return undefined;
|
1435
1432
|
}
|
1436
1433
|
else {
|
1437
1434
|
// If the class if marked final and it matches, then
|
1438
1435
|
// we can eliminate it in the negative case.
|
1439
|
-
if (matches &&
|
1436
|
+
if (matches && types_1.ClassType.isFinal(subtype)) {
|
1440
1437
|
return undefined;
|
1441
1438
|
}
|
1442
1439
|
// We can't eliminate the subtype in the negative
|
@@ -1445,11 +1442,11 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1445
1442
|
return subtype;
|
1446
1443
|
}
|
1447
1444
|
}
|
1448
|
-
else if ((0,
|
1445
|
+
else if ((0, types_1.isNoneInstance)(subtype)) {
|
1449
1446
|
return isPositiveTest ? undefined : subtype;
|
1450
1447
|
}
|
1451
|
-
else if ((0,
|
1452
|
-
return isPositiveTest ?
|
1448
|
+
else if ((0, types_1.isAnyOrUnknown)(subtype)) {
|
1449
|
+
return isPositiveTest ? types_1.ClassType.cloneAsInstance(classType) : subtype;
|
1453
1450
|
}
|
1454
1451
|
return unexpandedSubtype;
|
1455
1452
|
});
|
@@ -1460,9 +1457,9 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1460
1457
|
function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, isPositiveTest, isIsOperator) {
|
1461
1458
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1462
1459
|
subtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1463
|
-
if ((0,
|
1460
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isSameGenericClass(literalType, subtype)) {
|
1464
1461
|
if (subtype.literalValue !== undefined) {
|
1465
|
-
const literalValueMatches =
|
1462
|
+
const literalValueMatches = types_1.ClassType.isLiteralValueSame(subtype, literalType);
|
1466
1463
|
if ((literalValueMatches && !isPositiveTest) || (!literalValueMatches && isPositiveTest)) {
|
1467
1464
|
return undefined;
|
1468
1465
|
}
|
@@ -1476,12 +1473,12 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1476
1473
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1477
1474
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, subtype);
|
1478
1475
|
if (allLiteralTypes && allLiteralTypes.length > 0) {
|
1479
|
-
return (0,
|
1476
|
+
return (0, types_1.combineTypes)(allLiteralTypes.filter((type) => !types_1.ClassType.isLiteralValueSame(type, literalType)));
|
1480
1477
|
}
|
1481
1478
|
}
|
1482
1479
|
}
|
1483
1480
|
else if (isPositiveTest) {
|
1484
|
-
if (isIsOperator || (0,
|
1481
|
+
if (isIsOperator || (0, types_1.isNoneInstance)(subtype)) {
|
1485
1482
|
return undefined;
|
1486
1483
|
}
|
1487
1484
|
}
|
@@ -1489,22 +1486,22 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1489
1486
|
});
|
1490
1487
|
}
|
1491
1488
|
function enumerateLiteralsForType(evaluator, type) {
|
1492
|
-
if (
|
1489
|
+
if (types_1.ClassType.isBuiltIn(type, 'bool')) {
|
1493
1490
|
// Booleans have only two types: True and False.
|
1494
1491
|
return [
|
1495
|
-
|
1496
|
-
|
1492
|
+
types_1.ClassType.cloneWithLiteral(type, /* value */ true),
|
1493
|
+
types_1.ClassType.cloneWithLiteral(type, /* value */ false),
|
1497
1494
|
];
|
1498
1495
|
}
|
1499
|
-
if (
|
1496
|
+
if (types_1.ClassType.isEnumClass(type)) {
|
1500
1497
|
// Enumerate all of the values in this enumeration.
|
1501
1498
|
const enumList = [];
|
1502
1499
|
const fields = type.details.fields;
|
1503
1500
|
fields.forEach((symbol) => {
|
1504
1501
|
if (!symbol.isIgnoredForProtocolMatch()) {
|
1505
1502
|
const symbolType = evaluator.getEffectiveTypeOfSymbol(symbol);
|
1506
|
-
if ((0,
|
1507
|
-
|
1503
|
+
if ((0, types_1.isClassInstance)(symbolType) &&
|
1504
|
+
types_1.ClassType.isSameGenericClass(type, symbolType) &&
|
1508
1505
|
symbolType.literalValue !== undefined) {
|
1509
1506
|
enumList.push(symbolType);
|
1510
1507
|
}
|
@@ -1531,7 +1528,7 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1531
1528
|
return isPositiveTest ? undefined : subtype;
|
1532
1529
|
}
|
1533
1530
|
case 7 /* Class */: {
|
1534
|
-
if (
|
1531
|
+
if (types_1.TypeBase.isInstantiable(subtype)) {
|
1535
1532
|
return isPositiveTest ? subtype : undefined;
|
1536
1533
|
}
|
1537
1534
|
// See if the object is callable.
|
@@ -1546,24 +1543,24 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1546
1543
|
// new intersection type.
|
1547
1544
|
const className = `<callable subtype of ${subtype.details.name}>`;
|
1548
1545
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
1549
|
-
let newClassType =
|
1546
|
+
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 0 /* None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
1550
1547
|
/* declaredMetaclass */ undefined, subtype.details.effectiveMetaclass, subtype.details.docString);
|
1551
|
-
newClassType.details.baseClasses = [
|
1548
|
+
newClassType.details.baseClasses = [types_1.ClassType.cloneAsInstantiable(subtype)];
|
1552
1549
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
1553
1550
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, subtype.condition);
|
1554
1551
|
// Add a __call__ method to the new class.
|
1555
|
-
const callMethod =
|
1552
|
+
const callMethod = types_1.FunctionType.createSynthesizedInstance('__call__');
|
1556
1553
|
const selfParam = {
|
1557
1554
|
category: 0 /* Simple */,
|
1558
1555
|
name: 'self',
|
1559
|
-
type:
|
1556
|
+
type: types_1.ClassType.cloneAsInstance(newClassType),
|
1560
1557
|
hasDeclaredType: true,
|
1561
1558
|
};
|
1562
|
-
|
1563
|
-
|
1564
|
-
callMethod.details.declaredReturnType =
|
1559
|
+
types_1.FunctionType.addParameter(callMethod, selfParam);
|
1560
|
+
types_1.FunctionType.addDefaultParameters(callMethod);
|
1561
|
+
callMethod.details.declaredReturnType = types_1.UnknownType.create();
|
1565
1562
|
newClassType.details.fields.set('__call__', symbol_1.Symbol.createWithType(4 /* ClassMember */, callMethod));
|
1566
|
-
return
|
1563
|
+
return types_1.ClassType.cloneAsInstance(newClassType);
|
1567
1564
|
}
|
1568
1565
|
return undefined;
|
1569
1566
|
}
|