Skip to content
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

Type hints: Return Iterator instead of Iterable from __iter__ #27445

Closed
wants to merge 1 commit into from

Conversation

henribru
Copy link
Contributor

@henribru henribru commented Oct 6, 2019

__iter__ methods are supposed to return iterators (https://docs.python.org/3/reference/datamodel.html#object.__iter__), but some of them are typed to return iterables, which is too general. This results in error messages such as Iterable[Module[Any]]" has no attribute "__next__" from Mypy. Technically this should also have caused a type error here, but due to a bug in Mypy type checking isn't working correctly in untyped methods (this will be fixed in the next release though: python/mypy#7530).

@henribru henribru requested a review from apaszke as a code owner October 6, 2019 14:01
@pytorchbot pytorchbot added module: dataloader Related to torch.utils.data.DataLoader and Sampler module: nn Related to torch.nn module: typing Related to mypy type annotations labels Oct 6, 2019
@henribru henribru changed the title Return Iterator instead of Iterable from __iter__ Type hints: Return Iterator instead of Iterable from __iter__ Oct 6, 2019
Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@fmassa has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@henribru
Copy link
Contributor Author

The failure looks unrelated to my changes.

facebook-github-bot pushed a commit that referenced this pull request Oct 27, 2019
Summary:
I've typed some attributes from https://github.com/pytorch/pytorch/blob/ee920b92c4d52c3481ce4fa1594893fde8ccdd93/torch/csrc/autograd/python_variable.cpp#L490 that were not included in the stubs so that MyPy will be aware of them. I made sure to only add those attributes that are mentioned somewhere in the documentation. If there are attributes mentioned in the documentation that are not meant to be part of the public API (or the opposite), please let me know. I've also made sure that attributes that can't be set are typed as read-only properties. If setting `dtype`, `shape`, `device` or `names` directly is not part of the public API, let me know and I'll make them properties as well.

I've also added `__len__`, `__iter__` and `__contains__`, which means MyPy will no longer complain about `len(t)`, `t1 in t2` and `for t1 in t2`.

Shameless plug: I have another typing-related PR here that needs review: #27445

Fixes #28457
Pull Request resolved: #28578

Reviewed By: lerks

Differential Revision: D18113954

Pulled By: fmassa

fbshipit-source-id: 0b69a2966d22054d8d87392f19ec5aa3918773bc
@facebook-github-bot
Copy link
Contributor

@fmassa merged this pull request in 440b192.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: dataloader Related to torch.utils.data.DataLoader and Sampler module: nn Related to torch.nn module: typing Related to mypy type annotations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants