python / python/cpython

Support comments as part of multiline values in configparser

Open
#130,927 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

Today's configparser module does not consider lines being properly indented and starting with a comment_prefixes to be part of a multiline value. This can lead to unintended data loss during reading an INI file as:
config.ini

# I am a comment
    # I am also a comment
[dummy]
# I am a comment
key = I am a multiline
# I am a comment
    # multiline part which is not considered but should be
    being again part
# I am a comment

test.py

import configparser
config = configparser.ConfigParser(comment_prefixes='#', interpolation=None, inline_comment_prefixes=None)
config.read('config.ini')
print(config['dummy']['key'])
# output: 'I am a multiline\nbeing again part'
# expected output:  'I am a multiline\n# multiline part which is not considered but should be\nbeing again part''

Therefore, I would propose to add a new parameter allow_comment_in_value (default False) in the ConfigParser class to support such scenarios. Please note that in this scenario the comment_prefixes is set to '#' and any interpolation is disabled!

Thanks and best regards!

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ConfigParser entry point and reproduce the behavior using the config.ini and test.py example in the issue. Trace how comment_prefixes and multiline values are handled, then add coverage for the proposed option. Done means the indented comment is preserved in the multiline value while existing behavior remains unchanged by default.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.