configparser.write() loses UNNAMED_SECTION items to DEFAULT on round-trip
Open
Nobody has claimed this yet.
stdlib
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug description
ConfigParser.write() outputs UNNAMED_SECTION items after the [DEFAULT] header. When the output is read back, the headerless items appear under [DEFAULT] and are lost from UNNAMED_SECTION.
import configparser, io
US = configparser.UNNAMED_SECTION
cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg.read_string('key1 = val1\n[DEFAULT]\ndkey = dval\n[sect1]\nkey2 = val2\n')
out = io.StringIO()
cfg.write(out)
cfg2 = configparser.ConfigParser(allow_unnamed_section=True)
cfg2.read_string(out.getvalue())
print('key1' in cfg2[US]) # False -- lost
print('key1' in cfg2.defaults()) # True -- migrated to DEFAULT
CPython versions tested on
3.14, main
Operating systems tested on
macOS
Linked PRs
- gh-154597
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the round-trip shown in the issue, then inspect ConfigParser.write() and read_string() to trace how UNNAMED_SECTION items are emitted and parsed. Done means the headerless key remains in cfg2[UNNAMED_SECTION] after writing and reading the configuration; linked PR gh-154597 indicates work is already underway.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100