Skip to content

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

Description

@FasterSpeeding

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"
defbar(value):
returnvaluefoo= [resultif (result:=bar(v)) elsevforvin (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:
ERRORFailedtounparsenode<ast.ListCompobjectat0x000001C826AAF4C0>attest.py:4:<class'ast.NamedExpr'>Traceback (mostrecentcalllast):
File"C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line1240, insafe_get_valuereturnget_value(node)
File"C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line1226, inget_valuereturn_node_value_map[type(node)](node)
File"C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line1005, in_get_listcomp_valueelement=_get_value(node.elt)
File"C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line1212, in_get_valuereturn_node_value_map[type(node)](node)
File"C:\Users\Lucina\PycharmProjects\tanjun\venv\lib\site-packages\griffe\agents\nodes.py", line965, in_get_ifexp_valuereturnf"{_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", line1212, in_get_valuereturn_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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions