fix(item-storage-memory): throw when requested item does not exist

- `ItemStorageMemory.get` now throws an `ItemNotFoundError` when the requested `item_id` is not found.
- Update docstrings in ABC and tests.

The new memory item storage implementation implemented the `get` method incorrectly, by returning `None` if the item didn't exist.

The ABC typed `get` as returning `T`, while the SQLite implementation typed `get` as returning `Optional[T]`. The SQLite implementation was referenced when writing the memory implementation.

This mismatched typing is a violation of the Liskov substitution principle, because the signature of the implementation of `get` in the implementation is wider than the abstract class's definition. Using `pyright` in strict mode catches this.

In `invocation_stats_default`, this introduced an error. The `_prune_stats` method calls `get`, expecting the method to throw if the item is not found. If the graph is no longer stored in the bounded item storage, we will call `is_complete()` on `None`, causing the error.

Note: This error condition never arose the SQLite implementation because it parsed the item with pydantic before returning it, which would throw if the item was not found. It implicitly threw, while the memory implementation did not.
This commit is contained in:
psychedelicious
2024-02-03 18:30:41 +11:00
committed by Kent Keirsey
parent c2af124622
commit 88c08bbfc7
5 changed files with 29 additions and 11 deletions

View File

@ -3,6 +3,7 @@ import re
import pytest
from pydantic import BaseModel
from invokeai.app.services.item_storage.item_storage_common import ItemNotFoundError
from invokeai.app.services.item_storage.item_storage_memory import ItemStorageMemory
@ -58,8 +59,8 @@ def test_item_storage_memory_gets(item_storage_memory: ItemStorageMemory[MockIte
item = item_storage_memory.get("2")
assert item == item_2
item = item_storage_memory.get("3")
assert item is None
with pytest.raises(ItemNotFoundError, match=re.escape("Item with id 3 not found")):
item_storage_memory.get("3")
def test_item_storage_memory_deletes(item_storage_memory: ItemStorageMemory[MockItemModel]):