Skip to content

Commit 8f65a3c

Browse files
committed
Changed: raise LookupError instead of IndexError for all failed lookup methods; for get_person(),also make error message more meaningful and add Raises to docstring.
1 parent bcb2b6d commit 8f65a3c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

spond/spond.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ async def get_group(self, uid: str) -> dict:
6161
6262
Raises
6363
------
64-
IndexError if no group is matched.
65-
64+
LookupError if no group is matched.
6665
"""
6766

6867
if not self.groups:
6968
await self.get_groups()
7069
for group in self.groups:
7170
if group["id"] == uid:
7271
return group
73-
errmsg = f"No group with id='{uid}'"
74-
raise IndexError(errmsg)
72+
errmsg = f"No group with id='{uid}'."
73+
raise LookupError(errmsg)
7574

7675
@_SpondBase.require_authentication
7776
async def get_person(self, user: str) -> dict:
@@ -88,6 +87,10 @@ async def get_person(self, user: str) -> dict:
8887
Returns
8988
-------
9089
Member or guardian's details.
90+
91+
Raises
92+
------
93+
LookupError if no person is matched.
9194
"""
9295
if not self.groups:
9396
await self.get_groups()
@@ -113,7 +116,8 @@ async def get_person(self, user: str) -> dict:
113116
)
114117
):
115118
return guardian
116-
raise IndexError
119+
errmsg = f"No person matched with identifier '{user}'."
120+
raise LookupError(errmsg)
117121

118122
@_SpondBase.require_authentication
119123
async def get_messages(self) -> list[dict]:
@@ -299,16 +303,16 @@ async def get_event(self, uid: str) -> dict:
299303
300304
Raises
301305
------
302-
IndexError if no event is matched.
306+
LookupError if no event is matched.
303307
304308
"""
305309
if not self.events:
306310
await self.get_events()
307311
for event in self.events:
308312
if event["id"] == uid:
309313
return event
310-
errmsg = f"No event with id='{uid}'"
311-
raise IndexError(errmsg)
314+
errmsg = f"No event with id='{uid}'."
315+
raise LookupError(errmsg)
312316

313317
@_SpondBase.require_authentication
314318
async def update_event(self, uid: str, updates: dict):

tests/test_spond.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,25 @@ async def test_get_event__happy_path(mock_events, mock_token):
8080

8181
@pytest.mark.asyncio
8282
async def test_get_event__no_match_raises_exception(mock_events, mock_token):
83-
"""Test that a non-matched `id` raises IndexError."""
83+
"""Test that a non-matched `id` raises LookupError."""
8484

8585
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
8686
s.events = mock_events
8787
s.token = mock_token
8888

89-
with pytest.raises(IndexError):
89+
with pytest.raises(LookupError):
9090
await s.get_event("ID3")
9191

9292

9393
@pytest.mark.asyncio
9494
async def test_get_event__blank_id_match_raises_exception(mock_events, mock_token):
95-
"""Test that a blank `id` raises IndexError."""
95+
"""Test that a blank `id` raises LookupError."""
9696

9797
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
9898
s.events = mock_events
9999
s.token = mock_token
100100

101-
with pytest.raises(IndexError):
101+
with pytest.raises(LookupError):
102102
await s.get_event("")
103103

104104

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

120120
@pytest.mark.asyncio
121121
async def test_get_group__no_match_raises_exception(mock_groups, mock_token):
122-
"""Test that a non-matched `id` raises IndexError."""
122+
"""Test that a non-matched `id` raises LookupError."""
123123

124124
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
125125
s.groups = mock_groups
126126
s.token = mock_token
127127

128-
with pytest.raises(IndexError):
128+
with pytest.raises(LookupError):
129129
await s.get_group("ID3")
130130

131131

132132
@pytest.mark.asyncio
133133
async def test_get_group__blank_id_raises_exception(mock_groups, mock_token):
134-
"""Test that a blank `id` raises IndexError."""
134+
"""Test that a blank `id` raises LookupError."""
135135

136136
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
137137
s.groups = mock_groups
138138
s.token = mock_token
139139

140-
with pytest.raises(IndexError):
140+
with pytest.raises(LookupError):
141141
await s.get_group("")
142142

143143

0 commit comments

Comments
 (0)