summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBatuhan Taskaya <isidentical@gmail.com>2021-04-25 02:31:20 (GMT)
committerGitHub <noreply@github.com>2021-04-25 02:31:20 (GMT)
commit8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32 (patch)
treea6a762b7912545a0825028c9ec8a28186e2f1d13
parent196983563d05e32d2dcf217e955a919f9e0c25e1 (diff)
downloadcpython-8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32.zip
cpython-8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32.tar.gz
cpython-8cc3cfa8afab1651c4f6e9ba43a7ab7f10f64c32.tar.bz2
bpo-42737: annotations with complex targets no longer causes any runtime effects (GH-23952)
-rw-r--r--Doc/whatsnew/3.10.rst4
-rw-r--r--Lib/test/test_future.py11
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2021-04-22-22-48-30.bpo-42737.lsJ7pD.rst2
-rw-r--r--Python/compile.c6
4 files changed, 23 insertions, 0 deletions
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) {