-
-
Notifications
You must be signed in to change notification settings - Fork 292
Change frame
and scope
of NamedExpr
for certain parents
#1221
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
247a3d8
acd25f2
d9b6028
e445e3c
c19b2a3
d9a8dbd
f9e2e19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -682,6 +682,117 @@ def hello(False): | |
builder.parse(code) | ||
|
||
|
||
@pytest.mark.skipif(not PY38_PLUS, reason="needs assignment expressions") | ||
class TestNamedExprNode: | ||
"""Tests for the NamedExpr node""" | ||
|
||
@staticmethod | ||
def test_frame() -> None: | ||
"""Test if the frame of NamedExpr is correctly set for certain types | ||
of parent nodes. | ||
""" | ||
module = builder.parse( | ||
""" | ||
def func(var_1): | ||
pass | ||
|
||
def func_two(var_2, var_2 = (named_expr_1 := "walrus")): | ||
pass | ||
|
||
class MyBaseClass: | ||
pass | ||
|
||
class MyInheritedClass(MyBaseClass, var_3=(named_expr_2 := "walrus")): | ||
pass | ||
|
||
VAR = lambda y = (named_expr_3 := "walrus"): print(y) | ||
|
||
def func_with_lambda( | ||
var_5 = ( | ||
named_expr_4 := lambda y = (named_expr_5 := "walrus"): y | ||
) | ||
): | ||
pass | ||
|
||
COMPREHENSION = [y for i in (1, 2) if (y := i ** 2)] | ||
""" | ||
) | ||
function = module.body[0] | ||
assert function.args.frame() == function | ||
|
||
function_two = module.body[1] | ||
assert function_two.args.args[0].frame() == function_two | ||
assert function_two.args.args[1].frame() == function_two | ||
assert function_two.args.defaults[0].frame() == module | ||
DanielNoord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
inherited_class = module.body[3] | ||
assert inherited_class.keywords[0].frame() == inherited_class | ||
assert inherited_class.keywords[0].value.frame() == module | ||
|
||
lambda_assignment = module.body[4].value | ||
assert lambda_assignment.args.args[0].frame() == lambda_assignment | ||
assert lambda_assignment.args.defaults[0].frame() == module | ||
|
||
lambda_named_expr = module.body[5].args.defaults[0] | ||
assert lambda_named_expr.value.args.defaults[0].frame() == module | ||
|
||
comprehension = module.body[6].value | ||
assert comprehension.generators[0].ifs[0].frame() == module | ||
|
||
@staticmethod | ||
def test_scope() -> None: | ||
"""Test if the scope of NamedExpr is correctly set for certain types | ||
of parent nodes. | ||
""" | ||
module = builder.parse( | ||
""" | ||
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. Maybe we can create a fixture for the example? It seems the same example is used for frame and scope. 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. We could, although that might also make these tests less flexible in the future. I can explore this, but personally I'm fine with keeping it like this. 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. The example is the same. I'm just not sure it does make much sense to create a fixture for it. 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. Let's keep it that way. |
||
def func(var_1): | ||
pass | ||
|
||
def func_two(var_2, var_2 = (named_expr_1 := "walrus")): | ||
pass | ||
|
||
class MyBaseClass: | ||
pass | ||
|
||
class MyInheritedClass(MyBaseClass, var_3=(named_expr_2 := "walrus")): | ||
pass | ||
|
||
VAR = lambda y = (named_expr_3 := "walrus"): print(y) | ||
|
||
def func_with_lambda( | ||
var_5 = ( | ||
named_expr_4 := lambda y = (named_expr_5 := "walrus"): y | ||
) | ||
): | ||
pass | ||
|
||
COMPREHENSION = [y for i in (1, 2) if (y := i ** 2)] | ||
""" | ||
) | ||
function = module.body[0] | ||
assert function.args.scope() == function | ||
|
||
function_two = module.body[1] | ||
assert function_two.args.args[0].scope() == function_two | ||
assert function_two.args.args[1].scope() == function_two | ||
assert function_two.args.defaults[0].scope() == module | ||
|
||
inherited_class = module.body[3] | ||
assert inherited_class.keywords[0].scope() == inherited_class | ||
assert inherited_class.keywords[0].value.scope() == module | ||
|
||
lambda_assignment = module.body[4].value | ||
assert lambda_assignment.args.args[0].scope() == lambda_assignment | ||
assert lambda_assignment.args.defaults[0].scope() | ||
|
||
lambda_named_expr = module.body[5].args.defaults[0] | ||
assert lambda_named_expr.value.args.defaults[0].scope() == module | ||
|
||
comprehension = module.body[6].value | ||
assert comprehension.generators[0].ifs[0].scope() == module | ||
|
||
|
||
class AnnAssignNodeTest(unittest.TestCase): | ||
def test_primitive(self) -> None: | ||
code = textwrap.dedent( | ||
|
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 suppose it was discussed with Marc (sorry I did not read all the comment), and adding typing here should be done later and the docstring is enough for now ?
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 think the docstring is enough for now. I will need some guidance from @cdce8p to actually fully type
frame()
.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.
Yeah, typing
frame
is a bit complicated due to the current class / mixin structure. We should tackle that separately.