Skip to content

Commit 050f402

Browse files
authored
Merge pull request #11308 from reaganjlee/iter-reporting
Improve reporting from __iter__ exceptions
2 parents 4deb38b + d1722d5 commit 050f402

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

changelog/7966.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removes unhelpful error message from assertion rewrite mechanism when exceptions raised in __iter__ methods, and instead treats them as un-iterable.

src/_pytest/assertion/util.py

Lines changed: 1 addition & 1 deletion
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

testing/test_assertrewrite.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,25 @@ def myany(x) -> bool:
685685
assert msg is not None
686686
assert "<MY42 object> < 0" in msg
687687

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

0 commit comments

Comments
 (0)