Skip to content

Commit

Permalink
Fix the type hints in base_store.py
Browse files Browse the repository at this point in the history
- Use the _Element class instead of the Element factory for typing.
- Make BaseStore generic so that subclasses can have the precise
  return and parameter types instead of Any.
- The stored objects are required to have certain fields.
  • Loading branch information
gycsaba96 committed May 30, 2024
1 parent c2f650f commit fb866fe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion GTG/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------

import gi
import gi # type: ignore[import-untyped]


def gi_version_requires():
Expand Down
29 changes: 18 additions & 11 deletions GTG/core/base_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,50 @@
"""Base for all store classes."""


from gi.repository import GObject
from gi.repository import GObject # type: ignore[import-untyped]

from uuid import UUID
import logging

from lxml.etree import Element
from typing import Any, Dict, List, Optional
from lxml.etree import _Element
from typing import Any, Dict, List, Optional, TypeVar, Generic, Protocol, TypeAlias


log = logging.getLogger(__name__)

S = TypeVar('S',bound='Storeable')

class BaseStore(GObject.Object):
class Storeable(Protocol[S]):
id: UUID
parent: Optional[S]
children: List[S]


class BaseStore(GObject.Object,Generic[S]):
"""Base class for data stores."""


def __init__(self) -> None:
self.lookup: Dict[UUID, Any] = {}
self.data: List[Any] = []
self.lookup: Dict[UUID, S] = {}
self.data: List[S] = []

super().__init__()

# --------------------------------------------------------------------------
# BASIC MANIPULATION
# --------------------------------------------------------------------------

def new(self) -> Any:
def new(self) -> S:
raise NotImplementedError


def get(self, key: UUID) -> Any:
def get(self, key: UUID) -> S:
"""Get an item by id."""

return self.lookup[key]


def add(self, item: Any, parent_id: Optional[UUID] = None) -> None:
def add(self, item: S, parent_id: Optional[UUID] = None) -> None:
"""Add an existing item to the store."""

if item.id in self.lookup.keys():
Expand Down Expand Up @@ -169,11 +176,11 @@ def unparent(self, item_id: UUID, parent_id: UUID) -> None:
# SERIALIZING
# --------------------------------------------------------------------------

def from_xml(self, xml: Element) -> Any:
def from_xml(self, xml: _Element) -> None:
raise NotImplementedError


def to_xml(self) -> Element:
def to_xml(self) -> _Element:
raise NotImplementedError


Expand Down
2 changes: 1 addition & 1 deletion GTG/core/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def __hash__(self):
return id(self)


class TagStore(BaseStore):
class TagStore(BaseStore[Tag]):
"""A tree of tags."""

__gtype_name__ = 'gtg_TagStore'
Expand Down

0 comments on commit fb866fe

Please sign in to comment.