diff --git a/src/simple_config_parser/simple_config_parser.py b/src/simple_config_parser/simple_config_parser.py index a1a38cd..d3d7e87 100644 --- a/src/simple_config_parser/simple_config_parser.py +++ b/src/simple_config_parser/simple_config_parser.py @@ -311,7 +311,7 @@ class SimpleConfigParser: """Return the value of the given option in the given section as a converted value""" try: return conv(self.getval(section, option, fallback)) - except ValueError as e: + except (ValueError, TypeError, AttributeError) as e: if fallback is not _UNSET: return fallback raise ValueError( diff --git a/tests/public_api/test_options_api.py b/tests/public_api/test_options_api.py index 6283cd7..c094a32 100644 --- a/tests/public_api/test_options_api.py +++ b/tests/public_api/test_options_api.py @@ -57,6 +57,7 @@ def test_getval(parser): def test_getval_fallback(parser): assert parser.getval("section_1", "option_128", "fallback") == "fallback" + assert parser.getval("section_1", "option_128", None) is None def test_getval_exceptions(parser): @@ -89,6 +90,7 @@ def test_getint_from_boolean(parser): def test_getint_fallback(parser): assert parser.getint("section_1", "option_128", 128) == 128 + assert parser.getint("section_1", "option_128", None) is None def test_getboolean(parser): @@ -115,6 +117,7 @@ def test_getboolean_from_float(parser): def test_getboolean_fallback(parser): assert parser.getboolean("section_1", "option_128", True) is True assert parser.getboolean("section_1", "option_128", False) is False + assert parser.getboolean("section_1", "option_128", None) is None def test_getfloat(parser): @@ -139,6 +142,7 @@ def test_getfloat_from_boolean(parser): def test_getfloat_fallback(parser): assert parser.getfloat("section_1", "option_128", 1.234) == 1.234 + assert parser.getfloat("section_1", "option_128", None) is None def test_set_existing_option(parser): diff --git a/tests/value_conversion/test_get_conv.py b/tests/value_conversion/test_get_conv.py index 47828c2..893ecc1 100644 --- a/tests/value_conversion/test_get_conv.py +++ b/tests/value_conversion/test_get_conv.py @@ -25,50 +25,65 @@ def parser(): return parser -def test_get_conv(parser): - # Test conversion to int +def test_get_int_conv(parser): should_be_int = parser._get_conv("section_1", "option_1_2", int) assert isinstance(should_be_int, int) - # Test conversion to float + +def test_get_float_conv(parser): should_be_float = parser._get_conv("section_1", "option_1_3", float) assert isinstance(should_be_float, float) - # Test conversion to boolean + +def test_get_bool_conv(parser): should_be_bool = parser._get_conv( "section_1", "option_1_1", parser._convert_to_boolean ) assert isinstance(should_be_bool, bool) - # Test fallback for int + +def test_get_int_conv_fallback(parser): should_be_fallback_int = parser._get_conv( "section_1", "option_128", int, fallback=128 ) assert isinstance(should_be_fallback_int, int) assert should_be_fallback_int == 128 + assert parser._get_conv("section_1", "option_128", int, None) is None - # Test fallback for float + +def test_get_float_conv_fallback(parser): should_be_fallback_float = parser._get_conv( "section_1", "option_128", float, fallback=1.234 ) assert isinstance(should_be_fallback_float, float) assert should_be_fallback_float == 1.234 - # Test fallback for boolean + assert parser._get_conv("section_1", "option_128", float, None) is None + + +def test_get_bool_conv_fallback(parser): should_be_fallback_bool = parser._get_conv( "section_1", "option_128", parser._convert_to_boolean, fallback=True ) assert isinstance(should_be_fallback_bool, bool) assert should_be_fallback_bool is True - # Test ValueError exception for invalid int conversion + assert ( + parser._get_conv("section_1", "option_128", parser._convert_to_boolean, None) + is None + ) + + +def test_get_int_conv_exception(parser): with pytest.raises(ValueError): parser._get_conv("section_1", "option_1", int) - # Test ValueError exception for invalid float conversion + +def test_get_float_conv_exception(parser): with pytest.raises(ValueError): parser._get_conv("section_1", "option_1", float) - # Test ValueError exception for invalid boolean conversion + +def test_get_bool_conv_exception(parser): with pytest.raises(ValueError): parser._get_conv("section_1", "option_1", parser._convert_to_boolean)