Lack of control over behavior of Duplicate Sections and Options while reading with ConfigParser. #107428
Description
Bug report
Checklist
- I am confident this is a bug in CPython,
not a bug in a third-party project - I have searched the CPython issue tracker,
and am confident this bug has not been reported before
A clear and concise description of the bug
Currently, we do not have much control over the behavior of ConfigParser when it encounters a duplicate section header or a duplicate option inside a section. It will directly raise an exception in strict mode and the read will fail. In case of non-strict mode, it will parse the config but there's no way to add custom behavior to it.
For example, I had a business requirement where we had to log a warning incase a duplicate section/option is encountered. But to do this I had to subclass the ConfigParser and override the entire _read method with a lot of code duplication just to add two log lines.
Another use case would be having different duplicate handling for Sections and Options.
MRE:
Consider this config file:
[Section1]
opt1 = 1
opt2 = 2
[Section2]
opt1 = 1
opt2 = 2
opt1 = 3
[Section1]
opt1 = 2When I do ConfigParser().read("config.ini"), this will raise a DuplicateOptionError and the code will stop executing. If I pass strict=False, it will parse as expected but there is no way to intimate to the end user that the config contains duplicates.
Your environment
- CPython versions tested on: 3.9, 3.10, 3.11
- Operating system and architecture: Windows x64
Activity
serv-inc commentedon Aug 5, 2026
another use case are systemd config files, e.g. Environment can have several values