Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-123049: Allow to create the unnamed section from scratch. #123077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 18, 2024

Conversation

pslacerda
Copy link
Contributor

@pslacerda pslacerda commented Aug 16, 2024

@pslacerda pslacerda force-pushed the add-unnamed-section branch from c9c8363 to 450c6c1 Compare August 16, 2024 20:32
@pslacerda
Copy link
Contributor Author

I think now is fine.

Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Nice work including the test.

Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I just noticed something else. By looking at the docs, I see they no longer match the implementation. We'll want to update the docs to reflect the implementation and also indicate the version changed where UNNAMED_SECTION is allowed (3.14).

@bedevere-app
Copy link

bedevere-app bot commented Aug 16, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@pslacerda
Copy link
Contributor Author

Is this ok?

   .. method:: add_section(section)

      Add a section named *section* or :const:`UNNAMED_SECTION` to the instance.

      If the given section already exists, :exc:`DuplicateSectionError` is
      raised. If :const:`UNNAMED_SECTION` (with `allow_unnamed_section=True` on
      :meth:`!__init__`), or if the *default section* name is passed,
      :exc:`ValueError` is raised.

      Type of *section* is not checked which lets users create non-string named
      sections.  This behaviour is unsupported and may cause internal errors.

   .. versionchanged:: 3.14
        Added support for :const:`UNNAMED_SECTION`.

@pslacerda
Copy link
Contributor Author

pslacerda commented Aug 16, 2024

And the _validate_value_types check str explicitly, shoudn't it test for __str__?

Not great looking code ahead:

    def _validate_value_types(self, *, section="", option="", value=""):
        """Raises a TypeError for incorrect non-string values.

        Legal non-string values are UNNAMED_SECTION and valueless values if
        they are are allowed.

        For compatibility reasons this method is not used in classic set()
        for RawConfigParsers. It is invoked in every case for mapping protocol
        access and in ConfigParser.set().
        """
        if section is UNNAMED_SECTION:
            if not self._allow_unnamed_section:
                raise ValueError("UNNAMED_SECTION is not allowed")
        else:
            if not isinstance(section, str):
                raise TypeError("section names must be strings or UNNAMED_SECTION")
        if not isinstance(option, str):
            raise TypeError("option keys must be strings")
        if not self._allow_no_value or value:
            if not isinstance(value, str):
                raise TypeError("option values must be strings")

Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks for the extended effort!

@pslacerda
Copy link
Contributor Author

Thanks @jaraco and @picnixz.

Please, squash the messy commits on merging.

@jaraco jaraco merged commit be257c5 into python:main Aug 18, 2024
34 checks passed
@jaraco
Copy link
Member

jaraco commented Aug 18, 2024

Great work everyone. I've backported this functionality to configparser 7.1, so you can utilize the behavior from there until you can rely on Python 3.14.

@mtnpke
Copy link

mtnpke commented Aug 19, 2024

Works perfectly! Thanks!

jeremyhylton pushed a commit to jeremyhylton/cpython that referenced this pull request Aug 19, 2024
…om scratch. (python#123077)


---------

Co-authored-by: Jason R. Coombs <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
blhsing pushed a commit to blhsing/cpython that referenced this pull request Aug 22, 2024
…om scratch. (python#123077)


---------

Co-authored-by: Jason R. Coombs <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

configparser: unable to create unnamed section
4 participants