diff --git a/src/widgetastic/log.py b/src/widgetastic/log.py index a7780cd..45322a7 100644 --- a/src/widgetastic/log.py +++ b/src/widgetastic/log.py @@ -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 ( @@ -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"]) diff --git a/src/widgetastic/widget/table.py b/src/widgetastic/widget/table.py index e6b61f5..ddf33a2 100644 --- a/src/widgetastic/widget/table.py +++ b/src/widgetastic/widget/table.py @@ -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, @@ -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() @@ -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): diff --git a/testing/test_table.py b/testing/test_table.py new file mode 100644 index 0000000..6fbf57f --- /dev/null +++ b/testing/test_table.py @@ -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