pytorch
22d3ac79 - [torchgen] Prettify generated type annotations (#95877)

Commit
2 years ago
[torchgen] Prettify generated type annotations (#95877) Changes: 1. Use class inheritance for `torch/return_types.pyi`: Before: ```python max = NamedTuple("max", [("values", Tensor), ("indices", Tensor)]) ``` After: ```python class max(NamedTuple): values: Tensor indices: Tensor ``` ------ 2. Add missing spaces in generated type annotations. 1. Always has a space after `,`. 2. If an argument is annotated, then there need spaces around `=` when it has a default value. ```diff - def func(..., out: Optional[Tensor]=None, ...) -> Tensor: + def func(..., out: Optional[Tensor] = None, ...) -> Tensor: ``` 3. If an argument is not annotated, then there should be no spaces around `=` when it has a default value. ```python def contiguous(self, memory_format=torch.contiguous_format) -> Tensor: ... ``` ------ 3. ~Remove redundant import alias in `torch/nn/functional.pyi`:~ (Reverted) UPDATE: `mypy` needs the alias to work. Before: ```python from .. import conv1d as conv1d from .. import conv2d as conv2d from .. import conv3d as conv3d from .. import conv_transpose1d as conv_transpose1d from .. import conv_transpose2d as conv_transpose2d from .. import conv_transpose3d as conv_transpose3d from .. import conv_tbc as conv_tbc from .. import avg_pool1d as avg_pool1d from .. import relu_ as relu_ from .. import selu_ as selu_ from .. import celu_ as celu_ from .. import rrelu_ as rrelu_ from .. import pixel_shuffle as pixel_shuffle from .. import pixel_unshuffle as pixel_unshuffle from .. import channel_shuffle as channel_shuffle from .. import native_channel_shuffle as native_channel_shuffle from .. import pdist as pdist from .. import cosine_similarity as cosine_similarity ``` After: ```python from .. import ( conv1d, conv2d, conv3d, conv_transpose1d, conv_transpose2d, conv_transpose3d, conv_tbc, avg_pool1d, relu_, selu_, celu_, rrelu_, pixel_shuffle, pixel_unshuffle, channel_shuffle, native_channel_shuffle, pdist, cosine_similarity, ) ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/95877 Approved by: https://github.com/ezyang
Author
Committer
Parents
Loading