[ILibrary / LazyLibrary] raise a LibraryError instead of KeyError

This commit is contained in:
Jan Petykiewicz 2026-04-01 22:58:30 -07:00
commit ce7bf5ce70
2 changed files with 13 additions and 0 deletions

View file

@ -826,6 +826,8 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
Returns:
self
"""
if old_name not in self:
raise LibraryError(f'"{old_name}" does not exist in the library.')
self[new_name] = self[old_name]
del self[old_name]
if move_references:
@ -1479,6 +1481,8 @@ class LazyLibrary(ILibrary):
Returns:
self
"""
if old_name not in self.mapping:
raise LibraryError(f'"{old_name}" does not exist in the library.')
self[new_name] = self.mapping[old_name] # copy over function
if old_name in self.cache:
self.cache[new_name] = self.cache[old_name]

View file

@ -221,6 +221,15 @@ def test_library_rename() -> None:
assert "old" not in lib["parent"].refs
@pytest.mark.parametrize("library_cls", (Library, LazyLibrary))
def test_library_rename_missing_raises_library_error(library_cls: type[Library] | type[LazyLibrary]) -> None:
lib = library_cls()
lib["top"] = Pattern()
with pytest.raises(LibraryError, match="does not exist"):
lib.rename("missing", "new")
def test_library_dfs_can_replace_existing_patterns() -> None:
lib = Library()
child = Pattern()