Skip to content

Commit

Permalink
Fix the check for is_key_optional.
Browse files Browse the repository at this point in the history
(cherry picked from commit da260a9742d5060804ece46001f85a74f44a70a1)
  • Loading branch information
alexandervsokol committed Oct 22, 2024
1 parent 7a3994c commit 3274612
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions cl/runtime/db/local/local_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ def load_one(
is_record_optional: bool = False,
) -> TRecord | None:
# Check for an empty key
if not is_key_optional and record_or_key is None:
raise UserError(f"Key is None when trying to load record type {record_type.__name__} from DB.")
if record_or_key is None:
if is_key_optional:
return None
else:
raise UserError(f"Key is None when trying to load record type {record_type.__name__} from DB.")

if record_or_key is None or getattr(record_or_key, "get_key", None) is not None:
# Key instance is Record or None, return without lookup
Expand Down
7 changes: 5 additions & 2 deletions cl/runtime/db/mongo/basic_mongo_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ def load_one(
is_record_optional: bool = False,
) -> TRecord | None:
# Check for an empty key
if not is_key_optional and record_or_key is None:
raise UserError(f"Key is None when trying to load record type {record_type.__name__} from DB.")
if record_or_key is None:
if is_key_optional:
return None
else:
raise UserError(f"Key is None when trying to load record type {record_type.__name__} from DB.")

if record_or_key is None or getattr(record_or_key, "get_key", None) is not None:
# Record or None, return without lookup
Expand Down
7 changes: 5 additions & 2 deletions cl/runtime/db/sql/sqlite_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ def load_one(
is_record_optional: bool = False,
) -> TRecord | None:
# Check for an empty key
if not is_key_optional and record_or_key is None:
raise UserError(f"Key is None when trying to load record type {record_type.__name__} from DB.")
if record_or_key is None:
if is_key_optional:
return None
else:
raise UserError(f"Key is None when trying to load record type {record_type.__name__} from DB.")

# Delegate to load_many
result = next(iter(self.load_many(record_type, [record_or_key], dataset=dataset, identity=identity)))
Expand Down

0 comments on commit 3274612

Please sign in to comment.