Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,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
===========
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
"""
)

Expand Down Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Annotations for complex targets (everything beside simple names) no longer
cause any runtime effects with ``from __future__ import annotations``.
6 changes: 6 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5350,6 +5350,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) {
Expand Down