Skip to content

Commit

Permalink
Fix (almost) every typing error in saved_searches.py
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gycsaba96 authored and diegogangl committed Aug 13, 2024
1 parent f501dfb commit fb56a90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions GTG/core/saved_searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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__()

Expand All @@ -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
Expand Down Expand Up @@ -126,27 +126,29 @@ 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)

self.add(search)
log.debug('Added %s', search)


def to_xml(self) -> Element:
def to_xml(self) -> _Element:
"""Save searches to an LXML element."""

root = Element('searchlist')
Expand All @@ -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()
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_saved_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fb56a90

Please sign in to comment.