From 6ea49b0f882733e4fd7d0f637d76c2760afc6441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 6 Jul 2022 15:20:55 -0400 Subject: [PATCH 1/4] ENH: prevent 'from __future__ import annotations' --- CHANGELOG.md | 1 + README.md | 1 + pyi.py | 5 +++++ tests/imports.pyi | 3 +++ 4 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index af85cc27..2f3bb34d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Features: * Support Python 3.11. * Support `typing_extensions.overload` and `typing_extensions.NamedTuple`. (The latter is not yet released, but will be in the next version of `typing_extensions`.) +* Introduce Y044: Discourage unnecessary "from __future__ import annotations" import. ## 22.5.1 diff --git a/README.md b/README.md index 94eadbed..45eb33fd 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ currently emitted: | Y040 | Never explicitly inherit from `object`, as all classes implicitly inherit from `object` in Python 3. This error code is incompatible with stubs supporting Python 2. | Y041 | Y041 detects redundant numeric unions. For example, PEP 484 specifies that type checkers should treat `int` as an implicit subtype of `float`, so `int` is redundant in the union `int \| float`. In the same way, `int` is redundant in the union `int \| complex`, and `float` is redundant in the union `float \| complex`. | Y043 | Do not use names ending in "T" for private type aliases. (The "T" suffix implies that an object is a `TypeVar`.) +| Y044 | "from __future__ import annotations" has no effect in stub files. Many error codes enforce modern conventions, and some cannot yet be used in all cases: diff --git a/pyi.py b/pyi.py index f0f8f114..3d9c54e9 100644 --- a/pyi.py +++ b/pyi.py @@ -740,6 +740,10 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None: for obj in imported_objects ): return self.error(node, Y025) + elif module_name == "__future__" and any( + obj.name == "annotations" for obj in imported_objects + ): + return self.error(node, Y044) for obj in imported_objects: self._check_import_or_attribute( @@ -1669,3 +1673,4 @@ def parse_options( Y040 = 'Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3' Y041 = 'Y041 Use "{implicit_supertype}" instead of "{implicit_subtype} | {implicit_supertype}" (see "The numeric tower" in PEP 484)' Y043 = 'Y043 Bad name for a type alias (the "T" suffix implies a TypeVar)' +Y044 = 'Y044 "from __future__ import annotations" has no effect in stub files.' diff --git a/tests/imports.pyi b/tests/imports.pyi index 069cf6b1..1392827d 100644 --- a/tests/imports.pyi +++ b/tests/imports.pyi @@ -3,6 +3,9 @@ # Note: DO NOT RUN ISORT ON THIS FILE. # It's excluded in our pyproject.toml. +# BAD IMPORTS (Y044) +from __future__ import annotations # Y044 "from __future__ import annotations" has no effect in stub files. + # GOOD IMPORTS import typing import typing_extensions From 32ed6143b7ecab0f178498be43b8b0d7acea43c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 6 Jul 2022 16:13:03 -0400 Subject: [PATCH 2/4] Update CHANGELOG.md Co-authored-by: Alex Waygood --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f3bb34d..7a847b97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ Features: * Support Python 3.11. * Support `typing_extensions.overload` and `typing_extensions.NamedTuple`. (The latter is not yet released, but will be in the next version of `typing_extensions`.) -* Introduce Y044: Discourage unnecessary "from __future__ import annotations" import. +* Introduce Y044: Discourage unnecessary `from __future__ import annotations` import. ## 22.5.1 From 6723303fd742d7cd93a873a6282c09cdf3aec06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 6 Jul 2022 16:13:09 -0400 Subject: [PATCH 3/4] Update README.md Co-authored-by: Alex Waygood --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 45eb33fd..6f6bb786 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ currently emitted: | Y040 | Never explicitly inherit from `object`, as all classes implicitly inherit from `object` in Python 3. This error code is incompatible with stubs supporting Python 2. | Y041 | Y041 detects redundant numeric unions. For example, PEP 484 specifies that type checkers should treat `int` as an implicit subtype of `float`, so `int` is redundant in the union `int \| float`. In the same way, `int` is redundant in the union `int \| complex`, and `float` is redundant in the union `float \| complex`. | Y043 | Do not use names ending in "T" for private type aliases. (The "T" suffix implies that an object is a `TypeVar`.) -| Y044 | "from __future__ import annotations" has no effect in stub files. +| Y044 | `from __future__ import annotations` has no effect in stub files, as forward references in stubs are enabled by default. Many error codes enforce modern conventions, and some cannot yet be used in all cases: From 9d85fe0f643584ad4397b9d2197b55f2f097e593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 6 Jul 2022 16:16:34 -0400 Subject: [PATCH 4/4] contribution note --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a847b97..499a3f0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Features: * Support `typing_extensions.overload` and `typing_extensions.NamedTuple`. (The latter is not yet released, but will be in the next version of `typing_extensions`.) * Introduce Y044: Discourage unnecessary `from __future__ import annotations` import. + Contributed by Torsten Wörtwein. ## 22.5.1