From 61133ba83d0f24c40aa6b0c1473919c7aff3b16f Mon Sep 17 00:00:00 2001
From: Reagan Lee <96998476+reaganjlee@users.noreply.github.com>
Date: Sun, 20 Aug 2023 14:46:09 -0700
Subject: [PATCH] un-iterable fix
---
changelog/7966.bugfix.rst | 1 +
src/_pytest/assertion/util.py | 2 +-
testing/test_assertrewrite.py | 7 +++++--
3 files changed, 7 insertions(+), 3 deletions(-)
create mode 100644 changelog/7966.bugfix.rst
diff --git a/changelog/7966.bugfix.rst b/changelog/7966.bugfix.rst
new file mode 100644
index 000000000..de0557680
--- /dev/null
+++ b/changelog/7966.bugfix.rst
@@ -0,0 +1 @@
+Removes unhelpful error message from assertion rewrite mechanism when exceptions raised in __iter__ methods, and instead treats them as un-iterable.
diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py
index fc5dfdbd5..39ca5403e 100644
--- a/src/_pytest/assertion/util.py
+++ b/src/_pytest/assertion/util.py
@@ -132,7 +132,7 @@ def isiterable(obj: Any) -> bool:
try:
iter(obj)
return not istext(obj)
- except TypeError:
+ except Exception:
return False
diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py
index d85c8bed6..2c908133d 100644
--- a/testing/test_assertrewrite.py
+++ b/testing/test_assertrewrite.py
@@ -689,16 +689,19 @@ class TestAssertionRewrite:
def f() -> None:
class A:
def __iter__(self):
- raise TypeError("user message")
+ raise ValueError()
def __eq__(self, o: object) -> bool:
return self is o
+ def __repr__(self):
+ return ""
+
assert A() == A()
msg = getmsg(f)
assert msg is not None
- assert "Unexpected exception" in msg
+ assert " == " in msg
def test_formatchar(self) -> None:
def f() -> None: