1
0
forked from VimPlug/jedi

implement !=, ==, is, is not operators to work in if statements (they also work in in non if contexts and return a bool value.), includes tests.

This commit is contained in:
Dave Halter
2014-08-12 17:59:09 +02:00
parent 6f018e4884
commit 8ed89e8245
3 changed files with 59 additions and 3 deletions

View File

@@ -412,6 +412,9 @@ magic_function_class = CompiledObject(type(load_module), parent=builtin)
generator_obj = CompiledObject(_a_generator(1.0)) generator_obj = CompiledObject(_a_generator(1.0))
type_names = [] # Need this, because it's return in get_defined_names. type_names = [] # Need this, because it's return in get_defined_names.
type_names = builtin.get_by_name('type').get_defined_names() type_names = builtin.get_by_name('type').get_defined_names()
none_obj = builtin.get_by_name('None')
false_obj = builtin.get_by_name('False')
true_obj = builtin.get_by_name('True')
def compiled_objects_cache(func): def compiled_objects_cache(func):

View File

@@ -1,14 +1,24 @@
""" """
Handles operator precedence. Handles operator precedence.
""" """
import operator
from jedi._compatibility import unicode from jedi._compatibility import unicode
from jedi.parser import representation as pr from jedi.parser import representation as pr
from jedi import debug from jedi import debug
from jedi.common import PushBackIterator from jedi.common import PushBackIterator
from jedi.evaluate.compiled import CompiledObject, create, builtin from jedi.evaluate.compiled import (CompiledObject, create, builtin, false_obj,
true_obj, none_obj)
from jedi.evaluate import analysis from jedi.evaluate import analysis
# Maps Python syntax to the operator module.
OPERATOR_MAPPING = {
'==': operator.eq,
'!=': operator.ne,
'is': operator.is_,
'is not': operator.is_not,
}
class PythonGrammar(object): class PythonGrammar(object):
""" """
@@ -260,6 +270,15 @@ def _is_list(obj):
return isinstance(obj, iterable.Array) and obj.type == pr.Array.LIST return isinstance(obj, iterable.Array) and obj.type == pr.Array.LIST
def _keyword_from_value(obj):
if obj is None:
return none_obj
elif obj is False:
return false_obj
elif obj is True:
return true_obj
def _element_calculate(evaluator, left, operator, right): def _element_calculate(evaluator, left, operator, right):
from jedi.evaluate import iterable, representation as er from jedi.evaluate import iterable, representation as er
l_is_num = _is_number(left) l_is_num = _is_number(left)
@@ -282,6 +301,13 @@ def _element_calculate(evaluator, left, operator, right):
# With strings and numbers the left type typically remains. Except for # With strings and numbers the left type typically remains. Except for
# `int() % float()`. # `int() % float()`.
return [left] return [left]
elif operator in OPERATOR_MAPPING:
operation = OPERATOR_MAPPING[operator]
if isinstance(left, CompiledObject) and isinstance(right, CompiledObject):
# Possible, because the return is not an option. Just compare.
left = left.obj
right = right.obj
return [_keyword_from_value(operation(left, right))]
def check(obj): def check(obj):
"""Checks if a Jedi object is either a float or an int.""" """Checks if a Jedi object is either a float or an int."""

View File

@@ -88,12 +88,41 @@ def check(a):
if a is None: if a is None:
return 1 return 1
return '' return ''
return set
#? int() #? int()
check(None) check(None)
#? str() #? str()
check('asb') check('asb')
a = list
if 2 == True:
a = set
elif 1 == True:
a = 0
#? int()
a
if check != 1:
a = ''
#? str()
a
if check == check:
a = list
#? list
a
if check != check:
a = set
else:
a = dict
#? dict
a
if (check is not check):
a = 1
#? dict
a
# ----------------- # -----------------
# name resolution # name resolution
# ----------------- # -----------------
@@ -121,5 +150,3 @@ else:
#? int #? int
a a