Skip to content

Commit bae0196

Browse files
committed
Fix key type error handling
Signed-off-by: Aleksei Stepanov <penguinolog@gmail.com>
1 parent 6aa67f8 commit bae0196

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

doc/source/ExecResult.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ API: ExecResult
193193
:type item: typing.Union[int, slice, typing.Iterable[typing.Union[int, slice, ellipsis]]]
194194
:returns: Joined selected lines
195195
:rtype: str
196-
:raises KeyError: Unexpected key
196+
:raises TypeError: Unexpected key
197197

198198
.. py:method:: __str__(self)
199199

exec_helpers/exec_result.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,25 @@ def __getitem__(self, item: typing.Union[int, slice, typing.Iterable[typing.Unio
7474
:type item: typing.Union[int, slice, typing.Iterable[typing.Union[int, slice, ellipsis]]]
7575
:returns: Joined selected lines
7676
:rtype: str
77-
:raises KeyError: Unexpected key
77+
:raises TypeError: Unexpected key
7878
"""
7979
if isinstance(item, int):
8080
return _get_str_from_bin(_get_bytearray_from_array([self._data[item]]))
8181
if isinstance(item, slice):
8282
return _get_str_from_bin(_get_bytearray_from_array(self._data[item]))
83-
buf: typing.List[bytes] = []
84-
for rule in item:
85-
if isinstance(rule, int):
86-
buf.append(self._data[rule])
87-
elif isinstance(rule, slice):
88-
buf.extend(self._data[rule])
89-
elif rule is Ellipsis:
90-
buf.append(b"...\n")
91-
else:
92-
raise KeyError(f"Unexpected key: {rule!r} (from {item!r})")
93-
return _get_str_from_bin(_get_bytearray_from_array(buf))
83+
elif isinstance(item, tuple):
84+
buf: typing.List[bytes] = []
85+
for rule in item:
86+
if isinstance(rule, int):
87+
buf.append(self._data[rule])
88+
elif isinstance(rule, slice):
89+
buf.extend(self._data[rule])
90+
elif rule is Ellipsis:
91+
buf.append(b"...\n")
92+
else:
93+
raise TypeError(f"Unexpected key type: {rule!r} (from {item!r})")
94+
return _get_str_from_bin(_get_bytearray_from_array(buf))
95+
raise TypeError(f"Unexpected key type: {item!r}")
9496

9597
def __len__(self) -> int: # pragma: no cover
9698
"""Data len."""

test/test_exec_result.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,7 @@ def test_indexed_lines_access(self):
264264
self.assertEqual(result.stdout_lines[0, 2], 'line0\nline2')
265265
self.assertEqual(result.stdout_lines[0, ..., 2], 'line0\n...\nline2')
266266
self.assertEqual(result.stdout_lines[:1, ..., 2], 'line0\n...\nline2')
267-
with self.assertRaises(KeyError):
267+
with self.assertRaises(TypeError):
268+
_ = result.stdout_lines['aaa'] # noqa
269+
with self.assertRaises(TypeError):
268270
_ = result.stdout_lines[1, 'aaa'] # noqa

0 commit comments

Comments
 (0)