Overloads for fixed argument number as well as variadic number #1828
-
How can I overload a function Code sample in pyright playground from typing import assert_type, overload
@overload
def foo() -> tuple[()]: ...
@overload
def foo(x: int, /) -> tuple[int]: ...
@overload
def foo(x1: int, x2: int, /) -> tuple[int, int]: ...
@overload
def foo(*x: int) -> tuple[int, ...]: ...
def foo(*x: int) -> tuple[int, ...]:
return x
assert_type(foo(), tuple[()]) # ✅
assert_type(foo(1), tuple[int]) # ✅
assert_type(foo(1, 2), tuple[int, int]) # ✅
assert_type(foo(1,2,3), tuple[int, ...]) # ✅
assert_type(foo(*[1,2,3]), tuple[int, ...]) # ❌ received "tuple[()]" A different way of writing the overloads gives the same results: Code sample in pyright playground Here is a more real-world example where this is biting me: Code sample in pyright playground |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Related to #1216 (trying to work around the lack of this feature) Some prior discussion: #1545 |
Beta Was this translation helpful? Give feedback.
-
This was a bug in |
Beta Was this translation helpful? Give feedback.
This was a bug in
pyright
and has been fixed as of 1.1.375