Skip to content

Commit cfd1ab3

Browse files
committed
Add NamedExpr binding test
1 parent c3b8bb6 commit cfd1ab3

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

test/test_bind_names_python312.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,63 @@ def test_alias_paramspec_default():
165165
'''
166166

167167
assert_namespace_tree(source, expected_namespaces)
168+
169+
def test_namedexpr_in_module():
170+
if sys.version_info < (3, 8):
171+
pytest.skip('Test is for > python3.8 only')
172+
173+
source = '''
174+
(a := 1)
175+
'''
176+
177+
expected_namespaces = '''
178+
+ Module
179+
- NameBinding(name='a', allow_rename=True) <references=1>
180+
'''
181+
182+
assert_namespace_tree(source, expected_namespaces)
183+
184+
def test_namedexpr_in_function():
185+
if sys.version_info < (3, 8):
186+
pytest.skip('Test is for > python3.8 only')
187+
188+
source = '''
189+
def test():
190+
(a := 1)
191+
lambda x: (x := 1)
192+
'''
193+
194+
expected_namespaces = '''
195+
+ Module
196+
- NameBinding(name='test', allow_rename=True) <references=1>
197+
+ Function test
198+
- NameBinding(name='a', allow_rename=True) <references=1>
199+
+ Lambda
200+
- NameBinding(name='x', allow_rename=False) <references=2>
201+
'''
202+
203+
assert_namespace_tree(source, expected_namespaces)
204+
205+
def test_namedexpr():
206+
if sys.version_info < (3, 8):
207+
pytest.skip('Test is for > python3.8 only')
208+
209+
source = '''
210+
def f(arg, /):
211+
print([x for y in range(10) if (x := y // 2) & 1])
212+
print(arg, arg)
213+
'''
214+
215+
expected_namespaces = '''
216+
+ Module
217+
- NameBinding(name='f', allow_rename=True) <references=1>
218+
- BuiltinBinding(name='print', allow_rename=True) <references=2>
219+
- BuiltinBinding(name='range', allow_rename=True) <references=1>
220+
+ Function f
221+
- NameBinding(name='arg', allow_rename=True) <references=3>
222+
- NameBinding(name='x', allow_rename=True) <references=2>
223+
+ ListComp
224+
- NameBinding(name='y', allow_rename=True) <references=2>
225+
'''
226+
227+
assert_namespace_tree(source, expected_namespaces)

0 commit comments

Comments
 (0)