-
Notifications
You must be signed in to change notification settings - Fork 25
Add dict comprehension Support #217
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
"""Test Python Transpiler.""" | ||
|
||
# for running tests from local astx module | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove these comments. btw, if you install with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay sure I will do it. I actually did used poetry. |
||
# import os,sys | ||
# # print(os.path.abspath(os.path.join(os.getcwd(), ".", "src"))) | ||
# sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), ".", "src"))) | ||
|
||
import ast | ||
import sys | ||
|
||
|
@@ -1397,3 +1402,40 @@ def test_transpiler_literal_dict() -> None: | |
1: 10, | ||
2: 20, | ||
}, f"Expected '{expected_code}', but got '{generated_code}'" | ||
|
||
|
||
def test_dict_comprehension() -> None: | ||
"""Test astx.DictComprehension.""" | ||
dict_comp_1 = astx.DictComprehension( | ||
key=astx.Identifier("x"), | ||
value=astx.Identifier("x*x"), | ||
iterable=astx.Identifier("x"), | ||
iterator=astx.Identifier("my_list"), | ||
) | ||
|
||
generated_code_1 = transpiler.visit(dict_comp_1) | ||
expected_code_1 = "{x: x*x for x in my_list}" | ||
|
||
dict_comp_2 = astx.DictComprehension( | ||
key=astx.Identifier("x"), | ||
value=astx.Identifier("x*x"), | ||
iterable=astx.Identifier("x"), | ||
iterator=astx.LiteralList( | ||
elements=[ | ||
astx.LiteralInt32(10), | ||
astx.LiteralInt32(20), | ||
astx.LiteralInt32(30), | ||
] | ||
), | ||
) | ||
|
||
generated_code_2 = transpiler.visit(dict_comp_2) | ||
expected_code_2 = "{x: x*x for x in [10, 20, 30]}" | ||
|
||
assert generated_code_1 == expected_code_1, ( | ||
f"Expected code: {expected_code_1} ;" | ||
f" Generated code: {generated_code_1}" | ||
) | ||
assert generated_code_2 == expected_code_2, ( | ||
f"Expected code: {expected_code_2} ;Generated code: {generated_code_2}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key and value probably should be Expr instead of Indentifier ... but maybe we need to discuss a bit other part of this class.
let's consider this example:
se it seems we will also need to add
ifs
and the comprehension class as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will work on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually did considered Expr for these variables but during pre-commit checks I was failing some checks bcoz, Expr did not have value attribute. But my tests was doing fine coz I had initialized key value as identifiers in the object creation.