-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[FEAT or BUG] @overload
and default value matches
#18653
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
It's not a bug, mypy very much ignores defaults. |
Duplicate of #18581, but this example manifests in obviously incorrect typechecking without definition-site errors. |
Are you sure? I'm under the impression this issue is mostly about selecting the right overload based on defaults, which mirrors issue 3737 (but is different) and is a feature request. |
Yes, almost definitely: leaving a couple of corner cases behind the scenes (there's a bit of variadic args/kwargs magic that doesn't apply here), mypy picks the first matching overload, and here the first one matches. If you swap the definitions, you should get the opposite behavior. Two overloads are unsafely overlapping due to common zero-values call signature with incompatible return types, And there's nothing generic here to relate to 3737 - what am I missing?.. |
Mypy never looks at overload implementation. Overloads are simply tried in order of appearance until one matches, you can flip order to achieve what you want. As for selecting the overload depending on the default value, this is a non-starter (it may look simple, but it is actually really hard to implement in general), also it may be quite surprising when a matching overload is skipped for some reason. |
Thanks for your answers! I didn't know about |
I don't think it's worth adding code to check "are there two 0 arg overloads" because that would have to be quite a bit of extra implementation (current code just uses standard callable subtyping with a special subtype check but obviously the "standard callable subtyping" is what's passing this) |
Hello everyone,
In the following example, zero-argument call matches the first overload (with
Literal[True]
), despite the default value beingFalse
.Is this a bug or desired behaviour? The easy fix is the following (remove default value from the
Literal[True]
overload since it cannot be targetted.The problem with this fix, is that introduces dependance in the default value set in the actual definition of
f
. Indeed, if I were to change it fromFalse
toTrue
, then I would have to rewrite the overloads.I think it would be neat if the match took the default value into consideration, and I don't know if the fact that it is not here is a bug or a "non-feature".
What do you think about this?
Thanks for your time!
Élie
The text was updated successfully, but these errors were encountered: