Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

objects with multiline repr cause pytest to print a diff #73

Open
gjcarneiro opened this issue Jul 15, 2023 · 4 comments · May be fixed by #92
Open

objects with multiline repr cause pytest to print a diff #73

gjcarneiro opened this issue Jul 15, 2023 · 4 comments · May be fixed by #92

Comments

@gjcarneiro
Copy link

from dirty_equals import IsList


def test():
    actions = [
        "ddsdsdads ",
        "sfafsdsd",
        "dfds sdfsef",
        "sfdssdf ",
        "sdsadfs",
        "dsfsdfsd",
        "cfdfdfd",
        "dgffgfdgfd",
        "fdsfsdgfsdgsf",
    ]
    assert (actions, 1, 2, 3, 4, 5, 6, 7, 8, 9) == (
        IsList(length=...),
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        0,
    )

This prints:

nError: assert (['ddsdsdads ',\n  'sfafsdsd',\n  'dfds sdfsef',\n  'sfdssdf ',\n  'sdsadfs',\n  'dsfsdfsd',\n  'cfdfdfd',\n  'dgffgfdgfd',\n  'fdsfsdgfsdgsf'],\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9) == (['ddsdsdads ', 'sfafsdsd', 'dfds sdfsef', 'sfdssdf ', 'sdsadfs', 'dsfsdfsd', 'cfdfdfd', 'dgffgfdgfd', 'fdsfsdgfsdgsf'],\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 0)
E         At index 9 diff: 9 != 0
E         Full diff:
E           (
E         -  ['ddsdsdads ', 'sfafsdsd', 'dfds sdfsef', 'sfdssdf ', 'sdsadfs', 'dsfsdfsd', 'cfdfdfd', 'dgffgfdgfd', 'fdsfsdgfsdgsf'],
E         +  ['ddsdsdads ',
E         +   'sfafsdsd',
E         +   'dfds sdfsef',
E         +   'sfdssdf ',
E         +   'sdsadfs',
E         +   'dsfsdfsd',
E         +   'cfdfdfd',
E         +   'dgffgfdgfd',
E         +   'fdsfsdgfsdgsf'],
E            1,
E            2,
E            3,
E            4,
E            5,
E            6,
E            7,
E            8,
E         -  0,
E         +  9,
E           )

The 0 vs 9 at the end is the only real diff. But I also get a lot of noise due to the long list before. The differences are just formatting, they are not real differences. They seem to stem from the fact that dirty_equals formats long lists differently from pytest (7.1.3).

If the list is short all is well, the problem only occurs when the list is long and pytest decides to format it with one element in each line.

I've been trying for a little bit to come up with a possible fix for this, to no avail. Does not seem to be a simple fix. I am not sure if it is possible to fix this. But it bothers me a lot and I am willing to try. Any advice is welcome.

@gjcarneiro
Copy link
Author

Another similar example:

def test_obj():
    result = {
        "person": {
            "foo": "bar",
            "ready": False,
            "list": [
                "xxx",
                "xxx",
                "xxx",
                "xxx",
                "xxx",
                "xxx",
                "xxx",
                "xxx",
                "xxx",
                "xxx",
            ],
        },
    }

    assert result == {"person": AnyThing(), "xxx": "yyy"}

@gjcarneiro gjcarneiro changed the title long list repr causes pytest to print a diff objects with multiline repr cause pytest to print a diff Jul 15, 2023
@alexmojaki
Copy link
Contributor

pytest is using pprint.pformat to make these diffs. If you monkeypatched that so that pprint.pformat(dirty_obj) returned pprint.pformat(x) where x == dirty_obj passed (similar to how the __repr__ magic works) that might work.

@alexmojaki
Copy link
Contributor

I did this in alexmojaki#1. Now it gives the right diff in the first example:

E       AssertionError: assert (['ddsdsdads ', 'sfafsdsd', 'dfds sdfsef', 'sfdssdf ', 'sdsadfs', 'dsfsdfsd', 'cfdfdfd', 'dgffgfdgfd', 'fdsfsdgfsdgsf'], 1, 2, 3, 4, 5, 6, 7, 8, 9) == (['ddsdsdads ', 'sfafsdsd', 'dfds sdfsef', 'sfdssdf ', 'sdsadfs', 'dsfsdfsd', 'cfdfdfd', 'dgffgfdgfd', 'fdsfsdgfsdgsf'], 1, 2, 3, 4, 5, 6, 7, 8, 0)
E         At index 9 diff: 9 != 0
E         Full diff:
E           (
E            ['ddsdsdads ',
E             'sfafsdsd',
E             'dfds sdfsef',
E             'sfdssdf ',
E             'sdsadfs',
E             'dsfsdfsd',
E             'cfdfdfd',
E             'dgffgfdgfd',
E             'fdsfsdgfsdgsf'],
E            1,
E            2,
E            3,
E            4,
E            5,
E            6,
E            7,
E            8,
E         -  0,
E         +  9,
E           )

@gjcarneiro
Copy link
Author

This is nice, thanks! It solves at least some of the cases. Although in my (private) code, I have a more complex test that still shows spurious changes. If I manage to reproduce in a simple test case, I'll post it, maybe in another bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants