From 82d7fafce09ee94ce537b453f5ebf053966b7734 Mon Sep 17 00:00:00 2001 From: Andrew Haigh Date: Tue, 14 Sep 2021 17:05:48 +1000 Subject: Add inference of Compare nodes (#979) * Add inference for Compare nodes Ref #846. Identity checks are currently Uninferable as there is no sensible way to infer that two Instances refer to the same object without accurately modelling control flow. Co-authored-by: Pierre Sassoulas --- astroid/inference.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'astroid') diff --git a/astroid/inference.py b/astroid/inference.py index fd2735eb..02be060d 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -27,9 +27,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 @@ -790,6 +792,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", +} + + +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()) + + +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: 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 + def _infer_augassign(self, context=None): """Inference logic for augmented binary operations.""" -- cgit v1.2.1