mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 07:14:48 +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):
|
def __init__(self, array):
|
||||||
self._array = array
|
self._array = array
|
||||||
|
|
||||||
def get_index_types(self, index=None):
|
def get_index_types(self, index_call_list=None):
|
||||||
values = self._array.values
|
""" Tries to get the only element (key) of a TODO doc"""
|
||||||
if index is not None:
|
# array slicing
|
||||||
if [x for x in index if ':' in x]:
|
if index_call_list is not None:
|
||||||
|
if index_call_list and [x for x in index_call_list if ':' in x]:
|
||||||
return [self]
|
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,
|
# This is indexing only one element, with a fixed index number,
|
||||||
# otherwise it just ignores the index (e.g. [1+1]).
|
# otherwise it just ignores the index (e.g. [1+1]).
|
||||||
try:
|
try:
|
||||||
# Multiple elements in the array.
|
# Multiple elements in the array are not wanted. var_args
|
||||||
i = index.get_only_subelement().name
|
# and get_only_subelement can raise AttributeErrors.
|
||||||
|
i = index_possibilities[0].var_args.get_only_subelement()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
@@ -745,9 +749,10 @@ class Array(object):
|
|||||||
return self.get_exact_index_types(i)
|
return self.get_exact_index_types(i)
|
||||||
except (IndexError, KeyError):
|
except (IndexError, KeyError):
|
||||||
pass
|
pass
|
||||||
return self.follow_values(values)
|
return self.follow_values(self._array.values)
|
||||||
|
|
||||||
def get_exact_index_types(self, index):
|
def get_exact_index_types(self, index):
|
||||||
|
""" Here the index is an int. Raises IndexError/KeyError """
|
||||||
if self._array.type == parsing.Array.DICT:
|
if self._array.type == parsing.Array.DICT:
|
||||||
old_index = index
|
old_index = index
|
||||||
index = None
|
index = None
|
||||||
@@ -1155,7 +1160,8 @@ def follow_call(call):
|
|||||||
debug.warning('unknown type:', current.type, current)
|
debug.warning('unknown type:', current.type, current)
|
||||||
scopes = []
|
scopes = []
|
||||||
# Make instances of those number/string objects.
|
# 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:
|
else:
|
||||||
# This is the first global lookup.
|
# This is the first global lookup.
|
||||||
scopes = get_scopes_for_name(scope, current, position=position,
|
scopes = get_scopes_for_name(scope, current, position=position,
|
||||||
|
|||||||
@@ -77,3 +77,10 @@ def fast_parent_copy(obj):
|
|||||||
copied_list[i] = list_rec(el)
|
copied_list[i] = list_rec(el)
|
||||||
return copied_list
|
return copied_list
|
||||||
return recursion(obj)
|
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()
|
yield collection()
|
||||||
else:
|
else:
|
||||||
yield next(collection)
|
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'
|
DICT = 'dict'
|
||||||
SET = 'set'
|
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)
|
super(Array, self).__init__(None, arr_type, parent_stmt, parent)
|
||||||
|
|
||||||
self.values = values if values else []
|
self.values = values if values else []
|
||||||
|
|||||||
@@ -139,6 +139,29 @@ dic2[r'asdf']
|
|||||||
#? int() str()
|
#? int() str()
|
||||||
dic2['just_something']
|
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__
|
# __getitem__
|
||||||
# -----------------
|
# -----------------
|
||||||
|
|||||||
Reference in New Issue
Block a user