Skip to content

Lack of control over behavior of Duplicate Sections and Options while reading with ConfigParser. #107428

Open

Description

@Hyperclaw79

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 = 2

When 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

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 28, 2023
  2. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Jul 3, 2026
  3. serv-inc commented on Aug 5, 2026

    @serv-inc

    another use case are systemd config files, e.g. Environment can have several values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions