-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
Add inference of Compare nodes #979
Merged
Pierre-Sassoulas
merged 3 commits into
pylint-dev:main
from
nelfin:feature/846-literal-compare
Sep 14, 2021
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,11 @@ | |
"""this module contains a set of functions to handle inference on astroid trees | ||
""" | ||
|
||
import ast | ||
import functools | ||
import itertools | ||
import operator | ||
from typing import Any, Iterable | ||
|
||
import wrapt | ||
|
||
|
@@ -798,6 +800,98 @@ def infer_binop(self, context=None): | |
nodes.BinOp._infer_binop = _infer_binop | ||
nodes.BinOp._infer = infer_binop | ||
|
||
COMPARE_OPS = { | ||
"==": operator.eq, | ||
"!=": operator.ne, | ||
"<": operator.lt, | ||
"<=": operator.le, | ||
">": operator.gt, | ||
">=": operator.ge, | ||
"in": lambda a, b: a in b, | ||
"not in": lambda a, b: a not in b, | ||
} | ||
UNINFERABLE_OPS = { | ||
"is", | ||
"is not", | ||
} | ||
Comment on lines
+805
to
+808
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a frozenset |
||
|
||
|
||
def _to_literal(node: nodes.NodeNG) -> Any: | ||
# Can raise SyntaxError or ValueError from ast.literal_eval | ||
# Is this the stupidest idea or the simplest idea? | ||
return ast.literal_eval(node.as_string()) | ||
Comment on lines
+813
to
+814
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that! Something like this here would also work, but isn't as clean if isinstance(node, nodes.Const):
return node.value
if isinstance(node, nodes.Tuple):
return tuple(_to_literal(val) for val in node.elts)
if isinstance(node, nodes.List):
return [_to_literal(val) for val in node.elts]
if isinstance(node, nodes.Set):
return set(_to_literal(val) for val in node.elts)
if isinstance(node, nodes.Dict):
return {_to_literal(k): _to_literal(v) for k, v in node.items} |
||
|
||
|
||
def _do_compare( | ||
left_iter: Iterable[nodes.NodeNG], op: str, right_iter: Iterable[nodes.NodeNG] | ||
) -> "bool | type[util.Uninferable]": | ||
""" | ||
If all possible combinations are either True or False, return that: | ||
>>> _do_compare([1, 2], '<=', [3, 4]) | ||
True | ||
>>> _do_compare([1, 2], '==', [3, 4]) | ||
False | ||
|
||
If any item is uninferable, or if some combinations are True and some | ||
are False, return Uninferable: | ||
>>> _do_compare([1, 3], '<=', [2, 4]) | ||
util.Uninferable | ||
""" | ||
retval = None | ||
if op in UNINFERABLE_OPS: | ||
return util.Uninferable | ||
op_func = COMPARE_OPS[op] | ||
|
||
for left, right in itertools.product(left_iter, right_iter): | ||
if left is util.Uninferable or right is util.Uninferable: | ||
return util.Uninferable | ||
|
||
try: | ||
left, right = _to_literal(left), _to_literal(right) | ||
except (SyntaxError, ValueError): | ||
return util.Uninferable | ||
|
||
try: | ||
expr = op_func(left, right) | ||
except TypeError as exc: | ||
raise AstroidTypeError from exc | ||
|
||
if retval is None: | ||
retval = expr | ||
elif retval != expr: | ||
return util.Uninferable | ||
# (or both, but "True | False" is basically the same) | ||
|
||
return retval # it was all the same value | ||
|
||
|
||
def _infer_compare(self: nodes.Compare, context: contextmod.InferenceContext) -> Any: | ||
"""Chained comparison inference logic.""" | ||
retval = True | ||
|
||
ops = self.ops | ||
left_node = self.left | ||
lhs = list(left_node.infer(context=context)) | ||
# should we break early if first element is uninferable? | ||
for op, right_node in ops: | ||
# eagerly evaluate rhs so that values can be re-used as lhs | ||
rhs = list(right_node.infer(context=context)) | ||
try: | ||
retval = _do_compare(lhs, op, rhs) | ||
except AstroidTypeError: | ||
retval = util.Uninferable | ||
break | ||
if retval is not True: | ||
break # short-circuit | ||
lhs = rhs # continue | ||
if retval is util.Uninferable: | ||
yield retval | ||
else: | ||
yield nodes.Const(retval) | ||
|
||
|
||
nodes.Compare._infer = _infer_compare | ||
Pierre-Sassoulas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _infer_augassign(self, context=None): | ||
"""Inference logic for augmented binary operations.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would help inferring
op_func
andexpr
correctly.