-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Infering return type as a union of all returns in type-checked functions #17307
Comments
This would break PEP484 compliance of
The same from
|
Hmm, that's kinda old and imo it doesn't make sense to force a type checker to infer any (as that would make all inference-based checking non-compliant, see pytype and pyright). I don't see anything about this in https://typing.readthedocs.io/en/latest/spec/index.html , maybe that's a discussion for the modern typing spec to rectify. |
I'm actually quite against doing that, I love that all annotations are "opt in", and I do not have subtle errors and bunch of type-ignore comments when interfacing with some legacy stuff or just poorly typed codebases. And it's right there in typing spec (here):
(that's copied verbatim from current docs) |
Either way, this decision cannot be made by a type checker. If you think that typing spec should be amended, then typing-sig or https://github.com/python/typing/issues is the right place to discuss that. |
Thanks for pointing to me where that's addressed in the spec.
So default to Any, but if you can infer it that's fine to do so. That keeps pytype, pyright, this request and #4409 compliant. So I think they can be discussed on their own merits.
Now that I found (you showed me) the section about it, I think the spec is fine as-is. It offers guidelines, a default behaviour, and possibility for checkers to do more.
When you say "interfacing with legacy stuff", do you mean libraries that are not About poorly typed codebases:
|
Feature
Note: This is different than #4409 and #6646 which aim to change what is considered "annotated" and which functions are type-checked in the first place.
Mypy currently only type-check a function if its considered "annotated", ie: all parameters are annotated, or the return type is present (required for methods w/o parameters). Whilst mypy obviously can't infer a return type if it doesn't type-check the content of a function, I feel like methods that are checked could have their return type inferred as a union of whatever mypy think the type of the returns are.
Here's a very simple example comparing mypy and pyright/pylance
(same result in CLI)
Pitch
The idea would be to reduce clutter, and reduce the risk of hiding a useful true return type behind a more vague type.
Maybe there's a performance concern for libraries? (mypy having to read more code instead of stopping at a return annotation) How much is it? How much does caching help?
The text was updated successfully, but these errors were encountered: