Skip to content

Docs: show how to @overload function annotations in user code #2792

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

Closed
wants to merge 1 commit into from

Conversation

jkleint
Copy link

@jkleint jkleint commented Feb 2, 2017

The docs say @override doesn't work in user code, but it seems to work in mypy 0.470.
The update may be waiting on #2603, but that PR does not seem to include doc updates,
so feel free to put this patch in that PR.

The docs say `@override` doesn't work in user code, but it seems to work in mypy 0.470.
The update may be waiting on python#2603, but that PR does not seem to include doc updates,
so feel free to put this patch in that PR.
@gvanrossum
Copy link
Member

Hm, it doesn't actually work yet AFAICT. The example you give produces an error for me:

ov.py:11: error: Name '__getitem__' already defined

So we're still waiting for #2603. Thanks for the contribution though! Once that is in, this will be very helpful.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two small comments.

(By the way this is no more blocked.)


.. code-block:: python

# This is a stub file!
class Seq(Generic[T], Sequence[T]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic[T] here and below are redundant.

Now if we import ``abs`` as defined in the above library stub, we can
write code like this, and the types are inferred correctly:
class Seq(Generic[T], Sequence[T]):
@overload # These are just for the type checker, and overwritten by the real implementation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such long comments will look a bit ugly on readthedocs. I would recommend wrapping them or shortening to approx. 80 chars.

@gvanrossum
Copy link
Member

OK, I can merge once you approve it.

@pkch
Copy link
Contributor

pkch commented Mar 28, 2017

Is it worth adding the information about the order of @overload statements here? It seems that they matter:

Mypy lets you vary the return type even if multiple variants match if your signatures are ordered from narrower to more general.

but I'm not sure if it's official or undocumented feature; and also whether it's the entire signature needs to be "narrower" or just the argument or return type.

@gvanrossum gvanrossum mentioned this pull request Apr 12, 2017
@gvanrossum
Copy link
Member

Pardon my intervention, I'm superseding this with PR #3159.

@gvanrossum gvanrossum closed this Apr 12, 2017
gvanrossum added a commit that referenced this pull request Apr 12, 2017
…es) (#3159)

These are the doc updates corresponding to PR #2603 by @sixolet (specified in a revision of PEP 484: https://www.python.org/dev/peps/pep-0484/#function-method-overloading). The docs got started in PR #PR #2792 by @jkleint.
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.

4 participants