Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Response to Serhiy's review
  • Loading branch information
Lukasz Langa committed Dec 31, 2017
commit 7f88115354474dafa8f613bca31e988e8e129926
8 changes: 7 additions & 1 deletion Include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ PyAPI_FUNC(mod_ty) PyAST_FromNodeObject(
PyCompilerFlags *flags,
PyObject *filename,
PyArena *arena);
PyAPI_FUNC(PyObject *) PyAST_UnicodeFromAstExpr(

#ifndef Py_LIMITED_API

/* _PyAST_ExprAsUnicode is defined in ast_unparse.c */
PyAPI_FUNC(PyObject *) _PyAST_ExprAsUnicode(
expr_ty e,
int omit_parens,
int omit_string_brackets);

#endif /* !Py_LIMITED_API */

#ifdef __cplusplus
}
#endif
Expand Down
63 changes: 40 additions & 23 deletions Lib/test/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class AnnotationsFutureTestCase(unittest.TestCase):
from __future__ import annotations
def f() -> {ann}:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test also an argument's annotation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the same code but sure, I can add tests with that, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

...
def g(arg: {ann}) -> None:
...
var: {ann}
var2: {ann} = None
"""
Expand All @@ -118,12 +120,14 @@ def f() -> {ann}:
def getActual(self, annotation):
scope = {}
exec(self.template.format(ann=annotation), {}, scope)
func_ann = scope['f'].__annotations__['return']
func_ret_ann = scope['f'].__annotations__['return']
func_arg_ann = scope['g'].__annotations__['arg']
var_ann1 = scope['__annotations__']['var']
var_ann2 = scope['__annotations__']['var2']
self.assertEqual(func_ann, var_ann1)
self.assertEqual(func_ann, var_ann2)
return func_ann
self.assertEqual(func_ret_ann, func_arg_ann)
self.assertEqual(func_ret_ann, var_ann1)
self.assertEqual(func_ret_ann, var_ann2)
return func_ret_ann

def assertAnnotationEqual(
self, annotation, drop_parens=False, is_tuple=False
Expand Down Expand Up @@ -162,41 +166,44 @@ def test_annotations(self):
eq('Name1 or (Name2 and Name3)')
eq('(Name1 and Name2) or (Name3 and Name4)')
eq('Name1 or (Name2 and Name3) or Name4')
eq('1 << 2')
eq('1 >> 2')
eq('((1 + 2) - (3 * 4)) ^ (((5 ** 6) / 7) // 8)')
eq('not True')
eq('~True')
eq('+1')
eq('v1 << 2')
eq('1 >> v2')
eq(r'1 % finished')
eq('((1 + v2) - (v3 * 4)) ^ (((5 ** v6) / 7) // 8)')
eq('not great')
eq('~great')
eq('+value')
eq('-1')
eq('(~int) and (not ((True ^ (123 + 2)) | True))')
eq('(~int) and (not ((v1 ^ (123 + v2)) | True))')
eq('lambda arg: None')
eq('lambda a=True: a')
eq('lambda a, b, c=True: a')
eq("lambda a, b, c=True, *, d=(1 << 2), e='str': a")
eq("lambda a, b, c=True, *vararg, d=(1 << 2), e='str', **kwargs: a + b")
eq("lambda a, b, c=True, *, d=(1 << v2), e='str': a")
eq("lambda a, b, c=True, *vararg, d=(v1 << 2), e='str', **kwargs: a + b")
eq('1 if True else 2')
eq('(str or None) if True else (str or bytes or None)')
eq('(str or None) if (1 if True else 2) else (str or bytes or None)')
eq("{'2.7': dead, '3.7': (long_live or die_hard)}")
eq("{'2.7': dead, '3.7': (long_live or die_hard), **{'3.6': verygood}}")
eq("{**a, **b, **c}")
eq("{'2.7', '3.6', '3.7', '3.8', '3.9', ('4.0' if gilectomy else '3.10')}")
eq("({'a': 'b'}, (True or False), (+1), 'string', b'bytes') or None")
eq("({'a': 'b'}, (True or False), (+value), 'string', b'bytes') or None")
eq("()")
eq("(1, )")
eq("(1,)")
eq("(1, 2)")
eq("(1, 2, 3)")
eq("[]")
eq("[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]")
eq("{i for i in (1, 2, 3)}")
eq("{(i ** 2) for i in (1, 2, 3)}")
eq("{(i ** 2) for i, _ in [(1, 'a'), (2, 'b'), (3, 'c')]}")
eq("{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}")
eq("{((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)}")
eq("[i for i in (1, 2, 3)]")
eq("[(i ** 2) for i in (1, 2, 3)]")
eq("[(i ** 2) for i, _ in [(1, 'a'), (2, 'b'), (3, 'c')]]")
eq("[(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))]")
eq("[((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]")
eq("{i: 0 for i in (1, 2, 3)}")
eq("{i: j for i, j in [(1, 'a'), (2, 'b'), (3, 'c')]}")
eq(r"{i: 0 for i in (1, 2, 3)}")
eq("{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}")
eq("Python3 > Python2 > COBOL")
eq("Life is Life")
eq("call()")
Expand All @@ -206,11 +213,21 @@ def test_annotations(self):
eq("call(arg, another, kwarg='hey', **kwargs)")
eq("lukasz.langa.pl")
eq("call.me(maybe)")
eq("1 .real")
eq("1.0 .real")
eq("....__class__")
eq("list[str]")
eq("dict[str, int]")
eq("tuple[str, ...]")
eq("tuple[str, int, float, dict[str, int]]")
eq('(str or None) if (sys.version_info[0] > (3, )) else (str or bytes or None)')
eq("slice[0]")
eq("slice[0:1]")
eq("slice[0:1:2]")
eq("slice[:]")
eq("slice[:-1]")
eq("slice[1:]")
eq("slice[::-1]")
eq('(str or None) if (sys.version_info[0] > (3,)) else (str or bytes or None)')

def test_annotations_no_fstring_support_implemented(self):
# FIXME: Add f-string support in ast_unparse.c.
Expand All @@ -234,8 +251,8 @@ def test_annotations_inexact(self):
eq('Name1 or Name2 and Name3')
eq('Name1 and Name2 or Name3 and Name4')
eq('Name1 or Name2 and Name3 or Name4')
eq('1 + 2 - 3 * 4 ^ 5 ** 6 / 7 // 8')
eq('~int and not True ^ 123 + 2 | True')
eq('1 + v2 - v3 * 4 ^ v5 ** 6 / 7 // 8')
eq('~int and not v1 ^ 123 + v2 | True')
eq('str or None if True else str or bytes or None')
eq("{'2.7': dead, '3.7': long_live or die_hard}")
eq("{'2.7', '3.6', '3.7', '3.8', '3.9', '4.0' if gilectomy else '3.10'}")
Expand All @@ -252,7 +269,7 @@ def test_annotations_inexact(self):
eq("(Good, Bad, Ugly)")
eq("(i for i in (1, 2, 3))")
eq("((i ** 2) for i in (1, 2, 3))")
eq("((i ** 2) for i, _ in [(1, 'a'), (2, 'b'), (3, 'c')])")
eq("((i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c')))")
eq("(((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3))")
eq("(*starred)")
eq('(yield from outside_of_generator)')
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@
<ClCompile Include="..\Python\ast_opt.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\ast_unparse.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\bltinmodule.c">
<Filter>Python</Filter>
</ClCompile>
Expand Down
Loading