From 85f9089d774145b61d0eb5f0e606e727073af50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csaba=20Gy=C3=B6rgyi?= Date: Sat, 10 Aug 2024 20:00:43 +0200 Subject: [PATCH] Fix (almost) every typing error in saved_searches.py - use the _Element class instead of the Element factory for typing - convert tag IDs to UUID during XML parsing - expect None values returned by lxml - other minor typing fixes - fix a related test case --- GTG/core/saved_searches.py | 24 +++++++++++++----------- tests/core/test_saved_search.py | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/GTG/core/saved_searches.py b/GTG/core/saved_searches.py index b6fc36b6e..7a6426f91 100644 --- a/GTG/core/saved_searches.py +++ b/GTG/core/saved_searches.py @@ -19,13 +19,13 @@ """Everything related to saved searches.""" -from gi.repository import GObject, Gio +from gi.repository import GObject, Gio # type: ignore[import-untyped] from uuid import uuid4, UUID from typing import Optional import logging -from lxml.etree import Element, SubElement +from lxml.etree import Element, _Element, SubElement from GTG.core.base_store import BaseStore @@ -42,7 +42,7 @@ def __init__(self, id: UUID, name: str, query: str) -> None: self.id = id self._name = name self._query = query - self._icon = None + self._icon : Optional[str] = None super().__init__() @@ -58,7 +58,7 @@ def set_name(self, value: str) -> None: @GObject.Property(type=str) - def icon(self) -> str: + def icon(self) -> Optional[str]: """Read only property.""" return self._icon @@ -126,19 +126,21 @@ def find(self, name: str) -> Optional[SavedSearch]: for search in self.data: if search.name == name: return search + return None - def from_xml(self, xml: Element) -> None: + def from_xml(self, xml: _Element) -> None: """Load searches from an LXML element.""" elements = list(xml.iter(self.XML_TAG)) - # Do parent searches first for element in elements: - search_id = element.get('id') + search_id = UUID(element.get('id')) name = element.get('name') + assert name is not None, "Missing 'name' property for saved search "+str(search_id) query = element.get('query') + assert query is not None, "Missing 'query' property for saved search "+str(search_id) search = SavedSearch(id=search_id, name=name, query=query) @@ -146,7 +148,7 @@ def from_xml(self, xml: Element) -> None: log.debug('Added %s', search) - def to_xml(self) -> Element: + def to_xml(self) -> _Element: """Save searches to an LXML element.""" root = Element('searchlist') @@ -160,7 +162,7 @@ def to_xml(self) -> Element: return root - def new(self, name: str, query: str, parent: UUID = None) -> SavedSearch: + def new(self, name: str, query: str, parent: Optional[UUID] = None) -> SavedSearch: """Create a new saved search and add it to the store.""" search_id = uuid4() @@ -174,8 +176,8 @@ def new(self, name: str, query: str, parent: UUID = None) -> SavedSearch: return search - def add(self, item, parent_id: UUID = None) -> None: - """Add a tag to the tagstore.""" + def add(self, item, parent_id: Optional[UUID] = None) -> None: + """Add a saved search to the store.""" super().add(item, parent_id) self.model.append(item) diff --git a/tests/core/test_saved_search.py b/tests/core/test_saved_search.py index b1fbdfc99..6b95c0334 100644 --- a/tests/core/test_saved_search.py +++ b/tests/core/test_saved_search.py @@ -17,7 +17,7 @@ # ----------------------------------------------------------------------------- from unittest import TestCase -from uuid import uuid4 +from uuid import uuid4, UUID from GTG.core.saved_searches import SavedSearch, SavedSearchStore from lxml.etree import XML @@ -96,7 +96,7 @@ def test_xml_load_tree(self): store.from_xml(xml_doc) self.assertEqual(store.count(), 4) - self.assertEqual(store.lookup['4796b97b-3690-4e74-a056-4153061958df'].query, 'urgent') + self.assertEqual(store.lookup[UUID('4796b97b-3690-4e74-a056-4153061958df')].query, 'urgent') def test_xml_write_simple(self):