From ecae809d0a42f75868afe3c420f3dc040d7cf8ad Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Wed, 13 Nov 2024 20:08:25 +0800 Subject: [PATCH] keyerror to type error --- src/monty/collections.py | 8 ++++---- tests/test_collections.py | 35 ++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/monty/collections.py b/src/monty/collections.py index 992be719..3b0425c0 100644 --- a/src/monty/collections.py +++ b/src/monty/collections.py @@ -43,7 +43,7 @@ def __init__(self, *args, **kwargs) -> None: dict.__init__(self, *args, **kwargs) def __setitem__(self, key: Any, val: Any) -> None: - raise KeyError(f"Cannot overwrite existing key: {str(key)}") + raise TypeError(f"Cannot overwrite existing key: {str(key)}") def update(self, *args, **kwargs) -> None: """ @@ -51,7 +51,7 @@ def update(self, *args, **kwargs) -> None: args: Passthrough arguments for standard dict. kwargs: Passthrough keyword arguments for standard dict. """ - raise KeyError(f"Cannot update a {self.__class__.__name__}") + raise TypeError(f"Cannot update a {self.__class__.__name__}") class Namespace(dict): @@ -67,7 +67,7 @@ def __init__(self, *args, **kwargs) -> None: def __setitem__(self, key: Any, val: Any) -> None: if key in self: - raise KeyError(f"Cannot overwrite existing key: {key!s}") + raise TypeError(f"Cannot overwrite existing key: {key!s}") dict.__setitem__(self, key, val) @@ -137,7 +137,7 @@ def __getattribute__(self, name: str) -> Any: raise AttributeError(str(exc)) def __setattr__(self, name: str, value: Any) -> None: - raise KeyError( + raise TypeError( f"You cannot modify attribute {name} of {self.__class__.__name__}" ) diff --git a/tests/test_collections.py b/tests/test_collections.py index 42cc8ea7..a6540ec2 100644 --- a/tests/test_collections.py +++ b/tests/test_collections.py @@ -22,27 +22,34 @@ def test_tree(): assert x["a"]["b"]["c"]["d"] == 1 -def test_frozendict(): +def test_frozendict(): # DEBUG dct = frozendict({"hello": "world"}) + assert isinstance(dct, dict) assert dct["hello"] == "world" - with pytest.raises(KeyError, match="Cannot overwrite existing key"): + with pytest.raises(TypeError, match="Cannot overwrite existing key"): dct["key"] == "val" - with pytest.raises(KeyError, match="Cannot overwrite existing key"): - dct.update(key="value") + with pytest.raises(TypeError, match="Cannot overwrite existing key"): + dct.update(key="val") + + with pytest.raises(TypeError, match="Cannot overwrite existing key"): + dct |= {"key": "val"} -def test_namespace_dict(): +def test_namespace_dict(): # DEBUG dct = Namespace(key="val") + assert isinstance(dct, dict) dct["hello"] = "world" assert dct["key"] == "val" - with pytest.raises(KeyError, match="Cannot overwrite existing key"): + with pytest.raises(TypeError, match="Cannot overwrite existing key"): dct["key"] = "val" - with pytest.raises(KeyError, match="Cannot overwrite existing key"): + + with pytest.raises(TypeError, match="Cannot overwrite existing key"): dct.update({"key": "val"}) - with pytest.raises(KeyError, match="Cannot overwrite existing key"): + + with pytest.raises(TypeError, match="Cannot overwrite existing key"): dct |= {"key": "val"} @@ -56,21 +63,23 @@ def test_attr_dict(): assert dct["bar"] == "hello" -def test_frozen_attrdict(): +def test_frozen_attrdict(): # DEBUG dct = FrozenAttrDict({"hello": "world", 1: 2}) + assert isinstance(dct, dict) assert dct["hello"] == "world" assert dct.hello == "world" + assert dct["hello"] is dct.hello # Test adding item - with pytest.raises(KeyError, match="You cannot modify attribute"): + with pytest.raises(TypeError, match="You cannot modify attribute"): dct["foo"] = "bar" - with pytest.raises(KeyError, match="You cannot modify attribute"): + with pytest.raises(TypeError, match="You cannot modify attribute"): dct.foo = "bar" # Test modifying existing item - with pytest.raises(KeyError, match="You cannot modify attribute"): + with pytest.raises(TypeError, match="You cannot modify attribute"): dct.hello = "new" - with pytest.raises(KeyError, match="You cannot modify attribute"): + with pytest.raises(TypeError, match="You cannot modify attribute"): dct["hello"] = "new"