-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Use Generic TypeAlias (PEP 585) and Union Type (PEP 604) in generated .pyi
stub files
#129420
base: gh/XuehaiPan/72/base
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/129420
Note: Links to docs will display an error until the docs builds have been completed. ❌ 3 New Failures, 1 Unrelated FailureAs of commit 9d6f11e with merge base dbd6ada (): NEW FAILURES - The following jobs have failed:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
tools/pyi/gen_pyi.py
Outdated
@@ -1088,26 +1087,26 @@ def replace_special_case(hint: str) -> str: | |||
"def __init__(self, other: Tensor) -> None: ...", | |||
f"def __init__(self, size: _size, *, {DEVICE_PARAM}) -> None: ...", | |||
], | |||
"as_subclass": ["def as_subclass(self, cls: _Type[S]) -> S: ..."], | |||
"as_subclass": ["def as_subclass(self, cls: type[S]) -> S: ..."], |
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.
Use type[S]
instead of from typing import Type as _Type
and _Type[S]
. Using _Type
is hard to maintain while one can use Type[S]
which is torch._C.Type[S]
.
@@ -3,25 +3,33 @@ | |||
# mypy: allow-untyped-defs | |||
|
|||
import builtins | |||
from types import EllipsisType |
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.
ellipsis
in builtins
is deprecated. Import EllipsisType = type(Ellipsis)
explicitly.
SupportsIndex, | ||
Tuple, | ||
Type as _Type, |
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.
See commnet for type[S]
and _Type[S]
above.
torch/_C/__init__.pyi.in
Outdated
@@ -128,44 +115,40 @@ class Stream: | |||
device: _device # The device of the stream | |||
|
|||
@overload | |||
def __new__(self, device: Optional[DeviceLikeType] = None, *, priority: _int = 0) -> Stream: ... | |||
def __new__(cls, device: DeviceLikeType | None = None, *, priority: _int = 0) -> Stream: ... |
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.
self -> cls
is auto fixed by ruff
.
def _set_dispatch_mode(mode: TorchDispatchMode) -> None: ... | ||
def _get_dispatch_stack_at(idx: _int) -> Any: ... | ||
def _len_torch_dispatch_stack() -> _int: ... | ||
def _activate_gpu_trace() -> None: ... | ||
|
||
class _DisableTorchDispatch: | ||
def __init__(self): ... | ||
def __init__(self) -> None: ... |
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.
__init__(self, ...) -> None:
is auto fixed by ruff
.
torchgen/code_template.py
Outdated
@@ -42,7 +42,7 @@ def lookup(v: str) -> object: | |||
|
|||
def indent_lines(indent: str, v: Sequence[object]) -> str: | |||
return "".join( | |||
[indent + l + "\n" for e in v for l in str(e).splitlines()] | |||
[(indent + l).rstrip() + "\n" for e in v for l in str(e).splitlines()] |
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.
Removes white spaces in empty lines.
… `.pyi` stub files ghstack-source-id: e3f41c9fb6be223bbd43762b9b948af82a045f83 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: e6675f1506bd806e9593539d656022af6878a6ad Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: e6675f1506bd806e9593539d656022af6878a6ad Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: a9a7750c6e6a5a26f744bc935ecdc008057c390d Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f0695b605d936f8a34731aebf684a3f7871964ef Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f0695b605d936f8a34731aebf684a3f7871964ef Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f0695b605d936f8a34731aebf684a3f7871964ef Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f0695b605d936f8a34731aebf684a3f7871964ef Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f0695b605d936f8a34731aebf684a3f7871964ef Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f0695b605d936f8a34731aebf684a3f7871964ef Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 3806e9533634a37bab9aa41666cf496083c29ee5 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 3806e9533634a37bab9aa41666cf496083c29ee5 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 42e720e3a04c30d4c9c4ede0ad3fa633888617e9 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 903346beee6f507c0a086c3a48cc5cd09e1e4133 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: e971c2fc63604da189c8293ae9b7e6b956fd0cd2 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: e971c2fc63604da189c8293ae9b7e6b956fd0cd2 Pull Request resolved: pytorch#129420
#129001 (comment) is the motivation for the whole stack of PRs. In
torch/__init__.py
,torch._C.Type
shadowsfrom typing import Type
, and there is no type stub fortorch._C.Type
intorch/_C/__init__.pyi
. So we need to usefrom typing import Type as _Type
. After enabling Generic TypeAlias (PEP 585) in the.pyi
type stub files, we can usetype
instead oftyping.Type
orfrom typing import Type as _Type
.Stack from ghstack (oldest at bottom):
lintrunner
on generated.pyi
stub files in CI #129887torch/utils/data/datapipes/gen_pyi.py
withtorchgen
#129873PYI034
: non-self-return-type in tensor method hints #129886__all__
totorch/nn/functional.pyi
andtorch/return_types.pyi
#129872.pyi
stub files #129420torchgen.utils.FileManager
to acceptpathlib.Path
#129871typing.List[T] -> list[T]
,typing.Dict[KT, VT] -> dict[KT, VT]
,typing.Type[T] -> type[T]
.Union[X, Y] -> X | Y
,Optional[X] -> X | None
,Optional[Union[X, Y]] -> X | Y | None
.Note that in
.pyi
stub files, we do not needfrom __future__ import annotations
. So this PR does not violate issue #117449:cc @ezyang @malfet @xuzhao9 @gramster @albanD @Skylion007