8000 Deprecate and remove usage of from __future__ import annotations in codebase · Issue #117449 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Deprecate and remove usage of from __future__ import annotations in codebase #117449

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

Open
ezyang opened this issue Jan 14, 2024 · 3 comments
Open
Labels
module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@ezyang
Copy link
Contributor
ezyang commented Jan 14, 2024

🐛 Describe the bug

https://peps.python.org/pep-0563/ which introduced from __future__ import annotations has been superseded by https://peps.python.org/pep-0649/ , which was accepted last year. Among other things, the new PEP deprecates the old one, which means we should NOT use the early adoption future pragma in our codebase.

At time of writing, there are 150 occurrences of this pragma in the PyTorch codebase.

cc @malfet @xuzhao9 @gramster @Skylion007

Versions

main

@Skylion007
Copy link
Collaborator

I'm sure they'll be a pyupgrade rule for this shortly that will then get ported into ruff and we can enable it (and it will probably have a nice autofix too).

@malfet malfet added module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jan 15, 2024
@Skylion007
Copy link
Collaborator
Skylion007 commented Feb 16, 2024

@ezyang The Ruff TCH rules could help us migrate away from these statements and switch back to TYPE_CHECKING if statements. This would have the added benefit of likely improving import time. https://docs.astral.sh/ruff/rules/#flake8-type-checking-tch

@XuehaiPan
Copy link
Collaborator
XuehaiPan commented Jun 24, 2024

Postponed annotations are enabled by default in Python 3.10+ (no need for from __future__ import annotations).

Based on NEP 29 - Deprecation Policy, NumPy dropped Python 3.9 support on Apr 05, 2024. Currently, the lowest Python that PyTorch supports is Python 3.8. Maybe there will be a year or more when we drop Python 3.9 support because we need to drop Python 3.8 first :). I think it is worth to have postponed annotations feature before that (at least for tools / torchgen and .pyi stub files).

pytorchmergebot pushed a commit that referenced this issue Jun 28, 2024
…ub files (#129419)

------

- [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: #129419
Approved by: https://github.com/ezyang
ghstack dependencies: #129375, #129376
pytorchmergebot pushed a commit that referenced this issue Jun 29, 2024
…ub files (#129419)

------

- [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: #129419
Approved by: https://github.com/ezyang
ghstack dependencies: #129375, #129376
pytorchmergebot pushed a commit that referenced this issue 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 issue 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
module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants
0