-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Improve static typing for torch.Size #144218
Copy link
Copy link
Closed
Labels
module: typingRelated to mypy type annotationsRelated to mypy type annotationstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Description
🚀 The feature, motivation and pitch
There are several issues with the current type hints for torch.Size:
pytorch/torch/_C/__init__.pyi.in
Lines 172 to 180 in 9f94710
| # Defined in torch/csrc/Size.cpp | |
| class Size(Tuple[_int, ...]): | |
| # TODO: __reduce__ | |
| @overload # type: ignore[override] | |
| def __getitem__(self: Size, key: _int) -> _int: ... | |
| @overload | |
| def __getitem__(self: Size, key: slice) -> Size: ... | |
| def numel(self: Size) -> _int: ... |
It simply subclasses tuple[int, ...], but this causes several typing errors, for example:
import torch
x = torch.Size([1,2,3])
y = torch.Size([4,5,6])
reveal_type(x+y) # tuple[int, ...], not Size !!!This is because tuple.__add__ is annotated to return tuple.
Alternatives
No response
Additional context
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
module: typingRelated to mypy type annotationsRelated to mypy type annotationstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module