question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a built-in way to compare sequences ignoring order

See original GitHub issue

What’s the problem this feature will solve?

A convenient way to assert that two sequences contains the exact same elements regardless of their order.

Describe the solution you’d like

assert [1, 2, 3] == pytest.unordered([2, 1, 3])

For example, I could use it when order of elements are not part of the specification of a function:

# Per "Path.iterdir()" documentation: "The children are yielded in arbitrary order"
assert list(tmp_path.iterdir()) == pytest.unordered([tmp_path / "some_file.txt", tmp_path / "another_file.txt"])

Alternative Solutions

Sorting the sequence manually, using unittest.TestCase.assertCountEqual, using pytest-unordered plugin.

Additional context

This issue duplicates:

These tickets were closed with the suggestion to first create an external plugin and testing it during a few years. Following this, @utapyngo created the pytest-unordered plugin. It has been several years. I would like to bring back the suggestion of a built-in pytest.unordered() helper similar to pytest.approx() because I think pytest users would greatly benefit from it.

I don’t think it opens the room for too many assertion helpers: with approx() and unordered() all main cases are covered. For comparison, here is the list of helpers provided by the Catch2 C++ testing library. Python provides many built-in functions to ease assertion, but unordered() is missing and is non-trivial.

What do you think?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
RonnyPfannschmidtcommented, Jun 10, 2022

When order doesn’t matter, comparing the result of sorted is orders of magnitude simpler to comprehend.

If unique values should be folded, comparing sets is order of magnitude simpler to comprehend.

So far i haven’t seen a setup where unordered is necessary and more comprehensive.

I’d like to see one before adding such a non trivial tool.

2reactions
kalekundertcommented, Jun 10, 2022

If it helps to have an example of unordered in a real test, here’s one I wrote just the other day: stepwise/tests/test_reaction.py. This is exactly one of the cases that @Delgan described. I’m trying to compare two lists-of-dicts that each represent the edges of a graph data structure. Because dicts are neither sortable nor hashable, neither sorted nor set can work here.

I use unordered in almost every package I write tests for, and I think it’s very useful. I’d love to see it added to pytest proper.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Compare two List<T> objects for equality, ignoring order
I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear...
Read more >
Built-ins for sequences - Apache FreeMarker Manual
This built-in splits a sequence into multiple sequences of the size given with the 1st parameter to the built-in (like mySeq?chunk(3) ).
Read more >
Assert Two Lists for Equality Ignoring Order in Java - Baeldung
In this first test, the size of both lists is compared before we check if the elements in both lists are the same....
Read more >
difflib — Helpers for computing deltas — Python 3.11.1 ...
This module provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce information about...
Read more >
Comparison in Python is Not as Simple as You May Think
When comparing 2 sequences (tuple or list) using == , the order of elements is taken into consideration. According to Python documentation,.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found