8000 [BE] update type annotations for basic utilities in `torch/__init__.py` by XuehaiPan · Pull Request #129001 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[BE] update type annotations for basic utilities in torch/__init__.py #129001

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

Closed
wants to merge 2 commits into from

Conversation

XuehaiPan
Copy link
Collaborator
@XuehaiPan XuehaiPan commented Jun 18, 2024

Changes:

  1. Make some arguments positional-only as we only support Python 3.8+
  2. Clean up torch.typename(obj) implementation.
  3. Update type annotations., especially is_tensor() and is_masked_tensor() using TypeGuard.

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @yf225 @chauhang @d4l3k @ezyang @malfet @xuzhao9 @gramster

@XuehaiPan XuehaiPan added module: typing Related to mypy type annotations better-engineering Relatively self-contained tasks for better engineering contributors topic: not user facing topic category labels Jun 18, 2024
@XuehaiPan XuehaiPan requested a review from Skylion007 June 18, 2024 21:41
Copy link
pytorch-bot bot commented Jun 18, 2024

🔗 Helpful Links

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit a1b6bb8 with merge base d21f311 (image):

BROKEN TRUNK - The following jobs 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.

Comment on lines 25 to 27
import typing
from typing import Any, Callable, Dict, Optional, Set, Tuple, TYPE_CHECKING, Union
from typing_extensions import TypeGuard
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

typing.Type is shadowed by torch._C.Type.

Copy link
Contributor
@malfet malfet left a comment

Choose a reason for hiding this comment

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

LGTM, though please consider just importing typing.Type as _Type(OrtType`)

8000
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 18, 2024
@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

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@XuehaiPan XuehaiPan force-pushed the torch-basic-utilities branch from 5ef38a8 to 70d9e46 Compare June 19, 2024 09:10
@XuehaiPan XuehaiPan requested a review from albanD as a code owner June 19, 2024 09:10
@XuehaiPan XuehaiPan force-pushed the torch-basic-utilities branch 3 times, most recently from 68031a0 to b71e361 Compare June 19, 2024 17:34
@pytorch-bot pytorch-bot bot added the oncall: distributed Add this issue/PR to distributed oncall triage queue label Jun 19, 2024
@XuehaiPan XuehaiPan force-pushed the torch-basic-utilities branch from 7be9159 to 0a67083 Compare June 19, 2024 18:53
@malfet
Copy link
Contributor
malfet commented Jun 19, 2024

Feel free to ignore back compat(as arg rename is false positive). Changes still look OK, but please update the title to reflect why did you have to change more file rather than just init.py

@XuehaiPan XuehaiPan force-pushed the torch-basic-utilities branch from 0a67083 to e002eeb Compare June 20, 2024 07:20
@XuehaiPan XuehaiPan added the suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) label Jun 20, 2024
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@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

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge -f "stuck at win ci"

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

@kit1980
Copy link
Contributor
kit1980 commented Jun 24, 2024

The lint started failing after this was merged:

Lint for torch/_C/init.pyi:

Error (MYPY) [override]
Return type "TensorType | None" of "with_sizes" incompatible with return
type "JitType" in supertype "JitType"

https://github.com/pytorch/pytorch/actions/runs/9647316879/job/26605786852

@XuehaiPan
Copy link
Collaborator Author

@pytorchbot revert -m "lint failure"

Copy link
pytorch-bot bot commented Jun 24, 2024

❌ 🤖 pytorchbot command failed:

@pytorchbot revert: error: the following arguments are required: -c/--classification

usage: @pytorchbot revert -m MESSAGE -c
                          {nosignal,ignoredsignal,landrace,weird,ghfirst}

Try @pytorchbot --help for more info.

@XuehaiPan
Copy link
Collaborator Author

@pytorchbot revert -m "lint failure" -c nosignal

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot
Copy link
Collaborator

@XuehaiPan your PR has been successfully reverted.

pytorchmergebot added a commit that referenced this pull request Jun 24, 2024
…init__.py` (#129001)"

This reverts commit e53d959.

Reverted #129001 on behalf of https://github.com/XuehaiPan due to lint failure ([comment](#129001 (comment)))
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge -f "[reland] lintrunner is green"

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

@XuehaiPan XuehaiPan deleted the torch-basic-utilities branch July 22, 2024 08:29
pytorchmergebot pushed a commit that referenced this pull request May 15, 2025
…s (PEP 585) and Union Type (PEP 604) (#150727)

#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)](https://peps.python.org/pep-0585) 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)](https://peps.python.org/pep-0585): e.g. `typing.List[T] -> list[T]`, `typing.Dict[KT, VT] -> dict[KT, VT]`, `typing.Type[T] -> type[T]`.
- [Union Type (PEP 604)](https://peps.python.org/pep-0604): 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:

- #117449

------

Pull Request resolved: #150727
Approved by: https://github.com/aorenste
ghstack dependencies: #150726
pytorchmergebot pushed a commit that referenced this pull request May 15, 2025
…nd Union Type (PEP 604) (#150728)

#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)](https://peps.python.org/pep-0585) 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)](https://peps.python.org/pep-0585): e.g. `typing.List[T] -> list[T]`, `typing.Dict[KT, VT] -> dict[KT, VT]`, `typing.Type[T] -> type[T]`.
- [Union Type (PEP 604)](https://peps.python.org/pep-0604): 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:

- #117449

------

Pull Request resolved: #150728
Approved by: https://github.com/cyyever, https://github.com/aorenste
ghstack dependencies: #150726, #150727
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: typing Related to mypy type annotations oncall: distributed Add this issue/PR to distributed oncall triage queue open source Reverted suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0