Skip to content

Commit

Permalink
keyerror to type error
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Nov 13, 2024
1 parent 5f4bbea commit ecae809
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/monty/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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:
"""
Args:
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):
Expand All @@ -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)

Expand Down Expand Up @@ -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__}"
)

Expand Down
35 changes: 22 additions & 13 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}


Expand All @@ -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"


Expand Down

0 comments on commit ecae809

Please sign in to comment.