X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plomtask%2Fdb.py;h=cce2630cd58bfb8bf7283c2eb0d2f45006d8ba26;hb=500958d4d8ec670a3749941572c305e0bb302ca3;hp=f6ef1cb6724a4a8cb13cc3d74d8c745338294463;hpb=25b71c6f0b10db05907128daf50c6e543e514c35;p=plomtask
diff --git a/plomtask/db.py b/plomtask/db.py
index f6ef1cb..cce2630 100644
--- a/plomtask/db.py
+++ b/plomtask/db.py
@@ -337,9 +337,8 @@ class BaseModel(Generic[BaseModelId]):
def _get_cached(cls: type[BaseModelInstance],
id_: BaseModelId) -> BaseModelInstance | None:
"""Get object of id_ from class's cache, or None if not found."""
- # pylint: disable=consider-iterating-dictionary
cache = cls.get_cache()
- if id_ in cache.keys():
+ if id_ in cache:
obj = cache[id_]
assert isinstance(obj, cls)
return obj
@@ -388,9 +387,7 @@ class BaseModel(Generic[BaseModelId]):
return obj
@classmethod
- def by_id(cls, db_conn: DatabaseConnection,
- id_: BaseModelId | None
- ) -> Self:
+ def by_id(cls, db_conn: DatabaseConnection, id_: BaseModelId) -> Self:
"""Retrieve by id_, on failure throw NotFoundException.
First try to get from cls.cache_, only then check DB; if found,
@@ -414,11 +411,12 @@ class BaseModel(Generic[BaseModelId]):
"""Wrapper around .by_id, creating (not caching/saving) if not find."""
if not cls.can_create_by_id:
raise HandledException('Class cannot .by_id_or_create.')
+ if id_ is None:
+ return cls(None)
try:
return cls.by_id(db_conn, id_)
except NotFoundException:
- obj = cls(id_)
- return obj
+ return cls(id_)
@classmethod
def all(cls: type[BaseModelInstance],