From 8ab4133bfc5bae1656f74c87a56d2d5f45a1c788 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 27 May 2020 12:43:40 +0200 Subject: configtypes: Fix handling of Unset in _Numeric with bounds --- qutebrowser/config/configtypes.py | 18 +++++++++++++----- tests/unit/config/test_configtypes.py | 4 ++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 6eec13293..cfcc5f13a 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -797,11 +797,16 @@ class _Numeric(BaseType): # pylint: disable=abstract-method assert isinstance(bound, (int, float)), bound return bound - def _validate_bounds(self, value: typing.Union[None, int, float], - suffix: str = '') -> None: + def _validate_bounds( + self, + value: typing.Union[None, int, float, usertypes.Unset], + suffix: str = '' + ) -> None: """Validate self.minval and self.maxval.""" if value is None: return + elif isinstance(value, usertypes.Unset): + return elif self.minval is not None and value < self.minval: raise configexc.ValidationError( value, "must be {}{} or bigger!".format(self.minval, suffix)) @@ -837,7 +842,10 @@ class Int(_Numeric): self.to_py(intval) return intval - def to_py(self, value: typing.Optional[int]) -> typing.Optional[int]: + def to_py( + self, + value: typing.Union[None, int, usertypes.Unset] + ) -> typing.Union[None, int, usertypes.Unset]: self._basic_py_validation(value, int) self._validate_bounds(value) return value @@ -861,8 +869,8 @@ class Float(_Numeric): def to_py( self, - value: typing.Union[None, int, float], - ) -> typing.Union[None, int, float]: + value: typing.Union[None, int, float, usertypes.Unset], + ) -> typing.Union[None, int, float, usertypes.Unset]: self._basic_py_validation(value, (int, float)) self._validate_bounds(value) return value diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 18019fb4f..841892ef2 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1047,6 +1047,10 @@ class TestInt: converted = typ.from_str(text) assert typ.to_str(converted) == text + def test_bounds_handling_unset(self, klass): + typ = klass(minval=1, maxval=2) + assert typ.to_py(usertypes.UNSET) is usertypes.UNSET + class TestFloat: -- cgit v1.2.3-54-g00ecf