The call to sorted
produces a List[int]
which carries no information about length. As such, producing a tuple from it also has no information about the length. The number of elements simply is undefined by the types you use.
You must tell your type checker to trust you in such cases. Use # type: ignore
or cast
to unconditionally accept the target type as valid:
# ignore mismatch by annotation
a: Tuple[int, int] = tuple(sorted([1, 3])) # type: ignore
# ignore mismatch by cast
a = cast(Tuple[int, int], tuple(sorted([1, 3])))
Alternatively, create a lenght-aware sort:
def sort_pair(a: T, b: T) -> Tuple[T, T]:
return (a, b) if a < b else (b, a)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…