Skip to content

assert helper to compare list contents, ignoring order #7899

Closed
@soxofaan

Description

@soxofaan

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions