Merge pull request #11308 from reaganjlee/iter-reporting

Improve reporting from __iter__ exceptions
This commit is contained in:
Zac Hatfield-Dodds 2023-08-21 16:27:42 -07:00 committed by GitHub
commit 050f402816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1 @@
Removes unhelpful error message from assertion rewrite mechanism when exceptions raised in __iter__ methods, and instead treats them as un-iterable.

View File

@ -132,7 +132,7 @@ def isiterable(obj: Any) -> bool:
try: try:
iter(obj) iter(obj)
return not istext(obj) return not istext(obj)
except TypeError: except Exception:
return False return False

View File

@ -685,6 +685,25 @@ class TestAssertionRewrite:
assert msg is not None assert msg is not None
assert "<MY42 object> < 0" in msg assert "<MY42 object> < 0" in msg
def test_assert_handling_raise_in__iter__(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""\
class A:
def __iter__(self):
raise ValueError()
def __eq__(self, o: object) -> bool:
return self is o
def __repr__(self):
return "<A object>"
assert A() == A()
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*E*assert <A object> == <A object>"])
def test_formatchar(self) -> None: def test_formatchar(self) -> None:
def f() -> None: def f() -> None:
assert "%test" == "test" # type: ignore[comparison-overlap] assert "%test" == "test" # type: ignore[comparison-overlap]