-
Notifications
You must be signed in to change notification settings - Fork 240
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
[@overload]: What is the effect of the *implementation* type-signature? #1840
Comments
The type signature of the implementation is never visible to callers. It is used for two things:
Regarding adding a third overload, I'm going to switch your example to using str for clarity:
If you do not include the overload, type checkers will starting complaining on:
because they cannot guarantee the string is "apple" or "banana". If you include the overload, type checkers will match that third overload on unknown strings.
(Finally and more tangentially, I've found myself at times wanting an unsound literal feature (particularly so in typeshed). Where if the type checker knows the value of the literal, it uses the specific overloads, otherwise it aggregates information across the overloads) |
Do you mind if I file a PR to the docs (and maybe add a conformance test or two)? I think what you said makes sense, but just to check my understanding:
I think it's really the first point that I didn't understand. I assumed that the implementation type-signature was "automatically" counted as one of the overloads. |
@alanhdu, overloads are generally underspecified in the typing spec today. Earlier this week, I posted a proposed update to the "Overloads" chapter of the spec that attempts to fill in many of these gaps. It covers all of the points that you mention above (and many additional details). I welcome feedback on the proposed change. For example, if you think there are ways it could be clearer, please post comments to the thread or the PR. If and when that update is ratified and incorporated into the typing spec, we'll make sure there are corresponding conformance tests. The current conformance tests for overloads are pretty anemic, reflecting the fact that the spec doesn't provide much guidance here. The typing documentation site includes several different categories of documentation. My proposed update is to the typing specification, which is primarily targeted at type checker authors. In your points above, the wording might be more appropriate for the "Guides" style of documentation, which is primarily targeted at consumers of type checkers. I think that additional clarity about overloads within the "Guides" documentation would be a great contribution if you're interested in submitting a PR. |
One thing that I've found underspecified when using
@overload
is what the type signature of the actual function should be.For example, let's say I have a function like:
What is the effect of the last line? In particular:
def f(x, z):
). Is that the right thing to do (I sometimes getOverloaded implementation is not consistent with signature of overload 1
errors if I leave out the type annotations)?@overload
list -- in other words:Both pyright and mypy seem to interpret things differently with and without it (e.g. see this play link).
I understand that
@overload
is a complicated feature, but I'm hoping this is a small corner we can start with.The text was updated successfully, but these errors were encountered: