classification
Title: ConfigParser.items returns items present in `DEFAULTSECT` when section argument given.
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: chrBrd, docs@python, lukasz.langa
Priority: normal Keywords: patch

Created on 2018-04-22 11:20 by chrBrd, last changed 2018-04-22 22:05 by chrBrd.

Pull Requests
URL Status Linked Edit
PR 6446 chrBrd, 2018-04-22 11:27
PR 6567 closed chrBrd, 2018-04-22 21:13
Messages (4)
msg315608 - (view) Author: Chris Bradbury (chrBrd) Date: 2018-04-22 11:20
According to `ConfigParser.items` docs:

> When section is not given, return a list of section_name, section_proxy pairs, including DEFAULTSECT.

> Otherwise, return a list of name, value pairs for the options in the given section. Optional arguments have the same meaning as for the get() method.

https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.items

However due to `d = self._defaults.copy()` on line 843 of configparser.py the default section is always included, even when a section argument is specified.
msg315625 - (view) Author: Chris Bradbury (chrBrd) Date: 2018-04-22 18:03
The fix for the actual issue is quite trivial (and should fix bpo-33251 at the same time). However `ExtendedInterpolation::_interpolate_some` relies on the options from the default section always being returned.

Not only does this make the fix more complicated, it also makes me worry that aligning behaviour to match the documentation (which I personally feel makes more sense) will breake backwards compatibility.
msg315629 - (view) Author: Ɓukasz Langa (lukasz.langa) * (Python committer) Date: 2018-04-22 21:38
See line 379 in configparser.py. BasicInterpolation is also assumed to work for values from the default section.  This is why the default section exists in the first place.

More importantly, as you're pointing out, even though this is an omission of the documentation, existing code in the wild probably assumes that `items()` include pairs from the default section.  This is how ConfigParser worked in Python 2.  If you don't want this behavior, use cfgparser[section].items() instead which does what you want.

If you think this is helpful, go ahead and extend the documentation of ConfigParser.items() to point out this weirdness.  But we won't be going forward with PR 6567.
msg315630 - (view) Author: Chris Bradbury (chrBrd) Date: 2018-04-22 21:48
I'll make the documentation changes instead, not overly surprised to hear this is intentional.
History
Date User Action Args
2018-04-22 22:05:28chrBrdsetstatus: closed -> open

nosy: + docs@python
assignee: docs@python
components: + Documentation, - Library (Lib)
resolution: not a bug ->
2018-04-22 21:53:20chrBrdsetresolution: not a bug
2018-04-22 21:48:56chrBrdsetstatus: open -> closed

messages: + msg315630
stage: patch review -> resolved
2018-04-22 21:38:17lukasz.langasetmessages: + msg315629
2018-04-22 21:13:47chrBrdsetpull_requests: + pull_request6263
2018-04-22 18:03:14chrBrdsetmessages: + msg315625
2018-04-22 11:31:36serhiy.storchakasetnosy: + lukasz.langa
components: + Library (Lib)
2018-04-22 11:27:55chrBrdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request6261
2018-04-22 11:20:36chrBrdcreate