forked from VimPlug/jedi
Make generators return more correct values with while loops, fixes #683
This commit is contained in:
@@ -3,8 +3,10 @@ from jedi.common.utils import monkeypatch
|
|||||||
|
|
||||||
|
|
||||||
class AbstractLazyValue(object):
|
class AbstractLazyValue(object):
|
||||||
def __init__(self, data):
|
def __init__(self, data, min=1, max=1):
|
||||||
self.data = data
|
self.data = data
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s>' % (self.__class__.__name__, self.data)
|
return '<%s: %s>' % (self.__class__.__name__, self.data)
|
||||||
@@ -26,16 +28,16 @@ class LazyKnownValues(AbstractLazyValue):
|
|||||||
|
|
||||||
|
|
||||||
class LazyUnknownValue(AbstractLazyValue):
|
class LazyUnknownValue(AbstractLazyValue):
|
||||||
def __init__(self):
|
def __init__(self, min=1, max=1):
|
||||||
super(LazyUnknownValue, self).__init__(None)
|
super(LazyUnknownValue, self).__init__(None, min, max)
|
||||||
|
|
||||||
def infer(self):
|
def infer(self):
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
|
|
||||||
class LazyTreeValue(AbstractLazyValue):
|
class LazyTreeValue(AbstractLazyValue):
|
||||||
def __init__(self, context, node):
|
def __init__(self, context, node, min=1, max=1):
|
||||||
super(LazyTreeValue, self).__init__(node)
|
super(LazyTreeValue, self).__init__(node, min, max)
|
||||||
self.context = context
|
self.context = context
|
||||||
# We need to save the predefined names. It's an unfortunate side effect
|
# We need to save the predefined names. It's an unfortunate side effect
|
||||||
# that needs to be tracked otherwise results will be wrong.
|
# that needs to be tracked otherwise results will be wrong.
|
||||||
|
|||||||
@@ -797,7 +797,8 @@ def check_tuple_assignments(name, value_set):
|
|||||||
if isinstance(index, slice):
|
if isinstance(index, slice):
|
||||||
# For no star unpacking is not possible.
|
# For no star unpacking is not possible.
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
for _ in range(index + 1):
|
i = 0
|
||||||
|
while i <= index:
|
||||||
try:
|
try:
|
||||||
lazy_value = next(iterated)
|
lazy_value = next(iterated)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
@@ -806,6 +807,8 @@ def check_tuple_assignments(name, value_set):
|
|||||||
# index number is high. Therefore break if the loop is
|
# index number is high. Therefore break if the loop is
|
||||||
# finished.
|
# finished.
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
else:
|
||||||
|
i += lazy_value.max
|
||||||
value_set = lazy_value.infer()
|
value_set = lazy_value.infer()
|
||||||
return value_set
|
return value_set
|
||||||
|
|
||||||
|
|||||||
@@ -265,7 +265,7 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
|
|||||||
else:
|
else:
|
||||||
types = self.get_return_values(check_yields=True)
|
types = self.get_return_values(check_yields=True)
|
||||||
if types:
|
if types:
|
||||||
yield LazyKnownValues(types)
|
yield LazyKnownValues(types, min=0, max=float('inf'))
|
||||||
return
|
return
|
||||||
last_for_stmt = for_stmt
|
last_for_stmt = for_stmt
|
||||||
|
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ a, b = simple()
|
|||||||
#? int() str()
|
#? int() str()
|
||||||
a
|
a
|
||||||
# For now this is ok.
|
# For now this is ok.
|
||||||
#?
|
#? int() str()
|
||||||
b
|
b
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user