Last active
June 5, 2024 22:14
-
-
Save MatrixManAtYrService/e05ae14fb087312f7691ed614709b557 to your computer and use it in GitHub Desktop.
Using generics and abstract base classes to create something like a dict which maps between two custom types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
from collections.abc import Iterator, MutableMapping | |
from pathlib import Path | |
from typing import Generic, TypeVar, cast, Type, Dict | |
from pydantic import BaseModel, Field | |
K = TypeVar('K', bound='Key') | |
V = TypeVar('V', bound='Value') | |
# Abstract stuff | |
class Key(BaseModel, ABC): | |
@abstractmethod | |
def as_tuple(self) -> tuple[str, ...]: | |
raise NotImplementedError() | |
@abstractmethod | |
def name(self) -> str: | |
raise NotImplementedError() | |
def __lt__(self, other: "Key") -> bool: | |
return self.as_tuple() < other.as_tuple() | |
def __hash__(self) -> int: | |
return self.as_tuple().__hash__() | |
def filename(self) -> str: | |
return f"{self.name()}.json" | |
class Value(BaseModel): | |
key: Key | |
committed: bool = False | |
class FancyReport(Generic[K, V]): | |
committed: Dict[K, V] = Field(default_factory=dict) | |
pending: Dict[K, V] = Field(default_factory=dict) | |
class FancyDict(MutableMapping[K, V], Generic[K, V]): | |
def __init__(self, store: Path, KType: Type[K], VType: Type[V]): | |
self.store = store | |
# the world of python type hints is separate from the world of runtime objects (which may be types). | |
# Because of this we can't run V.parse_file below, mypy will complain: | |
# error: "V" is a type variable and only valid in type context [misc] | |
# The workaround is to pass the same types in at runtime, as described here: | |
# https://stackoverflow.com/a/70231012/1054322 | |
self.KType = KType | |
self.VType = VType | |
# unlike a less fancy dict, this has generic type references for its key and value types | |
# so you can have type-hinted functions like this one which use the abstract base classes | |
# above as interfaces for working with the caller-defined key and value types. | |
def report(self) -> FancyReport[K, V]: | |
report = FancyReport[K, V]() | |
for file in self.store.glob("*.json"): | |
# here we use the runtime type references instead of the generic ones | |
obj = self.VType.parse_file(file) | |
if obj.committed: | |
report.committed[cast(K, obj.key)] = obj | |
else: | |
report.pending[cast(K, obj.key)] = obj | |
# the casts above are necessary because python doesn't know that the runtime-provided | |
# types are in any way related to the type arguments | |
return report | |
def __getitem__(self, key: K) -> V: | |
raise NotImplementedError() | |
def __setitem__(self, key: K, value: V) -> None: | |
raise NotImplementedError() | |
def __delitem__(self, key: K) -> None: | |
raise NotImplementedError() | |
def __iter__(self) -> Iterator[K]: | |
raise NotImplementedError() | |
def __len__(self) -> int: | |
raise NotImplementedError() | |
# Concrete stuff | |
class MyKey(Key): | |
x: int | |
def name(self) -> str: | |
return f"thing-{self.x}" | |
def as_tuple(self) -> tuple[str, ...]: | |
return (str(self.x),) | |
class MyVal(Value): | |
key: MyKey | |
y: int | |
class MyDict(FancyDict[MyKey, MyVal]): | |
def __init__(self, store: Path): | |
super().__init__(store, MyKey, MyVal) | |
fancy_dict = MyDict(Path.cwd()) | |
report = fancy_dict.report() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment