Skip to content

Commit 8cc3cfa

Browse files
authored
bpo-42737: annotations with complex targets no longer causes any runtime effects (GH-23952)
1 parent 1969835 commit 8cc3cfa

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Doc/whatsnew/3.10.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ Other Language Changes
791791
Moreover, static methods are now callable as regular functions.
792792
(Contributed by Victor Stinner in :issue:`43682`.)
793793
794+
* Annotations for complex targets (everything beside ``simple name`` targets
795+
defined by :pep:`526`) no longer cause any runtime effects with ``from __future__ import annotations``.
796+
(Contributed by Batuhan Taskaya in :issue:`42737`.)
797+
794798
795799
New Modules
796800
===========

Lib/test/test_future.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ async def f2() -> {ann}:
134134
...
135135
async def g2(arg: {ann}) -> None:
136136
...
137+
class H:
138+
var: {ann}
139+
object.attr: {ann}
137140
var: {ann}
138141
var2: {ann} = None
142+
object.attr: {ann}
139143
"""
140144
)
141145

@@ -343,6 +347,13 @@ def test_infinity_numbers(self):
343347
self.assertAnnotationEqual("('inf', 1e1000, 'infxxx', 1e1000j)", expected=f"('inf', {inf}, 'infxxx', {infj})")
344348
self.assertAnnotationEqual("(1e1000, (1e1000j,))", expected=f"({inf}, ({infj},))")
345349

350+
def test_annotation_with_complex_target(self):
351+
with self.assertRaises(SyntaxError):
352+
exec(
353+
"from __future__ import annotations\n"
354+
"object.__debug__: int"
355+
)
356+
346357

347358
if __name__ == "__main__":
348359
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Annotations for complex targets (everything beside simple names) no longer
2+
cause any runtime effects with ``from __future__ import annotations``.

Python/compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5356,6 +5356,12 @@ check_ann_expr(struct compiler *c, expr_ty e)
53565356
static int
53575357
check_annotation(struct compiler *c, stmt_ty s)
53585358
{
5359+
/* Annotations of complex targets does not produce anything
5360+
under annotations future */
5361+
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5362+
return 1;
5363+
}
5364+
53595365
/* Annotations are only evaluated in a module or class. */
53605366
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
53615367
c->u->u_scope_type == COMPILER_SCOPE_CLASS) {

0 commit comments

Comments
 (0)