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.
This commit is contained in:
Florian Bruhin 2018-12-05 16:57:09 +01:00
parent f53fd56c3d
commit a9c1fc665f
2 changed files with 11 additions and 3 deletions

View File

@ -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):

View File

@ -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: