From ce7bf5ce708d901e9677ca40e9802fd597a6ea3c Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Wed, 1 Apr 2026 22:58:30 -0700 Subject: [PATCH] [ILibrary / LazyLibrary] raise a LibraryError instead of KeyError --- masque/library.py | 4 ++++ masque/test/test_library.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/masque/library.py b/masque/library.py index 825dbf0..5a60e8d 100644 --- a/masque/library.py +++ b/masque/library.py @@ -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] diff --git a/masque/test/test_library.py b/masque/test/test_library.py index 3b731ad..621e703 100644 --- a/masque/test/test_library.py +++ b/masque/test/test_library.py @@ -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()