]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42737: annotations with complex targets no longer causes any runtime effects...
authorBatuhan Taskaya <isidentical@gmail.com>
Sun, 25 Apr 2021 02:31:20 +0000 (05:31 +0300)
committerGitHub <noreply@github.com>
Sun, 25 Apr 2021 02:31:20 +0000 (05:31 +0300)
Doc/whatsnew/3.10.rst
Lib/test/test_future.py
Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst [new file with mode: 0644]
Python/compile.c

index 78f3c2d36b845c19f649a159af26dbfb127d4bd5..dac44cf03fa32b6bba5485953ccf1300bf3821da 100644 (file)
@@ -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
 ===========
index e4715587d21cf2013465c7777a1a57fde5404ea7..8a09853d1f78f8c333adbd037e909e5fe61d6ed3 100644 (file)
@@ -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 (file)
index 0000000..e55db43
--- /dev/null
@@ -0,0 +1,2 @@
+Annotations for complex targets (everything beside simple names) no longer
+cause any runtime effects with ``from __future__ import annotations``.
index 1b7a2e83b16ba96de66ce55009cd4322e727b8e3..2cf2f4a3824570f33400160524b270f0371a2a13 100644 (file)
@@ -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) {