Understand inherited defaults when removing an INI override
Deleting an INI section override can reveal the shared default. If the default enables a feature, removing a local setting may enable it again. Use an explicit value when a section must stay disabled.
Set up
Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save as inherited_settings.py:
from configparser import ConfigParser
config = ConfigParser(interpolation=None)
config.read_string("""
[DEFAULT]
enabled = yes
[worker]
enabled = no
[scheduler]
""")
worker = config["worker"]
print("Override:", worker.getboolean("enabled"))
del worker["enabled"]
print("After deletion:", worker.getboolean("enabled", fallback=False))
print("Still visible:", "enabled" in worker)
try:
del worker["enabled"]
except KeyError:
print("Inherited value cannot be deleted from this section")
else:
raise AssertionError("inherited value was deleted through the section")
worker["enabled"] = "no"
assert worker.getboolean("enabled") is False
assert config["scheduler"].getboolean("enabled") is True
print("Explicit worker setting:", worker.getboolean("enabled"))
print("Other section:", config["scheduler"].getboolean("enabled"))Run:
python3 -B inherited_settings.pyObserved output:
Override: False
After deletion: True
Still visible: True
Inherited value cannot be deleted from this section
Explicit worker setting: False
Other section: TrueDistinguish overrides from effective values
A section proxy exposes the effective configuration, including inherited defaults. The first deletion removes the local override. Looking up the same name then finds DEFAULT, so the fallback is not used and membership remains true.
The second deletion raises KeyError because there is no local value left to delete. Assigning "no" adds a local override again, leaving the scheduler’s inherited value unchanged.
Avoid changing every section accidentally
Removing the name from config.defaults() would affect all sections that inherit it. That is a different operation from disabling one worker. Use getboolean() for Boolean settings: applying bool() to the nonempty string "no" would give True.
These changes affect the in-memory parser only. Persisting them requires a separate write with appropriate file ownership and replacement handling. The mapping-protocol documentation explains inherited values, section deletion and defaults. No existing settings files are opened by this example.
Clean up
Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.
rm -- inherited_settings.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.