Skip to content

Conversation

@DavidAGInnovation
Copy link

Closes #13503. Modified SafeRepr to override repr_dict and avoid sorting keys, ensuring that assertion failure messages preserve the user's insertion order for dictionaries.

@DavidAGInnovation DavidAGInnovation force-pushed the feature/pytest-unsorted-repr branch from d415833 to 862ac24 Compare December 17, 2025 03:41
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Dec 17, 2025
@DavidAGInnovation DavidAGInnovation force-pushed the feature/pytest-unsorted-repr branch from 862ac24 to e1a94c1 Compare December 17, 2025 03:45
@DavidAGInnovation
Copy link
Author

Hey. Can someone review it?

Copy link
Member

Choose a reason for hiding this comment

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

Could you please also add an integration test like the one suggested in #14066 (comment)? Thanks!

)


def test_saferepr_dict_preserves_insertion_order():
Copy link
Member

Choose a reason for hiding this comment

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

Please add type annotations to the tests (-> None).



def test_saferepr_dict_preserves_insertion_order():
d = {"b": 1, "a": 2}
Copy link
Member

Choose a reason for hiding this comment

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

Could we have more keys here just for safety?

Suggested change
d = {"b": 1, "a": 2}
d = {
"b": 2,
"a": 1,
"d": 4,
"e": 5,
"c": 3,
}

@@ -0,0 +1 @@
Assertion comparison output now preserves dictionary insertion order instead of sorting keys.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Assertion comparison output now preserves dictionary insertion order instead of sorting keys.
Display dictionary differences in assertion failures using the original key insertion order instead of sorted order.



def test_saferepr_dict_fillvalue_when_level_is_zero():
from _pytest._io.saferepr import SafeRepr
Copy link
Member

Choose a reason for hiding this comment

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

Please move these imports from local to global.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Please take a look at my comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failure output order of dictionary keys is alphabetical instead of insertion order

2 participants