Skip to content

Commit

Permalink
Raise Exception instances, not classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles committed Dec 2, 2017
1 parent 2733296 commit 63be5d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/api.rst
Expand Up @@ -122,7 +122,7 @@ These tools return summarized or aggregated data from an iterable.

.. autofunction:: ilen
.. autofunction:: first(iterable[, default])
.. autofunction:: one(iterable, too_short=ValueError, too_long=ValueError)
.. autofunction:: one
.. autofunction:: unique_to_each
.. autofunction:: locate
.. autofunction:: consecutive_groups
Expand Down
20 changes: 11 additions & 9 deletions more_itertools/more.py
Expand Up @@ -420,7 +420,7 @@ def with_iter(context_manager):
yield item


def one(iterable, too_short=ValueError, too_long=ValueError):
def one(iterable, too_short=None, too_long=None):
"""Return the first item from *iterable*, which is expected to contain only
that item. Raise an exception if *iterable* is empty or has more than one
item.
Expand All @@ -430,31 +430,33 @@ def one(iterable, too_short=ValueError, too_long=ValueError):
that is expected to return a single row.
If *iterable* is empty, ``ValueError`` will be raised. You may specify a
different exception type with the *too_short* keyword:
different exception with the *too_short* keyword:
>>> it = []
>>> one(it) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: too many items in iterable (expected 1)'
>>> one(it, too_short=IndexError) # doctest: +IGNORE_EXCEPTION_DETAIL
>>> too_short = IndexError('too few items')
>>> one(it, too_short=too_short) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
IndexError: too few items in iterable (expected 1)'
IndexError: too few items
Similarly, if *iterable* contains more than one item, ``ValueError`` will
be raised. You may specify a different exception type with the *too_long*
be raised. You may specify a different exception with the *too_long*
keyword:
>>> it = ['too', 'many']
>>> one(it) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: too many items in iterable (expected 1)'
>>> one(it, too_long=RuntimeError) # doctest: +IGNORE_EXCEPTION_DETAIL
>>> too_long = RuntimeError
>>> one(it, too_long=too_long) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
RuntimeError: too many items in iterable (expected 1)'
RuntimeError
Note that :func:`one` attempts to advance *iterable* twice to ensure there
is only one item. If there is more than one, both items will be discarded.
Expand All @@ -467,14 +469,14 @@ def one(iterable, too_short=ValueError, too_long=ValueError):
try:
value = next(it)
except StopIteration:
raise too_short('two few items in iterable (expected 1)')
raise too_short or ValueError('too few items in iterable (expected 1)')

try:
next(it)
except StopIteration:
pass
else:
raise too_long('too many items in iterable (expected 1)')
raise too_long or ValueError('too many items in iterable (expected 1)')

return value

Expand Down

0 comments on commit 63be5d2

Please sign in to comment.