Skip to content

Fix relative ordering in a complex transitive cases#242

Merged
mrbean-bremen merged 14 commits into
pytest-dev:mainfrom
victor-nexthop:main
Jun 12, 2026
Merged

Fix relative ordering in a complex transitive cases#242
mrbean-bremen merged 14 commits into
pytest-dev:mainfrom
victor-nexthop:main

Conversation

@victor-nexthop

@victor-nexthop victor-nexthop commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

We only use before and after constraints edit: and a first/last, and sometimes, with sparse transitive dependencies, pytest-order won't produce the order that we expect, silently ignoring one of the constraints.

This is a set of changes that replaces the current algorithm with a topological sort, and adds a warning about impossible sets of constraints, for example: @order(first) def test_foo(): pass; @order(before="test_foo") def test_bar(): pass.

Created mostly by Claude.

This is a conversation starter. Please let me know if it needs to be squashed and split into smaller commits.

victor-nexthop and others added 7 commits May 28, 2026 19:46
apply_relative_constraints() unconditionally rebuilt sorted_list as
start_pinned + sorted_middle + end_pinned, discarding the unordered
items that sort_numbered_items() had interspersed between numbered
items in sparse ordering mode.

Early-return when there are no relative or dependency marks to
preserve the layout produced by sort_numbered_items().

Fixes 5 sparse ordering tests.
Comment thread tests/test_relative_ordering.py
Comment thread tests/test_relative_ordering.py
@victor-nexthop victor-nexthop changed the title Fix Fix relative ordering in a complex transitive cases Jun 2, 2026
@mrbean-bremen

Copy link
Copy Markdown
Member

Thanks! I will have a closer look at this, though this may take some time (just back from traveling).


pytest-order emits a WARNING for each such impossible constraint and preserves
the absolute ordinal positions unchanged.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the current behavior, which was intentional. Explicit dependencies between tests shall be prioritized over ordinal sorting - I still think that makes sense.
Can you elaborate why you want to change this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just in the mindset of "all ordering shall be satisfied", and didn't read the docs that deep. Wouldn't have made it a requirement.

@victor-nexthop victor-nexthop Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using topological sorting still makes sense?

I'll rework the changes according to the design decision, likely next week.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...reworked.

@victor-nexthop

Copy link
Copy Markdown
Contributor Author

Pushed a new version. It looks decent to me.
This PR is meant to be squashed into one commit.

Comment thread tests/test_order_transitive.py Outdated
Comment thread src/pytest_order/item.py Outdated
Comment thread src/pytest_order/item.py
Comment thread src/pytest_order/item.py Outdated
Comment thread CHANGELOG.md
absolute ordinal markers (`index`, `first`, `last`, …); a conflicting ordinal
position is relaxed instead of dropping the relative marker
- transitive relative chains are resolved as a single globally consistent order

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this merits a patch release after it is merged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of a patch. This may be a breaking change, because:

  • I found that before, some constraints were dropped silently, and after the chance they produce a warning. I don't have an example in tests now, would you require one?
  • Obviously, ordering for some cases has changed - I had to update tests to match it. Specifically, old versions of tests.test_relative_ordering.test_dependency_loop and tests.test_relative_ordering.test_failed_tests_after_dependency_loop fail on the new code.
  • And of course there are customers who rely on the old, incomplete ordering 😆 😢

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I still think of it as a bug fix. I know you are thinking of Hyrum's Law, but you just made cases possible that before have been handled (incorrectly) as a dependency loop. If somebody relied on that behavior to prevent some implied ordering, I certainly wouldn't bother about it...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the warning was missing before, so it just improved the visibility of an incorrect marker.

victor-nexthop and others added 3 commits June 11, 2026 10:33
Comment thread CHANGELOG.md Outdated
## Unreleased

### Fixes
- relative order markers (`before`/`after`) now always take preference over

@victor-nexthop victor-nexthop Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this comment is necessary - this is the way it always worked, isn't it?
Or was it only about combination of relative/absolute on the same item?

@mrbean-bremen mrbean-bremen Jun 11, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I actually wanted to ask in which cases it didn't work - I thought that you maybe had found such a case.
But yes, this should have been the way it work before (i.e., the comment can be removed).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed that item. No, I didn't see any violations of this behavior.

@victor-nexthop

Copy link
Copy Markdown
Contributor Author

Comments addressed. Please lmk if you see anything else.
I'll be happy to see it released!

@mrbean-bremen mrbean-bremen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mrbean-bremen mrbean-bremen merged commit c411fc8 into pytest-dev:main Jun 12, 2026
16 checks passed
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 this pull request may close these issues.

2 participants