-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add clarification for overloading example #3369
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I have some editorial requests.
docs/source/function_overloading.rst
Outdated
overloaded definition, and ignores the type annotations on the | ||
implementation of ``__getitem__``. The code in the body of the | ||
definition of ``__getitem__`` is checked against the annotations on | ||
the last function declaration. In this case the body is checked with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the last function declaration -> the corresponding declaration.
docs/source/function_overloading.rst
Outdated
the last function declaration. In this case the body is checked with | ||
``index: Union[int, slice]`` and a return type | ||
``Union[T, Sequence[T]]``. If there are no annotations on the last | ||
definition, then code in the function body is not type checked. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd start a new paragraph at "The annotations ..."
docs/source/function_overloading.rst
Outdated
definition, then code in the function body is not type checked. The | ||
annotations on the function body must be compatible with the types | ||
given for the overloaded variants listed above it. The type checker | ||
will ensure that all the types listed for parameters and the return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure -> verify
(It can't ensure anything, it has no powers except for pointing out errors. :-)
docs/source/function_overloading.rst
Outdated
annotations on the function body must be compatible with the types | ||
given for the overloaded variants listed above it. The type checker | ||
will ensure that all the types listed for parameters and the return | ||
type in the overloaded variants can inhabit the type given for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a different word than "inhabit"? It's not used nor explained anywhere else in the mypy docs, and as far as I can tell from your example it just means "to be compatible with", right? (That's our term for "is a subtype module Any
".)
Also IIRC the actual rules are more complicated -- I think it's something like each overload variant must be compatible with the implementation? @sixolet can explain it better than I can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. I don't think I really understand. It makes sense that you would need to check full signatures together to make sure generics are resolved correctly. To prevent this sort of thing:
from typing import List, Any, Union, Tuple, TypeVar, overload
A = TypeVar("A")
B = TypeVar("B")
@overload
def first(xs: List[A]) -> A: ...
@overload
def first(xs: Tuple[A, Any]) -> A: ...
def first(xs: Union[List[A], Tuple[B, Any]]) -> A:
return xs[0]
Tuple[A, Any] -> A
should not be compatible with Union[List[A], Tuple[B, Any]] -> A
But I ran that example and mypy didn't complain. Is there a different reason?
I think that should incorporate your suggestions, except for maybe the last one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read over this, and it looks good to me. Thanks! (And sorry for the long wait!)
Fix for #3360