8000 Implemented optimization for type evaluation for index expressions. I… · codemuse-app/scip-python@5ac7a93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ac7a93

Browse files
committed
Implemented optimization for type evaluation for index expressions. In cases where there are multiple subexpressions separated by commas, we can skip the check for the __index__ magic method.
1 parent 5fffd5b commit 5ac7a93

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

packages/pyright-internal/src/analyzer/typeEvaluator.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6526,33 +6526,35 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
65266526
// Speculatively attempt the call. We may need to replace the index
65276527
// type with 'int', and we don't want to emit errors before we know
65286528
// which type to use.
6529-
useSpeculativeMode(node, () => {
6530-
callResult = validateCallArguments(node, argList, { type: itemMethodType });
6531-
6532-
if (callResult.argumentErrors) {
6533-
// If the object supports "__index__" magic method, convert
6534-
// the index it to an int and try again.
6535-
if (isClassInstance(positionalIndexType) && keywordArgs.length === 0 && unpackedDictArgs.length === 0) {
6536-
const altArgList = [...argList];
6537-
altArgList[0] = { ...altArgList[0] };
6538-
const indexMethod = getTypeOfObjectMember(node, positionalIndexType, '__index__');
6539-
6540-
if (indexMethod) {
6541-
const intType = getBuiltInObject(node, 'int');
6542-
if (isClassInstance(intType)) {
6543-
altArgList[0].type = intType;
6529+
if (keywordArgs.length === 0 && unpackedDictArgs.length === 0 && positionalArgs.length === 1) {
6530+
useSpeculativeMode(node, () => {
6531+
callResult = validateCallArguments(node, argList, { type: itemMethodType });
6532+
6533+
if (callResult.argumentErrors) {
6534+
// If the object supports "__index__" magic method, convert
6535+
// the index to an int and try again.
6536+
if (isClassInstance(positionalIndexType)) {
6537+
const altArgList = [...argList];
6538+
altArgList[0] = { ...altArgList[0] };
6539+
const indexMethod = getTypeOfObjectMember(node, positionalIndexType, '__index__');
6540+
6541+
if (indexMethod) {
6542+
const intType = getBuiltInObject(node, 'int');
6543+
if (isClassInstance(intType)) {
6544+
altArgList[0].type = intType;
6545+
}
65446546
}
6545-
}
65466547

6547-
callResult = validateCallArguments(node, altArgList, { type: itemMethodType });
6548+
callResult = validateCallArguments(node, altArgList, { type: itemMethodType });
65486549

6549-
// We were successful, so replace the arg list.
6550-
if (!callResult.argumentErrors) {
6551-
argList = altArgList;
6550+
// We were successful, so replace the arg list.
6551+
if (!callResult.argumentErrors) {
6552+
argList = altArgList;
6553+
}
65526554
}
65536555
}
6554-
}
6555-
});
6556+
});
6557+
}
65566558

65576559
callResult = validateCallArguments(node, argList, { type: itemMethodType });
65586560

0 commit comments

Comments
 (0)
0