From 8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Sun, 25 Apr 2021 05:31:20 +0300 Subject: bpo-42737: annotations with complex targets no longer causes any runtime effects (GH-23952) --- Doc/whatsnew/3.10.rst | 4 ++++ Lib/test/test_future.py | 11 +++++++++++ .../2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst | 2 ++ Python/compile.c | 6 ++++++ 4 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 78f3c2d..dac44cf 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -791,6 +791,10 @@ Other Language Changes Moreover, static methods are now callable as regular functions. (Contributed by Victor Stinner in :issue:`43682`.) +* Annotations for complex targets (everything beside ``simple name`` targets + defined by :pep:`526`) no longer cause any runtime effects with ``from __future__ import annotations``. + (Contributed by Batuhan Taskaya in :issue:`42737`.) + New Modules =========== diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py index e471558..8a09853 100644 --- a/Lib/test/test_future.py +++ b/Lib/test/test_future.py @@ -134,8 +134,12 @@ class AnnotationsFutureTestCase(unittest.TestCase): ... async def g2(arg: {ann}) -> None: ... + class H: + var: {ann} + object.attr: {ann} var: {ann} var2: {ann} = None + object.attr: {ann} """ ) @@ -343,6 +347,13 @@ class AnnotationsFutureTestCase(unittest.TestCase): self.assertAnnotationEqual("('inf', 1e1000, 'infxxx', 1e1000j)", expected=f"('inf', {inf}, 'infxxx', {infj})") self.assertAnnotationEqual("(1e1000, (1e1000j,))", expected=f"({inf}, ({infj},))") + def test_annotation_with_complex_target(self): + with self.assertRaises(SyntaxError): + exec( + "from __future__ import annotations\n" + "object.__debug__: int" + ) + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst new file mode 100644 index 0000000..e55db43 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst @@ -0,0 +1,2 @@ +Annotations for complex targets (everything beside simple names) no longer +cause any runtime effects with ``from __future__ import annotations``. diff --git a/Python/compile.c b/Python/compile.c index 1b7a2e8..2cf2f4a 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5356,6 +5356,12 @@ check_ann_expr(struct compiler *c, expr_ty e) static int check_annotation(struct compiler *c, stmt_ty s) { + /* Annotations of complex targets does not produce anything + under annotations future */ + if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { + return 1; + } + /* Annotations are only evaluated in a module or class. */ if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || c->u->u_scope_type == COMPILER_SCOPE_CLASS) { -- cgit v0.12