Skip to content

Commit acf76f4

Browse files
author
Steve Canny
committed
tbl: add _Row.table
1 parent 3de03c8 commit acf76f4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

docx/table.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def style(self):
9393
def style(self, value):
9494
self._tblPr.style = value
9595

96+
@property
97+
def table(self):
98+
"""
99+
Provide child objects with reference to the |Table| object they
100+
belong to, without them having to know their direct parent is
101+
a |Table| object. This is the terminus of a series of `parent._table`
102+
calls from an arbitrary child through its ancestors.
103+
"""
104+
raise NotImplementedError
105+
96106
@property
97107
def _tblPr(self):
98108
return self._tbl.tblPr
@@ -315,7 +325,7 @@ def table(self):
315325
"""
316326
Reference to the |Table| object this row belongs to.
317327
"""
318-
raise NotImplementedError
328+
return self._parent.table
319329

320330
@property
321331
def _index(self):

tests/test_table.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ def it_provides_access_to_the_row_cells(self):
441441
cells = row.cells
442442
assert isinstance(cells, _RowCells)
443443

444+
def it_provides_access_to_the_table_it_belongs_to(self, table_fixture):
445+
row, table_ = table_fixture
446+
assert row.table is table_
447+
444448
# fixtures -------------------------------------------------------
445449

446450
@pytest.fixture
@@ -451,12 +455,22 @@ def cells_fixture(self, _index_, table_prop_, table_):
451455
table_.row_cells.return_value = list(expected_cells)
452456
return row, row_idx, expected_cells
453457

458+
@pytest.fixture
459+
def table_fixture(self, parent_, table_):
460+
row = _Row(None, parent_)
461+
parent_.table = table_
462+
return row, table_
463+
454464
# fixture components ---------------------------------------------
455465

456466
@pytest.fixture
457467
def _index_(self, request):
458468
return property_mock(request, _Row, '_index')
459469

470+
@pytest.fixture
471+
def parent_(self, request):
472+
return instance_mock(request, Table)
473+
460474
@pytest.fixture
461475
def table_(self, request):
462476
return instance_mock(request, Table)

0 commit comments

Comments
 (0)