This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Stig Johan Berggren
Recipients Stig Johan Berggren
Date 2018-07-26.20:23:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1532636603.09.0.56676864532.issue34242@psf.upfronthosting.co.za>
In-reply-to
Content
`get()` on a ConfigParser object behaves differently from `get()` on a section. The former raises an exception when the key does not exist and no fallback has been explicitly set. The latter returns None, with no option to raise an error for missing keys. I think this is confusing, and that both classes should have the same behaviour. I prefer raising an error, as it makes it easier to find errors such as typos in config files.

In addition, the docs state that the "parser-level `get` method
provides a custom, more complex interface, maintained for backwards
compatibility". The SectionProxy `get` method internally uses the parser-level `get`, so it seems unlikely that it is only maintained for backwards compatibility.

My proposed change is not backwards compatible, as any code that relies on a None being returned when a key does not exist would have to make this explicit through the fallback argument in `get`.

Here is the current behaviour in the latest build (3.8.0a0):

>>> import configparser
>>> c = configparser.ConfigParser()
>>> c.add_section('spam')
>>> c['spam'].get('eggs') # Returns None
>>> c['spam']['eggs']
Traceback (most recent call last):
  ...
KeyError: 'eggs'
>>> c.get('spam', 'eggs')
Traceback (most recent call last):
  ...
configparser.NoOptionError: No option 'eggs' in section: 'spam'
>>>
History
Date User Action Args
2018-07-26 20:23:23Stig Johan Berggrensetrecipients: + Stig Johan Berggren
2018-07-26 20:23:23Stig Johan Berggrensetmessageid: <1532636603.09.0.56676864532.issue34242@psf.upfronthosting.co.za>
2018-07-26 20:23:23Stig Johan Berggrenlinkissue34242 messages
2018-07-26 20:23:22Stig Johan Berggrencreate