You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-3Lines changed: 32 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -330,15 +330,16 @@ Each field value is resolved in the following order:
330
330
331
331
```mermaid
332
332
graph TD;
333
-
A[Default values] --> B(Data sources in the order listed) --> C(The values set at runtime)
333
+
A[Default values] --> B(Class sources) --> C(Field sources) --> D(Instance sources) --> E(The values set at runtime)
334
334
```
335
335
336
336
That is, values obtained from sources have higher priority than default values, but can be overwritten (unless you [prohibit it](#read-only-fields)) by other values at runtime.
337
337
338
-
There are two ways to specify a list of sources:
338
+
There are three ways to specify a list of sources:
339
339
340
340
- For the **whole class**.
341
341
- For a **specific field**.
342
+
- For a **specific instance**.
342
343
343
344
To specify a list of sources for the entire class, pass it to the class constructor:
344
345
@@ -363,9 +364,37 @@ class MyClass(Storage, sources=[TOMLSource('pyproject.toml', table='tool.my_tool
Without an ellipsis, instance-level sources completely replace both class-level and field-level sources. If you want instance-level sources to have the highest priority while still falling back to other sources, use an ellipsis:
In this case, instance sources are checked first, and if a value is not found, the lookup falls back to the sources that the field would normally use without `_sources`. The fallback rules are:
380
+
381
+
- If a field has no `sources` parameter → fallback to class-level sources directly.
382
+
- If a field has `sources` without `...` → fallback to field-level sources only (class-level sources are **not** included).
383
+
- If a field has `sources` with `...` → fallback to field-level sources, then class-level sources.
384
+
385
+
> ⚠️ This means that `...` in `_sources` does **not** always reach class-level sources. If a field defines its own `sources` without `...`, class-level sources are excluded for that field even when instance-level `_sources` contains `...`:
386
+
>
387
+
> ```python
388
+
>classMyClass(Storage, sources=[EnvSource()]):
389
+
># This field's sources do not include ..., so EnvSource() is unreachable for it:
Only `list`and`tuple` are accepted as the `_sources` collection type.
394
+
366
395
All values from sources are loaded when the config objectis created. This means that (theoretically) during program execution, you can, for example, change a configuration file, then create a new storage object, and its contents will be different. The old object will not automatically know that the config file has been changed. Avoid this pattern, as it can lead to subtle bugs.
367
396
368
-
Each data source behaves like a mapping, and field values are looked up by field name. If no value is found in any of the sources, only then will the default value be used. The order in which the contents of the sources are checked corresponds to the order in which the sources themselves are listed, with sources for a field having higher priority than sources for the class as a whole.
397
+
Each data source behaves like a mapping, and field values are looked up by field name. If no value is found inany of the sources, only then will the default value be used. The order in which the contents of the sources are checked corresponds to the order in which the sources themselves are listed. When multiple levels of sources are combined via ellipsis, instance-level sources have the highest priority, followed by field-level sources, and then class-level sources.
369
398
370
399
For any field, you can change the key used to search for its value in the sources using the `alias` parameter:
0 commit comments