Skip to content

Commit 4bf9228

Browse files
authored
docs: fix merge order in the config how-to (#529)
The example under "Merging config from multiple sources" merges in the wrong direction. Its comment claims it iterates "from least specific (site) to most specific (user)", but the iterators yield the user directory first: ```pycon >>> list(PlatformDirs("MyApp").iter_config_paths()) [PosixPath('/Users/me/Library/Application Support/MyApp'), PosixPath('/Library/Application Support/MyApp')] ``` So `config.update()` applies the site file last and the site defaults overwrite the user's own config, the opposite of what the paragraph above the block promises. With a site `config.json` of `{"theme": "site-default", "lang": "en"}` and a user one of `{"theme": "user-choice"}`, the documented loop yields `theme: site-default`. Reversing it yields `theme: user-choice` and still inherits `lang` from the site file. Found while reviewing #524, which cites this example as the use case its iterators serve. Docs only, no behaviour change.
1 parent c653668 commit 4bf9228

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

docs/changelog/529.doc.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the config merging example in the how-to guide. ``iter_config_paths`` yields the user directory first, so the
2+
``config.update`` loop let the site defaults override the user's config instead of the other way round.

docs/howto.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ user-specific overrides:
143143
dirs = PlatformDirs("MyApp")
144144
config = {}
145145
146-
# Iterate from least specific (site) to most specific (user)
147-
for config_dir in dirs.iter_config_paths():
146+
# The iterators yield the user directory first, so apply them in reverse to let it win
147+
for config_dir in reversed(list(dirs.iter_config_paths())):
148148
config_file = config_dir / "config.json"
149149
if config_file.exists():
150150
config.update(json.loads(config_file.read_text()))

tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,10 @@ def test_mypy_subclassing() -> None:
141141
class PlatformDirsSubclass(platformdirs.PlatformDirs): ...
142142

143143
class AppDirsSubclass(platformdirs.AppDirs): ...
144+
145+
146+
@pytest.mark.parametrize("kind", ["config", "data", "cache", "state", "log", "runtime"])
147+
def test_iter_dirs_yields_user_before_site(kind: str) -> None:
148+
# docs/howto.rst merges config in reverse of this order so the user directory wins.
149+
dirs = platformdirs.PlatformDirs("MyApp", "MyCompany", version="1.0")
150+
assert next(getattr(dirs, f"iter_{kind}_dirs")()) == getattr(dirs, f"user_{kind}_dir")

0 commit comments

Comments
 (0)