From 09854ae6ca1a791e7078937f78066c8c920bf3fe Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 4 Apr 2014 15:07:45 +0200 Subject: [PATCH] negative factors like -1 are now evaluated --- jedi/evaluate/precedence.py | 36 +++++++++++++++++++++++++----------- test/completion/operators.py | 2 ++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/jedi/evaluate/precedence.py b/jedi/evaluate/precedence.py index 63961d46..fab13749 100644 --- a/jedi/evaluate/precedence.py +++ b/jedi/evaluate/precedence.py @@ -175,34 +175,48 @@ def _check_operator(iterator, priority=PythonGrammar.LOWEST_PRIORITY): def calculate(left_result, operator, right_result): - if not left_result or not right_result: - return left_result + right_result - result = [] - for left in left_result: + if left_result is None and right_result: + # cases like `-1` or `1 + ~1` for right in right_result: - result += _element_calculate(left, operator, right) + result.append(_factor_calculate(operator, right)) + return result + else: + if not left_result or not right_result: + return left_result + right_result + + for left in left_result: + for right in right_result: + result += _element_calculate(left, operator, right) return result +def _factor_calculate(operator, right): + if _is_number(right): + if operator == '-': + return create(-right.obj) + return right + + +def _is_number(obj): + return isinstance(obj, CompiledObject) \ + and isinstance(obj.obj, (int, float)) + + def _element_calculate(left, operator, right): def is_string(obj): return isinstance(obj, CompiledObject) \ and isinstance(obj.obj, (str, unicode)) - def is_number(obj): - return isinstance(obj, CompiledObject) \ - and isinstance(obj.obj, (int, float)) - if operator == '*': # for iterables, ignore * operations from jedi.evaluate import iterable if isinstance(left, iterable.Array) or is_string(left): return [left] elif operator == '+': - if is_number(left) and is_number(right) or is_string(left) and is_string(right): + if _is_number(left) and _is_number(right) or is_string(left) and is_string(right): return [create(left.obj + right.obj)] elif operator == '-': - if is_number(left) and is_number(right): + if _is_number(left) and _is_number(right): return [create(left.obj - right.obj)] return [left, right] diff --git a/test/completion/operators.py b/test/completion/operators.py index 9c8083ac..de0db41d 100644 --- a/test/completion/operators.py +++ b/test/completion/operators.py @@ -18,6 +18,8 @@ index = 0 + 1 #? str() x[index] +#? int() +x[1 + (-1)] def calculate(number): return number + constant