Skip to content

Commit

Permalink
[enh] add .index_on_parent_raw and .index_on_parent to avoid ambiguit…
Browse files Browse the repository at this point in the history
…y due to list proxy
  • Loading branch information
Psycojoker committed Jan 30, 2015
1 parent 7aaa542 commit 5f24606
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
14 changes: 13 additions & 1 deletion redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ def _get_helpers(self):
'parse_code_block',
'parse_decorators',
'from_fst',
'index_on_parent',
'index_on_parent_raw',
])
return [x for x in dir(self) if not x.startswith("_") and x not in not_helpers and inspect.ismethod(getattr(self, x))]

Expand Down Expand Up @@ -965,6 +967,16 @@ def index_on_parent(self):
if not isinstance(getattr(self.parent, self.on_attribute), (NodeList, ProxyList)):
return None

return getattr(self.parent, self.on_attribute).index(self)

@property
def index_on_parent_raw(self):
if not self.parent:
return None

if not isinstance(getattr(self.parent, self.on_attribute), (NodeList, ProxyList)):
return None

if isinstance(getattr(self.parent, self.on_attribute), ProxyList):
return getattr(self.parent, self.on_attribute).node_list.index(self)
else:
Expand Down Expand Up @@ -1568,7 +1580,7 @@ def _diff_reduced_list(self):
def get_absolute_bounding_box_of_attribute(self, index):
if index >= len(self.data) or index < 0:
raise IndexError()
index = self[index].index_on_parent
index = self[index].index_on_parent_raw
path = self.path().to_baron_path() + [index]
return baron.path.path_to_bounding_box(self.root.fst(), path)

Expand Down
5 changes: 2 additions & 3 deletions tests/test_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def test_bounding_box_with_proxy_list():


def test_bounding_box_of_attribute_with_proxy_list():
assert ((1, 1), (32, 0)) == RED.absolute_bounding_box
assert ((1, 1), (32, 0)) == RED.class_.absolute_bounding_box
# assert ((1, 1), (32, 0)) == RED.absolute_bounding_box
# assert ((1, 1), (32, 0)) == RED.class_.absolute_bounding_box
assert ((2, 5), (5, 7)) == RED.class_.value.get_absolute_bounding_box_of_attribute(0)
assert ((6, 5), (9, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(1)
assert ((9, 18), (10, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(2)
Expand All @@ -143,4 +143,3 @@ def test_bounding_box_of_attribute_with_proxy_list():
assert ((28, 5), (31, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(7)
with pytest.raises(IndexError):
RED.class_.value.get_absolute_bounding_box_of_attribute(8)

9 changes: 8 additions & 1 deletion tests/test_redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ def test_other_name_assignment():

def test_index():
red = RedBaron("a = [1, 2, 3]")
assert red[0].value.value.node_list[2].index_on_parent == 2
assert red[0].value.value[2].index_on_parent == 2
assert red[0].index_on_parent == 0
assert red[0].value.index_on_parent is None


def test_index_raw():
red = RedBaron("a = [1, 2, 3]")
assert red[0].value.value.node_list[2].index_on_parent_raw == 2
assert red[0].index_on_parent == 0
assert red[0].value.index_on_parent_raw is None


def test_regression_find_all_recursive():
red = RedBaron("a.b()")
assert red[0].value("name", recursive=False) == [red.name, red("name")[1]]
Expand Down

0 comments on commit 5f24606

Please sign in to comment.