Skip to content

Commit

Permalink
Fix configtypes.Perc.to_str()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
The-Compiler committed Dec 5, 2018
1 parent f53fd56 commit a9c1fc6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 4 additions & 1 deletion qutebrowser/config/configtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,10 @@ def to_py(
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):
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/config/test_configtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,13 @@ def test_to_py_invalid(self, klass, kwargs, val):
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:
Expand Down

0 comments on commit a9c1fc6

Please sign in to comment.