Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested tuple unpacking in for loops #368

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions mako/codegen.py
Expand Up @@ -1251,8 +1251,13 @@ def visitCallTag(self, node):


_FOR_LOOP = re.compile(
r"^for\s+((?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z0-9_]*),??)*\s*(?:\)?))\s+in\s+(.*):"
r"^for\s+((?:\(?)\s*"
r"(?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z_0-9]*),??)*\s*(?:\)?)"
r"(?:\s*,\s*(?:"
r"(?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z_0-9]*),??)*\s*(?:\)?)"
r"),??)*\s*(?:\)?))\s+in\s+(.*):"
)


Expand Down
10 changes: 10 additions & 0 deletions test/test_loop.py
Expand Up @@ -31,6 +31,16 @@ def test__FOR_LOOP(self):
"x",
"[y+1 for y in [1, 2, 3]]",
),
(
"for ((key1, val1), (key2, val2)) in pairwise(dict.items()):",
"((key1, val1), (key2, val2))",
"pairwise(dict.items())"
),
(
"for (key1, val1), (key2, val2) in pairwise(dict.items()):",
"(key1, val1), (key2, val2)",
"pairwise(dict.items())"
),
):
match = _FOR_LOOP.match(statement)
assert match and match.groups() == (target_list, expression_list)
Expand Down