I was surprised to find that in configparser, getboolean() does not raise KeyError for a non-existent config parameter.
Demo program (Python 3.11.5, Windows 11):

import configparser

config = configparser.ConfigParser()
config.read('ThisFileDoesNotExist.ini') # This line could be removed
MY_BOOL= config['DEFAULT'].getboolean('MY_BOOL') # This does NOT raise an Exception!
print(f"{MY_BOOL=}")
MY_STR = config['DEFAULT']['MY_STR'] # This does raise an Exception, naturally

The output:

MY_BOOL=None
Traceback (most recent call last):
  File "R:\Test.py", line 7, in <module>
    MY_STR = config['DEFAULT']['MY_STR']  # This does raise an Exception, naturally
             ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Python313\Lib\configparser.py", line 1276, in __getitem__
    raise KeyError(key)
KeyError: 'MY_STR'

Is there a good reason for this?
Best wishes,
Rob Cliffe


--
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to