Skip to content

Commit cbcfeca

Browse files
cristianMelipre-commit-ci[bot]nicoddemus
authored
Cache.set preserves key order when saving dicts (#9206)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 0696d3e commit cbcfeca

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Christopher Gilling
7676
Claire Cecil
7777
Claudio Madotto
7878
CrazyMerlyn
79+
Cristian Vera
7980
Cyrus Maden
8081
Damian Skrzypczak
8182
Daniel Grana

changelog/9205.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`pytest.Cache.set` now preserves key order when saving dicts.

src/_pytest/cacheprovider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def set(self, key: str, value: object) -> None:
193193
return
194194
if not cache_dir_exists_already:
195195
self._ensure_supporting_files()
196-
data = json.dumps(value, indent=2, sort_keys=True)
196+
data = json.dumps(value, indent=2)
197197
try:
198198
f = path.open("w")
199199
except OSError:

testing/test_cacheprovider.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,17 @@ def test_gitignore(pytester: Pytester) -> None:
12101210
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
12111211

12121212

1213+
def test_preserve_keys_order(pytester: Pytester) -> None:
1214+
"""Ensure keys order is preserved when saving dicts (#9205)."""
1215+
from _pytest.cacheprovider import Cache
1216+
1217+
config = pytester.parseconfig()
1218+
cache = Cache.for_config(config, _ispytest=True)
1219+
cache.set("foo", {"z": 1, "b": 2, "a": 3, "d": 10})
1220+
read_back = cache.get("foo", None)
1221+
assert list(read_back.items()) == [("z", 1), ("b", 2), ("a", 3), ("d", 10)]
1222+
1223+
12131224
def test_does_not_create_boilerplate_in_existing_dirs(pytester: Pytester) -> None:
12141225
from _pytest.cacheprovider import Cache
12151226

0 commit comments

Comments
 (0)