diff --git a/tests/test_grammator_control_structures.py b/tests/test_grammator_control_structures.py index 92e94f71..8bb27ce3 100644 --- a/tests/test_grammator_control_structures.py +++ b/tests/test_grammator_control_structures.py @@ -835,41 +835,437 @@ def test_for_else_stmt_indent(): "indent": " " }, { - "type": "pass", - }, + "type": "pass", + }, { - "indent": "", - "formatting": [], - "type": "endl", - "value": "\n", - } + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n", + } ] }, "iterator": { - "type": "name", - "value": "i", -}, + "type": "name", + "value": "i", + }, "target": { - "type": "name", - "value": "b", -}, + "type": "name", + "value": "b", + }, "value": [ - { - "type": "endl", - "formatting": [], - "value": "\n", - "indent": " " - }, - { - "type": "pass", - }, - { - "indent": "", - "formatting": [], - "type": "endl", - "value": "\n" - } -], + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], + } + ]) + +def test_for_else_stmt_indent_unpack(): + """ + for i, j in b: + pass + else: + pass + """ + parse_multi([ + ('FOR', 'for', [], [('SPACE', ' ')]), + ('NAME', 'i'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'j'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ('ELSE', 'else'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ], [ + { + "type": "for", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "fifth_formatting": [], + "else": { + "type": "else", + "first_formatting": [], + "second_formatting": [], + "value": [ + { + "type": "endl", + "value": "\n", + "formatting": [], + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n", + } + ] + }, + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, + "value": [ + {"type": "name", "value": "i"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "j"}, + ], + }, + "target": { + "type": "name", + "value": "b", + }, + "value": [ + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], + } + ]) + +def test_for_else_stmt_indent_unpack_paren(): + """ + for (i, j) in b: + pass + else: + pass + """ + parse_multi([ + ('FOR', 'for', [], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '('), + ('NAME', 'i'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'j'), + ('RIGHT_PARENTHESIS', ')'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ('ELSE', 'else'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ], [ + { + "type": "for", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "fifth_formatting": [], + "else": { + "type": "else", + "first_formatting": [], + "second_formatting": [], + "value": [ + { + "type": "endl", + "value": "\n", + "formatting": [], + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n", + } + ] + }, + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "i"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "j"}, + ], + }, + "target": { + "type": "name", + "value": "b", + }, + "value": [ + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], + } + ]) + +def test_for_else_stmt_indent_from_tuple(): + """ + for i in b, c: + pass + else: + pass + """ + parse_multi([ + ('FOR', 'for', [], [('SPACE', ' ')]), + ('NAME', 'i'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'c'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ('ELSE', 'else'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ], [ + { + "type": "for", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "fifth_formatting": [], + "else": { + "type": "else", + "first_formatting": [], + "second_formatting": [], + "value": [ + { + "type": "endl", + "value": "\n", + "formatting": [], + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n", + } + ] + }, + "iterator": { + "type": "name", + "value": "i", + }, + "target": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, + "value": [ + {"type": "name", "value": "b"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "c"}, + ], + }, + "value": [ + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], + } + ]) + +def test_for_else_stmt_indent_from_tuple_paren(): + """ + for i in (b, c): + pass + else: + pass + """ + parse_multi([ + ('FOR', 'for', [], [('SPACE', ' ')]), + ('NAME', 'i'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '('), + ('NAME', 'b'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'c'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ('ELSE', 'else'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ], [ + { + "type": "for", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "fifth_formatting": [], + "else": { + "type": "else", + "first_formatting": [], + "second_formatting": [], + "value": [ + { + "type": "endl", + "value": "\n", + "formatting": [], + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n", + } + ] + }, + "iterator": { + "type": "name", + "value": "i", + }, + "target": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "b"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "c"}, + ], + }, + "value": [ + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], } ]) diff --git a/tests/test_grammator_data_structures.py b/tests/test_grammator_data_structures.py index 0ed4e58d..90be5f99 100644 --- a/tests/test_grammator_data_structures.py +++ b/tests/test_grammator_data_structures.py @@ -729,45 +729,821 @@ def test_generator_comprehension_double_if_if(): ], }, { - "type": "comprehension_loop", + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [ + { + "type": "comprehension_if", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "value": { + "type": "name", + "value": "y" + }, + } + ], + } + ] + } + ]) + +def test_generator_comprehension_double_unpack1(): + "( a for b, c in c for d in e )" + parse_simple([ + ('LEFT_PARENTHESIS', '(', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'c'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ], [ + { + "type": "generator_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, + "value": [ + {"type": "name", "value": "b"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "c"}, + ], + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], + } + ] + } + ]) + +def test_generator_comprehension_double_unpack1_paren(): + "( a for (b, c) in c for d in e )" + parse_simple([ + ('LEFT_PARENTHESIS', '(', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '(', []), + ('NAME', 'b'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'c'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ], [ + { + "type": "generator_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "b"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "c"}, + ], + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], + } + ] + } + ]) + +def test_generator_comprehension_double_unpack2(): + "( a for b in c for c, d in e )" + parse_simple([ + ('LEFT_PARENTHESIS', '(', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ], [ + { + "type": "generator_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, + "value": [ + {"type": "name", "value": "c"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "d"}, + ], + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], + } + ] + } + ]) + +def test_generator_comprehension_double_unpack2_paren(): + "( a for b in c for (c, d) in e )" + parse_simple([ + ('LEFT_PARENTHESIS', '(', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '(', []), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'd'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ], [ + { + "type": "generator_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "c"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "d"}, + ], + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], + } + ] + } + ]) + +def test_generator_comprehension_double_tuple_paren(): + "( a for b in (c, d) for d in e )" + parse_simple([ + ('LEFT_PARENTHESIS', '(', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '(', []), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'd'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ], [ + { + "type": "generator_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "c"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "d"}, + ], + }, + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], + } + ] + } + ]) + +def test_list_comprehension(): + "[ a for b in c ]" + parse_simple([ + ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ], [ + { + "type": "list_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [], + } + ] + } + ]) + + +def test_list_comprehension_if(): + "[ a for b in c if d ]" + parse_simple([ + ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('IF', 'if', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ], [ + { + "type": "list_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [ + { + "type": "comprehension_if", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "value": { + "type": "name", + "value": "d" + }, + } + ], + } + ] + } + ]) + +def test_list_comprehension_if_if(): + "[ a for b in c if d if d ]" + parse_simple([ + ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('IF', 'if', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IF', 'if', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ], [ + { + "type": "list_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [ + { + "type": "comprehension_if", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "value": { + "type": "name", + "value": "d" + }, + }, + { + "type": "comprehension_if", "first_formatting": [{"type": "space", "value": " "}], "second_formatting": [{"type": "space", "value": " "}], - "third_formatting": [{"type": "space", "value": " "}], - "fourth_formatting": [{"type": "space", "value": " "}], - "iterator": { - "type": "name", - "value": "d", - }, - "target": { + "value": { "type": "name", - "value": "e", + "value": "d" }, - "ifs": [ +} + ], + } + ] + } + ]) + +def test_list_comprehension_tuple(): + "[ a for b in c, d ]" + parse_simple([ + ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'd'), + ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ], [ { - "type": "comprehension_if", - "first_formatting": [{"type": "space", "value": " "}], + "type": "list_comprehension", + "first_formatting": [], "second_formatting": [{"type": "space", "value": " "}], - "value": { + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { "type": "name", - "value": "y" + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "tuple", + "with_parenthesis": False, + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "value": [ + { + "type": "name", + "value": "c", + }, + { + "type": "comma", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + }, + { + "type": "name", + "value": "d", + } + ], + }, + "ifs": [], + } + ] + } + ]) + + +def test_list_comprehension_tuple_more(): + "[ a for b in c, d, e ]" + parse_simple([ + ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'd'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ], [ + { + "type": "list_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "b", + }, + "target": { + "type": "tuple", + "with_parenthesis": False, + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "value": [ + { + "type": "name", + "value": "c", + }, + { + "type": "comma", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + }, + { + "type": "name", + "value": "d", + }, + { + "type": "comma", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + }, + { + "type": "name", + "value": "e", + } + ], + }, + "ifs": [], + } + ] } - ], -} + ]) + +def test_list_comprehension_double_unpack1(): + "[ a for b, c in c for d in e ]" + parse_simple([ + ('LEFT_SQUARE_BRACKET', '(', [], [('SPACE', ' ')]), + ('NAME', 'a'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'b'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'c'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_SQUARE_BRACKET', ')', [('SPACE', ' ')]), + ], [ + { + "type": "list_comprehension", + "first_formatting": [], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "result": { + "type": "name", + "value": "a", + }, + "generators": [ + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, + "value": [ + {"type": "name", "value": "b"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "c"}, + ], + }, + "target": { + "type": "name", + "value": "c", + }, + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], + } ] } ]) -def test_list_comprehension(): - "[ a for b in c ]" +def test_list_comprehension_double_unpack1_paren(): + "[ a for b, c in c for d in e ]" parse_simple([ - ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('LEFT_SQUARE_BRACKET', '(', [], [('SPACE', ' ')]), ('NAME', 'a'), ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '(', []), ('NAME', 'b'), + ('COMMA', ',', [], [('SPACE', ' ')]), + ('NAME', 'c'), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'c'), - ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_SQUARE_BRACKET', ')', [('SPACE', ' ')]), ], [ { "type": "list_comprehension", @@ -787,32 +1563,64 @@ def test_list_comprehension(): "third_formatting": [{"type": "space", "value": " "}], "fourth_formatting": [{"type": "space", "value": " "}], "iterator": { - "type": "name", - "value": "b", + "first_formatting": [], + "second_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "b"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] + }, + {"type": "name", "value": "c"}, + ], }, "target": { "type": "name", "value": "c", }, "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], } ] } ]) - -def test_list_comprehension_if(): - "[ a for b in c if d ]" +def test_list_comprehension_double_unpack2(): + "[ a for b in c for c, d in e ]" parse_simple([ - ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('LEFT_SQUARE_BRACKET', '(', [], [('SPACE', ' ')]), ('NAME', 'a'), ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'b'), ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'c'), - ('IF', 'if', [('SPACE', ' ')], [('SPACE', ' ')]), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), ('NAME', 'd'), - ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_SQUARE_BRACKET', ')', [('SPACE', ' ')]), ], [ { "type": "list_comprehension", @@ -839,36 +1647,59 @@ def test_list_comprehension_if(): "type": "name", "value": "c", }, - "ifs": [ - { - "type": "comprehension_if", - "first_formatting": [{"type": "space", "value": " "}], - "second_formatting": [{"type": "space", "value": " "}], - "value": { - "type": "name", - "value": "d" + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, + "value": [ + {"type": "name", "value": "c"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] }, - } - ], + {"type": "name", "value": "d"}, + ], + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], } ] } ]) -def test_list_comprehension_if_if(): - "[ a for b in c if d if d ]" +def test_list_comprehension_double_unpack2_paren(): + "[ a for b in c for (c, d) in e ]" parse_simple([ - ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('LEFT_SQUARE_BRACKET', '(', [], [('SPACE', ' ')]), ('NAME', 'a'), ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'b'), ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'c'), - ('IF', 'if', [('SPACE', ' ')], [('SPACE', ' ')]), - ('NAME', 'd'), - ('IF', 'if', [('SPACE', ' ')], [('SPACE', ' ')]), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '(', []), + ('NAME', 'c'), + ('COMMA', ',', [], [('SPACE', ' ')]), ('NAME', 'd'), - ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_SQUARE_BRACKET', ')', [('SPACE', ' ')]), ], [ { "type": "list_comprehension", @@ -895,35 +1726,45 @@ def test_list_comprehension_if_if(): "type": "name", "value": "c", }, - "ifs": [ - { - "type": "comprehension_if", - "first_formatting": [{"type": "space", "value": " "}], - "second_formatting": [{"type": "space", "value": " "}], - "value": { - "type": "name", - "value": "d" + "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "first_formatting": [], + "second_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, + "value": [ + {"type": "name", "value": "c"}, + { + "first_formatting": [], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] }, - }, - { - "type": "comprehension_if", - "first_formatting": [{"type": "space", "value": " "}], - "second_formatting": [{"type": "space", "value": " "}], - "value": { - "type": "name", - "value": "d" - }, -} - ], + {"type": "name", "value": "d"}, + ], + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], } ] } ]) -def test_list_comprehension_tuple(): - "[ a for b in c, d ]" +def test_list_comprehension_double_tuple(): + "( a for b in c, d for d in e )" parse_simple([ - ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('LEFT_SQUARE_BRACKET', '(', [], [('SPACE', ' ')]), ('NAME', 'a'), ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'b'), @@ -931,7 +1772,11 @@ def test_list_comprehension_tuple(): ('NAME', 'c'), ('COMMA', ',', [], [('SPACE', ' ')]), ('NAME', 'd'), - ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'e'), + ('RIGHT_SQUARE_BRACKET', ')', [('SPACE', ' ')]), ], [ { "type": "list_comprehension", @@ -955,49 +1800,62 @@ def test_list_comprehension_tuple(): "value": "b", }, "target": { - "type": "tuple", - "with_parenthesis": False, "first_formatting": [], "second_formatting": [], "third_formatting": [], "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": False, "value": [ + {"type": "name", "value": "c"}, { - "type": "name", - "value": "c", - }, - { - "type": "comma", "first_formatting": [], - "second_formatting": [{"type": "space", "value": " "}], + "type": "comma", + "second_formatting": [{"type": "space", "value": " "}] }, - { - "type": "name", - "value": "d", - } + {"type": "name", "value": "d"}, ], }, "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], } ] } ]) - -def test_list_comprehension_tuple_more(): - "[ a for b in c, d, e ]" +def test_list_comprehension_double_tuple_paren(): + "( a for b in (c, d) for d in e )" parse_simple([ - ('LEFT_SQUARE_BRACKET', '[', [], [('SPACE', ' ')]), + ('LEFT_SQUARE_BRACKET', '(', [], [('SPACE', ' ')]), ('NAME', 'a'), ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'b'), ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), + ('LEFT_PARENTHESIS', '(', []), ('NAME', 'c'), ('COMMA', ',', [], [('SPACE', ' ')]), ('NAME', 'd'), - ('COMMA', ',', [], [('SPACE', ' ')]), + ('RIGHT_PARENTHESIS', ')', [('SPACE', ' ')]), + ('FOR', 'for', [('SPACE', ' ')], [('SPACE', ' ')]), + ('NAME', 'd'), + ('IN', 'in', [('SPACE', ' ')], [('SPACE', ' ')]), ('NAME', 'e'), - ('RIGHT_SQUARE_BRACKET', ']', [('SPACE', ' ')]), + ('RIGHT_SQUARE_BRACKET', ')', [('SPACE', ' ')]), ], [ { "type": "list_comprehension", @@ -1021,38 +1879,39 @@ def test_list_comprehension_tuple_more(): "value": "b", }, "target": { - "type": "tuple", - "with_parenthesis": False, "first_formatting": [], "second_formatting": [], - "third_formatting": [], + "third_formatting": [{"type": "space", "value": " "}], "fourth_formatting": [], + "type": "tuple", + "with_parenthesis": True, "value": [ + {"type": "name", "value": "c"}, { - "type": "name", - "value": "c", - }, - { - "type": "comma", "first_formatting": [], - "second_formatting": [{"type": "space", "value": " "}], - }, - { - "type": "name", - "value": "d", - }, - { "type": "comma", - "first_formatting": [], - "second_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}] }, - { - "type": "name", - "value": "e", - } + {"type": "name", "value": "d"}, ], }, "ifs": [], + }, + { + "type": "comprehension_loop", + "first_formatting": [{"type": "space", "value": " "}], + "second_formatting": [{"type": "space", "value": " "}], + "third_formatting": [{"type": "space", "value": " "}], + "fourth_formatting": [{"type": "space", "value": " "}], + "iterator": { + "type": "name", + "value": "d", + }, + "target": { + "type": "name", + "value": "e", + }, + "ifs": [], } ] }