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

create type hint stub files for module torch #16089

Merged
merged 4 commits into from Jan 24, 2019

Conversation

ezyang
Copy link
Contributor

@ezyang ezyang commented Jan 16, 2019

This was backported from #12500 but with tests removed and
the stub file checked in directly, so we don't have to also
make sure all build shenanigans work. Master will be properly
tested. The stub file was generated by running
'python setup.py create_pyi' and then copying the generated
pyi file in the build directory (find -name *.pyi) to torch/

Original PR description:

This is more a basis for discussion that a ready solution,
as it does lots of funny things, and for many of them
a better solution will be found.

  • Initial stab at creating a type torch/init.pyi .
  • We only do this for Python 3 because we want to
    use type hint introspection.
  • So far, we only aim at doing this for torch functions
    and torch.Tensor.
  • We need to import the newly build torch. Thus we
    do this at the end of the build.
    We use os.fork to only import the module in a child
    process.
  • We use an annotate decorator to be able to put
    type hints on the native python functions in a way that
    a) they're available in the usual place for Python 3
    b) we stay Python 2 compatible
  • Some annotatons in torch/functional.py are provided
    as examples, but the remaining ones still need to be done.

This could end up fixing #7318

cc @t-vi @soumith

@soumith
Copy link
Member

soumith commented Jan 16, 2019

@ezyang can you rebase so that tests can run

This is more a basis for discussion that a ready solution,
as it does lots of funny things, and for many of them
a better solution will be found.

- Initial stab at creating a type torch/__init__.pyi .
- We only do this for Python 3 because we want to
  use type hint introspection.
- So far, we only aim at doing this for torch functions
  and torch.Tensor.
- We need to import the newly build torch. Thus we
  do this at the end of the build.
  We use os.fork to only import the module in a child
  process.
- We use an annotate decorator to be able to put
  type hints on the native python functions in a way that
  a) they're available in the usual place for Python 3
  b) we stay Python 2 compatible
- Some annotatons in torch/functional.py are provided
  as examples, but the remaining ones still need to be done.

This could end up fixing pytorch#7318

This was backported from pytorch#12500 but with tests removed and
the stub file checked in directly, so we don't have to also
make sure all build shenanigans work.  Master will be properly
tested.  The stub file was generated by running
'python setup.py create_pyi' and then copying the generated
pyi file in the build directory (find -name *.pyi) to torch/
@ezyang
Copy link
Contributor Author

ezyang commented Jan 16, 2019

Done.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
@ezyang ezyang requested a review from soumith January 17, 2019 06:36
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
@soumith soumith added this to the 1.0.1 milestone Jan 20, 2019
@ezyang
Copy link
Contributor Author

ezyang commented Jan 22, 2019

This is ready

@soumith soumith merged commit 6dd31de into pytorch:v1.0.1 Jan 24, 2019
@soumith soumith added the cherry-picked This PR was cherry-picked onto a release branch from master label Jan 24, 2019
soumith pushed a commit that referenced this pull request Jan 29, 2019
* create type hint stub files for module torch

This is more a basis for discussion that a ready solution,
as it does lots of funny things, and for many of them
a better solution will be found.

- Initial stab at creating a type torch/__init__.pyi .
- We only do this for Python 3 because we want to
  use type hint introspection.
- So far, we only aim at doing this for torch functions
  and torch.Tensor.
- We need to import the newly build torch. Thus we
  do this at the end of the build.
  We use os.fork to only import the module in a child
  process.
- We use an annotate decorator to be able to put
  type hints on the native python functions in a way that
  a) they're available in the usual place for Python 3
  b) we stay Python 2 compatible
- Some annotatons in torch/functional.py are provided
  as examples, but the remaining ones still need to be done.

This could end up fixing #7318

This was backported from #12500 but with tests removed and
the stub file checked in directly, so we don't have to also
make sure all build shenanigans work.  Master will be properly
tested.  The stub file was generated by running
'python setup.py create_pyi' and then copying the generated
pyi file in the build directory (find -name *.pyi) to torch/

* Ignore pyi in flake8

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

* Real fix for lint error

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-picked This PR was cherry-picked onto a release branch from master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants