Skip to content

Commit

Permalink
Fix regression on Compare node inference (#1185)
Browse files Browse the repository at this point in the history
This deals with pylint-dev/pylint#5048
  • Loading branch information
DanielNoord authored Sep 21, 2021
1 parent 5b516c9 commit 4ffdf11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Release date: TBA
Closes PyCQA/pylint#5030
Closes PyCQA/pylint#5036

* Fix regression on Compare node inference

Closes PyCQA/pylint#5048


What's New in astroid 2.8.0?
============================
Expand Down
3 changes: 2 additions & 1 deletion astroid/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ def infer_binop(self, context=None):

def _to_literal(node: nodes.NodeNG) -> Any:
# Can raise SyntaxError or ValueError from ast.literal_eval
# Can raise AttributeError from node.as_string() as not all nodes have a visitor
# Is this the stupidest idea or the simplest idea?
return ast.literal_eval(node.as_string())

Expand Down Expand Up @@ -840,7 +841,7 @@ def _do_compare(

try:
left, right = _to_literal(left), _to_literal(right)
except (SyntaxError, ValueError):
except (SyntaxError, ValueError, AttributeError):
return util.Uninferable

try:
Expand Down
12 changes: 12 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5527,6 +5527,18 @@ def func(a, b):
assert inferred[0] is util.Uninferable


def test_compare_unknown() -> None:
code = """
def func(a):
if tuple() + (a[1],) in set():
raise Exception()
"""
node = extract_node(code)
inferred = list(node.infer())
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.FunctionDef)


def test_limit_inference_result_amount() -> None:
"""Test setting limit inference result amount"""
code = """
Expand Down

0 comments on commit 4ffdf11

Please sign in to comment.