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

assert helper to compare list contents, ignoring order #7899

Closed
soxofaan opened this issue Oct 15, 2020 · 4 comments
Closed

assert helper to compare list contents, ignoring order #7899

soxofaan opened this issue Oct 15, 2020 · 4 comments
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@soxofaan
Copy link
Contributor

I created this small pytest assert helper to compare lists/tuples ignoring item order:
https://gist.github.com/soxofaan/8e6512f765f0f0df697311c2561be57e

It's roughly assert sorted(actual) == sorted(expected), but with some added benefits:

  • container type (list vs tuple) is also checked
  • inspired by pytest.approx, to make it more explicit what the intention of the comparison is. You only have to use it in the expected object, you can leave the actual object as is. It also works inside nested structures, e.g.
    assert {"foo": [1, 2, 3]} == {"foo": IgnoreOrder([3, 2, 1])}
    

Would it be worthwhile to create a PR for this?

@nicoddemus nicoddemus added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Oct 15, 2020
@nicoddemus
Copy link
Member

nicoddemus commented Oct 15, 2020

I like the idea; often one goes and uses a sorted() call, but that's not ideal because as you mention, it doesn't check the container type. Also using it inside nested structures is a plus.

Now to bike shedding. 😁

What should we call it? pytest.anyorder?

What others think?

@asottile
Copy link
Member

I believe this is a duplicate of #5548

@nicoddemus
Copy link
Member

Ahh indeed, we discussed this before and pytest-unordered was created because of that, although I'm not sure pytest-unordered also checks the types of the sequences being compared...

@soxofaan
Copy link
Contributor Author

thanks for the reference

closing this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

3 participants