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

This commit is contained in:
Batuhan Taskaya 2021-04-25 05:31:20 +03:00 committed by GitHub
parent 196983563d
commit 8cc3cfa8af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 0 deletions

View 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
===========

View 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()

View File

@ -0,0 +1,2 @@
Annotations for complex targets (everything beside simple names) no longer
cause any runtime effects with ``from __future__ import annotations``.

View 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) {