@zzzen/pyright-internal 1.2.0-dev.20240707 → 1.2.0-dev.20240714
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/analyzerNodeInfo.js +74 -46
- package/dist/analyzer/analyzerNodeInfo.js.map +1 -1
- package/dist/analyzer/binder.js +506 -512
- package/dist/analyzer/binder.js.map +1 -1
- package/dist/analyzer/checker.js +649 -634
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.d.ts +5 -3
- package/dist/analyzer/codeFlowEngine.js +87 -101
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/codeFlowTypes.js +30 -28
- package/dist/analyzer/codeFlowTypes.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +68 -64
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructorTransform.js +22 -22
- package/dist/analyzer/constructorTransform.js.map +1 -1
- package/dist/analyzer/constructors.js +51 -48
- package/dist/analyzer/constructors.js.map +1 -1
- package/dist/analyzer/dataClasses.js +109 -175
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/declarationUtils.js +14 -15
- package/dist/analyzer/declarationUtils.js.map +1 -1
- package/dist/analyzer/decorators.js +77 -77
- package/dist/analyzer/decorators.js.map +1 -1
- package/dist/analyzer/enums.js +70 -56
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/functionTransform.js +8 -18
- package/dist/analyzer/functionTransform.js.map +1 -1
- package/dist/analyzer/importResolver.d.ts +2 -1
- package/dist/analyzer/importResolver.js +40 -29
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/importStatementUtils.js +27 -27
- package/dist/analyzer/importStatementUtils.js.map +1 -1
- package/dist/analyzer/namedTuples.js +33 -59
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.js +66 -66
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/packageTypeVerifier.js +66 -62
- package/dist/analyzer/packageTypeVerifier.js.map +1 -1
- package/dist/analyzer/parameterUtils.d.ts +4 -4
- package/dist/analyzer/parameterUtils.js +28 -46
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/parseTreeUtils.js +268 -269
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/parseTreeWalker.js +76 -76
- package/dist/analyzer/parseTreeWalker.js.map +1 -1
- package/dist/analyzer/patternMatching.js +176 -143
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.js +1 -1
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/properties.js +78 -147
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.js +21 -21
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/sourceFile.js +3 -3
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceMapper.js +20 -19
- package/dist/analyzer/sourceMapper.js.map +1 -1
- package/dist/analyzer/staticExpressions.js +83 -84
- package/dist/analyzer/staticExpressions.js.map +1 -1
- package/dist/analyzer/testWalker.js +2 -2
- package/dist/analyzer/testWalker.js.map +1 -1
- package/dist/analyzer/tracePrinter.js +20 -20
- package/dist/analyzer/tracePrinter.js.map +1 -1
- package/dist/analyzer/typeDocStringUtils.js +25 -25
- package/dist/analyzer/typeDocStringUtils.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +1884 -1937
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +6 -5
- package/dist/analyzer/typeEvaluatorTypes.js +4 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +211 -211
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.js +118 -115
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeStubWriter.js +104 -103
- package/dist/analyzer/typeStubWriter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +14 -3
- package/dist/analyzer/typeUtils.js +432 -402
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.js +8 -8
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typeWalker.js +13 -10
- package/dist/analyzer/typeWalker.js.map +1 -1
- package/dist/analyzer/typedDicts.js +96 -198
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +144 -102
- package/dist/analyzer/types.js +662 -494
- package/dist/analyzer/types.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +33 -33
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/fileSystem.d.ts +1 -0
- package/dist/common/fileSystem.js.map +1 -1
- package/dist/common/fullAccessHost.js +6 -4
- package/dist/common/fullAccessHost.js.map +1 -1
- package/dist/common/realFileSystem.d.ts +1 -0
- package/dist/common/realFileSystem.js +4 -0
- package/dist/common/realFileSystem.js.map +1 -1
- package/dist/common/textEditTracker.js +14 -14
- package/dist/common/textEditTracker.js.map +1 -1
- package/dist/languageService/autoImporter.js +10 -10
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.js +23 -23
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.js +153 -151
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/definitionProvider.d.ts +1 -1
- package/dist/languageService/definitionProvider.js +3 -1
- package/dist/languageService/definitionProvider.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.js +19 -19
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/hoverProvider.js +25 -24
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/importSorter.js +8 -8
- package/dist/languageService/importSorter.js.map +1 -1
- package/dist/languageService/referencesProvider.js +8 -8
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/signatureHelpProvider.d.ts +1 -1
- package/dist/languageService/signatureHelpProvider.js +6 -6
- package/dist/languageService/signatureHelpProvider.js.map +1 -1
- package/dist/languageService/symbolIndexer.js +3 -3
- package/dist/languageService/symbolIndexer.js.map +1 -1
- package/dist/languageService/tooltipUtils.js +13 -13
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/parser/parseNodes.d.ts +468 -401
- package/dist/parser/parseNodes.js +626 -355
- package/dist/parser/parseNodes.js.map +1 -1
- package/dist/parser/parser.d.ts +1 -0
- package/dist/parser/parser.js +316 -294
- package/dist/parser/parser.js.map +1 -1
- package/dist/tests/classDeclaration.test.js +14 -14
- package/dist/tests/classDeclaration.test.js.map +1 -1
- package/dist/tests/harness/vfs/filesystem.d.ts +3 -2
- package/dist/tests/harness/vfs/filesystem.js +6 -2
- package/dist/tests/harness/vfs/filesystem.js.map +1 -1
- package/dist/tests/importResolver.test.js +4 -3
- package/dist/tests/importResolver.test.js.map +1 -1
- package/dist/tests/parseTreeUtils.test.js +5 -5
- package/dist/tests/parseTreeUtils.test.js.map +1 -1
- package/dist/tests/parser.test.js +8 -8
- package/dist/tests/parser.test.js.map +1 -1
- package/dist/tests/sourceMapperUtils.test.js +7 -7
- package/dist/tests/sourceMapperUtils.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +8 -3
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typePrinter.test.js +14 -39
- package/dist/tests/typePrinter.test.js.map +1 -1
- package/package.json +1 -1
@@ -59,27 +59,27 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
59
59
|
return getTypeNarrowingCallbackForAssignmentExpression(evaluator, reference, testExpression, isPositiveTest, recursionCount);
|
60
60
|
}
|
61
61
|
if (testExpression.nodeType === 7 /* ParseNodeType.BinaryOperation */) {
|
62
|
-
const isOrIsNotOperator = testExpression.operator === 39 /* OperatorType.Is */ || testExpression.operator === 40 /* OperatorType.IsNot */;
|
63
|
-
const equalsOrNotEqualsOperator = testExpression.operator === 12 /* OperatorType.Equals */ || testExpression.operator === 28 /* OperatorType.NotEquals */;
|
62
|
+
const isOrIsNotOperator = testExpression.d.operator === 39 /* OperatorType.Is */ || testExpression.d.operator === 40 /* OperatorType.IsNot */;
|
63
|
+
const equalsOrNotEqualsOperator = testExpression.d.operator === 12 /* OperatorType.Equals */ || testExpression.d.operator === 28 /* OperatorType.NotEquals */;
|
64
64
|
const comparisonOperator = equalsOrNotEqualsOperator ||
|
65
|
-
testExpression.operator === 20 /* OperatorType.LessThan */ ||
|
66
|
-
testExpression.operator === 21 /* OperatorType.LessThanOrEqual */ ||
|
67
|
-
testExpression.operator === 15 /* OperatorType.GreaterThan */ ||
|
68
|
-
testExpression.operator === 16 /* OperatorType.GreaterThanOrEqual */;
|
65
|
+
testExpression.d.operator === 20 /* OperatorType.LessThan */ ||
|
66
|
+
testExpression.d.operator === 21 /* OperatorType.LessThanOrEqual */ ||
|
67
|
+
testExpression.d.operator === 15 /* OperatorType.GreaterThan */ ||
|
68
|
+
testExpression.d.operator === 16 /* OperatorType.GreaterThanOrEqual */;
|
69
69
|
if (isOrIsNotOperator || equalsOrNotEqualsOperator) {
|
70
70
|
// Invert the "isPositiveTest" value if this is an "is not" operation.
|
71
|
-
const adjIsPositiveTest = testExpression.operator === 39 /* OperatorType.Is */ || testExpression.operator === 12 /* OperatorType.Equals */
|
71
|
+
const adjIsPositiveTest = testExpression.d.operator === 39 /* OperatorType.Is */ || testExpression.d.operator === 12 /* OperatorType.Equals */
|
72
72
|
? isPositiveTest
|
73
73
|
: !isPositiveTest;
|
74
74
|
// Look for "X is None", "X is not None", "X == None", and "X != None".
|
75
75
|
// These are commonly-used patterns used in control flow.
|
76
|
-
if (testExpression.
|
77
|
-
testExpression.
|
76
|
+
if (testExpression.d.rightExpr.nodeType === 14 /* ParseNodeType.Constant */ &&
|
77
|
+
testExpression.d.rightExpr.d.constType === 26 /* KeywordType.None */) {
|
78
78
|
// Allow the LHS to be either a simple expression or an assignment
|
79
79
|
// expression that assigns to a simple name.
|
80
|
-
let leftExpression = testExpression.
|
80
|
+
let leftExpression = testExpression.d.leftExpr;
|
81
81
|
if (leftExpression.nodeType === 4 /* ParseNodeType.AssignmentExpression */) {
|
82
|
-
leftExpression = leftExpression.name;
|
82
|
+
leftExpression = leftExpression.d.name;
|
83
83
|
}
|
84
84
|
if (ParseTreeUtils.isMatchingExpression(reference, leftExpression)) {
|
85
85
|
return (type) => {
|
@@ -87,15 +87,15 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
87
87
|
};
|
88
88
|
}
|
89
89
|
if (leftExpression.nodeType === 27 /* ParseNodeType.Index */ &&
|
90
|
-
ParseTreeUtils.isMatchingExpression(reference, leftExpression.
|
91
|
-
leftExpression.items.length === 1 &&
|
92
|
-
!leftExpression.trailingComma &&
|
93
|
-
leftExpression.items[0].
|
94
|
-
!leftExpression.items[0].name &&
|
95
|
-
leftExpression.items[0].
|
96
|
-
leftExpression.items[0].
|
97
|
-
!leftExpression.items[0].
|
98
|
-
const indexValue = leftExpression.items[0].
|
90
|
+
ParseTreeUtils.isMatchingExpression(reference, leftExpression.d.leftExpr) &&
|
91
|
+
leftExpression.d.items.length === 1 &&
|
92
|
+
!leftExpression.d.trailingComma &&
|
93
|
+
leftExpression.d.items[0].d.argCategory === 0 /* ArgumentCategory.Simple */ &&
|
94
|
+
!leftExpression.d.items[0].d.name &&
|
95
|
+
leftExpression.d.items[0].d.valueExpr.nodeType === 40 /* ParseNodeType.Number */ &&
|
96
|
+
leftExpression.d.items[0].d.valueExpr.d.isInteger &&
|
97
|
+
!leftExpression.d.items[0].d.valueExpr.d.isImaginary) {
|
98
|
+
const indexValue = leftExpression.d.items[0].d.valueExpr.d.value;
|
99
99
|
if (typeof indexValue === 'number') {
|
100
100
|
return (type) => {
|
101
101
|
return {
|
@@ -107,12 +107,12 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
107
107
|
}
|
108
108
|
}
|
109
109
|
// Look for "X is ...", "X is not ...", "X == ...", and "X != ...".
|
110
|
-
if (testExpression.
|
110
|
+
if (testExpression.d.rightExpr.nodeType === 21 /* ParseNodeType.Ellipsis */) {
|
111
111
|
// Allow the LHS to be either a simple expression or an assignment
|
112
112
|
// expression that assigns to a simple name.
|
113
|
-
let leftExpression = testExpression.
|
113
|
+
let leftExpression = testExpression.d.leftExpr;
|
114
114
|
if (leftExpression.nodeType === 4 /* ParseNodeType.AssignmentExpression */) {
|
115
|
-
leftExpression = leftExpression.name;
|
115
|
+
leftExpression = leftExpression.d.name;
|
116
116
|
}
|
117
117
|
if (ParseTreeUtils.isMatchingExpression(reference, leftExpression)) {
|
118
118
|
return (type) => {
|
@@ -124,14 +124,14 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
124
124
|
}
|
125
125
|
}
|
126
126
|
// Look for "type(X) is Y", "type(X) is not Y", "type(X) == Y" or "type(X) != Y".
|
127
|
-
if (testExpression.
|
128
|
-
if (testExpression.
|
129
|
-
testExpression.
|
130
|
-
const arg0Expr = testExpression.
|
127
|
+
if (testExpression.d.leftExpr.nodeType === 9 /* ParseNodeType.Call */) {
|
128
|
+
if (testExpression.d.leftExpr.d.args.length === 1 &&
|
129
|
+
testExpression.d.leftExpr.d.args[0].d.argCategory === 0 /* ArgumentCategory.Simple */) {
|
130
|
+
const arg0Expr = testExpression.d.leftExpr.d.args[0].d.valueExpr;
|
131
131
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
132
|
-
const callType = evaluator.getTypeOfExpression(testExpression.
|
132
|
+
const callType = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */).type;
|
133
133
|
if ((0, types_1.isInstantiableClass)(callType) && types_1.ClassType.isBuiltIn(callType, 'type')) {
|
134
|
-
const classTypeResult = evaluator.getTypeOfExpression(testExpression.
|
134
|
+
const classTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
135
135
|
const classType = evaluator.makeTopLevelTypeVarsConcrete(classTypeResult.type);
|
136
136
|
if ((0, types_1.isInstantiableClass)(classType)) {
|
137
137
|
return (type) => {
|
@@ -146,13 +146,13 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
146
146
|
}
|
147
147
|
}
|
148
148
|
if (isOrIsNotOperator) {
|
149
|
-
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
150
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
149
|
+
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr)) {
|
150
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
151
151
|
const rightType = rightTypeResult.type;
|
152
152
|
// Look for "X is Y" or "X is not Y" where Y is a an enum or bool literal.
|
153
153
|
if ((0, types_1.isClassInstance)(rightType) &&
|
154
154
|
(types_1.ClassType.isEnumClass(rightType) || types_1.ClassType.isBuiltIn(rightType, 'bool')) &&
|
155
|
-
rightType.literalValue !== undefined) {
|
155
|
+
rightType.priv.literalValue !== undefined) {
|
156
156
|
return (type) => {
|
157
157
|
return {
|
158
158
|
type: narrowTypeForLiteralComparison(evaluator, type, rightType, adjIsPositiveTest,
|
@@ -172,17 +172,17 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
172
172
|
}
|
173
173
|
}
|
174
174
|
// Look for X[<literal>] is <literal> or X[<literal>] is not <literal>.
|
175
|
-
if (testExpression.
|
176
|
-
testExpression.
|
177
|
-
!testExpression.
|
178
|
-
testExpression.
|
179
|
-
ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
180
|
-
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.
|
175
|
+
if (testExpression.d.leftExpr.nodeType === 27 /* ParseNodeType.Index */ &&
|
176
|
+
testExpression.d.leftExpr.d.items.length === 1 &&
|
177
|
+
!testExpression.d.leftExpr.d.trailingComma &&
|
178
|
+
testExpression.d.leftExpr.d.items[0].d.argCategory === 0 /* ArgumentCategory.Simple */ &&
|
179
|
+
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr)) {
|
180
|
+
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.items[0].d.valueExpr);
|
181
181
|
const indexType = indexTypeResult.type;
|
182
182
|
if ((0, types_1.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
183
183
|
if (types_1.ClassType.isBuiltIn(indexType, 'str')) {
|
184
|
-
const rightType = evaluator.getTypeOfExpression(testExpression.
|
185
|
-
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
184
|
+
const rightType = evaluator.getTypeOfExpression(testExpression.d.rightExpr).type;
|
185
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.priv.literalValue !== undefined) {
|
186
186
|
return (type) => {
|
187
187
|
return {
|
188
188
|
type: narrowTypeForDiscriminatedDictEntryComparison(evaluator, type, indexType, rightType, adjIsPositiveTest),
|
@@ -192,15 +192,15 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
192
192
|
}
|
193
193
|
}
|
194
194
|
else if (types_1.ClassType.isBuiltIn(indexType, 'int')) {
|
195
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
195
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
196
196
|
const rightType = rightTypeResult.type;
|
197
|
-
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
197
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.priv.literalValue !== undefined) {
|
198
198
|
let canNarrow = false;
|
199
199
|
// Narrowing can be applied only for bool or enum literals.
|
200
200
|
if (types_1.ClassType.isBuiltIn(rightType, 'bool')) {
|
201
201
|
canNarrow = true;
|
202
202
|
}
|
203
|
-
else if (rightType.literalValue instanceof types_1.EnumLiteral) {
|
203
|
+
else if (rightType.priv.literalValue instanceof types_1.EnumLiteral) {
|
204
204
|
canNarrow = true;
|
205
205
|
}
|
206
206
|
if (canNarrow) {
|
@@ -218,16 +218,16 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
218
218
|
}
|
219
219
|
if (equalsOrNotEqualsOperator) {
|
220
220
|
// Look for X == <literal> or X != <literal>
|
221
|
-
const adjIsPositiveTest = testExpression.operator === 12 /* OperatorType.Equals */ ? isPositiveTest : !isPositiveTest;
|
222
|
-
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
221
|
+
const adjIsPositiveTest = testExpression.d.operator === 12 /* OperatorType.Equals */ ? isPositiveTest : !isPositiveTest;
|
222
|
+
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr)) {
|
223
223
|
// Use speculative mode here to avoid polluting the type cache. This is
|
224
224
|
// important in cases where evaluation of the right expression creates
|
225
225
|
// a false dependency on another variable.
|
226
|
-
const rightTypeResult = evaluator.useSpeculativeMode(testExpression.
|
227
|
-
return evaluator.getTypeOfExpression(testExpression.
|
226
|
+
const rightTypeResult = evaluator.useSpeculativeMode(testExpression.d.rightExpr, () => {
|
227
|
+
return evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
228
228
|
});
|
229
229
|
const rightType = rightTypeResult.type;
|
230
|
-
if ((0, types_1.isClassInstance)(rightType) && rightType.literalValue !== undefined) {
|
230
|
+
if ((0, types_1.isClassInstance)(rightType) && rightType.priv.literalValue !== undefined) {
|
231
231
|
return (type) => {
|
232
232
|
return {
|
233
233
|
type: narrowTypeForLiteralComparison(evaluator, type, rightType, adjIsPositiveTest,
|
@@ -238,16 +238,16 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
238
238
|
}
|
239
239
|
}
|
240
240
|
// Look for X[<literal>] == <literal> or X[<literal>] != <literal>
|
241
|
-
if (testExpression.
|
242
|
-
testExpression.
|
243
|
-
!testExpression.
|
244
|
-
testExpression.
|
245
|
-
ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
246
|
-
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.
|
241
|
+
if (testExpression.d.leftExpr.nodeType === 27 /* ParseNodeType.Index */ &&
|
242
|
+
testExpression.d.leftExpr.d.items.length === 1 &&
|
243
|
+
!testExpression.d.leftExpr.d.trailingComma &&
|
244
|
+
testExpression.d.leftExpr.d.items[0].d.argCategory === 0 /* ArgumentCategory.Simple */ &&
|
245
|
+
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr)) {
|
246
|
+
const indexTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.items[0].d.valueExpr);
|
247
247
|
const indexType = indexTypeResult.type;
|
248
248
|
if ((0, types_1.isClassInstance)(indexType) && (0, typeUtils_1.isLiteralType)(indexType)) {
|
249
249
|
if (types_1.ClassType.isBuiltIn(indexType, ['str', 'int'])) {
|
250
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
250
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
251
251
|
const rightType = rightTypeResult.type;
|
252
252
|
if ((0, typeUtils_1.isLiteralTypeOrUnion)(rightType)) {
|
253
253
|
return (type) => {
|
@@ -270,16 +270,16 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
270
270
|
}
|
271
271
|
// Look for X.Y == <literal> or X.Y != <literal>
|
272
272
|
if (equalsOrNotEqualsOperator &&
|
273
|
-
testExpression.
|
274
|
-
ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
275
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
273
|
+
testExpression.d.leftExpr.nodeType === 35 /* ParseNodeType.MemberAccess */ &&
|
274
|
+
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr)) {
|
275
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
276
276
|
const rightType = rightTypeResult.type;
|
277
|
-
const memberName = testExpression.
|
277
|
+
const memberName = testExpression.d.leftExpr.d.member;
|
278
278
|
if ((0, types_1.isClassInstance)(rightType)) {
|
279
|
-
if (rightType.literalValue !== undefined || (0, typeUtils_1.isNoneInstance)(rightType)) {
|
279
|
+
if (rightType.priv.literalValue !== undefined || (0, typeUtils_1.isNoneInstance)(rightType)) {
|
280
280
|
return (type) => {
|
281
281
|
return {
|
282
|
-
type: narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, type, memberName.value, rightType, adjIsPositiveTest),
|
282
|
+
type: narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, type, memberName.d.value, rightType, adjIsPositiveTest),
|
283
283
|
isIncomplete: !!rightTypeResult.isIncomplete,
|
284
284
|
};
|
285
285
|
};
|
@@ -288,17 +288,17 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
288
288
|
}
|
289
289
|
// Look for X.Y is <literal> or X.Y is not <literal> where <literal> is
|
290
290
|
// an enum or bool literal
|
291
|
-
if (testExpression.
|
292
|
-
ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
293
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
291
|
+
if (testExpression.d.leftExpr.nodeType === 35 /* ParseNodeType.MemberAccess */ &&
|
292
|
+
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr)) {
|
293
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
294
294
|
const rightType = rightTypeResult.type;
|
295
|
-
const memberName = testExpression.
|
295
|
+
const memberName = testExpression.d.leftExpr.d.member;
|
296
296
|
if ((0, types_1.isClassInstance)(rightType) &&
|
297
297
|
(types_1.ClassType.isEnumClass(rightType) || types_1.ClassType.isBuiltIn(rightType, 'bool')) &&
|
298
|
-
rightType.literalValue !== undefined) {
|
298
|
+
rightType.priv.literalValue !== undefined) {
|
299
299
|
return (type) => {
|
300
300
|
return {
|
301
|
-
type: narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, type, memberName.value, rightType, adjIsPositiveTest),
|
301
|
+
type: narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, type, memberName.d.value, rightType, adjIsPositiveTest),
|
302
302
|
isIncomplete: !!rightTypeResult.isIncomplete,
|
303
303
|
};
|
304
304
|
};
|
@@ -306,14 +306,14 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
306
306
|
}
|
307
307
|
// Look for X.Y is None or X.Y is not None
|
308
308
|
// These are commonly-used patterns used in control flow.
|
309
|
-
if (testExpression.
|
310
|
-
ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
311
|
-
testExpression.
|
312
|
-
testExpression.
|
313
|
-
const memberName = testExpression.
|
309
|
+
if (testExpression.d.leftExpr.nodeType === 35 /* ParseNodeType.MemberAccess */ &&
|
310
|
+
ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr.d.leftExpr) &&
|
311
|
+
testExpression.d.rightExpr.nodeType === 14 /* ParseNodeType.Constant */ &&
|
312
|
+
testExpression.d.rightExpr.d.constType === 26 /* KeywordType.None */) {
|
313
|
+
const memberName = testExpression.d.leftExpr.d.member;
|
314
314
|
return (type) => {
|
315
315
|
return {
|
316
|
-
type: narrowTypeForDiscriminatedFieldNoneComparison(evaluator, type, memberName.value, adjIsPositiveTest),
|
316
|
+
type: narrowTypeForDiscriminatedFieldNoneComparison(evaluator, type, memberName.d.value, adjIsPositiveTest),
|
317
317
|
isIncomplete: false,
|
318
318
|
};
|
319
319
|
};
|
@@ -321,32 +321,32 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
321
321
|
}
|
322
322
|
// Look for len(x) == <literal>, len(x) != <literal>, len(x) < <literal>, etc.
|
323
323
|
if (comparisonOperator &&
|
324
|
-
testExpression.
|
325
|
-
testExpression.
|
326
|
-
const arg0Expr = testExpression.
|
324
|
+
testExpression.d.leftExpr.nodeType === 9 /* ParseNodeType.Call */ &&
|
325
|
+
testExpression.d.leftExpr.d.args.length === 1) {
|
326
|
+
const arg0Expr = testExpression.d.leftExpr.d.args[0].d.valueExpr;
|
327
327
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
328
|
-
const callTypeResult = evaluator.getTypeOfExpression(testExpression.
|
328
|
+
const callTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
329
329
|
const callType = callTypeResult.type;
|
330
|
-
if ((0, types_1.isFunction)(callType) && callType.
|
331
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
330
|
+
if ((0, types_1.isFunction)(callType) && callType.shared.fullName === 'builtins.len') {
|
331
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
332
332
|
const rightType = rightTypeResult.type;
|
333
333
|
if ((0, types_1.isClassInstance)(rightType) &&
|
334
|
-
typeof rightType.literalValue === 'number' &&
|
335
|
-
rightType.literalValue >= 0) {
|
336
|
-
let tupleLength = rightType.literalValue;
|
334
|
+
typeof rightType.priv.literalValue === 'number' &&
|
335
|
+
rightType.priv.literalValue >= 0) {
|
336
|
+
let tupleLength = rightType.priv.literalValue;
|
337
337
|
// We'll treat <, <= and == as positive tests with >=, > and != as
|
338
338
|
// their negative counterparts.
|
339
|
-
const isLessOrEqual = testExpression.operator === 12 /* OperatorType.Equals */ ||
|
340
|
-
testExpression.operator === 20 /* OperatorType.LessThan */ ||
|
341
|
-
testExpression.operator === 21 /* OperatorType.LessThanOrEqual */;
|
339
|
+
const isLessOrEqual = testExpression.d.operator === 12 /* OperatorType.Equals */ ||
|
340
|
+
testExpression.d.operator === 20 /* OperatorType.LessThan */ ||
|
341
|
+
testExpression.d.operator === 21 /* OperatorType.LessThanOrEqual */;
|
342
342
|
const adjIsPositiveTest = isLessOrEqual ? isPositiveTest : !isPositiveTest;
|
343
343
|
// For <= (or its negative counterpart >), adjust the tuple length by 1.
|
344
|
-
if (testExpression.operator === 21 /* OperatorType.LessThanOrEqual */ ||
|
345
|
-
testExpression.operator === 15 /* OperatorType.GreaterThan */) {
|
344
|
+
if (testExpression.d.operator === 21 /* OperatorType.LessThanOrEqual */ ||
|
345
|
+
testExpression.d.operator === 15 /* OperatorType.GreaterThan */) {
|
346
346
|
tupleLength++;
|
347
347
|
}
|
348
|
-
const isEqualityCheck = testExpression.operator === 12 /* OperatorType.Equals */ ||
|
349
|
-
testExpression.operator === 28 /* OperatorType.NotEquals */;
|
348
|
+
const isEqualityCheck = testExpression.d.operator === 12 /* OperatorType.Equals */ ||
|
349
|
+
testExpression.d.operator === 28 /* OperatorType.NotEquals */;
|
350
350
|
return (type) => {
|
351
351
|
return {
|
352
352
|
type: narrowTypeForTupleLength(evaluator, type, tupleLength, adjIsPositiveTest, !isEqualityCheck),
|
@@ -357,12 +357,12 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
357
357
|
}
|
358
358
|
}
|
359
359
|
}
|
360
|
-
if (testExpression.operator === 41 /* OperatorType.In */ || testExpression.operator === 42 /* OperatorType.NotIn */) {
|
360
|
+
if (testExpression.d.operator === 41 /* OperatorType.In */ || testExpression.d.operator === 42 /* OperatorType.NotIn */) {
|
361
361
|
// Look for "x in y" or "x not in y" where y is one of several built-in types.
|
362
|
-
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
363
|
-
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.
|
362
|
+
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.d.leftExpr)) {
|
363
|
+
const rightTypeResult = evaluator.getTypeOfExpression(testExpression.d.rightExpr);
|
364
364
|
const rightType = rightTypeResult.type;
|
365
|
-
const adjIsPositiveTest = testExpression.operator === 41 /* OperatorType.In */ ? isPositiveTest : !isPositiveTest;
|
365
|
+
const adjIsPositiveTest = testExpression.d.operator === 41 /* OperatorType.In */ ? isPositiveTest : !isPositiveTest;
|
366
366
|
return (type) => {
|
367
367
|
return {
|
368
368
|
type: narrowTypeForContainerType(evaluator, type, rightType, adjIsPositiveTest),
|
@@ -370,13 +370,13 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
370
370
|
};
|
371
371
|
};
|
372
372
|
}
|
373
|
-
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
373
|
+
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.d.rightExpr)) {
|
374
374
|
// Look for <string literal> in y where y is a union that contains
|
375
375
|
// one or more TypedDicts.
|
376
|
-
const leftTypeResult = evaluator.getTypeOfExpression(testExpression.
|
376
|
+
const leftTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr);
|
377
377
|
const leftType = leftTypeResult.type;
|
378
378
|
if ((0, types_1.isClassInstance)(leftType) && types_1.ClassType.isBuiltIn(leftType, 'str') && (0, typeUtils_1.isLiteralType)(leftType)) {
|
379
|
-
const adjIsPositiveTest = testExpression.operator === 41 /* OperatorType.In */ ? isPositiveTest : !isPositiveTest;
|
379
|
+
const adjIsPositiveTest = testExpression.d.operator === 41 /* OperatorType.In */ ? isPositiveTest : !isPositiveTest;
|
380
380
|
return (type) => {
|
381
381
|
return {
|
382
382
|
type: narrowTypeForTypedDictKey(evaluator, type, types_1.ClassType.cloneAsInstantiable(leftType), adjIsPositiveTest),
|
@@ -389,24 +389,18 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
389
389
|
}
|
390
390
|
if (testExpression.nodeType === 9 /* ParseNodeType.Call */) {
|
391
391
|
// Look for "isinstance(X, Y)" or "issubclass(X, Y)".
|
392
|
-
if (testExpression.
|
392
|
+
if (testExpression.d.args.length === 2) {
|
393
393
|
// Make sure the first parameter is a supported expression type
|
394
394
|
// and the second parameter is a valid class type or a tuple
|
395
395
|
// of valid class types.
|
396
|
-
const arg0Expr = testExpression.
|
397
|
-
const arg1Expr = testExpression.
|
396
|
+
const arg0Expr = testExpression.d.args[0].d.valueExpr;
|
397
|
+
const arg1Expr = testExpression.d.args[1].d.valueExpr;
|
398
398
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
399
|
-
const callTypeResult = evaluator.getTypeOfExpression(testExpression.
|
399
|
+
const callTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
400
400
|
const callType = callTypeResult.type;
|
401
|
-
if ((0, types_1.isFunction)(callType) &&
|
402
|
-
|
403
|
-
const
|
404
|
-
const arg1TypeResult = evaluator.getTypeOfExpression(arg1Expr, 512 /* EvalFlags.AllowMissingTypeArgs */ |
|
405
|
-
8 /* EvalFlags.StrLiteralAsType */ |
|
406
|
-
32 /* EvalFlags.NoParamSpec */ |
|
407
|
-
64 /* EvalFlags.NoTypeVarTuple */ |
|
408
|
-
16 /* EvalFlags.NoFinal */ |
|
409
|
-
2 /* EvalFlags.NoSpecialize */);
|
401
|
+
if ((0, types_1.isFunction)(callType) && types_1.FunctionType.isBuiltIn(callType, ['isinstance', 'issubclass'])) {
|
402
|
+
const isInstanceCheck = types_1.FunctionType.isBuiltIn(callType, 'isinstance');
|
403
|
+
const arg1TypeResult = evaluator.getTypeOfExpression(arg1Expr, 536871546 /* EvalFlags.IsInstanceArgDefaults */);
|
410
404
|
const arg1Type = arg1TypeResult.type;
|
411
405
|
const classTypeList = getIsInstanceClassTypes(evaluator, arg1Type);
|
412
406
|
const isIncomplete = !!callTypeResult.isIncomplete || !!arg1TypeResult.isIncomplete;
|
@@ -433,12 +427,12 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
433
427
|
}
|
434
428
|
}
|
435
429
|
// Look for "callable(X)"
|
436
|
-
if (testExpression.
|
437
|
-
const arg0Expr = testExpression.
|
430
|
+
if (testExpression.d.args.length === 1) {
|
431
|
+
const arg0Expr = testExpression.d.args[0].d.valueExpr;
|
438
432
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
439
|
-
const callTypeResult = evaluator.getTypeOfExpression(testExpression.
|
433
|
+
const callTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
440
434
|
const callType = callTypeResult.type;
|
441
|
-
if ((0, types_1.isFunction)(callType) &&
|
435
|
+
if ((0, types_1.isFunction)(callType) && types_1.FunctionType.isBuiltIn(callType, 'callable')) {
|
442
436
|
return (type) => {
|
443
437
|
let narrowedType = narrowTypeForCallable(evaluator, type, isPositiveTest, testExpression,
|
444
438
|
/* allowIntersections */ false);
|
@@ -453,9 +447,9 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
453
447
|
}
|
454
448
|
}
|
455
449
|
// Look for "bool(X)"
|
456
|
-
if (testExpression.
|
457
|
-
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.
|
458
|
-
const callTypeResult = evaluator.getTypeOfExpression(testExpression.
|
450
|
+
if (testExpression.d.args.length === 1 && !testExpression.d.args[0].d.name) {
|
451
|
+
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.d.args[0].d.valueExpr)) {
|
452
|
+
const callTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
459
453
|
const callType = callTypeResult.type;
|
460
454
|
if ((0, types_1.isInstantiableClass)(callType) && types_1.ClassType.isBuiltIn(callType, 'bool')) {
|
461
455
|
return (type) => {
|
@@ -468,17 +462,17 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
468
462
|
}
|
469
463
|
}
|
470
464
|
// Look for a TypeGuard function.
|
471
|
-
if (testExpression.
|
472
|
-
const arg0Expr = testExpression.
|
465
|
+
if (testExpression.d.args.length >= 1) {
|
466
|
+
const arg0Expr = testExpression.d.args[0].d.valueExpr;
|
473
467
|
if (ParseTreeUtils.isMatchingExpression(reference, arg0Expr)) {
|
474
468
|
// Does this look like it's a custom type guard function?
|
475
469
|
let isPossiblyTypeGuard = false;
|
476
470
|
const isFunctionReturnTypeGuard = (type) => {
|
477
|
-
return (type.
|
478
|
-
(0, types_1.isClassInstance)(type.
|
479
|
-
types_1.ClassType.isBuiltIn(type.
|
471
|
+
return (type.shared.declaredReturnType &&
|
472
|
+
(0, types_1.isClassInstance)(type.shared.declaredReturnType) &&
|
473
|
+
types_1.ClassType.isBuiltIn(type.shared.declaredReturnType, ['TypeGuard', 'TypeIs']));
|
480
474
|
};
|
481
|
-
const callTypeResult = evaluator.getTypeOfExpression(testExpression.
|
475
|
+
const callTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
482
476
|
const callType = callTypeResult.type;
|
483
477
|
if ((0, types_1.isFunction)(callType) && isFunctionReturnTypeGuard(callType)) {
|
484
478
|
isPossiblyTypeGuard = true;
|
@@ -496,9 +490,9 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
496
490
|
const functionReturnType = functionReturnTypeResult.type;
|
497
491
|
if ((0, types_1.isClassInstance)(functionReturnType) &&
|
498
492
|
types_1.ClassType.isBuiltIn(functionReturnType, 'bool') &&
|
499
|
-
functionReturnType.typeGuardType) {
|
500
|
-
const isStrictTypeGuard = !!functionReturnType.isStrictTypeGuard;
|
501
|
-
const typeGuardType = functionReturnType.typeGuardType;
|
493
|
+
functionReturnType.priv.typeGuardType) {
|
494
|
+
const isStrictTypeGuard = !!functionReturnType.priv.isStrictTypeGuard;
|
495
|
+
const typeGuardType = functionReturnType.priv.typeGuardType;
|
502
496
|
const isIncomplete = !!callTypeResult.isIncomplete || !!functionReturnTypeResult.isIncomplete;
|
503
497
|
return (type) => {
|
504
498
|
return {
|
@@ -529,8 +523,9 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
529
523
|
// by the binder when it creates condition flow nodes, but we can find this
|
530
524
|
// in the case of local variables type narrowing.
|
531
525
|
if (reference.nodeType === 38 /* ParseNodeType.Name */) {
|
532
|
-
if (testExpression.nodeType === 55 /* ParseNodeType.UnaryOperation */ &&
|
533
|
-
|
526
|
+
if (testExpression.nodeType === 55 /* ParseNodeType.UnaryOperation */ &&
|
527
|
+
testExpression.d.operator === 38 /* OperatorType.Not */) {
|
528
|
+
return getTypeNarrowingCallback(evaluator, reference, testExpression.d.expr, !isPositiveTest, recursionCount);
|
534
529
|
}
|
535
530
|
}
|
536
531
|
return undefined;
|
@@ -592,7 +587,7 @@ function getDeclsForLocalVar(evaluator, name, reachableFrom, requireUnique) {
|
|
592
587
|
if ((scope === null || scope === void 0 ? void 0 : scope.type) !== 2 /* ScopeType.Function */ && (scope === null || scope === void 0 ? void 0 : scope.type) !== 4 /* ScopeType.Module */) {
|
593
588
|
return undefined;
|
594
589
|
}
|
595
|
-
const symbol = scope.lookUpSymbol(name.value);
|
590
|
+
const symbol = scope.lookUpSymbol(name.d.value);
|
596
591
|
if (!symbol) {
|
597
592
|
return undefined;
|
598
593
|
}
|
@@ -608,7 +603,7 @@ function getDeclsForLocalVar(evaluator, name, reachableFrom, requireUnique) {
|
|
608
603
|
// "nonlocal" reference), don't consider it a local variable.
|
609
604
|
let prevDeclScope;
|
610
605
|
if (decls.some((decl) => {
|
611
|
-
const nodeToConsider = decl.type === 2 /* DeclarationType.Parameter */ ? decl.node.name : decl.node;
|
606
|
+
const nodeToConsider = decl.type === 2 /* DeclarationType.Parameter */ ? decl.node.d.name : decl.node;
|
612
607
|
const declScopeNode = ParseTreeUtils.getExecutionScopeNode(nodeToConsider);
|
613
608
|
if (prevDeclScope && declScopeNode !== prevDeclScope) {
|
614
609
|
return true;
|
@@ -623,7 +618,7 @@ function getDeclsForLocalVar(evaluator, name, reachableFrom, requireUnique) {
|
|
623
618
|
}
|
624
619
|
function getTypeNarrowingCallbackForAssignmentExpression(evaluator, reference, testExpression, isPositiveTest, recursionCount) {
|
625
620
|
var _a;
|
626
|
-
return ((_a = getTypeNarrowingCallback(evaluator, reference, testExpression.
|
621
|
+
return ((_a = getTypeNarrowingCallback(evaluator, reference, testExpression.d.rightExpr, isPositiveTest, recursionCount)) !== null && _a !== void 0 ? _a : getTypeNarrowingCallback(evaluator, reference, testExpression.d.name, isPositiveTest, recursionCount));
|
627
622
|
}
|
628
623
|
function narrowTypeForUserDefinedTypeGuard(evaluator, type, typeGuardType, isPositiveTest, isStrictTypeGuard, errorNode) {
|
629
624
|
// For non-strict type guards, always narrow to the typeGuardType
|
@@ -660,14 +655,14 @@ function narrowTypeForTruthiness(evaluator, type, isPositiveTest) {
|
|
660
655
|
function narrowTupleTypeForIsNone(evaluator, type, isPositiveTest, indexValue) {
|
661
656
|
return evaluator.mapSubtypesExpandTypeVars(type, /* options */ undefined, (subtype) => {
|
662
657
|
const tupleType = (0, typeUtils_1.getSpecializedTupleType)(subtype);
|
663
|
-
if (!tupleType || (0, typeUtils_1.isUnboundedTupleClass)(tupleType) || !tupleType.tupleTypeArguments) {
|
658
|
+
if (!tupleType || (0, typeUtils_1.isUnboundedTupleClass)(tupleType) || !tupleType.priv.tupleTypeArguments) {
|
664
659
|
return subtype;
|
665
660
|
}
|
666
|
-
const tupleLength = tupleType.tupleTypeArguments.length;
|
661
|
+
const tupleLength = tupleType.priv.tupleTypeArguments.length;
|
667
662
|
if (indexValue < 0 || indexValue >= tupleLength) {
|
668
663
|
return subtype;
|
669
664
|
}
|
670
|
-
const typeOfEntry = evaluator.makeTopLevelTypeVarsConcrete(tupleType.tupleTypeArguments[indexValue].type);
|
665
|
+
const typeOfEntry = evaluator.makeTopLevelTypeVarsConcrete(tupleType.priv.tupleTypeArguments[indexValue].type);
|
671
666
|
if (isPositiveTest) {
|
672
667
|
if (!evaluator.assignType(typeOfEntry, evaluator.getNoneType())) {
|
673
668
|
return undefined;
|
@@ -689,6 +684,7 @@ function narrowTypeForIsNone(evaluator, type, isPositiveTest) {
|
|
689
684
|
let resultIncludesNoneSubtype = false;
|
690
685
|
const result = evaluator.mapSubtypesExpandTypeVars(expandedType,
|
691
686
|
/* options */ undefined, (subtype, unexpandedSubtype) => {
|
687
|
+
var _a;
|
692
688
|
if ((0, types_1.isAnyOrUnknown)(subtype)) {
|
693
689
|
// We need to assume that "Any" is always both None and not None,
|
694
690
|
// so it matches regardless of whether the test is positive or negative.
|
@@ -697,20 +693,20 @@ function narrowTypeForIsNone(evaluator, type, isPositiveTest) {
|
|
697
693
|
// If this is a TypeVar that isn't constrained, use the unexpanded
|
698
694
|
// TypeVar. For all other cases (including constrained TypeVars),
|
699
695
|
// use the expanded subtype.
|
700
|
-
const adjustedSubtype = (0, types_1.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.
|
696
|
+
const adjustedSubtype = (0, types_1.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.shared.constraints.length === 0
|
701
697
|
? unexpandedSubtype
|
702
698
|
: subtype;
|
703
699
|
// See if it's a match for object.
|
704
700
|
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'object')) {
|
705
701
|
resultIncludesNoneSubtype = true;
|
706
702
|
return isPositiveTest
|
707
|
-
? (0, typeUtils_1.addConditionToType)(evaluator.getNoneType(), subtype.condition)
|
703
|
+
? (0, typeUtils_1.addConditionToType)(evaluator.getNoneType(), (_a = subtype.props) === null || _a === void 0 ? void 0 : _a.condition)
|
708
704
|
: adjustedSubtype;
|
709
705
|
}
|
710
706
|
// See if it's a match for None.
|
711
707
|
if ((0, typeUtils_1.isNoneInstance)(subtype) === isPositiveTest) {
|
712
708
|
resultIncludesNoneSubtype = true;
|
713
|
-
if ((0, types_1.isTypeVar)(adjustedSubtype) && adjustedSubtype.
|
709
|
+
if ((0, types_1.isTypeVar)(adjustedSubtype) && adjustedSubtype.shared.isSynthesizedSelf) {
|
714
710
|
return adjustedSubtype;
|
715
711
|
}
|
716
712
|
return subtype;
|
@@ -733,6 +729,7 @@ function narrowTypeForIsEllipsis(evaluator, type, isPositiveTest) {
|
|
733
729
|
return (0, typeUtils_1.transformPossibleRecursiveTypeAlias)(subtype);
|
734
730
|
});
|
735
731
|
return evaluator.mapSubtypesExpandTypeVars(expandedType, /* options */ undefined, (subtype, unexpandedSubtype) => {
|
732
|
+
var _a;
|
736
733
|
if ((0, types_1.isAnyOrUnknown)(subtype)) {
|
737
734
|
// We need to assume that "Any" is always both None and not None,
|
738
735
|
// so it matches regardless of whether the test is positive or negative.
|
@@ -741,12 +738,14 @@ function narrowTypeForIsEllipsis(evaluator, type, isPositiveTest) {
|
|
741
738
|
// If this is a TypeVar that isn't constrained, use the unexpanded
|
742
739
|
// TypeVar. For all other cases (including constrained TypeVars),
|
743
740
|
// use the expanded subtype.
|
744
|
-
const adjustedSubtype = (0, types_1.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.
|
741
|
+
const adjustedSubtype = (0, types_1.isTypeVar)(unexpandedSubtype) && unexpandedSubtype.shared.constraints.length === 0
|
745
742
|
? unexpandedSubtype
|
746
743
|
: subtype;
|
747
744
|
// See if it's a match for object.
|
748
745
|
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, 'object')) {
|
749
|
-
return isPositiveTest
|
746
|
+
return isPositiveTest
|
747
|
+
? (0, typeUtils_1.addConditionToType)(evaluator.getNoneType(), (_a = subtype.props) === null || _a === void 0 ? void 0 : _a.condition)
|
748
|
+
: adjustedSubtype;
|
750
749
|
}
|
751
750
|
const isEllipsis = (0, types_1.isClassInstance)(subtype) && types_1.ClassType.isBuiltIn(subtype, ['EllipsisType', 'ellipsis']);
|
752
751
|
// See if it's a match for "...".
|
@@ -781,9 +780,9 @@ function getIsInstanceClassTypes(evaluator, argType) {
|
|
781
780
|
classTypeList.push(subtype);
|
782
781
|
}
|
783
782
|
else if ((0, types_1.isFunction)(subtype) &&
|
784
|
-
subtype.
|
785
|
-
subtype.
|
786
|
-
subtype.
|
783
|
+
subtype.shared.parameters.length === 2 &&
|
784
|
+
subtype.shared.parameters[0].category === 1 /* ParameterCategory.ArgsList */ &&
|
785
|
+
subtype.shared.parameters[1].category === 2 /* ParameterCategory.KwargsDict */) {
|
787
786
|
classTypeList.push(subtype);
|
788
787
|
}
|
789
788
|
else {
|
@@ -796,8 +795,8 @@ function getIsInstanceClassTypes(evaluator, argType) {
|
|
796
795
|
return;
|
797
796
|
}
|
798
797
|
if ((0, types_1.isClass)(type) && types_1.TypeBase.isInstance(type) && (0, typeUtils_1.isTupleClass)(type)) {
|
799
|
-
if (type.tupleTypeArguments) {
|
800
|
-
type.tupleTypeArguments.forEach((tupleEntry) => {
|
798
|
+
if (type.priv.tupleTypeArguments) {
|
799
|
+
type.priv.tupleTypeArguments.forEach((tupleEntry) => {
|
801
800
|
addClassTypesRecursive(tupleEntry.type, recursionCount + 1);
|
802
801
|
});
|
803
802
|
}
|
@@ -814,13 +813,13 @@ function getIsInstanceClassTypes(evaluator, argType) {
|
|
814
813
|
return foundNonClassType ? undefined : classTypeList;
|
815
814
|
}
|
816
815
|
function isIsinstanceFilterSuperclass(evaluator, varType, concreteVarType, filterType, concreteFilterType, isInstanceCheck) {
|
817
|
-
if ((0, types_1.isTypeVar)(filterType) || concreteFilterType.literalValue !== undefined) {
|
816
|
+
if ((0, types_1.isTypeVar)(filterType) || concreteFilterType.priv.literalValue !== undefined) {
|
818
817
|
return (0, types_1.isTypeSame)((0, typeUtils_1.convertToInstance)(filterType), varType);
|
819
818
|
}
|
820
819
|
// If the filter type represents all possible subclasses
|
821
820
|
// of a type, we can't make any statements about its superclass
|
822
821
|
// relationship with concreteVarType.
|
823
|
-
if (concreteFilterType.includeSubclasses) {
|
822
|
+
if (concreteFilterType.priv.includeSubclasses) {
|
824
823
|
return false;
|
825
824
|
}
|
826
825
|
if (types_1.ClassType.isDerivedFrom(concreteVarType, concreteFilterType)) {
|
@@ -878,6 +877,7 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
878
877
|
// and returns the list of types the varType could be after
|
879
878
|
// applying the filter.
|
880
879
|
const filterClassType = (varType, concreteVarType, conditions, negativeFallbackType) => {
|
880
|
+
var _a, _b, _c;
|
881
881
|
const filteredTypes = [];
|
882
882
|
let foundSuperclass = false;
|
883
883
|
let isClassRelationshipIndeterminate = false;
|
@@ -894,7 +894,7 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
894
894
|
// If the class was implicitly specialized (e.g. because its type
|
895
895
|
// parameters have default values), replace the default type arguments
|
896
896
|
// with Unknown.
|
897
|
-
if (concreteFilterType.typeArguments && !concreteFilterType.isTypeArgumentExplicit) {
|
897
|
+
if (concreteFilterType.priv.typeArguments && !concreteFilterType.priv.isTypeArgumentExplicit) {
|
898
898
|
concreteFilterType = (0, typeUtils_1.specializeWithUnknownTypeArgs)(types_1.ClassType.cloneForSpecialization(concreteFilterType,
|
899
899
|
/* typeArguments */ undefined,
|
900
900
|
/* isTypeArgumentExplicit */ false), evaluator.getTupleClassType());
|
@@ -925,7 +925,7 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
925
925
|
// If the variable type is a subclass of the isinstance filter,
|
926
926
|
// we haven't learned anything new about the variable type.
|
927
927
|
// If the varType is a Self or type[Self], retain the unnarrowedType.
|
928
|
-
if ((0, types_1.isTypeVar)(varType) && varType.
|
928
|
+
if ((0, types_1.isTypeVar)(varType) && varType.shared.isSynthesizedSelf) {
|
929
929
|
filteredTypes.push((0, typeUtils_1.addConditionToType)(varType, conditions));
|
930
930
|
}
|
931
931
|
else {
|
@@ -947,9 +947,9 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
947
947
|
// be passed to isinstance or issubclass.
|
948
948
|
if ((0, types_1.isClass)(filterType)) {
|
949
949
|
if (types_1.ClassType.isSpecialBuiltIn(filterType) ||
|
950
|
-
filterType.
|
951
|
-
if (!filterType.typeArguments ||
|
952
|
-
!filterType.isTypeArgumentExplicit ||
|
950
|
+
filterType.shared.typeParameters.length > 0) {
|
951
|
+
if (!filterType.priv.typeArguments ||
|
952
|
+
!filterType.priv.isTypeArgumentExplicit ||
|
953
953
|
!types_1.ClassType.isSameGenericClass(concreteVarType, filterType)) {
|
954
954
|
const typeVarContext = new typeVarContext_1.TypeVarContext((0, typeUtils_1.getTypeVarScopeId)(filterType));
|
955
955
|
const unspecializedFilterType = types_1.ClassType.cloneForSpecialization(filterType,
|
@@ -977,34 +977,34 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
977
977
|
// be a mix-in class used with the other. In this case, we'll
|
978
978
|
// synthesize a new class type that represents an intersection of
|
979
979
|
// the two types.
|
980
|
-
const className = `<subclass of ${concreteVarType.
|
980
|
+
const className = `<subclass of ${concreteVarType.shared.name} and ${concreteFilterType.shared.name}>`;
|
981
981
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
982
982
|
// The effective metaclass of the intersection is the narrower of the two metaclasses.
|
983
|
-
let effectiveMetaclass = concreteVarType.
|
984
|
-
if (concreteFilterType.
|
983
|
+
let effectiveMetaclass = concreteVarType.shared.effectiveMetaclass;
|
984
|
+
if (concreteFilterType.shared.effectiveMetaclass) {
|
985
985
|
if (!effectiveMetaclass ||
|
986
|
-
evaluator.assignType(effectiveMetaclass, concreteFilterType.
|
987
|
-
effectiveMetaclass = concreteFilterType.
|
986
|
+
evaluator.assignType(effectiveMetaclass, concreteFilterType.shared.effectiveMetaclass)) {
|
987
|
+
effectiveMetaclass = concreteFilterType.shared.effectiveMetaclass;
|
988
988
|
}
|
989
989
|
}
|
990
990
|
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, 0 /* ClassTypeFlags.None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
991
|
-
/* declaredMetaclass */ undefined, effectiveMetaclass, concreteVarType.
|
992
|
-
newClassType.
|
991
|
+
/* declaredMetaclass */ undefined, effectiveMetaclass, concreteVarType.shared.docString);
|
992
|
+
newClassType.shared.baseClasses = [
|
993
993
|
types_1.ClassType.cloneAsInstantiable(concreteVarType),
|
994
994
|
concreteFilterType,
|
995
995
|
];
|
996
996
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
997
|
-
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, concreteFilterType.condition);
|
997
|
+
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, (_a = concreteFilterType.props) === null || _a === void 0 ? void 0 : _a.condition);
|
998
998
|
if ((0, types_1.isTypeVar)(varType) &&
|
999
|
-
!varType.
|
1000
|
-
varType.
|
999
|
+
!varType.shared.isParamSpec &&
|
1000
|
+
varType.shared.constraints.length === 0) {
|
1001
1001
|
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, [
|
1002
1002
|
{ typeVar: varType, constraintIndex: 0 },
|
1003
1003
|
]);
|
1004
1004
|
}
|
1005
1005
|
let newClassInstanceType = types_1.ClassType.cloneAsInstance(newClassType);
|
1006
|
-
if (concreteVarType.condition) {
|
1007
|
-
newClassInstanceType = (0, typeUtils_1.addConditionToType)(newClassInstanceType, concreteVarType.condition);
|
1006
|
+
if ((_b = concreteVarType.props) === null || _b === void 0 ? void 0 : _b.condition) {
|
1007
|
+
newClassInstanceType = (0, typeUtils_1.addConditionToType)(newClassInstanceType, (_c = concreteVarType.props) === null || _c === void 0 ? void 0 : _c.condition);
|
1008
1008
|
}
|
1009
1009
|
// If this is a issubclass check, we do a double conversion from instantiable
|
1010
1010
|
// to instance back to instantiable to make sure that the includeSubclasses flag
|
@@ -1115,14 +1115,14 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
1115
1115
|
for (const filterType of filterTypes) {
|
1116
1116
|
const concreteFilterType = evaluator.makeTopLevelTypeVarsConcrete(filterType);
|
1117
1117
|
if ((0, types_1.isInstantiableClass)(concreteFilterType)) {
|
1118
|
-
const filterMetaclass = concreteFilterType.
|
1118
|
+
const filterMetaclass = concreteFilterType.shared.effectiveMetaclass;
|
1119
1119
|
if (filterMetaclass && (0, types_1.isInstantiableClass)(filterMetaclass)) {
|
1120
1120
|
let isMetaclassOverlap = evaluator.assignType(metaclassType, types_1.ClassType.cloneAsInstance(filterMetaclass));
|
1121
1121
|
// Handle the special case where the metaclass for the filter is type.
|
1122
1122
|
// This will normally be treated as type[Any], which is compatible with
|
1123
1123
|
// any metaclass, but we specifically want to treat type as the class
|
1124
1124
|
// type[object] in this case.
|
1125
|
-
if (types_1.ClassType.isBuiltIn(filterMetaclass, 'type') && !filterMetaclass.isTypeArgumentExplicit) {
|
1125
|
+
if (types_1.ClassType.isBuiltIn(filterMetaclass, 'type') && !filterMetaclass.priv.isTypeArgumentExplicit) {
|
1126
1126
|
if (!types_1.ClassType.isBuiltIn(metaclassType, 'type')) {
|
1127
1127
|
isMetaclassOverlap = false;
|
1128
1128
|
}
|
@@ -1132,7 +1132,8 @@ function narrowTypeForIsInstanceInternal(evaluator, type, filterTypes, isInstanc
|
|
1132
1132
|
filteredTypes.push(filterType);
|
1133
1133
|
foundPositiveMatch = true;
|
1134
1134
|
}
|
1135
|
-
else if (!(0, types_1.isTypeSame)(metaclassType, filterMetaclass) ||
|
1135
|
+
else if (!(0, types_1.isTypeSame)(metaclassType, filterMetaclass) ||
|
1136
|
+
filterMetaclass.priv.includeSubclasses) {
|
1136
1137
|
filteredTypes.push(metaclassType);
|
1137
1138
|
isMatchIndeterminate = true;
|
1138
1139
|
}
|
@@ -1306,23 +1307,23 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1306
1307
|
// If it's not a tuple, we can't narrow it.
|
1307
1308
|
if (!(0, types_1.isClassInstance)(concreteSubtype) ||
|
1308
1309
|
!(0, typeUtils_1.isTupleClass)(concreteSubtype) ||
|
1309
|
-
!concreteSubtype.tupleTypeArguments) {
|
1310
|
+
!concreteSubtype.priv.tupleTypeArguments) {
|
1310
1311
|
return subtype;
|
1311
1312
|
}
|
1312
1313
|
// If the tuple contains a variadic TypeVar, we can't narrow it.
|
1313
|
-
if (concreteSubtype.tupleTypeArguments.some((typeArg) => (0, types_1.isUnpackedVariadicTypeVar)(typeArg.type))) {
|
1314
|
+
if (concreteSubtype.priv.tupleTypeArguments.some((typeArg) => (0, types_1.isUnpackedVariadicTypeVar)(typeArg.type))) {
|
1314
1315
|
return subtype;
|
1315
1316
|
}
|
1316
1317
|
// If the tuple contains no unbounded elements, then we know its length exactly.
|
1317
|
-
if (!concreteSubtype.tupleTypeArguments.some((typeArg) => typeArg.isUnbounded)) {
|
1318
|
+
if (!concreteSubtype.priv.tupleTypeArguments.some((typeArg) => typeArg.isUnbounded)) {
|
1318
1319
|
const tupleLengthMatches = isLessThanCheck
|
1319
|
-
? concreteSubtype.tupleTypeArguments.length < lengthValue
|
1320
|
-
: concreteSubtype.tupleTypeArguments.length === lengthValue;
|
1320
|
+
? concreteSubtype.priv.tupleTypeArguments.length < lengthValue
|
1321
|
+
: concreteSubtype.priv.tupleTypeArguments.length === lengthValue;
|
1321
1322
|
return tupleLengthMatches === isPositiveTest ? subtype : undefined;
|
1322
1323
|
}
|
1323
1324
|
// The tuple contains a "...". We'll expand this into as many elements as
|
1324
1325
|
// necessary to match the lengthValue.
|
1325
|
-
const elementsToAdd = lengthValue - concreteSubtype.tupleTypeArguments.length + 1;
|
1326
|
+
const elementsToAdd = lengthValue - concreteSubtype.priv.tupleTypeArguments.length + 1;
|
1326
1327
|
if (!isLessThanCheck) {
|
1327
1328
|
// If the specified length is smaller than the minimum length of this tuple,
|
1328
1329
|
// we can rule it out for a positive test and rule it in for a negative test.
|
@@ -1335,7 +1336,7 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1335
1336
|
return expandUnboundedTupleElement(concreteSubtype, elementsToAdd, /* keepUnbounded */ false);
|
1336
1337
|
}
|
1337
1338
|
// If this is a tuple related to an "*args: P.args" parameter, don't expand it.
|
1338
|
-
if ((0, types_1.isParamSpec)(subtype) && subtype.paramSpecAccess) {
|
1339
|
+
if ((0, types_1.isParamSpec)(subtype) && subtype.priv.paramSpecAccess) {
|
1339
1340
|
return subtype;
|
1340
1341
|
}
|
1341
1342
|
// Place an upper limit on the number of union subtypes we
|
@@ -1362,7 +1363,7 @@ function narrowTypeForTupleLength(evaluator, referenceType, lengthValue, isPosit
|
|
1362
1363
|
// to) the unbounded element.
|
1363
1364
|
function expandUnboundedTupleElement(tupleType, elementsToAdd, keepUnbounded) {
|
1364
1365
|
const tupleTypeArgs = [];
|
1365
|
-
tupleType.tupleTypeArguments.forEach((typeArg) => {
|
1366
|
+
tupleType.priv.tupleTypeArguments.forEach((typeArg) => {
|
1366
1367
|
if (!typeArg.isUnbounded) {
|
1367
1368
|
tupleTypeArgs.push(typeArg);
|
1368
1369
|
}
|
@@ -1390,13 +1391,13 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1390
1391
|
// with a known length.
|
1391
1392
|
if (!(0, types_1.isClassInstance)(containerType) ||
|
1392
1393
|
!types_1.ClassType.isBuiltIn(containerType, 'tuple') ||
|
1393
|
-
!containerType.tupleTypeArguments) {
|
1394
|
+
!containerType.priv.tupleTypeArguments) {
|
1394
1395
|
return referenceType;
|
1395
1396
|
}
|
1396
1397
|
// Determine which tuple types can be eliminated. Only "None" and
|
1397
1398
|
// literal types can be handled here.
|
1398
1399
|
const typesToEliminate = [];
|
1399
|
-
containerType.tupleTypeArguments.forEach((tupleEntry) => {
|
1400
|
+
containerType.priv.tupleTypeArguments.forEach((tupleEntry) => {
|
1400
1401
|
if (!tupleEntry.isUnbounded) {
|
1401
1402
|
if ((0, typeUtils_1.isNoneInstance)(tupleEntry.type)) {
|
1402
1403
|
typesToEliminate.push(tupleEntry.type);
|
@@ -1411,7 +1412,7 @@ function narrowTypeForContainerType(evaluator, referenceType, containerType, isP
|
|
1411
1412
|
}
|
1412
1413
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (referenceSubtype) => {
|
1413
1414
|
referenceSubtype = evaluator.makeTopLevelTypeVarsConcrete(referenceSubtype);
|
1414
|
-
if ((0, types_1.isClassInstance)(referenceSubtype) && referenceSubtype.literalValue === undefined) {
|
1415
|
+
if ((0, types_1.isClassInstance)(referenceSubtype) && referenceSubtype.priv.literalValue === undefined) {
|
1415
1416
|
// If we're able to enumerate all possible literal values
|
1416
1417
|
// (for bool or enum), we can eliminate all others in a negative test.
|
1417
1418
|
const allLiteralTypes = enumerateLiteralsForType(evaluator, referenceSubtype);
|
@@ -1431,12 +1432,12 @@ function getElementTypeForContainerNarrowing(containerType) {
|
|
1431
1432
|
if (!(0, types_1.isClassInstance)(containerType) || !types_1.ClassType.isBuiltIn(containerType, supportedContainers)) {
|
1432
1433
|
return undefined;
|
1433
1434
|
}
|
1434
|
-
if (!containerType.typeArguments || containerType.typeArguments.length < 1) {
|
1435
|
+
if (!containerType.priv.typeArguments || containerType.priv.typeArguments.length < 1) {
|
1435
1436
|
return undefined;
|
1436
1437
|
}
|
1437
|
-
let elementType = containerType.typeArguments[0];
|
1438
|
-
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.tupleTypeArguments) {
|
1439
|
-
elementType = (0, types_1.combineTypes)(containerType.tupleTypeArguments.map((t) => t.type));
|
1438
|
+
let elementType = containerType.priv.typeArguments[0];
|
1439
|
+
if ((0, typeUtils_1.isTupleClass)(containerType) && containerType.priv.tupleTypeArguments) {
|
1440
|
+
elementType = (0, types_1.combineTypes)(containerType.priv.tupleTypeArguments.map((t) => t.type));
|
1440
1441
|
}
|
1441
1442
|
return elementType;
|
1442
1443
|
}
|
@@ -1511,7 +1512,7 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1511
1512
|
}
|
1512
1513
|
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
1513
1514
|
const entries = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype, /* allowNarrowed */ true);
|
1514
|
-
const tdEntry = (_a = entries.knownItems.get(literalKey.literalValue)) !== null && _a !== void 0 ? _a : entries.extraItems;
|
1515
|
+
const tdEntry = (_a = entries.knownItems.get(literalKey.priv.literalValue)) !== null && _a !== void 0 ? _a : entries.extraItems;
|
1515
1516
|
if (isPositiveTest) {
|
1516
1517
|
if (!tdEntry) {
|
1517
1518
|
return undefined;
|
@@ -1521,9 +1522,9 @@ function narrowTypeForTypedDictKey(evaluator, referenceType, literalKey, isPosit
|
|
1521
1522
|
if (tdEntry.isRequired || tdEntry.isProvided) {
|
1522
1523
|
return subtype;
|
1523
1524
|
}
|
1524
|
-
const newNarrowedEntriesMap = new Map((_b = subtype.typedDictNarrowedEntries) !== null && _b !== void 0 ? _b : []);
|
1525
|
+
const newNarrowedEntriesMap = new Map((_b = subtype.priv.typedDictNarrowedEntries) !== null && _b !== void 0 ? _b : []);
|
1525
1526
|
// Add the new entry.
|
1526
|
-
newNarrowedEntriesMap.set(literalKey.literalValue, {
|
1527
|
+
newNarrowedEntriesMap.set(literalKey.priv.literalValue, {
|
1527
1528
|
valueType: tdEntry.valueType,
|
1528
1529
|
isReadOnly: tdEntry.isReadOnly,
|
1529
1530
|
isRequired: false,
|
@@ -1548,7 +1549,7 @@ function narrowTypeForDiscriminatedDictEntryComparison(evaluator, referenceType,
|
|
1548
1549
|
const narrowedType = (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
1549
1550
|
if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isTypedDictClass(subtype)) {
|
1550
1551
|
const symbolMap = (0, typedDicts_1.getTypedDictMembersForClass)(evaluator, subtype);
|
1551
|
-
const tdEntry = symbolMap.knownItems.get(indexLiteralType.literalValue);
|
1552
|
+
const tdEntry = symbolMap.knownItems.get(indexLiteralType.priv.literalValue);
|
1552
1553
|
if (tdEntry && (0, typeUtils_1.isLiteralTypeOrUnion)(tdEntry.valueType)) {
|
1553
1554
|
if (isPositiveTest) {
|
1554
1555
|
let foundMatch = false;
|
@@ -1583,11 +1584,13 @@ function narrowTypeForDiscriminatedTupleComparison(evaluator, referenceType, ind
|
|
1583
1584
|
if ((0, types_1.isClassInstance)(subtype) &&
|
1584
1585
|
types_1.ClassType.isTupleClass(subtype) &&
|
1585
1586
|
!(0, typeUtils_1.isUnboundedTupleClass)(subtype) &&
|
1586
|
-
typeof indexLiteralType.literalValue === 'number' &&
|
1587
|
+
typeof indexLiteralType.priv.literalValue === 'number' &&
|
1587
1588
|
(0, types_1.isClassInstance)(literalType)) {
|
1588
|
-
const indexValue = indexLiteralType.literalValue;
|
1589
|
-
if (subtype.tupleTypeArguments &&
|
1590
|
-
|
1589
|
+
const indexValue = indexLiteralType.priv.literalValue;
|
1590
|
+
if (subtype.priv.tupleTypeArguments &&
|
1591
|
+
indexValue >= 0 &&
|
1592
|
+
indexValue < subtype.priv.tupleTypeArguments.length) {
|
1593
|
+
const tupleEntryType = (_a = subtype.priv.tupleTypeArguments[indexValue]) === null || _a === void 0 ? void 0 : _a.type;
|
1591
1594
|
if (tupleEntryType && (0, typeUtils_1.isLiteralTypeOrUnion)(tupleEntryType)) {
|
1592
1595
|
if (isPositiveTest) {
|
1593
1596
|
return evaluator.assignType(tupleEntryType, literalType) ? subtype : undefined;
|
@@ -1622,8 +1625,8 @@ function narrowTypeForDiscriminatedLiteralFieldComparison(evaluator, referenceTy
|
|
1622
1625
|
// Handle the case where the field is a property
|
1623
1626
|
// that has a declared literal return type for its getter.
|
1624
1627
|
if ((0, types_1.isClassInstance)(subtype) && (0, types_1.isClassInstance)(memberType) && (0, typeUtils_1.isProperty)(memberType)) {
|
1625
|
-
const getterType = (_a = memberType.fgetInfo) === null || _a === void 0 ? void 0 : _a.methodType;
|
1626
|
-
if (getterType && getterType.
|
1628
|
+
const getterType = (_a = memberType.priv.fgetInfo) === null || _a === void 0 ? void 0 : _a.methodType;
|
1629
|
+
if (getterType && getterType.shared.declaredReturnType) {
|
1627
1630
|
const getterReturnType = types_1.FunctionType.getEffectiveReturnType(getterType);
|
1628
1631
|
if (getterReturnType) {
|
1629
1632
|
memberType = getterReturnType;
|
@@ -1685,6 +1688,7 @@ function narrowTypeForDiscriminatedFieldNoneComparison(evaluator, referenceType,
|
|
1685
1688
|
function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
1686
1689
|
return evaluator.mapSubtypesExpandTypeVars(type,
|
1687
1690
|
/* options */ undefined, (subtype, unexpandedSubtype) => {
|
1691
|
+
var _a;
|
1688
1692
|
if ((0, types_1.isClassInstance)(subtype)) {
|
1689
1693
|
const matches = types_1.ClassType.isDerivedFrom(classType, types_1.ClassType.cloneAsInstantiable(subtype));
|
1690
1694
|
if (isPositiveTest) {
|
@@ -1692,13 +1696,13 @@ function narrowTypeForTypeIs(evaluator, type, classType, isPositiveTest) {
|
|
1692
1696
|
if (types_1.ClassType.isSameGenericClass(subtype, classType)) {
|
1693
1697
|
return subtype;
|
1694
1698
|
}
|
1695
|
-
return (0, typeUtils_1.addConditionToType)(types_1.ClassType.cloneAsInstance(classType), subtype.condition);
|
1699
|
+
return (0, typeUtils_1.addConditionToType)(types_1.ClassType.cloneAsInstance(classType), (_a = subtype.props) === null || _a === void 0 ? void 0 : _a.condition);
|
1696
1700
|
}
|
1697
|
-
if (!classType.includeSubclasses) {
|
1701
|
+
if (!classType.priv.includeSubclasses) {
|
1698
1702
|
return undefined;
|
1699
1703
|
}
|
1700
1704
|
}
|
1701
|
-
else if (!classType.includeSubclasses) {
|
1705
|
+
else if (!classType.priv.includeSubclasses) {
|
1702
1706
|
// If the class if marked final and it matches, then
|
1703
1707
|
// we can eliminate it in the negative case.
|
1704
1708
|
if (matches && types_1.ClassType.isFinal(subtype)) {
|
@@ -1732,8 +1736,8 @@ function narrowTypeForClassComparison(evaluator, referenceType, classType, isPos
|
|
1732
1736
|
types_1.TypeBase.isInstance(subtype) &&
|
1733
1737
|
types_1.ClassType.isBuiltIn(concreteSubtype, 'type')) {
|
1734
1738
|
concreteSubtype =
|
1735
|
-
concreteSubtype.typeArguments && concreteSubtype.typeArguments.length > 0
|
1736
|
-
? (0, typeUtils_1.convertToInstantiable)(concreteSubtype.typeArguments[0])
|
1739
|
+
concreteSubtype.priv.typeArguments && concreteSubtype.priv.typeArguments.length > 0
|
1740
|
+
? (0, typeUtils_1.convertToInstantiable)(concreteSubtype.priv.typeArguments[0])
|
1737
1741
|
: types_1.UnknownType.create();
|
1738
1742
|
}
|
1739
1743
|
if ((0, types_1.isAnyOrUnknown)(concreteSubtype)) {
|
@@ -1745,11 +1749,11 @@ function narrowTypeForClassComparison(evaluator, referenceType, classType, isPos
|
|
1745
1749
|
}
|
1746
1750
|
const isSuperType = isIsinstanceFilterSuperclass(evaluator, subtype, concreteSubtype, classType, classType,
|
1747
1751
|
/* isInstanceCheck */ false);
|
1748
|
-
if (!classType.includeSubclasses) {
|
1752
|
+
if (!classType.priv.includeSubclasses) {
|
1749
1753
|
// Handle the case where the LHS and RHS operands are specific
|
1750
1754
|
// classes, as opposed to types that represent classes and their
|
1751
1755
|
// subclasses.
|
1752
|
-
if (!concreteSubtype.includeSubclasses) {
|
1756
|
+
if (!concreteSubtype.priv.includeSubclasses) {
|
1753
1757
|
return types_1.ClassType.isSameGenericClass(concreteSubtype, classType) ? classType : undefined;
|
1754
1758
|
}
|
1755
1759
|
const isSubType = isIsinstanceFilterSubclass(evaluator, concreteSubtype, classType,
|
@@ -1790,7 +1794,7 @@ function narrowTypeForLiteralComparison(evaluator, referenceType, literalType, i
|
|
1790
1794
|
return subtype;
|
1791
1795
|
}
|
1792
1796
|
else if ((0, types_1.isClassInstance)(subtype) && types_1.ClassType.isSameGenericClass(literalType, subtype)) {
|
1793
|
-
if (subtype.literalValue !== undefined) {
|
1797
|
+
if (subtype.priv.literalValue !== undefined) {
|
1794
1798
|
const literalValueMatches = types_1.ClassType.isLiteralValueSame(subtype, literalType);
|
1795
1799
|
if ((literalValueMatches && !isPositiveTest) || (!literalValueMatches && isPositiveTest)) {
|
1796
1800
|
return undefined;
|
@@ -1829,7 +1833,7 @@ function enumerateLiteralsForType(evaluator, type) {
|
|
1829
1833
|
if (types_1.ClassType.isEnumClass(type)) {
|
1830
1834
|
// Enum expansion doesn't apply to enum classes that derive
|
1831
1835
|
// from enum.Flag.
|
1832
|
-
if (type.
|
1836
|
+
if (type.shared.baseClasses.some((baseClass) => (0, types_1.isClass)(baseClass) && types_1.ClassType.isBuiltIn(baseClass, 'Flag'))) {
|
1833
1837
|
return undefined;
|
1834
1838
|
}
|
1835
1839
|
// Enumerate all of the values in this enumeration.
|
@@ -1842,7 +1846,7 @@ function enumerateLiteralsForType(evaluator, type) {
|
|
1842
1846
|
symbolType = (_a = (0, enums_1.transformTypeForEnumMember)(evaluator, type, name)) !== null && _a !== void 0 ? _a : symbolType;
|
1843
1847
|
if ((0, types_1.isClassInstance)(symbolType) &&
|
1844
1848
|
types_1.ClassType.isSameGenericClass(type, symbolType) &&
|
1845
|
-
symbolType.literalValue !== undefined) {
|
1849
|
+
symbolType.priv.literalValue !== undefined) {
|
1846
1850
|
enumList.push(symbolType);
|
1847
1851
|
}
|
1848
1852
|
}
|
@@ -1858,6 +1862,7 @@ exports.enumerateLiteralsForType = enumerateLiteralsForType;
|
|
1858
1862
|
// it's not callable.
|
1859
1863
|
function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allowIntersections) {
|
1860
1864
|
return evaluator.mapSubtypesExpandTypeVars(type, /* options */ undefined, (subtype) => {
|
1865
|
+
var _a;
|
1861
1866
|
switch (subtype.category) {
|
1862
1867
|
case 4 /* TypeCategory.Function */:
|
1863
1868
|
case 5 /* TypeCategory.OverloadedFunction */: {
|
@@ -1883,24 +1888,19 @@ function narrowTypeForCallable(evaluator, type, isPositiveTest, errorNode, allow
|
|
1883
1888
|
// The type appears to not be callable. It's possible that the
|
1884
1889
|
// two type is a subclass that is callable. We'll synthesize a
|
1885
1890
|
// new intersection type.
|
1886
|
-
const className = `<callable subtype of ${subtype.
|
1891
|
+
const className = `<callable subtype of ${subtype.shared.name}>`;
|
1887
1892
|
const fileInfo = (0, analyzerNodeInfo_1.getFileInfo)(errorNode);
|
1888
1893
|
let newClassType = types_1.ClassType.createInstantiable(className, ParseTreeUtils.getClassFullName(errorNode, fileInfo.moduleName, className), fileInfo.moduleName, fileInfo.fileUri, 0 /* ClassTypeFlags.None */, ParseTreeUtils.getTypeSourceId(errorNode),
|
1889
|
-
/* declaredMetaclass */ undefined, subtype.
|
1890
|
-
newClassType.
|
1894
|
+
/* declaredMetaclass */ undefined, subtype.shared.effectiveMetaclass, subtype.shared.docString);
|
1895
|
+
newClassType.shared.baseClasses = [types_1.ClassType.cloneAsInstantiable(subtype)];
|
1891
1896
|
(0, typeUtils_1.computeMroLinearization)(newClassType);
|
1892
|
-
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, subtype.condition);
|
1897
|
+
newClassType = (0, typeUtils_1.addConditionToType)(newClassType, (_a = subtype.props) === null || _a === void 0 ? void 0 : _a.condition);
|
1893
1898
|
// Add a __call__ method to the new class.
|
1894
1899
|
const callMethod = types_1.FunctionType.createSynthesizedInstance('__call__');
|
1895
|
-
const selfParam =
|
1896
|
-
category: 0 /* ParameterCategory.Simple */,
|
1897
|
-
name: 'self',
|
1898
|
-
type: types_1.ClassType.cloneAsInstance(newClassType),
|
1899
|
-
hasDeclaredType: true,
|
1900
|
-
};
|
1900
|
+
const selfParam = types_1.FunctionParam.create(0 /* ParameterCategory.Simple */, types_1.ClassType.cloneAsInstance(newClassType), types_1.FunctionParamFlags.TypeDeclared, 'self');
|
1901
1901
|
types_1.FunctionType.addParameter(callMethod, selfParam);
|
1902
1902
|
types_1.FunctionType.addDefaultParameters(callMethod);
|
1903
|
-
callMethod.
|
1903
|
+
callMethod.shared.declaredReturnType = types_1.UnknownType.create();
|
1904
1904
|
types_1.ClassType.getSymbolTable(newClassType).set('__call__', symbol_1.Symbol.createWithType(4 /* SymbolFlags.ClassMember */, callMethod));
|
1905
1905
|
return types_1.ClassType.cloneAsInstance(newClassType);
|
1906
1906
|
}
|