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 |
Answered by
randolf-scholz
Aug 8, 2024
Replies: 2 comments
|
This was a bug in |
0 replies
Answer selected by
randolf-scholz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a bug in
pyrightand has been fixed as of 1.1.375