Skip to content

bpo-37779 : Add information about the overriding behavior of ConfigParser.read #15177

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 8 commits into from
Sep 23, 2020
22 changes: 22 additions & 0 deletions Doc/library/configparser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ involves the ``DEFAULT`` section which provides default values for all other
sections [1]_. Note also that keys in sections are
case-insensitive and stored in lowercase [1]_.

If it is necessary to read several configurations, with each one having more
priority than the previous one, an instance of :class:`ConfigParser` can be
used to override previously defined properties and retain existing ones.
Copy link
Member

Choose a reason for hiding this comment

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

One problem I see with this is that the first few clauses of this sentence seem to be introducing a tutorial, i.e. "If you want to do X, you can achieve it with Y", but then it turns out that you are actually describing the existing behavior of ConfigParser rather than providing a recipe or something.

I would recommend rewriting this as an introduction to the properties of a ConfigParser, like so:

It is also possible to read several configurations into a single :class:`ConfigParser` object, in which case the most recent configuration has priority over the previous ones; conflicting keys will be taken from the new configuration and existing keys will be retained.

That's mostly an example, I'm not super happy with that particular wording...

Copy link
Contributor

@aeros aeros Sep 11, 2019

Choose a reason for hiding this comment

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

@pganssle:

That's mostly an example, I'm not super happy with that particular wording...

What do you think of this wording?

It is possible to read several configurations into a single :class:ConfigParser, where the most recently added configuration has the highest priority. Any conflicting keys are taken from the more recent configuration while the previously existing keys are retained.

Copy link
Member

Choose a reason for hiding this comment

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

:shipit: That looks great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pganssle I pushed @aeros167 's version to the repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have made the requested changes; please review again


.. doctest::

>>> config = configparser.ConfigParser()
>>> config.read('example.ini')
['example.ini']
>>> config['topsecret.server.com']['Port']
'50022'
>>> config.read_string("[topsecret.server.com]\nPort=48484")
>>> config['topsecret.server.com']['Port']
'48484'
>>> config.read_dict({"topsecret.server.com": {"Port": 21212}})
>>> config['topsecret.server.com']['Port']
'21212'
>>> config['topsecret.server.com']['ForwardX11']
'no'


This behaviour is equivalent to a :meth:`ConfigParser.read` call with several files passed to ``filenames`` parameter.

Supported Datatypes
-------------------
Expand Down