mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
Improved handling of +
operator when both operands are tuples. It now produces a more accurate type when one (but not both) of the two tuples is of indeterminate length. (#6410)
This commit is contained in:
parent
2436c8d9aa
commit
ace7edb41e
@ -368,20 +368,28 @@ export function validateBinaryOperation(
|
||||
isClassInstance(leftSubtypeExpanded) &&
|
||||
isTupleClass(leftSubtypeExpanded) &&
|
||||
leftSubtypeExpanded.tupleTypeArguments &&
|
||||
!isUnboundedTupleClass(leftSubtypeExpanded) &&
|
||||
isClassInstance(rightSubtypeExpanded) &&
|
||||
isTupleClass(rightSubtypeExpanded) &&
|
||||
rightSubtypeExpanded.tupleTypeArguments &&
|
||||
!isUnboundedTupleClass(rightSubtypeExpanded) &&
|
||||
tupleClassType &&
|
||||
isInstantiableClass(tupleClassType)
|
||||
) {
|
||||
return ClassType.cloneAsInstance(
|
||||
specializeTupleClass(tupleClassType, [
|
||||
...leftSubtypeExpanded.tupleTypeArguments,
|
||||
...rightSubtypeExpanded.tupleTypeArguments,
|
||||
])
|
||||
);
|
||||
// If at least one of the tuples is of fixed size, we can
|
||||
// combine them into a precise new type. If both are unbounded
|
||||
// (or contain an unbounded element), we cannot combine them
|
||||
// in this manner because tuples can contain at most one
|
||||
// unbounded element.
|
||||
if (
|
||||
!isUnboundedTupleClass(leftSubtypeExpanded) ||
|
||||
!isUnboundedTupleClass(rightSubtypeExpanded)
|
||||
) {
|
||||
return ClassType.cloneAsInstance(
|
||||
specializeTupleClass(tupleClassType, [
|
||||
...leftSubtypeExpanded.tupleTypeArguments,
|
||||
...rightSubtypeExpanded.tupleTypeArguments,
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const magicMethodName = binaryOperatorMap[operator][0];
|
||||
|
@ -10,10 +10,18 @@ def func1(a: tuple[int, int, int], b: tuple[str, str]):
|
||||
|
||||
|
||||
def func2(a: tuple[int, int, int], b: tuple[str, ...]):
|
||||
reveal_type(a + b, expected_text="tuple[int, int, int, *tuple[str, ...]]")
|
||||
|
||||
|
||||
def func3(a: tuple[int, ...], b: tuple[str, ...]):
|
||||
reveal_type(a + b, expected_text="tuple[int | str, ...]")
|
||||
|
||||
|
||||
def func3(input_list):
|
||||
def func4(a: tuple[str, *tuple[int, ...]], b: tuple[str, int]):
|
||||
reveal_type(a + b, expected_text="tuple[str, *tuple[int, ...], str, int]")
|
||||
|
||||
|
||||
def func5(input_list):
|
||||
output_tuple = ()
|
||||
|
||||
for _, value in enumerate([]):
|
||||
|
Loading…
Reference in New Issue
Block a user