diff options
author | Florian Bruhin <me@the-compiler.org> | 2021-04-08 09:51:16 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2021-04-08 09:51:16 +0200 |
commit | e2c5fe6262564d9d85806bfa9d4486a411cf5045 (patch) | |
tree | 21602b102bd37a5d46edff1c98cd531d09c7390e /qutebrowser/utils/utils.py | |
parent | e0657a550a80876c6236bc065593b01ef098f18c (diff) | |
download | qutebrowser-e2c5fe6262564d9d85806bfa9d4486a411cf5045.tar.gz qutebrowser-e2c5fe6262564d9d85806bfa9d4486a411cf5045.zip |
Fix enum stringification for Python 3.10 a7+
https://bugs.python.org/issue40066
https://mail.python.org/archives/list/python-dev@python.org/message/CHQW6THTDYNPPFWQ2KDDTUYSAJDCZFNP/
https://github.com/python/cpython/commit/b775106d940e3d77c8af7967545bb9a5b7b162df
Diffstat (limited to 'qutebrowser/utils/utils.py')
-rw-r--r-- | qutebrowser/utils/utils.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 762d2d370..56ebe45c4 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -375,6 +375,18 @@ def is_enum(obj: Any) -> bool: return False +def pyenum_str(value: enum.Enum) -> str: + """Get a string representation of a Python enum value. + + This will have the form of "EnumType.membername", which is the default string + representation for Python up to 3.10. Unfortunately, that changes with Python 3.10: + https://bugs.python.org/issue40066 + """ + if sys.version_info[:2] >= (3, 10): + return repr(value) + return str(value) + + def get_repr(obj: Any, constructor: bool = False, **attrs: Any) -> str: """Get a suitable __repr__ string for an object. @@ -387,8 +399,14 @@ def get_repr(obj: Any, constructor: bool = False, **attrs: Any) -> str: cls = qualname(obj.__class__) parts = [] items = sorted(attrs.items()) + for name, val in items: - parts.append('{}={!r}'.format(name, val)) + if isinstance(val, enum.Enum): + s = pyenum_str(val) + else: + s = repr(val) + parts.append(f'{name}={s}') + if constructor: return '{}({})'.format(cls, ', '.join(parts)) else: |