Skip to content

Failure when trying to call __repr__ because __iter__ is called? #7966

@Holt59

Description

@Holt59

1. Problem

The following code

class A:
    def __iter__(self):
        raise ValueError()

    def __eq__(self, o: object) -> bool:
        return self is o


def test_a():
    assert A() == A()

...will throw:

    def test_a():
>       assert A() == A()
E       assert <tests.test_p...0019C132F1490> == <tests.test_p...0019C132F12B0>
E         (pytest_assertion plugin: representation of details failed: XXX\test_pp.py:3: ValueError.
E          Probably an object has a faulty __repr__.)

...because __iter__ is called in order to repr() the object?

Is there a way to avoid __iter__ being called when a test fails?

2. Setup

Python 3.8.2 | pytest 6.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: rewriterelated to the assertion rewrite mechanismtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions