Skip to content

configparser: unable to create unnamed section #123049

Closed

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

Activity

  1. pslacerda commented on Aug 15, 2024

    @pslacerda
    Contributor

    Hi, I have interest to contribute on this feature.

  2. jaraco commented on Aug 16, 2024

    @jaraco
    MemberAuthor

    You got it. Let me know if you have any questions.

  3. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Aug 16, 2024
  4. added 2 commits that reference this issue on Aug 16, 2024
    02c5266
    450c6c1
  5. added a commit that references this issue on Aug 18, 2024
  6. added a commit that references this issue on Aug 19, 2024
  7. added a commit that references this issue on Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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