From 049eec8474d3238b056a42fa07558c4293a97f05 Mon Sep 17 00:00:00 2001 From: Reagan Lee <96998476+reaganjlee@users.noreply.github.com> Date: Sun, 20 Aug 2023 14:04:20 -0700 Subject: [PATCH] Revert iter raises checks This reverts commit e938580257e8120dabbea789d5b7a3dd3354d0a3. Revert "improve error msg and test" This reverts commit c0cf822ca1d5d6ac74f46ddccbe377c254c6084a. Revert "error msg" This reverts commit ec1053cc162250e42ae220ebe91c3bf00b22b084. Revert "changelog" This reverts commit d2dc8a70b593b61a013ae3eba99bb3f10e82eb65. Revert "simplify code / take out user-gen typeerror case" This reverts commit b9cb87d862bb3f841d67723ac5717ce90b219dc2. --- changelog/7966.bugfix.rst | 1 - src/_pytest/assertion/util.py | 28 +++++++--------------------- testing/test_assertrewrite.py | 4 ++-- 3 files changed, 9 insertions(+), 24 deletions(-) delete mode 100644 changelog/7966.bugfix.rst diff --git a/changelog/7966.bugfix.rst b/changelog/7966.bugfix.rst deleted file mode 100644 index 40cad46c6..000000000 --- a/changelog/7966.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Assertion rewrite mechanism now gives a seperate, more detailed error message from failures within __iter__. diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index d20c2363e..fc5dfdbd5 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -134,13 +134,6 @@ def isiterable(obj: Any) -> bool: return not istext(obj) except TypeError: return False - except Exception as e: - raise ValueError( - [ - f"pytest_assertion plugin: unexpected exception {e!r} while testing object {obj!r}", - ", probably from __iter__", - ] - ) def has_default_eq( @@ -202,20 +195,13 @@ def assertrepr_compare( explanation = _notin_text(left, right, verbose) except outcomes.Exit: raise - except Exception as e: - if ( - isinstance(e, ValueError) - and (len(e.args) == 1) - and ("__iter__" in str(e.args[0])) - ): - explanation = e.args[0] - else: - explanation = [ - "(pytest_assertion plugin: representation of details failed: {}.".format( - _pytest._code.ExceptionInfo.from_current()._getreprcrash() - ), - " Probably an object has a faulty __repr__.)", - ] + except Exception: + explanation = [ + "(pytest_assertion plugin: representation of details failed: {}.".format( + _pytest._code.ExceptionInfo.from_current()._getreprcrash() + ), + " Probably an object has a faulty __repr__.)", + ] if not explanation: return None diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 62c6bb8d4..d85c8bed6 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -689,7 +689,7 @@ class TestAssertionRewrite: def f() -> None: class A: def __iter__(self): - raise ValueError() + raise TypeError("user message") def __eq__(self, o: object) -> bool: return self is o @@ -698,7 +698,7 @@ class TestAssertionRewrite: msg = getmsg(f) assert msg is not None - assert "__iter__" in msg and "__repr__" not in msg + assert "Unexpected exception" in msg def test_formatchar(self) -> None: def f() -> None: