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
Is it possible to overload depending on the presence of an argument? #248
The function returns a bool when the out argument is not present (so foo(), foo(x, y), foo(arg2=x) all are boolean values).
The function returns a T when the out argument is present (so foo(out="hello"), foo(x, y, out="hello"), foo(out="hello", arg2=x) all are string values).
I was trying to describe that as an overload (and restricting to keyword only args to avoid mistakes):
but the revealed_type for the result of the first call is "Any". I don't think my definition should be ok (my overloaded signatures are ambiguous), but an Any there is certainly unexpected there. Also, I'm not sure what would be (if possible) the way to express this situation with mypy's type system
I was going to reply that the problem is that you gave the second variant a default value for out -- so a call without out matches both variants. And I think that should be the correct answer. But removing the default for out doesn't change the output -- it still thinks that the first call returns None. So I think that's a bug. (And it doesn't need a type variable to repro -- I still get the same behavior if I replace T with str in the second variant.
@dmoisset I also think that the second variant should have no default, i.e. out:T=None should be replaced by simply out:T. As I see this is a bug in mypy, not a bug in typing.py, so that you probably should open an issue there.
I wasn't award that keyword-only args were allowed to be mandatory (i.e. no default), that probably solves this at specification level, so I'll close this and open a simpler case as a bug in mypy regarding the "Any", thanks
Activity
gvanrossum commentedon Jul 19, 2016
I was going to reply that the problem is that you gave the second variant a default value for
out-- so a call withoutoutmatches both variants. And I think that should be the correct answer. But removing the default foroutdoesn't change the output -- it still thinks that the first call returnsNone. So I think that's a bug. (And it doesn't need a type variable to repro -- I still get the same behavior if I replaceTwithstrin the second variant.ilevkivskyi commentedon Jul 19, 2016
@dmoisset I also think that the second variant should have no default, i.e.
out:T=Noneshould be replaced by simplyout:T. As I see this is a bug in mypy, not a bug in typing.py, so that you probably should open an issue there.dmoisset commentedon Jul 19, 2016
I wasn't award that keyword-only args were allowed to be mandatory (i.e. no default), that probably solves this at specification level, so I'll close this and open a simpler case as a bug in mypy regarding the "Any", thanks