Skip to content

Commit 7f5e974

Browse files
committed
change to TypeError
1 parent 5062d6c commit 7f5e974

4 files changed

Lines changed: 9 additions & 7 deletions

File tree

Doc/library/configparser.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,9 +1206,9 @@ ConfigParser Objects
12061206
``'1'``, ``'yes'``, ``'true'``, and ``'on'``, which cause this method to
12071207
return ``True``, and ``'0'``, ``'no'``, ``'false'``, and ``'off'``, which
12081208
cause it to return ``False``. These string values are checked in a
1209-
case-insensitive manner. Any other value will cause it to raise
1210-
:exc:`ValueError`. See :meth:`get` for explanation of *raw*, *vars* and
1211-
*fallback*.
1209+
case-insensitive manner. Any other string value will cause it to raise
1210+
:exc:`ValueError`. If the option has no value, :exc:`TypeError` is
1211+
raised. See :meth:`get` for explanation of *raw*, *vars* and *fallback*.
12121212

12131213

12141214
.. method:: items(raw=False, vars=None)

Lib/configparser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,9 @@ 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 is None or value.lower() not in self.BOOLEAN_STATES:
1225+
if value is None:
1226+
raise TypeError('Cannot convert a value-less option to a boolean')
1227+
if value.lower() not in self.BOOLEAN_STATES:
12261228
raise ValueError('Not a boolean: %s' % value)
12271229
return self.BOOLEAN_STATES[value.lower()]
12281230

Lib/test/test_configparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,9 @@ class ConfigParserTestCaseNoValue(ConfigParserTestCase):
13411341
def test_getboolean_with_no_value(self):
13421342
cf = self.fromstring("[section]\noption\n")
13431343

1344-
with self.assertRaisesRegex(ValueError, "Not a boolean: None"):
1344+
with self.assertRaises(TypeError):
13451345
cf.getboolean("section", "option")
1346-
with self.assertRaisesRegex(ValueError, "Not a boolean: None"):
1346+
with self.assertRaises(TypeError):
13471347
cf["section"].getboolean("option")
13481348

13491349

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
:class:`configparser.ConfigParser` now raises :exc:`ValueError` instead of
1+
:class:`configparser.ConfigParser` now raises :exc:`TypeError` instead of
22
:exc:`AttributeError` when :meth:`~configparser.ConfigParser.getboolean` is
33
called on an option without a value while ``allow_no_value=True``.

0 commit comments

Comments
 (0)