KEMBAR78
create type hint stub files for module torch by ezyang · Pull Request #16089 · pytorch/pytorch · GitHub
Skip to content

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 ezyang force-pushed the pr/type-hint-1.0.1 branch from 0eaba9a to 7de357d Compare January 16, 2019 22:24
@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.

3 participants