Skip to content

Commit 531d76d

Browse files
[7.4.x] Improve reporting from __iter__ exceptions (#11749)
1 parent b1f3387 commit 531d76d

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Diff for: changelog/7966.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed unhelpful error message from assertion rewrite mechanism when exceptions are raised in ``__iter__`` methods. Now they are treated un-iterable instead.

Diff for: src/_pytest/assertion/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def isiterable(obj: Any) -> bool:
132132
try:
133133
iter(obj)
134134
return not istext(obj)
135-
except TypeError:
135+
except Exception:
136136
return False
137137

138138

Diff for: testing/test_assertrewrite.py

+19
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,25 @@ def myany(x) -> bool:
686686
assert msg is not None
687687
assert "<MY42 object> < 0" in msg
688688

689+
def test_assert_handling_raise_in__iter__(self, pytester: Pytester) -> None:
690+
pytester.makepyfile(
691+
"""\
692+
class A:
693+
def __iter__(self):
694+
raise ValueError()
695+
696+
def __eq__(self, o: object) -> bool:
697+
return self is o
698+
699+
def __repr__(self):
700+
return "<A object>"
701+
702+
assert A() == A()
703+
"""
704+
)
705+
result = pytester.runpytest()
706+
result.stdout.fnmatch_lines(["*E*assert <A object> == <A object>"])
707+
689708
def test_formatchar(self) -> None:
690709
def f() -> None:
691710
assert "%test" == "test" # type: ignore[comparison-overlap]

0 commit comments

Comments
 (0)