mirror of
https://github.com/microsoft/pyright.git
synced 2024-10-06 12:57:14 +03:00
Simplified previous commit.
This commit is contained in:
parent
5cea702a7e
commit
28b452ac0f
@ -336,42 +336,31 @@ function narrowTypeBasedOnSequencePattern(
|
||||
}
|
||||
|
||||
// Can we narrow a tuple?
|
||||
if (canNarrowTuple) {
|
||||
if (canNarrowTuple && negativeNarrowedDims.length > 0) {
|
||||
const tupleClassType = evaluator.getBuiltInType(pattern, 'tuple');
|
||||
if (tupleClassType && isInstantiableClass(tupleClassType)) {
|
||||
if (negativeNarrowedDims.length === 1) {
|
||||
entry.subtype = ClassType.cloneAsInstance(
|
||||
specializeTupleClass(
|
||||
tupleClassType,
|
||||
narrowedEntryTypes.map((t) => {
|
||||
return { type: t, isUnbounded: false };
|
||||
})
|
||||
)
|
||||
);
|
||||
} else if (negativeNarrowedDims.length > 1) {
|
||||
// Expand the tuple in the dimensions that were narrowed.
|
||||
// Start with the fully-narrowed set of entries.
|
||||
const expandedEntryTypes = [];
|
||||
// Expand the tuple in the dimensions that were narrowed.
|
||||
// Start with the fully-narrowed set of entries.
|
||||
const expandedEntryTypes = [];
|
||||
|
||||
for (const dim of negativeNarrowedDims) {
|
||||
const newEntryTypes = [...unnarrowedEntryTypes];
|
||||
newEntryTypes[dim] = narrowedEntryTypes[dim];
|
||||
expandedEntryTypes.push(newEntryTypes);
|
||||
}
|
||||
|
||||
entry.subtype = combineTypes(
|
||||
expandedEntryTypes.map((entryTypes) => {
|
||||
return ClassType.cloneAsInstance(
|
||||
specializeTupleClass(
|
||||
tupleClassType,
|
||||
entryTypes.map((t) => {
|
||||
return { type: t, isUnbounded: false };
|
||||
})
|
||||
)
|
||||
);
|
||||
})
|
||||
);
|
||||
for (const dim of negativeNarrowedDims) {
|
||||
const newEntryTypes = [...unnarrowedEntryTypes];
|
||||
newEntryTypes[dim] = narrowedEntryTypes[dim];
|
||||
expandedEntryTypes.push(newEntryTypes);
|
||||
}
|
||||
|
||||
entry.subtype = combineTypes(
|
||||
expandedEntryTypes.map((entryTypes) => {
|
||||
return ClassType.cloneAsInstance(
|
||||
specializeTupleClass(
|
||||
tupleClassType,
|
||||
entryTypes.map((t) => {
|
||||
return { type: t, isUnbounded: false };
|
||||
})
|
||||
)
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user