From 66ecfa8a01e3eae02e54c19c301eca4d3f7193de Mon Sep 17 00:00:00 2001 From: brendanator Date: Fri, 22 Jun 2018 13:46:05 +0100 Subject: Improve as_string output of operators, elif, with, return & docs The precedence and associativity rules of operators are respected and parens are only wrapped around child nodes when needed A single If node inside the else block is output as `elif` Unneccesary parens in with statements are removed Unneccesary parens in tuple returns are removed Doc strings in classes and functions no longer get additional indenting --- astroid/tests/testdata/python2/data/module.py | 15 ++++++------ astroid/tests/testdata/python2/data/module2.py | 28 +++++++++++----------- .../testdata/python2/data/operator_precedence.py | 27 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 astroid/tests/testdata/python2/data/operator_precedence.py (limited to 'astroid/tests/testdata/python2') diff --git a/astroid/tests/testdata/python2/data/module.py b/astroid/tests/testdata/python2/data/module.py index 6a67b9b6..1205c1dd 100644 --- a/astroid/tests/testdata/python2/data/module.py +++ b/astroid/tests/testdata/python2/data/module.py @@ -23,7 +23,8 @@ def global_access(key, val): class YO: - """hehe""" + """hehe + haha""" a = 1 def __init__(self): @@ -45,7 +46,8 @@ class YOUPI(YO): self.member = None def method(self): - """method test""" + """method + test""" global MY_DICT try: MY_DICT = {} @@ -53,9 +55,8 @@ class YOUPI(YO): autre = [a for (a, b) in MY_DICT if b] if b in autre: return b - else: - if a in autre: - return a + elif a in autre: + return a global_access(local, val=autre) finally: return local @@ -81,9 +82,9 @@ def four_args(a, b, c, d): else: b += -2 if c: - d = ((a) and (b)) or (c) + d = a and (b or c) else: - c = ((a) and (b)) or (d) + c = a and b or d map(lambda x, y: (y, x), a) redirect = four_args diff --git a/astroid/tests/testdata/python2/data/module2.py b/astroid/tests/testdata/python2/data/module2.py index 0a1bd1ad..7042a4c1 100644 --- a/astroid/tests/testdata/python2/data/module2.py +++ b/astroid/tests/testdata/python2/data/module2.py @@ -71,10 +71,10 @@ del YO.member del YO [SYN1, SYN2] = (Concrete0, Concrete1) assert '1' -b = (1) | (((2) & (3)) ^ (8)) -bb = ((1) | (two)) | (6) -ccc = ((one) & (two)) & (three) -dddd = ((x) ^ (o)) ^ (r) +b = (1 | 2) & (3 ^ 8) +bb = 1 | (two | 6) +ccc = one & two & three +dddd = x ^ (o ^ r) exec 'c = 3' exec 'c = 3' in {}, {} @@ -82,15 +82,15 @@ def raise_string(a=2, *args, **kwargs): raise Exception, 'yo' yield 'coucou' yield -a = (b) + (2) -c = (b) * (2) -c = (b) / (2) -c = (b) // (2) -c = (b) - (2) -c = (b) % (2) -c = (b) ** (2) -c = (b) << (2) -c = (b) >> (2) +a = b + 2 +c = b * 2 +c = b / 2 +c = b // 2 +c = b - 2 +c = b % 2 +c = b**2 +c = b << 2 +c = b >> 2 c = ~b c = not b d = [c] @@ -131,7 +131,7 @@ def not_a_generator(): def generator(): yield - genl = lambda : (yield) + genl = lambda: (yield) def with_metaclass(meta, *bases): return meta('NewBase', bases, {}) diff --git a/astroid/tests/testdata/python2/data/operator_precedence.py b/astroid/tests/testdata/python2/data/operator_precedence.py new file mode 100644 index 00000000..07a3c772 --- /dev/null +++ b/astroid/tests/testdata/python2/data/operator_precedence.py @@ -0,0 +1,27 @@ +assert not not True == True +assert (not False or True) == True +assert True or False and True +assert (True or False) and True + +assert True is not (False is True) == False +assert True is (not False is True == False) + +assert 1 + 2 + 3 == 6 +assert 5 - 4 + 3 == 4 +assert 4 - 5 - 6 == -7 +assert 7 - (8 - 9) == 8 +assert 2**3**4 == 2**81 +assert (2**3)**4 == 8**4 + +assert 1 + 2 if (0.5 if True else 0.2) else 1 if True else 2 == 3 +assert (0 if True else 1) if False else 2 == 2 +assert lambda x: x if (0 if False else 0) else 0 if False else 0 +assert (lambda x: x) if (0 if True else 0.2) else 1 if True else 2 == 1 + +assert ('1' + '2').replace('1', '3') == '32' +assert (lambda x: x)(1) == 1 +assert ([0] + [1])[1] == 1 +assert (lambda x: lambda: x + 1)(2)() == 3 + +f = lambda x, y, z: y(x, z) +assert f(1, lambda x, y: x + y[1], (2, 3)) == 4 -- cgit v1.2.1