You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Overloads with default arguments do not work properly #5486
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.
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
This is not a bug, mypy shows an actual error. As @srittau mentioned, what is the type of func() (no args)? Is it int or str, because it matches both overloads. You should remove the default value (i.e. ...) from at least one of the overloads. Also please read the docs https://mypy.readthedocs.io/en/latest/more_types.html#function-overloading
Specifically, in overload 2 you can mark param2 as a required keyword-only argument by doing *, param2: str.
@JelleZijlstra But then Mypy no longer allows you to call the function as func("foo", 3), you have to do func("foo", param2=3) even though passing them as positional arguments are fine at runtime.
Activity
srittau commentedon Aug 15, 2018
The problem here is that both overloads match a call of
func(). Removing the default from the second overload should fix your problem:func()matches (only) the first overloadfunc(None)matches the first overloadfunc(42)matches the second overloadpadix-key commentedon Aug 15, 2018
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
Is there a way to fix this problem?
ilevkivskyi commentedon Aug 15, 2018
This is not a bug, mypy shows an actual error. As @srittau mentioned, what is the type of
func()(no args)? Is itintorstr, because it matches both overloads. You should remove the default value (i.e....) from at least one of the overloads. Also please read the docs https://mypy.readthedocs.io/en/latest/more_types.html#function-overloadingilevkivskyi commentedon Aug 15, 2018
@padix-key you can just add more overloads that match runtime semantics of your function.
JelleZijlstra commentedon Aug 15, 2018
Specifically, in overload 2 you can mark
param2as a required keyword-only argument by doing*, param2: str.henribru commentedon Jun 6, 2020
@JelleZijlstra But then Mypy no longer allows you to call the function as
func("foo", 3), you have to dofunc("foo", param2=3)even though passing them as positional arguments are fine at runtime.henribru commentedon Feb 6, 2021
Okay, after reading https://mail.python.org/archives/list/typing-sig@python.org/thread/3P36ZCFAMOKFJMHFULTPGRNRSGTHTOLX/ I realize this workaround actually requires three overloads to avoid the problem I mentioned:
It feels a bit esoteric and unintuitive. Would a PR to better document the interaction between overloads and default values be welcome?
bashtage commentedon Feb 24, 2021
I think a tutorial/extended help page on complex
overloading would be a great addition.