-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Overloads with default arguments do not work properly #5486
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
Comments
The problem here is that both overloads match a call of
|
Unfortunately, I cannot simply remove the default value from the second overload, because in my actual problem there are default parameters before this parameter, like from typing import overload
@overload
def func(
param1: str = "foo",
param2: None = ...
) -> int: ...
@overload
def func(
param1: str = "foo",
param2: int = ...
) -> str: ... Is there a way to fix this problem? |
This is not a bug, mypy shows an actual error. As @srittau mentioned, what is the type of |
@padix-key you can just add more overloads that match runtime semantics of your function. |
Specifically, in overload 2 you can mark |
@JelleZijlstra But then Mypy no longer allows you to call the function as |
Okay, after reading https://mail.python.org/archives/list/[email protected]/thread/3P36ZCFAMOKFJMHFULTPGRNRSGTHTOLX/ I realize this workaround actually requires three overloads to avoid the problem I mentioned: from typing import overload
@overload
def func(
param1: str = "foo",
param2: None = ...
) -> int: ...
@overload
def func(
param1: str = "foo",
*,
param2: int
) -> str: ...
@overload
def func(
param1: str,
param2: int
) -> str: ... It feels a bit esoteric and unintuitive. Would a PR to better document the interaction between overloads and default values be welcome? |
I think a tutorial/extended help page on complex |
I have the follwoing stub file (
test.pyi
):When I run
mypy test.pyi
, mypy shows the following error:test.pyi:4: error: Overloaded function signatures 1 and 2 overlap with incompatible return types
The parameter
param
clearly has two different types. Hence, the functions should not overlap to my understanding.mypy 0.620 has been used.
The text was updated successfully, but these errors were encountered: