Skip to content

[BE] Update .pyi stub template to use Generic TypeAlias (PEP 585) and Union Type (PEP 604)#150728

Closed
XuehaiPan wants to merge 24 commits intogh/XuehaiPan/265/basefrom
gh/XuehaiPan/265/head
Closed

[BE] Update .pyi stub template to use Generic TypeAlias (PEP 585) and Union Type (PEP 604)#150728
XuehaiPan wants to merge 24 commits intogh/XuehaiPan/265/basefrom
gh/XuehaiPan/265/head

Conversation

@XuehaiPan
Copy link
Collaborator

@XuehaiPan XuehaiPan commented Apr 5, 2025

#129001 (comment) is the motivation for the whole stack of PRs. In torch/__init__.py, torch._C.Type shadows from typing import Type, and there is no type stub for torch._C.Type in torch/_C/__init__.pyi. So we need to use from typing import Type as _Type. After enabling Generic TypeAlias (PEP 585) in the .pyi type stub files, we can use type instead of typing.Type or from typing import Type as _Type.


  • Generic TypeAlias (PEP 585): e.g. typing.List[T] -> list[T], typing.Dict[KT, VT] -> dict[KT, VT], typing.Type[T] -> type[T].
  • Union Type (PEP 604): e.g. 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 need from __future__ import annotations. So this PR does not violate issue #117449:


Stack from ghstack (oldest at bottom):

cc @ezyang @malfet @xuzhao9 @gramster @bhosmer @bdhirsh @kadeng

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Apr 5, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/150728

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 0bf4d53 with merge base e2ce17c (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
[ghstack-poisoned]
@XuehaiPan XuehaiPan requested review from Skylion007 and Copilot April 5, 2025 08:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (3)
  • torch/_C/_VariableFunctions.pyi.in: Language not supported
  • torch/_C/_nn.pyi.in: Language not supported
  • torch/_C/return_types.pyi.in: Language not supported

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
XuehaiPan added 3 commits May 2, 2025 16:28
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 2, 2025
…nd Union Type (PEP 604)

ghstack-source-id: 1b3c032
Pull Request resolved: pytorch#150728
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 10, 2025
…nd Union Type (PEP 604)

ghstack-source-id: d3c3cda
Pull Request resolved: pytorch#150728
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 14, 2025
…nd Union Type (PEP 604)

ghstack-source-id: 64ee282
Pull Request resolved: pytorch#150728
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 14, 2025
…nd Union Type (PEP 604)

ghstack-source-id: bb1495f
Pull Request resolved: pytorch#150728
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 15, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions github-actions bot deleted the gh/XuehaiPan/265/head branch June 18, 2025 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

better-engineering Relatively self-contained tasks for better engineering contributors ciflow/trunk Trigger trunk jobs on your pull request Merged module: codegen Issues related to the codegen for Aten and Autograd module: typing Related to mypy type annotations open source topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants