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

Improve Tensor type hints #28578

Closed
wants to merge 4 commits into from

Conversation

henribru
Copy link
Contributor

I've typed some attributes from

static struct PyGetSetDef THPVariable_properties[] = {
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

- Add some missing attributes
- Add some missing methods
- Type read-only attributes as properties

Fixes pytorch#28457
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.

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.

torch/__init__.pyi.in Outdated Show resolved Hide resolved
I suspect that python/mypy#4146 could
cause an issue with the layout property.
@henribru
Copy link
Contributor Author

henribru commented Oct 24, 2019

python/mypy#4146 is causing problems it seems. I'll have a look.

Edit: Should be good now.

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.

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.

@facebook-github-bot
Copy link
Contributor

@fmassa merged this pull request in 764e0ee.

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

Successfully merging this pull request may close these issues.

No type hints for Tensor.T
4 participants