-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Delete torch/__init__.pyi, deferring to direct extension stubs #38157
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
Conversation
This removes the error prone process of assembling torch/__init__.pyi (and frequently forgetting to expose things), since now we can simply rely on the true source file to get things done. Most of the old codegen in gen_pyi.py is now rerouted to various files: - torch/_C/__init__.pyi (the dumping pile of all misc bindings) - torch/_C/_nn.pyi (NN function bindings) - torch/_C/_VariableFunctions.pyi (torch function bindings) torch.types grew a bunch more definitions that previously where defined in torch/__init__.pyi Some miscellaneous changes - Fixed a bug where we treat single TensorList argument as implying varargs are accepted. This is actually only supported on IntList. This means we can correctly generate a stub for dequantize. - Add missing manual stub for nonzero - Switched torch/onnx/operators.py to directly refer to _C module, since apparently mypy doesn't think that methods prefixed with underscores get reexported. This may be a recurring theme; maybe we need to find a better way to solve it. Because I was really lazy, I dumped namedtuple definitions in both torch._C and torch._C._VariableFunctions. This is definitely wrong. Signed-off-by: Edward Z. Yang <ezyang@fb.com> [ghstack-poisoned]
This removes the error prone process of assembling torch/__init__.pyi (and frequently forgetting to expose things), since now we can simply rely on the true source file to get things done. Most of the old codegen in gen_pyi.py is now rerouted to various files: - torch/_C/__init__.pyi (the dumping pile of all misc bindings) - torch/_C/_nn.pyi (NN function bindings) - torch/_C/_VariableFunctions.pyi (torch function bindings) torch.types grew a bunch more definitions that previously where defined in torch/__init__.pyi Some miscellaneous changes - Fixed a bug where we treat single TensorList argument as implying varargs are accepted. This is actually only supported on IntList. This means we can correctly generate a stub for dequantize. - Add missing manual stub for nonzero - Switched torch/onnx/operators.py to directly refer to _C module, since apparently mypy doesn't think that methods prefixed with underscores get reexported. This may be a recurring theme; maybe we need to find a better way to solve it. Because I was really lazy, I dumped namedtuple definitions in both torch._C and torch._C._VariableFunctions. This is definitely wrong. Signed-off-by: Edward Z. Yang <ezyang@fb.com> ghstack-source-id: 7c33437 Pull Request resolved: #38157
💊 CI failures summary and remediationsAs of commit 6fac1e1 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker. This comment has been revised 10 times. |
…tubs" This removes the error prone process of assembling torch/__init__.pyi (and frequently forgetting to expose things), since now we can simply rely on the true source file to get things done. Most of the old codegen in gen_pyi.py is now rerouted to various files: - torch/_C/__init__.pyi (the dumping pile of all misc bindings) - torch/_C/_nn.pyi (NN function bindings) - torch/_C/_VariableFunctions.pyi (torch function bindings) torch.types grew a bunch more definitions that previously where defined in torch/__init__.pyi Some miscellaneous changes - Fixed a bug where we treat single TensorList argument as implying varargs are accepted. This is actually only supported on IntList. This means we can correctly generate a stub for dequantize. - Add missing manual stub for nonzero - Switched torch/onnx/operators.py to directly refer to _C module, since apparently mypy doesn't think that methods prefixed with underscores get reexported. This may be a recurring theme; maybe we need to find a better way to solve it. Because I was really lazy, I dumped namedtuple definitions in both torch._C and torch._C._VariableFunctions. This is definitely wrong. Signed-off-by: Edward Z. Yang <ezyang@fb.com> [ghstack-poisoned]
This removes the error prone process of assembling torch/__init__.pyi (and frequently forgetting to expose things), since now we can simply rely on the true source file to get things done. Most of the old codegen in gen_pyi.py is now rerouted to various files: - torch/_C/__init__.pyi (the dumping pile of all misc bindings) - torch/_C/_nn.pyi (NN function bindings) - torch/_C/_VariableFunctions.pyi (torch function bindings) torch.types grew a bunch more definitions that previously where defined in torch/__init__.pyi Some miscellaneous changes - Fixed a bug where we treat single TensorList argument as implying varargs are accepted. This is actually only supported on IntList. This means we can correctly generate a stub for dequantize. - Add missing manual stub for nonzero - Switched torch/onnx/operators.py to directly refer to _C module, since apparently mypy doesn't think that methods prefixed with underscores get reexported. This may be a recurring theme; maybe we need to find a better way to solve it. Because I was really lazy, I dumped namedtuple definitions in both torch._C and torch._C._VariableFunctions. This is definitely wrong. Signed-off-by: Edward Z. Yang <ezyang@fb.com> ghstack-source-id: 300b9f6 Pull Request resolved: #38157
_dtype = torch.dtype | ||
_device = torch.device | ||
_qscheme = torch.qscheme | ||
_size = Union[torch.Size, List[_int], Tuple[_int, ...]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little inconsistent compared with all the other underscored names above, one would expect _size
to mean torch.size
.
The generated _C/__init__.pyi
now has a number of signatures with:
self, size: _size, *, dtype: _dtype=None, layout: _layout=strided, device: Union[_device, str, None]=None,
I think it would make sense to change _device
above to _device: Union[torch.device, str, None]
so the signatures become:
self, size: _size, *, dtype: _dtype=None, layout: _layout=strided, device: _device=None, ...
And if people need to use device
rather than that union to annotate a device parameter, they can always use the explicit torch.device
, the fully-qualified name should avoid mypy/issues/4146
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing to note is that these names are all grandfathered from the old type stubs. So while I'm generally amenable to changing things up, it will greatly increase the surface area of the patch.
Hmm, CI passed but I'm getting a number of errors locally like:
I'll do a full rebuild, maybe some codegen doesn't get triggered correctly on a partial rebuild. |
# top-level values. The underscore variants let us refer to these | ||
# types. See https://github.com/python/mypy/issues/4146 for why these | ||
# workarounds is necessary | ||
_int = builtins.int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is not really a mypy bug, the issue is that torch.int
exists and means torch.int32
rather than builtins.int
. Given that torch.types
cannot be used in the main torch
namespace (have to avoid an import cycle), the better approach here would be that there's no shadowing in any other modules, so one can use plain int
there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth writing down the rules for what goes into torch.types
, and making that a consistent design. Now for types._name
, name
can refer to a builtin, something in the torch
namespace, or a useful Union
defined inside types
.
I'd lean towards being conservative for now, and only define unions here. The aliasing problem can be avoided by being careful in other files, and using fully-qualified names like torch.layout
.
@ezyang and I discussed offline: let's merge |
…tubs" This removes the error prone process of assembling `torch/__init__.pyi` (and frequently forgetting to expose things), since now we can simply rely on the true source file to get things done. Most of the old codegen in gen_pyi.py is now rerouted to various files: - `torch/_C/__init__.pyi` (the dumping pile of all misc bindings) - `torch/_C/_nn.pyi` (NN function bindings) - `torch/_C/_VariableFunctions.pyi` (torch function bindings) `torch.types` grew a bunch more definitions that previously where defined in `torch/__init__.pyi` Some miscellaneous changes - Fixed a bug where we treat single TensorList argument as implying varargs are accepted. This is actually only supported on IntList. This means we can correctly generate a stub for dequantize. - Add missing manual stub for nonzero - Switched torch/onnx/operators.py to directly refer to _C module, since apparently mypy doesn't think that methods prefixed with underscores get reexported. This may be a recurring theme; maybe we need to find a better way to solve it. Because I was really lazy, I dumped namedtuple definitions in both `torch._C` and `torch._C._VariableFunctions`. This is definitely wrong. Signed-off-by: Edward Z. Yang <ezyang@fb.com> [ghstack-poisoned]
…tubs" This removes the error prone process of assembling `torch/__init__.pyi` (and frequently forgetting to expose things), since now we can simply rely on the true source file to get things done. Most of the old codegen in gen_pyi.py is now rerouted to various files: - `torch/_C/__init__.pyi` (the dumping pile of all misc bindings) - `torch/_C/_nn.pyi` (NN function bindings) - `torch/_C/_VariableFunctions.pyi` (torch function bindings) `torch.types` grew a bunch more definitions that previously where defined in `torch/__init__.pyi` Some miscellaneous changes - Fixed a bug where we treat single TensorList argument as implying varargs are accepted. This is actually only supported on IntList. This means we can correctly generate a stub for dequantize. - Add missing manual stub for nonzero - Switched torch/onnx/operators.py to directly refer to _C module, since apparently mypy doesn't think that methods prefixed with underscores get reexported. This may be a recurring theme; maybe we need to find a better way to solve it. Because I was really lazy, I dumped namedtuple definitions in both `torch._C` and `torch._C._VariableFunctions`. This is definitely wrong. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Differential Revision: [D21497400](https://our.internmc.facebook.com/intern/diff/D21497400) [ghstack-poisoned]
PR pytorch#38157 fixed type checking for mypy by including `if False` guards on some type-checker-only imports. However other typecheckers - like pyright - will respect this logic and ignore the imports. Using the TYPE_CHECKING var instead means both mypy and pyright will work.
Summary: PR #38157 fixed type checking for mypy by including `if False` guards on some type-checker-only imports. However other typecheckers - [like pyright](microsoft/pylance-release#262 (comment)) - will respect this logic and ignore the imports. Using [`if TYPE_CHECKING`](https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING) instead means both mypy and pyright will work correctly. [For background, an example of where the current code fails](microsoft/pylance-release#262) is if you make a file `tmp.py` with the contents ```python import torch torch.ones((1,)) ``` Then [`pyright tmp.py --lib`](https://github.com/microsoft/pyright#command-line) will fail with a `"ones" is not a known member of module` error. This is because it can't find the `_VariableFunctions.pyi` stub file, as pyright respects the `if False` logic. After adding the `TYPE_CHECKING` guard, all works correctly. Credit to erictraut for suggesting the fix. Pull Request resolved: #43339 Reviewed By: agolynski Differential Revision: D23348142 Pulled By: ezyang fbshipit-source-id: c8a58122a7b0016845c311da39a1cc48748ba03f
Stack from ghstack:
This removes the error prone process of assembling
torch/__init__.pyi
(and frequently forgetting to expose things), since now we can simply
rely on the true source file to get things done. Most of the old
codegen in gen_pyi.py is now rerouted to various files:
torch/_C/__init__.pyi
(the dumping pile of all misc bindings)torch/_C/_nn.pyi
(NN function bindings)torch/_C/_VariableFunctions.pyi
(torch function bindings)torch.types
grew a bunch more definitions that previously wheredefined in
torch/__init__.pyi
Some miscellaneous changes
varargs are accepted. This is actually only supported on IntList.
This means we can correctly generate a stub for dequantize.
since apparently mypy doesn't think that methods prefixed with
underscores get reexported. This may be a recurring theme; maybe
we need to find a better way to solve it.
Because I was really lazy, I dumped namedtuple definitions in both
torch._C
andtorch._C._VariableFunctions
. This is definitely wrong.Signed-off-by: Edward Z. Yang ezyang@fb.com
Differential Revision: D21497400