Skip to content

Commit

Permalink
Merge pull request #550 from MarcinKonowalczyk/iter-except-docs
Browse files Browse the repository at this point in the history
Doc and tests for multiple exceptions in `iter_except`
  • Loading branch information
bbayles committed Sep 10, 2021
2 parents 3df138f + 127dada commit fb0cc84
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

import sphinx_rtd_theme

import more_itertools

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('..'))

import more_itertools

# -- General configuration -----------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
Expand Down
10 changes: 10 additions & 0 deletions more_itertools/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ def iter_except(func, exception, first=None):
>>> list(iter_except(l.pop, IndexError))
[2, 1, 0]
Multiple exceptions can be specified as a stopping condition:
>>> l = [1, 2, 3, '...', 4, 5, 6]
>>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
[7, 6, 5]
>>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
[4, 3, 2]
>>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
[]
"""
try:
if first is not None:
Expand Down
6 changes: 4 additions & 2 deletions more_itertools/recipes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ def unique_justseen(
) -> Iterator[_T]: ...
@overload
def iter_except(
func: Callable[[], _T], exception: Type[BaseException], first: None = ...
func: Callable[[], _T],
exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]],
first: None = ...,
) -> Iterator[_T]: ...
@overload
def iter_except(
func: Callable[[], _T],
exception: Type[BaseException],
exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]],
first: Callable[[], _U],
) -> Iterator[Union[_T, _U]]: ...
@overload
Expand Down
24 changes: 24 additions & 0 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ def test_first(self):
i = mi.iter_except(l.pop, IndexError, f)
self.assertEqual(list(i), [25, 3, 2, 1])

def test_multiple(self):
"""ensure can catch multiple exceptions"""

class Fiz(Exception):
pass

class Buzz(Exception):
pass

i = 0

def fizbuzz():
nonlocal i
i += 1
if i % 3 == 0:
raise Fiz
if i % 5 == 0:
raise Buzz
return i

expected = ([1, 2], [4], [], [7, 8], [])
for x in expected:
self.assertEqual(list(mi.iter_except(fizbuzz, (Fiz, Buzz))), x)


class FirstTrueTests(TestCase):
"""Tests for ``first_true()``"""
Expand Down

0 comments on commit fb0cc84

Please sign in to comment.