Skip to content

Clarify wording of order='deterministic' in docstrings#1011

Merged
jcrist merged 2 commits intomainfrom
825-deterministic-doc-wording
Apr 10, 2026
Merged

Clarify wording of order='deterministic' in docstrings#1011
jcrist merged 2 commits intomainfrom
825-deterministic-doc-wording

Conversation

@Siyet
Copy link
Copy Markdown
Collaborator

@Siyet Siyet commented Apr 10, 2026

Closes #825.

Dicts in Python 3.7+ are ordered by insertion order per the language spec, so describing them as "unordered collections" in the order='deterministic' docstring was technically incorrect and confusing (as raised in #825).

The actual reason order='deterministic' sorts dict keys is not "consistency between runs" (insertion order is already stable within a run), but to ensure that dicts which compare equal produce identical encoded output, regardless of how they were constructed:

a = {"b": 1, "a": 2}
b = {"a": 2, "b": 1}
assert a == b  # True, but without sorting they'd encode differently

Reword the bullet across all encoder docstrings (json.encode, msgpack.encode, yaml.encode, toml.encode, Encoder, JSONEncoder, convert) to make this clearer. No behavior change.

Dicts in Python 3.7+ are ordered by insertion order per the language
spec, so calling them "unordered collections" was incorrect. Reword to
explain that dict keys and set elements are sorted so that values which
compare equal produce identical encoded output, regardless of insertion
or iteration order.

Closes #825
@jcrist
Copy link
Copy Markdown
Owner

jcrist commented Apr 10, 2026

Thanks!

@jcrist jcrist merged commit 20cc59d into main Apr 10, 2026
22 checks passed
@Siyet Siyet mentioned this pull request Apr 12, 2026
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.

Ordering of dict keys when using msgspec.json.encode

2 participants