Skip to content

Commit

Permalink
Change clear_cache method to work with table_tree (#213)
Browse files Browse the repository at this point in the history
* Change clear_cache method to work with table_tree

The table_tree cannot be removed as an attribute as it is a property.
Setting the _table_tree value to its default of None will force, any call to table_tree to recompute the table_tree cache

* Update pre-commit and move flake8 repo to github

* Set Table Cached properties as a list and test that all belong are cached properties

* Add deleter to table tree property to make it work with delattr

* Add docstring to test

* Treat table tree as a non cache property (because it is not)

* Empty-Commit

* Ignore typing errors on Python 3.10

On Python 3.10 the logger has defined the extra as optional.

* Set assertions to validate values is not None
  • Loading branch information
ikerreyes authored Jan 27, 2023
1 parent c40d7f5 commit 4b87e38
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/widgetastic/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PrependParentsAdapter(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: MutableMapping[str, Any]
) -> Tuple[str, MutableMapping[str, Any]]:
assert self.extra is not None # python 3.10+ type check
widget_path = cast(str, self.extra["widget_path"])
# Sanitizing %->%% for formatter working properly
return (
Expand All @@ -51,6 +52,7 @@ def process(
)

def __repr__(self) -> str:
assert self.extra is not None # python 3.10+ type check
return "{}({!r}, {!r})".format(type(self).__name__, self.logger, self.extra["widget_path"])


Expand Down
22 changes: 14 additions & 8 deletions src/widgetastic/widget/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,14 @@ class MyCustomTable(Table):

Row = TableRow

_CACHED_PROPERTIES = [
"headers",
"attributized_headers",
"header_index_mapping",
"index_header_mapping",
"assoc_column_position",
]

def __init__(
self,
parent,
Expand Down Expand Up @@ -445,6 +453,10 @@ def table_tree(self):
if self.has_rowcolspan:
return self._get_table_tree()

@table_tree.deleter
def table_tree(self):
self._table_tree = None

@cached_property
def resolver(self):
return TableResolver()
Expand Down Expand Up @@ -485,18 +497,12 @@ def _process_negative_index(self, nindex):

def clear_cache(self):
"""Clear all cached properties."""
for item in [
"headers",
"attributized_headers",
"header_index_mapping",
"index_header_mapping",
"assoc_column_position",
"table_tree",
]:
for item in self._CACHED_PROPERTIES:
try:
delattr(self, item)
except AttributeError:
pass
del self.table_tree

@cached_property
def headers(self):
Expand Down
31 changes: 31 additions & 0 deletions testing/test_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from cached_property import cached_property

from widgetastic.widget import Table
from widgetastic.widget import View


def test_table_cached_properties():
"""Cached properties are defined as such"""
for item in Table._CACHED_PROPERTIES:
attribute = getattr(Table, item)
assert isinstance(attribute, cached_property)


def test_table_clear_cache(browser):
class TestForm(View):
table = Table("#rowcolspan_table")

view = TestForm(browser)
table = view.table

# invoke properties
for item in Table._CACHED_PROPERTIES:
getattr(table, item)
tree = table.table_tree
assert tree is not None, "If the table has not row or col span it won't have a tree"

table.clear_cache()

for item in Table._CACHED_PROPERTIES:
assert item not in table.__dict__
assert table._table_tree is None

0 comments on commit 4b87e38

Please sign in to comment.