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

Unhandled case of a walrus operator in the output part of a list comprehension #152

Closed
FasterSpeeding opened this issue May 3, 2023 · 1 comment

Comments

@FasterSpeeding
Copy link
Contributor

Describe the bug
Griffe raises a KeyError when it encounters a walrus operator (NamedExpr) in the output part of a list, set or dict comprehension, or a generator expression.

To Reproduce
Steps to reproduce the behavior:

  1. Save the following example code to "test.py"
def bar(value):
    return value

foo = [result if (result := bar(v)) else v for v in (1,2,3)]
  1. Run command griffe dump -s . test
  2. Scroll up to the start of the command's output
  3. See an error like the following:
ERROR      Failed to unparse node <ast.ListComp object at 0x000001C826AAF4C0> at test.py:4: <class 'ast.NamedExpr'>
Traceback (most recent call last):
  File "C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line 1240, in safe_get_value
    return get_value(node)
  File "C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line 1226, in get_value
    return _node_value_map[type(node)](node)
  File "C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line 1005, in _get_listcomp_value
    element = _get_value(node.elt)
  File "C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line 1212, in _get_value
    return _node_value_map[type(node)](node)
  File "C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line 965, in _get_ifexp_value
    return f"{_get_value(node.body)} if {_get_value(node.test)} else {_get_value(node.orelse)}"
  File "C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line 1212, in _get_value
    return _node_value_map[type(node)](node)
KeyError: <class 'ast.NamedExpr'>

Expected behavior
This should parse without error as this is valid syntax (which runs fine).

System (please complete the following information):

  • griffe version: griffe-0.27.1
  • Python version: 3.10.9
  • OS: Windows
pawamoy added a commit that referenced this issue May 3, 2023
@pawamoy
Copy link
Member

pawamoy commented May 3, 2023

Hey @FasterSpeeding, thanks for the report! Fixed in v0.27.2.

@pawamoy pawamoy closed this as completed May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants