Skip to content

Order of configparser.ConfigParser.read() arguments #96765

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

Closed
DimitriPapadopoulos opened this issue Sep 12, 2022 · 2 comments
Closed

Order of configparser.ConfigParser.read() arguments #96765

DimitriPapadopoulos opened this issue Sep 12, 2022 · 2 comments
Labels
docs Documentation in the Doc dir easy

Comments

@DimitriPapadopoulos
Copy link

DimitriPapadopoulos commented Sep 12, 2022

Bug report

When multiple INI configuration files with identical section(s) and key(s) are fed to configparser.ConfigParser.read, the result is undefined / not documented. Actually the last file containing a given section/key takes precedence over previous files, as I would have expected from a straightforward implementation. However:

  • This behaviour should be documented.
  • Perhaps this behaviour should be changed, and the first file take precedence over subsequent files. In practice that would break compatibility, but since the result is currently undocumented, that may be OK.

Example

Here are two INI files:

1.ini

[section]
a = 1
b = "11"

2.ini

[section]
a = 2
b = "22"

And here is the order of the filenames changes the result:

>>> import sys
>>> import configparser
>>> config = configparser.ConfigParser()
>>> 
>>> config.read(["1.ini", "2.ini"])
['1.ini', '2.ini']
>>> config.write(sys.stdout)
[section]
a = 2
b = "22"

>>> 
>>> config.read(["2.ini", "1.ini"])
['2.ini', '1.ini']
>>> config.write(sys.stdout)
[section]
a = 1
b = "11"

>>> 

Your environment

  • CPython versions tested on: 3.6.9
  • Operating system and architecture: Ubuntu 18.04

Linked PRs

@sappelhoff
Copy link

I believe this is documented here: https://docs.python.org/3/library/configparser.html

It is possible to read several configurations into a single 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.

@DimitriPapadopoulos
Copy link
Author

DimitriPapadopoulos commented Oct 23, 2022

Indeed. Sorry about that.

Still, I think this piece of information needs to be moved or copied from Quick Start to the configparser.ConfigParser.read documentation.

@ambv ambv added docs Documentation in the Doc dir easy and removed type-bug An unexpected behavior, bug, or error labels Jul 31, 2023
timonviola added a commit to timonviola/cpython that referenced this issue Jul 13, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 13, 2024
…example (pythonGH-121664)

(cherry picked from commit fc21781)

Co-authored-by: Timon Viola <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 13, 2024
…example (pythonGH-121664)

(cherry picked from commit fc21781)

Co-authored-by: Timon Viola <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
encukou pushed a commit that referenced this issue Jul 13, 2024
… example (GH-121664) (GH-121688)

(cherry picked from commit fc21781)

Co-authored-by: Timon Viola <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
encukou pushed a commit that referenced this issue Jul 13, 2024
… example (GH-121664) (GH-121687)

(cherry picked from commit fc21781)

Co-authored-by: Timon Viola <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
IljaManakov pushed a commit to IljaManakov/cpython that referenced this issue Jul 13, 2024
@ambv ambv closed this as completed Jul 13, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants