Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple functions arguements to be friendly with MyPy #129

Closed
fatrybl opened this issue Jan 19, 2024 · 3 comments
Closed

Multiple functions arguements to be friendly with MyPy #129

fatrybl opened this issue Jan 19, 2024 · 3 comments

Comments

@fatrybl
Copy link

fatrybl commented Jan 19, 2024

Hello!
I saw many discussions about MyPy friendly dispatching. But none of them answer the question:
how to dispatch a function with variable number of positional arguments s.t. MyPy wont blame on it ?
i saw this issue
and the instruction here
but there are equal number of arguments in overloaded methods and one needs to type annotate all of them in "dispatch". whereas in my case i have variable number of args in methods. So the final "dispatch" method can not handle this.
If i do not annotate any args in final "dispatch" method, MyPy also blames

for example:

from plum import dispatch, overload

@overload
def f(x: int) -> int:
    return x + 1

@overload
def f(x: str) -> str:
    return x

@overload
def f(x: str, y: str) -> str:
    return x + y

@dispatch
def f(x):
    raise NotImplementedError

image

Sorry, maybe i skipped an answer somehow, but is there a solution ?

@wesselb
Copy link
Member

wesselb commented Jan 20, 2024

Hey @fatrybl!

I think you've forgotten to let the implement accept y:

from plum import dispatch, overload


@overload
def f(x: int) -> int:
    return x + 1


@overload
def f(x: str) -> str:
    return x


@overload
def f(x: str, y: str) -> str:
    return x + y


@dispatch
def f(x, y=None):
    raise NotImplementedError

This checks out with mypy on my end.

@fatrybl
Copy link
Author

fatrybl commented Jan 22, 2024

Thank you @wesselb ! So the idea is to list all the args anyway but they can be set to None. It works in my PyCharm with latest MyPy as well

@fatrybl fatrybl closed this as completed Jan 22, 2024
@wesselb
Copy link
Member

wesselb commented Jan 22, 2024

@fatrybl That’s exactly right! Glad to hear that it works for you. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants