-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Use Generic TypeAlias (PEP 585) and Union Type (PEP 604) in generated .pyi
stub files
#129420
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
🔗 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. ❌ 4 New Failures, 1 Unrelated FailureAs of commit 13ae75b with merge base 09c4da9 ( 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: 30623d4 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 30623d4 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 1e80e66 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 33706e9 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 33706e9 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 33706e9 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 9e49fdb Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: bdd93e6 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: bdd93e6 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: f8aa3d1 Pull Request resolved: pytorch/pytorch#129420
… `.pyi` stub files ghstack-source-id: 3ed051e Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 697aa2f Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 697aa2f Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: ec656d8 Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 0712fde Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 0712fde Pull Request resolved: pytorch#129420
… `.pyi` stub files ghstack-source-id: 0712fde 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
#150626__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