From a9c1fc665fa54c28bfd6dfc51df0ce7849d382a6 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 5 Dec 2018 16:57:09 +0100 Subject: [PATCH] Fix configtypes.Perc.to_str() If we used an int/float in config.py for a Perc value (e.g. zoom.default), to_str() returned int/float instead of str, causing qWarnings and bugs. --- qutebrowser/config/configtypes.py | 5 ++++- tests/unit/config/test_configtypes.py | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 61dba365e..3f134f770 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -847,7 +847,10 @@ class Perc(_Numeric): def to_str(self, value: typing.Union[None, float, int, str]) -> str: if value is None: return '' - return value + elif isinstance(value, str): + return value + else: + return '{}%'.format(value) class PercOrInt(_Numeric): diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 76bdb9199..ef41e5d75 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1113,8 +1113,13 @@ class TestPerc: with pytest.raises(configexc.ValidationError): klass(**kwargs).to_py(val) - def test_to_str(self, klass): - assert klass().to_str('42%') == '42%' + @pytest.mark.parametrize('value, expected', [ + ('42%', '42%'), + (42, '42%'), + (42.5, '42.5%'), + ]) + def test_to_str(self, klass, value, expected): + assert klass().to_str(value) == expected class TestPercOrInt: