@zzzen/pyright-internal 1.2.0-dev.20230416 → 1.2.0-dev.20230430
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/analyzerFileInfo.d.ts +4 -1
- package/dist/analyzer/analyzerFileInfo.js.map +1 -1
- package/dist/analyzer/backgroundAnalysisProgram.d.ts +4 -4
- package/dist/analyzer/backgroundAnalysisProgram.js +10 -7
- package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
- package/dist/analyzer/binder.js +13 -10
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +6 -3
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +17 -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/dataClasses.js +5 -2
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/declarationUtils.d.ts +6 -1
- package/dist/analyzer/declarationUtils.js +10 -8
- 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/importResolver.d.ts +15 -15
- package/dist/analyzer/importResolver.js +477 -477
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/namedTuples.d.ts +1 -1
- package/dist/analyzer/namedTuples.js +29 -41
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/packageTypeReport.d.ts +2 -1
- package/dist/analyzer/packageTypeReport.js +2 -1
- package/dist/analyzer/packageTypeReport.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.d.ts +1 -1
- package/dist/analyzer/packageTypeVerifier.js +27 -13
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/patternMatching.d.ts +6 -2
- package/dist/analyzer/patternMatching.js +108 -2
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +34 -44
- package/dist/analyzer/program.js +1382 -1529
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/protocols.js +1 -1
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/service.d.ts +14 -27
- package/dist/analyzer/service.js +81 -114
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.d.ts +7 -21
- package/dist/analyzer/sourceFile.js +58 -130
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceFileInfoUtils.d.ts +7 -1
- package/dist/analyzer/sourceFileInfoUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +398 -334
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +6 -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 +175 -176
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +1 -0
- package/dist/analyzer/typeUtils.js +44 -17
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/types.js +10 -2
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +10 -11
- package/dist/backgroundAnalysisBase.js +87 -87
- package/dist/backgroundAnalysisBase.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +8 -8
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/commands/quickActionCommand.js +4 -1
- package/dist/commands/quickActionCommand.js.map +1 -1
- package/dist/common/cancellationUtils.d.ts +1 -1
- package/dist/common/cancellationUtils.js +9 -9
- package/dist/common/cancellationUtils.js.map +1 -1
- package/dist/common/commandLineOptions.d.ts +1 -2
- package/dist/common/commandLineOptions.js.map +1 -1
- package/dist/common/configOptions.d.ts +2 -2
- package/dist/common/configOptions.js.map +1 -1
- package/dist/common/console.d.ts +5 -9
- package/dist/common/console.js +46 -33
- package/dist/common/console.js.map +1 -1
- package/dist/common/deferred.js +10 -10
- package/dist/common/deferred.js.map +1 -1
- package/dist/common/extensibility.d.ts +33 -9
- package/dist/common/extensibility.js +1 -1
- package/dist/common/extensibility.js.map +1 -1
- package/dist/common/fileBasedCancellationUtils.js +5 -5
- package/dist/common/fileBasedCancellationUtils.js.map +1 -1
- package/dist/common/fileSystem.d.ts +12 -10
- package/dist/common/fileSystem.js.map +1 -1
- package/dist/common/fullAccessHost.d.ts +3 -3
- package/dist/common/fullAccessHost.js +6 -6
- package/dist/common/fullAccessHost.js.map +1 -1
- package/dist/common/pathUtils.d.ts +2 -2
- package/dist/common/pathUtils.js.map +1 -1
- package/dist/common/realFileSystem.js +12 -7
- package/dist/common/realFileSystem.js.map +1 -1
- package/dist/common/uriParser.d.ts +2 -2
- package/dist/common/uriParser.js +3 -3
- package/dist/common/uriParser.js.map +1 -1
- package/dist/common/workspaceEditUtils.js +7 -4
- package/dist/common/workspaceEditUtils.js.map +1 -1
- package/dist/languageServerBase.d.ts +32 -30
- package/dist/languageServerBase.js +276 -337
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/analyzerServiceExecutor.js +0 -1
- package/dist/languageService/analyzerServiceExecutor.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.d.ts +16 -12
- package/dist/languageService/callHierarchyProvider.js +138 -41
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.js +119 -10
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/definitionProvider.d.ts +23 -9
- package/dist/languageService/definitionProvider.js +116 -91
- package/dist/languageService/definitionProvider.js.map +1 -1
- package/dist/languageService/documentHighlightProvider.d.ts +8 -3
- package/dist/languageService/documentHighlightProvider.js +17 -6
- package/dist/languageService/documentHighlightProvider.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.d.ts +2 -2
- package/dist/languageService/documentSymbolCollector.js +4 -3
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/documentSymbolProvider.js +5 -3
- package/dist/languageService/documentSymbolProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.d.ts +28 -28
- package/dist/languageService/hoverProvider.js +142 -120
- 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 +6 -1
- package/dist/languageService/indentationUtils.js.map +1 -1
- package/dist/languageService/insertionPointUtils.js +3 -3
- package/dist/languageService/insertionPointUtils.js.map +1 -1
- package/dist/languageService/navigationUtils.d.ts +6 -0
- package/dist/languageService/navigationUtils.js +28 -0
- package/dist/languageService/navigationUtils.js.map +1 -0
- package/dist/languageService/quickActions.d.ts +2 -2
- package/dist/languageService/quickActions.js +12 -1
- package/dist/languageService/quickActions.js.map +1 -1
- package/dist/languageService/referencesProvider.d.ts +8 -2
- package/dist/languageService/referencesProvider.js +81 -3
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/renameModuleProvider.d.ts +13 -13
- package/dist/languageService/renameModuleProvider.js +43 -43
- package/dist/languageService/renameModuleProvider.js.map +1 -1
- package/dist/languageService/signatureHelpProvider.d.ts +19 -23
- package/dist/languageService/signatureHelpProvider.js +111 -18
- package/dist/languageService/signatureHelpProvider.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/parser/characterStream.d.ts +3 -3
- package/dist/parser/characterStream.js +12 -12
- package/dist/parser/characterStream.js.map +1 -1
- package/dist/parser/parser.d.ts +1 -1
- package/dist/parser/parser.js.map +1 -1
- package/dist/pyright.js +57 -54
- package/dist/pyright.js.map +1 -1
- package/dist/pyrightFileSystem.d.ts +1 -1
- package/dist/pyrightFileSystem.js +21 -21
- package/dist/pyrightFileSystem.js.map +1 -1
- package/dist/readonlyAugmentedFileSystem.d.ts +6 -6
- package/dist/readonlyAugmentedFileSystem.js +28 -28
- package/dist/readonlyAugmentedFileSystem.js.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.js.map +1 -1
- package/dist/tests/chainedSourceFiles.test.js +1 -1
- package/dist/tests/chainedSourceFiles.test.js.map +1 -1
- package/dist/tests/checker.test.js +2 -2
- package/dist/tests/completions.test.js +173 -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.override2.fourslash.js +15 -0
- package/dist/tests/fourslash/completions.override2.fourslash.js.map +1 -1
- package/dist/tests/fourslash/fourslash.d.ts +11 -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/testLanguageService.d.ts +3 -3
- package/dist/tests/harness/fourslash/testLanguageService.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.d.ts +14 -9
- package/dist/tests/harness/fourslash/testState.js +144 -102
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.d.ts +8 -8
- package/dist/tests/harness/vfs/filesystem.js +68 -68
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/importAdder.test.js +69 -1
- package/dist/tests/importAdder.test.js.map +1 -1
- package/dist/tests/importStatementUtils.test.js +34 -0
- package/dist/tests/importStatementUtils.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.updateReference.test.js +14 -0
- package/dist/tests/moveSymbol.updateReference.test.js.map +1 -1
- package/dist/tests/signatureHelp.test.js +5 -1
- package/dist/tests/signatureHelp.test.js.map +1 -1
- package/dist/tests/sourceFile.test.js +1 -1
- package/dist/tests/sourceFile.test.js.map +1 -1
- package/dist/tests/testStateUtils.js +1 -1
- package/dist/tests/testStateUtils.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 +12 -0
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +7 -1
- 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/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,
|
@@ -872,7 +867,9 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
872
867
|
}
|
873
868
|
filteredTypes.push((0, typeUtils_1.addConditionToType)(specializedFilterType, constraints));
|
874
869
|
}
|
875
|
-
else if (allowIntersections
|
870
|
+
else if (allowIntersections &&
|
871
|
+
!types_1.ClassType.isFinal(varType) &&
|
872
|
+
!types_1.ClassType.isFinal(concreteFilterType)) {
|
876
873
|
// The two types appear to have no relation. It's possible that the
|
877
874
|
// two types are protocols or the program is expecting one type to
|
878
875
|
// be a mix-in class used with the other. In this case, we'll
|
@@ -880,38 +877,38 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
880
877
|
// the two types.
|
881
878
|
const className = `<subclass of ${varType.details.name} and ${concreteFilterType.details.name}>`;
|
882
879
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
883
|
-
let newClassType =
|
880
|
+
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 0 /* None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
884
881
|
/* declaredMetaclass */ undefined, varType.details.effectiveMetaclass, varType.details.docString);
|
885
|
-
newClassType.details.baseClasses = [
|
882
|
+
newClassType.details.baseClasses = [types_1.ClassType.cloneAsInstantiable(varType), concreteFilterType];
|
886
883
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
887
884
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, concreteFilterType.condition);
|
888
|
-
if ((0,
|
885
|
+
if ((0, types_1.isTypeVar)(unexpandedType) &&
|
889
886
|
!unexpandedType.details.isParamSpec &&
|
890
887
|
unexpandedType.details.constraints.length === 0) {
|
891
888
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, [
|
892
889
|
{
|
893
|
-
typeVarName:
|
890
|
+
typeVarName: types_1.TypeVarType.getNameWithScope(unexpandedType),
|
894
891
|
constraintIndex: 0,
|
895
892
|
isConstrainedTypeVar: false,
|
896
893
|
},
|
897
894
|
]);
|
898
895
|
}
|
899
|
-
let newClassInstanceType =
|
896
|
+
let newClassInstanceType = types_1.ClassType.cloneAsInstance(newClassType);
|
900
897
|
if (varType.condition) {
|
901
898
|
newClassInstanceType = (0, typeUtils_1.addConditionToType)(newClassInstanceType, varType.condition);
|
902
899
|
}
|
903
900
|
// If this is a issubclass check, we do a double conversion from instantiable
|
904
901
|
// to instance back to instantiable to make sure that the includeSubclasses flag
|
905
902
|
// gets cleared.
|
906
|
-
filteredTypes.push(isInstanceCheck ? newClassInstanceType :
|
903
|
+
filteredTypes.push(isInstanceCheck ? newClassInstanceType : types_1.ClassType.cloneAsInstantiable(newClassInstanceType));
|
907
904
|
}
|
908
905
|
}
|
909
906
|
}
|
910
|
-
else if ((0,
|
907
|
+
else if ((0, types_1.isTypeVar)(filterType) && types_1.TypeBase.isInstantiable(filterType)) {
|
911
908
|
// Handle the case where the filter type is Type[T] and the unexpanded
|
912
909
|
// subtype is some instance type, possibly T.
|
913
|
-
if (isInstanceCheck &&
|
914
|
-
if ((0,
|
910
|
+
if (isInstanceCheck && types_1.TypeBase.isInstance(unexpandedType)) {
|
911
|
+
if ((0, types_1.isTypeVar)(unexpandedType) && (0, types_1.isTypeSame)((0, typeUtils_1.convertToInstance)(filterType), unexpandedType)) {
|
915
912
|
// If the unexpanded subtype is T, we can definitively filter
|
916
913
|
// in both the positive and negative cases.
|
917
914
|
if (isPositiveTest) {
|
@@ -930,8 +927,8 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
930
927
|
}
|
931
928
|
}
|
932
929
|
}
|
933
|
-
else if (!isInstanceCheck &&
|
934
|
-
if ((0,
|
930
|
+
else if (!isInstanceCheck && types_1.TypeBase.isInstantiable(unexpandedType)) {
|
931
|
+
if ((0, types_1.isTypeVar)(unexpandedType) && (0, types_1.isTypeSame)(filterType, unexpandedType)) {
|
935
932
|
if (isPositiveTest) {
|
936
933
|
filteredTypes.push(unexpandedType);
|
937
934
|
}
|
@@ -947,12 +944,12 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
947
944
|
}
|
948
945
|
}
|
949
946
|
}
|
950
|
-
else if ((0,
|
947
|
+
else if ((0, types_1.isFunction)(filterType)) {
|
951
948
|
// Handle an isinstance check against Callable.
|
952
949
|
if (isInstanceCheck) {
|
953
950
|
let isCallable = false;
|
954
|
-
if ((0,
|
955
|
-
if (
|
951
|
+
if ((0, types_1.isClass)(varType)) {
|
952
|
+
if (types_1.TypeBase.isInstantiable(unexpandedType)) {
|
956
953
|
isCallable = true;
|
957
954
|
}
|
958
955
|
else {
|
@@ -984,7 +981,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
984
981
|
// We perform a double conversion from instance to instantiable
|
985
982
|
// here to make sure that the includeSubclasses flag is cleared
|
986
983
|
// if it's a class.
|
987
|
-
return filteredTypes.map((t) => ((0,
|
984
|
+
return filteredTypes.map((t) => ((0, types_1.isInstantiableClass)(t) ? (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.convertToInstance)(t)) : t));
|
988
985
|
}
|
989
986
|
return filteredTypes.map((t) => (0, typeUtils_1.convertToInstance)(t));
|
990
987
|
};
|
@@ -996,7 +993,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
996
993
|
if (evaluator.assignType(varType, (0, typeUtils_1.convertToInstance)(concreteFilterType))) {
|
997
994
|
// If the filter type is a Callable, use the original type. If the
|
998
995
|
// filter type is a callback protocol, use the filter type.
|
999
|
-
if ((0,
|
996
|
+
if ((0, types_1.isFunction)(filterType)) {
|
1000
997
|
filteredTypes.push(unexpandedType);
|
1001
998
|
}
|
1002
999
|
else {
|
@@ -1009,7 +1006,7 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1009
1006
|
// If the filter type is a runtime checkable protocol class, it can
|
1010
1007
|
// be used in an instance check.
|
1011
1008
|
const concreteFilterType = evaluator.makeTopLevelTypeVarsConcrete(filterType);
|
1012
|
-
if ((0,
|
1009
|
+
if ((0, types_1.isClass)(concreteFilterType) && !types_1.ClassType.isProtocolClass(concreteFilterType)) {
|
1013
1010
|
return false;
|
1014
1011
|
}
|
1015
1012
|
return evaluator.assignType(varType, (0, typeUtils_1.convertToInstance)(concreteFilterType));
|
@@ -1028,30 +1025,30 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1028
1025
|
// on a constrained TypeVar that they want to filter based on its constrained
|
1029
1026
|
// parts.
|
1030
1027
|
const negativeFallback = (0, typeUtils_1.getTypeCondition)(subtype) ? subtype : unexpandedSubtype;
|
1031
|
-
const isSubtypeTypeObject = (0,
|
1032
|
-
if (isPositiveTest && (0,
|
1028
|
+
const isSubtypeTypeObject = (0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'type');
|
1029
|
+
if (isPositiveTest && (0, types_1.isAnyOrUnknown)(subtype)) {
|
1033
1030
|
// If this is a positive test and the effective type is Any or
|
1034
1031
|
// Unknown, we can assume that the type matches one of the
|
1035
1032
|
// specified types.
|
1036
1033
|
if (isInstanceCheck) {
|
1037
|
-
anyOrUnknownSubstitutions.push((0,
|
1034
|
+
anyOrUnknownSubstitutions.push((0, types_1.combineTypes)(classTypeList.map((classType) => (0, typeUtils_1.convertToInstance)(classType))));
|
1038
1035
|
}
|
1039
1036
|
else {
|
1040
1037
|
// We perform a double conversion from instance to instantiable
|
1041
1038
|
// here to make sure that the includeSubclasses flag is cleared
|
1042
1039
|
// if it's a class.
|
1043
|
-
anyOrUnknownSubstitutions.push((0,
|
1040
|
+
anyOrUnknownSubstitutions.push((0, types_1.combineTypes)(classTypeList.map((classType) => (0, typeUtils_1.convertToInstantiable)((0, typeUtils_1.convertToInstance)(classType)))));
|
1044
1041
|
}
|
1045
1042
|
anyOrUnknown.push(subtype);
|
1046
1043
|
return undefined;
|
1047
1044
|
}
|
1048
1045
|
if (isInstanceCheck) {
|
1049
|
-
if ((0,
|
1046
|
+
if ((0, types_1.isNoneInstance)(subtype)) {
|
1050
1047
|
const containsNoneType = classTypeList.some((t) => {
|
1051
|
-
if ((0,
|
1048
|
+
if ((0, types_1.isNoneTypeClass)(t)) {
|
1052
1049
|
return true;
|
1053
1050
|
}
|
1054
|
-
return (0,
|
1051
|
+
return (0, types_1.isInstantiableClass)(t) && types_1.ClassType.isBuiltIn(t, 'NoneType');
|
1055
1052
|
});
|
1056
1053
|
if (isPositiveTest) {
|
1057
1054
|
return containsNoneType ? subtype : undefined;
|
@@ -1060,28 +1057,28 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1060
1057
|
return containsNoneType ? undefined : subtype;
|
1061
1058
|
}
|
1062
1059
|
}
|
1063
|
-
if ((0,
|
1060
|
+
if ((0, types_1.isModule)(subtype) || ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'ModuleType'))) {
|
1064
1061
|
// Handle type narrowing for runtime-checkable protocols
|
1065
1062
|
// when applied to modules.
|
1066
1063
|
if (isPositiveTest) {
|
1067
1064
|
const filteredTypes = classTypeList.filter((classType) => {
|
1068
1065
|
const concreteClassType = evaluator.makeTopLevelTypeVarsConcrete(classType);
|
1069
|
-
return ((0,
|
1066
|
+
return ((0, types_1.isInstantiableClass)(concreteClassType) && types_1.ClassType.isProtocolClass(concreteClassType));
|
1070
1067
|
});
|
1071
1068
|
if (filteredTypes.length > 0) {
|
1072
|
-
return (0, typeUtils_1.convertToInstance)((0,
|
1069
|
+
return (0, typeUtils_1.convertToInstance)((0, types_1.combineTypes)(filteredTypes));
|
1073
1070
|
}
|
1074
1071
|
}
|
1075
1072
|
}
|
1076
|
-
if ((0,
|
1077
|
-
return (0,
|
1073
|
+
if ((0, types_1.isClassInstance)(subtype) && !isSubtypeTypeObject) {
|
1074
|
+
return (0, types_1.combineTypes)(filterClassType(types_1.ClassType.cloneAsInstantiable(subtype), (0, typeUtils_1.convertToInstance)(unexpandedSubtype), (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1078
1075
|
}
|
1079
|
-
if (((0,
|
1080
|
-
return (0,
|
1076
|
+
if (((0, types_1.isFunction)(subtype) || (0, types_1.isOverloadedFunction)(subtype)) && isInstanceCheck) {
|
1077
|
+
return (0, types_1.combineTypes)(filterFunctionType(subtype, (0, typeUtils_1.convertToInstance)(unexpandedSubtype)));
|
1081
1078
|
}
|
1082
|
-
if ((0,
|
1079
|
+
if ((0, types_1.isInstantiableClass)(subtype) || isSubtypeTypeObject) {
|
1083
1080
|
// Handle the special case of isinstance(x, type).
|
1084
|
-
const includesTypeType = classTypeList.some((classType) => (0,
|
1081
|
+
const includesTypeType = classTypeList.some((classType) => (0, types_1.isInstantiableClass)(classType) && types_1.ClassType.isBuiltIn(classType, 'type'));
|
1085
1082
|
if (isPositiveTest) {
|
1086
1083
|
return includesTypeType ? negativeFallback : undefined;
|
1087
1084
|
}
|
@@ -1091,13 +1088,13 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1091
1088
|
}
|
1092
1089
|
}
|
1093
1090
|
else {
|
1094
|
-
if ((0,
|
1095
|
-
return (0,
|
1091
|
+
if ((0, types_1.isInstantiableClass)(subtype)) {
|
1092
|
+
return (0, types_1.combineTypes)(filterClassType(subtype, unexpandedSubtype, (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1096
1093
|
}
|
1097
1094
|
if (isSubtypeTypeObject) {
|
1098
1095
|
const objectType = evaluator.getBuiltInObject(errorNode, 'object');
|
1099
|
-
if (objectType && (0,
|
1100
|
-
return (0,
|
1096
|
+
if (objectType && (0, types_1.isClassInstance)(objectType)) {
|
1097
|
+
return (0, types_1.combineTypes)(filterClassType(types_1.ClassType.cloneAsInstantiable(objectType), (0, typeUtils_1.convertToInstantiable)(unexpandedSubtype), (0, typeUtils_1.getTypeCondition)(subtype), negativeFallback));
|
1101
1098
|
}
|
1102
1099
|
}
|
1103
1100
|
}
|
@@ -1109,11 +1106,11 @@ function narrowTypeForIsInstance(evaluator, type, classTypeList, isInstanceCheck
|
|
1109
1106
|
// Any/Unknown. For example, if the statement is "isinstance(x, list)"
|
1110
1107
|
// and the type of x is "List[str] | int | Any", the result should be
|
1111
1108
|
// "List[str]", not "List[str] | List[Unknown]".
|
1112
|
-
if ((0,
|
1113
|
-
return (0,
|
1109
|
+
if ((0, types_1.isNever)(filteredType) && anyOrUnknownSubstitutions.length > 0) {
|
1110
|
+
return (0, types_1.combineTypes)(anyOrUnknownSubstitutions);
|
1114
1111
|
}
|
1115
|
-
if ((0,
|
1116
|
-
return (0,
|
1112
|
+
if ((0, types_1.isNever)(filteredType) && anyOrUnknown.length > 0) {
|
1113
|
+
return (0, types_1.combineTypes)(anyOrUnknown);
|
1117
1114
|
}
|
1118
1115
|
return filteredType;
|
1119
1116
|
}
|
@@ -1122,7 +1119,7 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1122
1119
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1123
1120
|
const concreteSubtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1124
1121
|
// If it's not a tuple, we can't narrow it.
|
1125
|
-
if (!(0,
|
1122
|
+
if (!(0, types_1.isClassInstance)(concreteSubtype) ||
|
1126
1123
|
!(0, typeUtils_1.isTupleClass)(concreteSubtype) ||
|
1127
1124
|
(0, typeUtils_1.isUnboundedTupleClass)(concreteSubtype) ||
|
1128
1125
|
!concreteSubtype.tupleTypeArguments) {
|
@@ -1143,8 +1140,8 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1143
1140
|
}
|
1144
1141
|
// Narrowing in the negative case is possible only with tuples
|
1145
1142
|
// with a known length.
|
1146
|
-
if (!(0,
|
1147
|
-
!
|
1143
|
+
if (!(0, types_1.isClassInstance)(containerType) ||
|
1144
|
+
!types_1.ClassType.isBuiltIn(containerType, 'tuple') ||
|
1148
1145
|
!containerType.tupleTypeArguments) {
|
1149
1146
|
return referenceType;
|
1150
1147
|
}
|
@@ -1153,10 +1150,10 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1153
1150
|
const typesToEliminate = [];
|
1154
1151
|
containerType.tupleTypeArguments.forEach((tupleEntry) => {
|
1155
1152
|
if (!tupleEntry.isUnbounded) {
|
1156
|
-
if ((0,
|
1153
|
+
if ((0, types_1.isNoneInstance)(tupleEntry.type)) {
|
1157
1154
|
typesToEliminate.push(tupleEntry.type);
|
1158
1155
|
}
|
1159
|
-
else if ((0,
|
1156
|
+
else if ((0, types_1.isClassInstance)(tupleEntry.type) && (0, typeUtils_1.isLiteralType)(tupleEntry.type)) {
|
1160
1157
|
typesToEliminate.push(tupleEntry.type);
|
1161
1158
|
}
|
1162
1159
|
}
|
@@ -1166,15 +1163,15 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1166
1163
|
}
|
1167
1164
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1168
1165
|
referenceSubtype = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1169
|
-
if ((0,
|
1166
|
+
if ((0, types_1.isClassInstance)(referenceSubtype) && referenceSubtype.literalValue === undefined) {
|
1170
1167
|
// If we're able to enumerate all possible literal values
|
1171
1168
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1172
1169
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, referenceSubtype);
|
1173
1170
|
if (allLiteralTypes && allLiteralTypes.length > 0) {
|
1174
|
-
return (0,
|
1171
|
+
return (0, types_1.combineTypes)(allLiteralTypes.filter((type) => !typesToEliminate.some((t) => (0, types_1.isTypeSame)(t, type))));
|
1175
1172
|
}
|
1176
1173
|
}
|
1177
|
-
if (typesToEliminate.some((t) => (0,
|
1174
|
+
if (typesToEliminate.some((t) => (0, types_1.isTypeSame)(t, referenceSubtype))) {
|
1178
1175
|
return undefined;
|
1179
1176
|
}
|
1180
1177
|
return referenceSubtype;
|
@@ -1183,7 +1180,7 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1183
1180
|
function getElementTypeForContainerNarrowing(containerType) {
|
1184
1181
|
// We support contains narrowing only for certain built-in types that have been specialized.
|
1185
1182
|
const supportedContainers = ['list', 'set', 'frozenset', 'deque', 'tuple', 'dict', 'defaultdict', 'OrderedDict'];
|
1186
|
-
if (!(0,
|
1183
|
+
if (!(0, types_1.isClassInstance)(containerType) || !types_1.ClassType.isBuiltIn(containerType, supportedContainers)) {
|
1187
1184
|
return undefined;
|
1188
1185
|
}
|
1189
1186
|
if (!containerType.typeArguments || containerType.typeArguments.length < 1) {
|
@@ -1191,7 +1188,7 @@ function getElementTypeForContainerNarrowing(containerType) {
|
|
1191
1188
|
}
|
1192
1189
|
let elementType = containerType.typeArguments[0];
|
1193
1190
|
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.tupleTypeArguments) {
|
1194
|
-
elementType = (0,
|
1191
|
+
elementType = (0, types_1.combineTypes)(containerType.tupleTypeArguments.map((t) => t.type));
|
1195
1192
|
}
|
1196
1193
|
return elementType;
|
1197
1194
|
}
|
@@ -1205,12 +1202,12 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1205
1202
|
// narrow the reference type to "float" because it is a supertype of "int".
|
1206
1203
|
const narrowedSupertypes = (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1207
1204
|
const concreteReferenceType = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1208
|
-
if ((0,
|
1205
|
+
if ((0, types_1.isAnyOrUnknown)(concreteReferenceType)) {
|
1209
1206
|
canNarrow = false;
|
1210
1207
|
return referenceSubtype;
|
1211
1208
|
}
|
1212
1209
|
// Handle "type" specially.
|
1213
|
-
if ((0,
|
1210
|
+
if ((0, types_1.isClassInstance)(concreteReferenceType) && types_1.ClassType.isBuiltIn(concreteReferenceType, 'type')) {
|
1214
1211
|
canNarrow = false;
|
1215
1212
|
return referenceSubtype;
|
1216
1213
|
}
|
@@ -1219,7 +1216,7 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1219
1216
|
}
|
1220
1217
|
if (evaluator.assignType(elementTypeWithoutLiteral, concreteReferenceType)) {
|
1221
1218
|
return (0, typeUtils_1.mapSubtypes)(elementType, (elementSubtype) => {
|
1222
|
-
if ((0,
|
1219
|
+
if ((0, types_1.isClassInstance)(elementSubtype) && (0, types_1.isSameWithoutLiteralValue)(referenceSubtype, elementSubtype)) {
|
1223
1220
|
return elementSubtype;
|
1224
1221
|
}
|
1225
1222
|
return undefined;
|
@@ -1234,7 +1231,7 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1234
1231
|
// subtypes of "object".
|
1235
1232
|
const narrowedSubtypes = (0, typeUtils_1.mapSubtypes)(elementType, (elementSubtype) => {
|
1236
1233
|
const concreteElementType = evaluator.makeTopLevelTypeVarsConcrete(elementSubtype);
|
1237
|
-
if ((0,
|
1234
|
+
if ((0, types_1.isAnyOrUnknown)(concreteElementType)) {
|
1238
1235
|
canNarrow = false;
|
1239
1236
|
return referenceType;
|
1240
1237
|
}
|
@@ -1243,14 +1240,14 @@ function narrowTypeForContainerElementType(evaluator, referenceType, elementType
|
|
1243
1240
|
}
|
1244
1241
|
return undefined;
|
1245
1242
|
});
|
1246
|
-
return canNarrow ? (0,
|
1243
|
+
return canNarrow ? (0, types_1.combineTypes)([narrowedSupertypes, narrowedSubtypes]) : referenceType;
|
1247
1244
|
}
|
1248
1245
|
exports.narrowTypeForContainerElementType = narrowTypeForContainerElementType;
|
1249
1246
|
// Attempts to narrow a type based on whether it is a TypedDict with
|
1250
1247
|
// a literal key value.
|
1251
1248
|
function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPositiveTest) {
|
1252
1249
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1253
|
-
if ((0,
|
1250
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
1254
1251
|
const entries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype, /* allowNarrowed */ true);
|
1255
1252
|
const tdEntry = entries.get(literalKey.literalValue);
|
1256
1253
|
if (isPositiveTest) {
|
@@ -1259,7 +1256,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1259
1256
|
// the TypedDict doesn't define this entry, it is not this type.
|
1260
1257
|
// If it's not final, we can't say this because it could be a
|
1261
1258
|
// subclass of this TypedDict that adds more fields.
|
1262
|
-
return
|
1259
|
+
return types_1.ClassType.isFinal(subtype) ? undefined : subtype;
|
1263
1260
|
}
|
1264
1261
|
// If the entry is currently not required and not marked provided, we can mark
|
1265
1262
|
// it as provided after this guard expression confirms it is.
|
@@ -1281,7 +1278,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1281
1278
|
isProvided: true,
|
1282
1279
|
});
|
1283
1280
|
// Clone the TypedDict object with the new entries.
|
1284
|
-
return
|
1281
|
+
return types_1.ClassType.cloneAsInstance(types_1.ClassType.cloneForNarrowedTypedDictEntries(types_1.ClassType.cloneAsInstantiable(subtype), newNarrowedEntriesMap));
|
1285
1282
|
}
|
1286
1283
|
else {
|
1287
1284
|
return tdEntry !== undefined && (tdEntry.isRequired || tdEntry.isProvided) ? undefined : subtype;
|
@@ -1297,7 +1294,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1297
1294
|
function narrowTypeForDiscriminatedDictEntryComparison(evaluator, referenceType, indexLiteralType, literalType, isPositiveTest) {
|
1298
1295
|
let canNarrow = true;
|
1299
1296
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1300
|
-
if ((0,
|
1297
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
1301
1298
|
const symbolMap = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype);
|
1302
1299
|
const tdEntry = symbolMap.get(indexLiteralType.literalValue);
|
1303
1300
|
if (tdEntry && (0, typeUtils_1.isLiteralTypeOrUnion)(tdEntry.valueType)) {
|
@@ -1314,12 +1311,13 @@ function narrowTypeForDiscriminatedDictEntryComparison(evaluator, referenceType,
|
|
1314
1311
|
});
|
1315
1312
|
return canNarrow ? narrowedType : referenceType;
|
1316
1313
|
}
|
1314
|
+
exports.narrowTypeForDiscriminatedDictEntryComparison = narrowTypeForDiscriminatedDictEntryComparison;
|
1317
1315
|
function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, indexLiteralType, literalType, isPositiveTest) {
|
1318
1316
|
let canNarrow = true;
|
1319
1317
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1320
1318
|
var _a;
|
1321
|
-
if ((0,
|
1322
|
-
|
1319
|
+
if ((0, types_1.isClassInstance)(subtype) &&
|
1320
|
+
types_1.ClassType.isTupleClass(subtype) &&
|
1323
1321
|
!(0, typeUtils_1.isUnboundedTupleClass)(subtype) &&
|
1324
1322
|
typeof indexLiteralType.literalValue === 'number') {
|
1325
1323
|
const indexValue = indexLiteralType.literalValue;
|
@@ -1340,28 +1338,29 @@ function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, ind
|
|
1340
1338
|
});
|
1341
1339
|
return canNarrow ? narrowedType : referenceType;
|
1342
1340
|
}
|
1341
|
+
exports.narrowTypeForDiscriminatedTupleComparison = narrowTypeForDiscriminatedTupleComparison;
|
1343
1342
|
// Attempts to narrow a type based on a comparison (equal or not equal)
|
1344
1343
|
// between a discriminating field that has a declared literal type to a
|
1345
1344
|
// literal value.
|
1346
1345
|
function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceType, memberName, literalType, isPositiveTest) {
|
1347
1346
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1348
1347
|
let memberInfo;
|
1349
|
-
if ((0,
|
1348
|
+
if ((0, types_1.isClassInstance)(subtype)) {
|
1350
1349
|
memberInfo = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
|
1351
1350
|
}
|
1352
|
-
else if ((0,
|
1351
|
+
else if ((0, types_1.isInstantiableClass)(subtype)) {
|
1353
1352
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
|
1354
1353
|
}
|
1355
1354
|
if (memberInfo && memberInfo.isTypeDeclared) {
|
1356
1355
|
let memberType = evaluator.getTypeOfMember(memberInfo);
|
1357
1356
|
// Handle the case where the field is a property
|
1358
1357
|
// that has a declared literal return type for its getter.
|
1359
|
-
if ((0,
|
1358
|
+
if ((0, types_1.isClassInstance)(subtype) && (0, typeUtils_1.isProperty)(memberType)) {
|
1360
1359
|
const getterInfo = (0, typeUtils_1.lookUpObjectMember)(memberType, 'fget');
|
1361
1360
|
if (getterInfo && getterInfo.isTypeDeclared) {
|
1362
1361
|
const getterType = evaluator.getTypeOfMember(getterInfo);
|
1363
|
-
if ((0,
|
1364
|
-
const getterReturnType =
|
1362
|
+
if ((0, types_1.isFunction)(getterType) && getterType.details.declaredReturnType) {
|
1363
|
+
const getterReturnType = types_1.FunctionType.getSpecializedReturnType(getterType);
|
1365
1364
|
if (getterReturnType) {
|
1366
1365
|
memberType = getterReturnType;
|
1367
1366
|
}
|
@@ -1387,10 +1386,10 @@ function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceTy
|
|
1387
1386
|
function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType, memberName, isPositiveTest) {
|
1388
1387
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1389
1388
|
let memberInfo;
|
1390
|
-
if ((0,
|
1389
|
+
if ((0, types_1.isClassInstance)(subtype)) {
|
1391
1390
|
memberInfo = (0, typeUtils_1.lookUpObjectMember)(subtype, memberName);
|
1392
1391
|
}
|
1393
|
-
else if ((0,
|
1392
|
+
else if ((0, types_1.isInstantiableClass)(subtype)) {
|
1394
1393
|
memberInfo = (0, typeUtils_1.lookUpClassMember)(subtype, memberName);
|
1395
1394
|
}
|
1396
1395
|
if (memberInfo && memberInfo.isTypeDeclared) {
|
@@ -1403,13 +1402,13 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1403
1402
|
if ((0, typeUtils_1.isProperty)(memberSubtype) || (0, typeUtils_1.isMaybeDescriptorInstance)(memberSubtype)) {
|
1404
1403
|
canNarrow = false;
|
1405
1404
|
}
|
1406
|
-
if ((0,
|
1405
|
+
if ((0, types_1.isAnyOrUnknown)(memberSubtype) || (0, types_1.isNoneInstance)(memberSubtype) || (0, types_1.isNever)(memberSubtype)) {
|
1407
1406
|
canNarrow = false;
|
1408
1407
|
}
|
1409
1408
|
});
|
1410
1409
|
}
|
1411
1410
|
else {
|
1412
|
-
canNarrow = (0,
|
1411
|
+
canNarrow = (0, types_1.isNoneInstance)(memberType);
|
1413
1412
|
}
|
1414
1413
|
if (canNarrow) {
|
1415
1414
|
return undefined;
|
@@ -1422,21 +1421,21 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1422
1421
|
function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
1423
1422
|
return evaluator.mapSubtypesExpandTypeVars(type,
|
1424
1423
|
/* conditionFilter */ undefined, (subtype, unexpandedSubtype) => {
|
1425
|
-
if ((0,
|
1426
|
-
const matches =
|
1424
|
+
if ((0, types_1.isClassInstance)(subtype)) {
|
1425
|
+
const matches = types_1.ClassType.isDerivedFrom(classType, types_1.ClassType.cloneAsInstantiable(subtype));
|
1427
1426
|
if (isPositiveTest) {
|
1428
1427
|
if (matches) {
|
1429
|
-
if (
|
1428
|
+
if (types_1.ClassType.isSameGenericClass(subtype, classType)) {
|
1430
1429
|
return subtype;
|
1431
1430
|
}
|
1432
|
-
return (0, typeUtils_1.addConditionToType)(
|
1431
|
+
return (0, typeUtils_1.addConditionToType)(types_1.ClassType.cloneAsInstance(classType), subtype.condition);
|
1433
1432
|
}
|
1434
1433
|
return undefined;
|
1435
1434
|
}
|
1436
1435
|
else {
|
1437
1436
|
// If the class if marked final and it matches, then
|
1438
1437
|
// we can eliminate it in the negative case.
|
1439
|
-
if (matches &&
|
1438
|
+
if (matches && types_1.ClassType.isFinal(subtype)) {
|
1440
1439
|
return undefined;
|
1441
1440
|
}
|
1442
1441
|
// We can't eliminate the subtype in the negative
|
@@ -1445,11 +1444,11 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1445
1444
|
return subtype;
|
1446
1445
|
}
|
1447
1446
|
}
|
1448
|
-
else if ((0,
|
1447
|
+
else if ((0, types_1.isNoneInstance)(subtype)) {
|
1449
1448
|
return isPositiveTest ? undefined : subtype;
|
1450
1449
|
}
|
1451
|
-
else if ((0,
|
1452
|
-
return isPositiveTest ?
|
1450
|
+
else if ((0, types_1.isAnyOrUnknown)(subtype)) {
|
1451
|
+
return isPositiveTest ? types_1.ClassType.cloneAsInstance(classType) : subtype;
|
1453
1452
|
}
|
1454
1453
|
return unexpandedSubtype;
|
1455
1454
|
});
|
@@ -1460,9 +1459,9 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1460
1459
|
function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, isPositiveTest, isIsOperator) {
|
1461
1460
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1462
1461
|
subtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
1463
|
-
if ((0,
|
1462
|
+
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isSameGenericClass(literalType, subtype)) {
|
1464
1463
|
if (subtype.literalValue !== undefined) {
|
1465
|
-
const literalValueMatches =
|
1464
|
+
const literalValueMatches = types_1.ClassType.isLiteralValueSame(subtype, literalType);
|
1466
1465
|
if ((literalValueMatches && !isPositiveTest) || (!literalValueMatches && isPositiveTest)) {
|
1467
1466
|
return undefined;
|
1468
1467
|
}
|
@@ -1476,12 +1475,12 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1476
1475
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1477
1476
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, subtype);
|
1478
1477
|
if (allLiteralTypes && allLiteralTypes.length > 0) {
|
1479
|
-
return (0,
|
1478
|
+
return (0, types_1.combineTypes)(allLiteralTypes.filter((type) => !types_1.ClassType.isLiteralValueSame(type, literalType)));
|
1480
1479
|
}
|
1481
1480
|
}
|
1482
1481
|
}
|
1483
1482
|
else if (isPositiveTest) {
|
1484
|
-
if (isIsOperator || (0,
|
1483
|
+
if (isIsOperator || (0, types_1.isNoneInstance)(subtype)) {
|
1485
1484
|
return undefined;
|
1486
1485
|
}
|
1487
1486
|
}
|
@@ -1489,22 +1488,22 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1489
1488
|
});
|
1490
1489
|
}
|
1491
1490
|
function enumerateLiteralsForType(evaluator, type) {
|
1492
|
-
if (
|
1491
|
+
if (types_1.ClassType.isBuiltIn(type, 'bool')) {
|
1493
1492
|
// Booleans have only two types: True and False.
|
1494
1493
|
return [
|
1495
|
-
|
1496
|
-
|
1494
|
+
types_1.ClassType.cloneWithLiteral(type, /* value */ true),
|
1495
|
+
types_1.ClassType.cloneWithLiteral(type, /* value */ false),
|
1497
1496
|
];
|
1498
1497
|
}
|
1499
|
-
if (
|
1498
|
+
if (types_1.ClassType.isEnumClass(type)) {
|
1500
1499
|
// Enumerate all of the values in this enumeration.
|
1501
1500
|
const enumList = [];
|
1502
1501
|
const fields = type.details.fields;
|
1503
1502
|
fields.forEach((symbol) => {
|
1504
1503
|
if (!symbol.isIgnoredForProtocolMatch()) {
|
1505
1504
|
const symbolType = evaluator.getEffectiveTypeOfSymbol(symbol);
|
1506
|
-
if ((0,
|
1507
|
-
|
1505
|
+
if ((0, types_1.isClassInstance)(symbolType) &&
|
1506
|
+
types_1.ClassType.isSameGenericClass(type, symbolType) &&
|
1508
1507
|
symbolType.literalValue !== undefined) {
|
1509
1508
|
enumList.push(symbolType);
|
1510
1509
|
}
|
@@ -1531,7 +1530,7 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1531
1530
|
return isPositiveTest ? undefined : subtype;
|
1532
1531
|
}
|
1533
1532
|
case 7 /* Class */: {
|
1534
|
-
if (
|
1533
|
+
if (types_1.TypeBase.isInstantiable(subtype)) {
|
1535
1534
|
return isPositiveTest ? subtype : undefined;
|
1536
1535
|
}
|
1537
1536
|
// See if the object is callable.
|
@@ -1546,24 +1545,24 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1546
1545
|
// new intersection type.
|
1547
1546
|
const className = `<callable subtype of ${subtype.details.name}>`;
|
1548
1547
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
1549
|
-
let newClassType =
|
1548
|
+
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.filePath, 0 /* None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
1550
1549
|
/* declaredMetaclass */ undefined, subtype.details.effectiveMetaclass, subtype.details.docString);
|
1551
|
-
newClassType.details.baseClasses = [
|
1550
|
+
newClassType.details.baseClasses = [types_1.ClassType.cloneAsInstantiable(subtype)];
|
1552
1551
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
1553
1552
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, subtype.condition);
|
1554
1553
|
// Add a __call__ method to the new class.
|
1555
|
-
const callMethod =
|
1554
|
+
const callMethod = types_1.FunctionType.createSynthesizedInstance('__call__');
|
1556
1555
|
const selfParam = {
|
1557
1556
|
category: 0 /* Simple */,
|
1558
1557
|
name: 'self',
|
1559
|
-
type:
|
1558
|
+
type: types_1.ClassType.cloneAsInstance(newClassType),
|
1560
1559
|
hasDeclaredType: true,
|
1561
1560
|
};
|
1562
|
-
|
1563
|
-
|
1564
|
-
callMethod.details.declaredReturnType =
|
1561
|
+
types_1.FunctionType.addParameter(callMethod, selfParam);
|
1562
|
+
types_1.FunctionType.addDefaultParameters(callMethod);
|
1563
|
+
callMethod.details.declaredReturnType = types_1.UnknownType.create();
|
1565
1564
|
newClassType.details.fields.set('__call__', symbol_1.Symbol.createWithType(4 /* ClassMember */, callMethod));
|
1566
|
-
return
|
1565
|
+
return types_1.ClassType.cloneAsInstance(newClassType);
|
1567
1566
|
}
|
1568
1567
|
return undefined;
|
1569
1568
|
}
|