[Python-checkins] bpo-42737: annotations with complex targets no longer causes any runtime effects (GH-23952)

isidentical webhook-mailer at python.org
Sat Apr 24 22:31:28 EDT 2021


https://github.com/python/cpython/commit/8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32
commit: 8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32
branch: master
author: Batuhan Taskaya <isidentical at gmail.com>
committer: isidentical <isidentical at gmail.com>
date: 2021-04-25T05:31:20+03:00
summary:

bpo-42737: annotations with complex targets no longer causes any runtime effects (GH-23952)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst
M Doc/whatsnew/3.10.rst
M Lib/test/test_future.py
M Python/compile.c

diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 78f3c2d36b845..dac44cf03fa32 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 e4715587d21cf..8a09853d1f78f 100644
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -134,8 +134,12 @@ async def f2() -> {ann}:
             ...
         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 @@ def test_infinity_numbers(self):
         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 0000000000000..e55db436896af
--- /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 1b7a2e83b16ba..2cf2f4a382457 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) {



More information about the Python-checkins mailing list