diff --git a/progressbar/utils.py b/progressbar/utils.py index fb0c72b..dd194c1 100644 --- a/progressbar/utils.py +++ b/progressbar/utils.py @@ -429,13 +429,13 @@ class AttributeDict(dict): AttributeError: No such attribute: spam """ - def __getattr__(self, name: str) -> int: + def __getattr__(self, name: str) -> types.Any: if name in self: return self[name] else: raise AttributeError(f'No such attribute: {name}') - def __setattr__(self, name: str, value: int) -> None: + def __setattr__(self, name: str, value: types.Any) -> None: self[name] = value def __delattr__(self, name: str) -> None: diff --git a/tests/test_utils.py b/tests/test_utils.py index e347acd..54db09b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -112,3 +112,57 @@ def raise_error(): fd.isatty = raise_error assert not progressbar.env.is_ansi_terminal(fd) + + +@pytest.mark.parametrize( + 'value,expected', + [ + ('', ''), + (b'', b''), + ('\x1b[31m', ''), + (b'\x1b[31m', b''), + ('\x1b[1m\x1b[31mtext\x1b[0m', 'text'), + (b'\x1b[1m\x1b[31mtext\x1b[0m', b'text'), + ('\x1b[38;5;208mhello world\x1b[0m', 'hello world'), + ], +) +def test_no_color(value, expected) -> None: + assert progressbar.utils.no_color(value) == expected + + +def test_no_color_type_error() -> None: + with pytest.raises(TypeError): + progressbar.utils.no_color(123) + + +@pytest.mark.parametrize( + 'value,expected', + [ + ('', 0), + (b'', 0), + ('\x1b[31m', 0), + ('\x1b[1m\x1b[31mtext\x1b[0m', 4), + ('\x1b[38;5;208mhello world\x1b[0m', 11), + ], +) +def test_len_color(value, expected) -> None: + assert progressbar.utils.len_color(value) == expected + + +def test_attribute_dict_empty() -> None: + attrs = progressbar.utils.AttributeDict() + assert len(attrs) == 0 + with pytest.raises(AttributeError): + attrs.missing + + +def test_attribute_dict_set_get_del() -> None: + attrs = progressbar.utils.AttributeDict() + attrs.spam = 123 + assert attrs['spam'] == 123 + assert attrs.spam == 123 + del attrs.spam + with pytest.raises(AttributeError): + attrs.spam + with pytest.raises(AttributeError): + del attrs.spam