10000 Fix better enum formatting by samsja · Pull Request #4520 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix better enum formatting #4520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion jina/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
parallel_type: any
"""

from enum import Enum, EnumMeta, IntEnum
from enum import Enum, EnumMeta, Flag, IntEnum


class EnumType(EnumMeta):
Expand Down Expand Up @@ -56,6 +56,24 @@ class BetterEnum(IntEnum, metaclass=EnumType):
def __str__(self):
return self.name

def __format__(self, format_spec): # noqa
"""
override format method for python 3.7
:parameter format_spec: format_spec
:return: format using actual value type unless __str__ has been overridden.
"""
# credit python 3.9 : https://github.com/python/cpython/blob/612019e60e3a5340542122dabbc7ce5a27a8c635/Lib/enum.py#L755
# fix to enum BetterEnum not correctly formated
str_overridden = type(self).__str__ not in (Enum.__str__, Flag.__str__)
if self._member_type_ is object or str_overridden:
cls = str
val = str(self)
# mix-in branch
else:
cls = self._member_type_
val = self._value_
return cls.__format__(val, format_spec)

@classmethod
def from_string(cls, s: str):
"""
Expand Down
0