Skip to content

Changed: raise KeyError instead of IndexError for all failed lookup methods #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ async def get_group(self, uid: str) -> dict:

Raises
------
IndexError if no group is matched.

KeyError if no group is matched.
"""

if not self.groups:
await self.get_groups()
for group in self.groups:
if group["id"] == uid:
return group
errmsg = f"No group with id='{uid}'"
raise IndexError(errmsg)
errmsg = f"No group with id='{uid}'."
raise KeyError(errmsg)

@_SpondBase.require_authentication
async def get_person(self, user: str) -> dict:
Expand All @@ -88,6 +87,10 @@ async def get_person(self, user: str) -> dict:
Returns
-------
Member or guardian's details.

Raises
------
KeyError if no person is matched.
"""
if not self.groups:
await self.get_groups()
Expand All @@ -113,7 +116,8 @@ async def get_person(self, user: str) -> dict:
)
):
return guardian
raise IndexError
errmsg = f"No person matched with identifier '{user}'."
raise KeyError(errmsg)

@_SpondBase.require_authentication
async def get_messages(self) -> list[dict]:
Expand Down Expand Up @@ -299,16 +303,16 @@ async def get_event(self, uid: str) -> dict:

Raises
------
IndexError if no event is matched.
KeyError if no event is matched.

"""
if not self.events:
await self.get_events()
for event in self.events:
if event["id"] == uid:
return event
errmsg = f"No event with id='{uid}'"
raise IndexError(errmsg)
errmsg = f"No event with id='{uid}'."
raise KeyError(errmsg)

@_SpondBase.require_authentication
async def update_event(self, uid: str, updates: dict):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ async def test_get_event__happy_path(mock_events, mock_token):

@pytest.mark.asyncio
async def test_get_event__no_match_raises_exception(mock_events, mock_token):
"""Test that a non-matched `id` raises IndexError."""
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_event("ID3")


@pytest.mark.asyncio
async def test_get_event__blank_id_match_raises_exception(mock_events, mock_token):
"""Test that a blank `id` raises IndexError."""
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_event("")


Expand All @@ -119,25 +119,25 @@ async def test_get_group__happy_path(mock_groups, mock_token):

@pytest.mark.asyncio
async def test_get_group__no_match_raises_exception(mock_groups, mock_token):
"""Test that a non-matched `id` raises IndexError."""
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_group("ID3")


@pytest.mark.asyncio
async def test_get_group__blank_id_raises_exception(mock_groups, mock_token):
"""Test that a blank `id` raises IndexError."""
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_group("")


Expand Down