You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
configparser: unable to create unnamed section #123049
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'
Activity
pslacerda commentedon Aug 15, 2024
Hi, I have interest to contribute on this feature.
jaraco commentedon Aug 16, 2024
You got it. Let me know if you have any questions.
Allow to create the unnamed section from scratch.
Add support to unnamed section on `RawConfigParser.add_section`
gh-123049: configparser: Allow to create the unnamed section from scr…
pythongh-123049: configparser: Allow to create the unnamed section fr…
pythongh-123049: configparser: Allow to create the unnamed section fr…