Skip to content

Commit 9d69e26

Browse files
committed
gh-155436: Fix configparser.getboolean() for value-less options
1 parent 998b890 commit 9d69e26

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/configparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ def _unify_values(self, section, vars):
12221222
def _convert_to_boolean(self, value):
12231223
"""Return a boolean value translating from other types if necessary.
12241224
"""
1225-
if value.lower() not in self.BOOLEAN_STATES:
1225+
if value is None or value.lower() not in self.BOOLEAN_STATES:
12261226
raise ValueError('Not a boolean: %s' % value)
12271227
return self.BOOLEAN_STATES[value.lower()]
12281228

Lib/test/test_configparser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,14 @@ def test_other_errors(self):
13381338
class ConfigParserTestCaseNoValue(ConfigParserTestCase):
13391339
allow_no_value = True
13401340

1341+
def test_getboolean_with_no_value(self):
1342+
cf = self.fromstring("[section]\noption\n")
1343+
1344+
with self.assertRaisesRegex(ValueError, "Not a boolean: None"):
1345+
cf.getboolean("section", "option")
1346+
with self.assertRaisesRegex(ValueError, "Not a boolean: None"):
1347+
cf["section"].getboolean("option")
1348+
13411349

13421350
class NoValueAndExtendedInterpolation(CfgParserTestCaseClass):
13431351
interpolation = configparser.ExtendedInterpolation()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`configparser.ConfigParser` now raises :exc:`ValueError` instead of
2+
:exc:`AttributeError` when :meth:`~configparser.ConfigParser.getboolean` is
3+
called on an option without a value while ``allow_no_value=True``.

0 commit comments

Comments
 (0)