Skip to content

configparser: unable to create unnamed section #123049

Closed
@jaraco

Description

@jaraco

Hi, just chiming in because I really love this new feature - however, I haven't managed to create a new INI file from scratch with an unnamed section yet. Is this possible? Here are my futile attempts with configparser 7.0.0 from backports:

Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
>>> from backports.configparser import UNNAMED_SECTION, ConfigParser
>>> cp = ConfigParser(allow_unnamed_section=True)
>>> cp.set(UNNAMED_SECTION, "option1", "hello world")
Traceback (most recent call last):
    super().set(section, option, value)
    raise NoSectionError(section) from None
backports.configparser.NoSectionError: No section: <UNNAMED_SECTION>
>>> cp.add_section(UNNAMED_SECTION)
  File "C:\Users\kerling\AppData\Local\pypoetry\Cache\virtualenvs\non-package-mode-l42eA81v-py3.12\Lib\site-packages\backports\configparser\__init__.py", line 1294, in _validate_value_types
    raise TypeError("section names must be strings")
TypeError: section names must be strings
>>> cp[UNNAMED_SECTION]["option1"] = "hello world"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\kerling\AppData\Local\pypoetry\Cache\virtualenvs\non-package-mode-l42eA81v-py3.12\Lib\site-packages\backports\configparser\__init__.py", line 1070, in __getitem__
    raise KeyError(key)
KeyError: <UNNAMED_SECTION>
>>> cp[UNNAMED_SECTION] = { "option1": "hello world" }
>>> cp.sections()
['<UNNAMED_SECTION>']
>>> import io
>>> fp = io.StringIO()
>>> cp.write(fp)
>>> fp.getvalue()
'[<UNNAMED_SECTION>]\noption1 = hello world\n\n'

Originally posted by @mtnpke in #66449 (comment)

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions