mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
array indexing works now also with variables
This commit is contained in:
24
evaluate.py
24
evaluate.py
@@ -727,17 +727,21 @@ class Array(object):
|
||||
def __init__(self, array):
|
||||
self._array = array
|
||||
|
||||
def get_index_types(self, index=None):
|
||||
values = self._array.values
|
||||
if index is not None:
|
||||
if [x for x in index if ':' in x]:
|
||||
def get_index_types(self, index_call_list=None):
|
||||
""" Tries to get the only element (key) of a TODO doc"""
|
||||
# array slicing
|
||||
if index_call_list is not None:
|
||||
if index_call_list and [x for x in index_call_list if ':' in x]:
|
||||
return [self]
|
||||
else:
|
||||
|
||||
index_possibilities = list(follow_call_list(index_call_list))
|
||||
if len(index_possibilities) == 1:
|
||||
# This is indexing only one element, with a fixed index number,
|
||||
# otherwise it just ignores the index (e.g. [1+1]).
|
||||
try:
|
||||
# Multiple elements in the array.
|
||||
i = index.get_only_subelement().name
|
||||
# Multiple elements in the array are not wanted. var_args
|
||||
# and get_only_subelement can raise AttributeErrors.
|
||||
i = index_possibilities[0].var_args.get_only_subelement()
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
@@ -745,9 +749,10 @@ class Array(object):
|
||||
return self.get_exact_index_types(i)
|
||||
except (IndexError, KeyError):
|
||||
pass
|
||||
return self.follow_values(values)
|
||||
return self.follow_values(self._array.values)
|
||||
|
||||
def get_exact_index_types(self, index):
|
||||
""" Here the index is an int. Raises IndexError/KeyError """
|
||||
if self._array.type == parsing.Array.DICT:
|
||||
old_index = index
|
||||
index = None
|
||||
@@ -1155,7 +1160,8 @@ def follow_call(call):
|
||||
debug.warning('unknown type:', current.type, current)
|
||||
scopes = []
|
||||
# Make instances of those number/string objects.
|
||||
scopes = [Instance(s) for s in scopes]
|
||||
arr = helpers.generate_param_array([current.name])
|
||||
scopes = [Instance(s, arr) for s in scopes]
|
||||
else:
|
||||
# This is the first global lookup.
|
||||
scopes = get_scopes_for_name(scope, current, position=position,
|
||||
|
||||
@@ -77,3 +77,10 @@ def fast_parent_copy(obj):
|
||||
copied_list[i] = list_rec(el)
|
||||
return copied_list
|
||||
return recursion(obj)
|
||||
|
||||
def generate_param_array(args_tuple, parent_stmt=None):
|
||||
""" This generates an array, that can be used as a param """
|
||||
values = []
|
||||
for arg in args_tuple:
|
||||
values.append([arg])
|
||||
return parsing.Array(parsing.Array.TUPLE, parent_stmt, values=values)
|
||||
|
||||
@@ -99,3 +99,12 @@ def iter(collection, sentinel=None):
|
||||
yield collection()
|
||||
else:
|
||||
yield next(collection)
|
||||
|
||||
# basic types
|
||||
class int():
|
||||
def __init__(self, x, base=None):
|
||||
self.x = x
|
||||
|
||||
class str():
|
||||
def __init__(self, obj):
|
||||
self.obj = obj
|
||||
|
||||
@@ -830,7 +830,8 @@ class Array(Call):
|
||||
DICT = 'dict'
|
||||
SET = 'set'
|
||||
|
||||
def __init__(self, arr_type, parent_stmt=None, parent=None, values=None):
|
||||
def __init__(self, arr_type=NOARRAY, parent_stmt=None, parent=None,
|
||||
values=None):
|
||||
super(Array, self).__init__(None, arr_type, parent_stmt, parent)
|
||||
|
||||
self.values = values if values else []
|
||||
|
||||
@@ -139,6 +139,29 @@ dic2[r'asdf']
|
||||
#? int() str()
|
||||
dic2['just_something']
|
||||
|
||||
# -----------------
|
||||
# with variable as index
|
||||
# -----------------
|
||||
a = (1, "")
|
||||
index = 1
|
||||
#? str()
|
||||
a[index]
|
||||
|
||||
# these should just ouput the whole array
|
||||
index = int
|
||||
#? int() str()
|
||||
a[index]
|
||||
index = int()
|
||||
#? int() str()
|
||||
a[index]
|
||||
|
||||
# dicts
|
||||
index = 'asdf'
|
||||
|
||||
dic2 = {'asdf': 3, 'b': 'str'}
|
||||
#? int()
|
||||
dic2[index]
|
||||
|
||||
# -----------------
|
||||
# __getitem__
|
||||
# -----------------
|
||||
|
||||
Reference in New Issue
Block a user