@property
def as_dict(self) -> dict[str, object]:
"""Return self as (json.dumps-compatible) dict."""
- library: dict[str, dict[str | int, object]] = {}
+ library: dict[str, dict[str, object] | dict[int, object]] = {}
d: dict[str, object] = {'id': self.id_, '_library': library}
for to_save in self.to_save_simples:
attr = getattr(self, to_save)
library: dict[str, dict[str | int, object]]
) -> int | str:
"""Return self.id_ while writing .as_dict into library."""
+ # NB: For tighter mypy testing, we might prefer the library argument
+ # to be of type dict[str, dict[str, object] | dict[int, object]
+ # instead. But my current coding knowledge only manage to make that
+ # work by turning the code much more complex, so let's leave it at
+ # that for now …
+
def into_library(library: dict[str, dict[str | int, object]],
cls_name: str,
id_: str | int,
raise HandledException(msg)
else:
library[cls_name][id_] = d
+
as_dict = self.as_dict
assert isinstance(as_dict['_library'], dict)
for cls_name, dict_of_objs in as_dict['_library'].items():
self.table_name = table_name
self._default = default
self.history: dict[str, str | float] = {}
+ # NB: For tighter mypy testing, we might prefer self.history to be
+ # dict[str, float] | dict[str, str] instead, but my current coding
+ # knowledge only manages to make that work by adding much further
+ # complexity, so let's leave it at that for now …
def __hash__(self) -> int:
history_tuples = tuple((k, v) for k, v in self.history.items())
__: str,
attr: VersionedAttribute,
default: str | float,
- to_set: list[str | float]
+ to_set: list[str] | list[float]
) -> None:
"""Test VersionedAttribute.set() behaves as expected."""
attr.set(default)
__: str,
attr: VersionedAttribute,
default: str | float,
- to_set: list[str | float]
+ to_set: list[str] | list[float]
) -> None:
"""Test VersionedAttribute.newest."""
# check .newest on empty history returns .default
__: str,
attr: VersionedAttribute,
default: str | float,
- to_set: list[str | float]
+ to_set: list[str] | list[float]
) -> None:
"""Test .at() returns values nearest to queried time, or default."""
# check .at() return default on empty history
class TestCaseWithDB(TestCaseAugmented):
"""Module tests not requiring DB setup."""
- default_ids: tuple[int | str, int | str, int | str] = (1, 2, 3)
+ default_ids: tuple[int, int, int] | tuple[str, str, str] = (1, 2, 3)
def setUp(self) -> None:
Condition.empty_cache()
attr_name: str,
attr: VersionedAttribute,
_: str | float,
- to_set: list[str | float]
+ to_set: list[str] | list[float]
) -> None:
"""Test storage and initialization of versioned attributes."""
_: str,
attr: VersionedAttribute,
default: str | float,
- to_set: list[str | float]
+ to_set: list[str] | list[float]
) -> None:
""""Test VersionedAttribute.history_from_row() knows its DB rows."""
attr.set(to_set[0])
attr_name: str,
attr: VersionedAttribute,
_: str | float,
- to_set: list[str | float]
+ to_set: list[str] | list[float]
) -> None:
"""Test singularity of VersionedAttributes on saving."""
owner.save(self.db_conn)
@staticmethod
def as_id_list(items: list[dict[str, object]]) -> list[int | str]:
"""Return list of only 'id' fields of items."""
+ # NB: To tighten the mypy test, consider to, instead of returning
+ # list[str | int], returnlist[int] | list[str]. But since so far to me
+ # the only way to make that work seems to be to repaclement of the
+ # currently active last line with complexity of the out-commented code
+ # block beneath, I currently opt for the status quo.
id_list = []
for item in items:
assert isinstance(item['id'], (int, str))
id_list += [item['id']]
return id_list
+ # if id_list:
+ # if isinstance(id_list[0], int):
+ # for id_ in id_list:
+ # assert isinstance(id_, int)
+ # l_int: list[int] = [id_ for id_ in id_list
+ # if isinstance(id_, int)]
+ # return l_int
+ # for id_ in id_list:
+ # assert isinstance(id_, str)
+ # l_str: list[str] = [id_ for id_ in id_list
+ # if isinstance(id_, str)]
+ # return l_str
+ # return []
@staticmethod
def as_refs(items: list[dict[str, object]]