Skip to content

Fix relative ordering in a complex transitive cases#242

Open
victor-nexthop wants to merge 7 commits into
pytest-dev:mainfrom
victor-nexthop:main
Open

Fix relative ordering in a complex transitive cases#242
victor-nexthop wants to merge 7 commits into
pytest-dev:mainfrom
victor-nexthop:main

Conversation

@victor-nexthop
Copy link
Copy Markdown

We only use before and after constraints, 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.
"""
)
result = test_path.runpytest("-v", "--error-on-failed-ordering")
if int(pytest.__version__.split(".")[0]) < 6:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This looks like a special case for some past version. Does it make sense to preserve 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.

Sure, we don't support pytest < 6 anymore.

result.stdout.fnmatch_lines(
[
"test_failed_ordering.py::test_2 PASSED",
"test_failed_ordering.py::test_1 ERROR",
Copy link
Copy Markdown
Author

@victor-nexthop victor-nexthop Jun 1, 2026

Choose a reason for hiding this comment

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

Not sure what this tested and if it still makes sense.

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 was testing the --error-on-failed-ordering option, though the test used was one that failed due a shortcoming of the algorithm. Not sure if there still is the possibility of cyclic dependencies that cannot be resolved.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see, thanks. Why, there is always a possibility of a cyclic dependency, isn't there?

@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
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.

Copy link
Copy Markdown
Author

@victor-nexthop victor-nexthop Jun 4, 2026

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.

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