Skip to content

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

Merged
merged 2 commits into from
Jul 11, 2017

Conversation

tyehle
Copy link
Contributor

@tyehle tyehle commented May 16, 2017

Fix for #3360

Copy link
Member

@gvanrossum gvanrossum left a 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.

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
Copy link
Member

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.

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
Copy link
Member

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 ..."

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
Copy link
Member

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. :-)

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
Copy link
Member

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.

Copy link
Contributor Author

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?

@tyehle
Copy link
Contributor Author

tyehle commented May 17, 2017

I think that should incorporate your suggestions, except for maybe the last one.

Copy link
Collaborator

@ddfisher ddfisher left a 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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants