Skip to content

Commit

Permalink
[FIX] pylint_odoo: Fix parsing dictionary from manifest (#510)
Browse files Browse the repository at this point in the history
The following code:
 - `{"key": "" or ""}`

It is parsed correctly the node code

But `ast.literal_eval` shows the following traceback:

```txt
Exception on node <Dict.dict l.3 at 0x10ef6aba0>
Traceback (most recent call last):
  File "pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
    ~~~~~~~~^^^^^^^^^
  File "pylint_odoo/checkers/odoo_addons.py", line 1034, in visit_dict
    manifest_dict = ast.literal_eval(node.as_string())
  File "3.13/lib/python3.13/ast.py", line 114, in literal_eval
    return _convert(node_or_string)
  File "3.13/lib/python3.13/ast.py", line 103, in _convert
    return dict(zip(map(_convert, node.keys),
                    map(_convert, node.values)))
  File "3.13/lib/python3.13/ast.py", line 113, in _convert
    return _convert_signed_num(node)
  File "3.13/lib/python3.13/ast.py", line 87, in _convert_signed_num
    return _convert_num(node)
  File "3.13/lib/python3.13/ast.py", line 78, in _convert_num
    _raise_malformed_node(node)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "3.13/lib/python3.13/ast.py", line 75, in _raise_malformed_node
    raise ValueError(msg + f': {node!r}')
ValueError: malformed node or string on line 1: <ast.BoolOp object at 0x1104df790>
````
  • Loading branch information
moylop260 authored Dec 19, 2024
1 parent fc39ad4 commit 03aa3e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/pylint_odoo/checkers/odoo_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,12 @@ def visit_dict(self, node):
node.parent, nodes.Expr
):
return
manifest_dict = ast.literal_eval(node.as_string())
try:
manifest_dict = ast.literal_eval(node.as_string())
except ValueError:
# There is code that the node is formed but literal_eval raises error
# e.g. {"key": "" or ""}
return
manifest_keys_nodes = {
key_node.value: key_node for key_node, _value in node.items if isinstance(key_node, nodes.Const)
}
Expand Down
4 changes: 4 additions & 0 deletions testing/resources/test_repo/broken_module3/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Verify a dictionary parsed correctly as node but raising error as literal_eval
{
"key": "" or "",
}

0 comments on commit 03aa3e4

Please sign in to comment.