From 22ae1e6c8494c27432140f57bf0e6594a7eaa755 Mon Sep 17 00:00:00 2001 From: Sami Jaghouar Date: Thu, 17 Mar 2022 14:51:45 +0100 Subject: [PATCH 1/3] fix: force stringify protocol enum --- jina/orchestrate/flow/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jina/orchestrate/flow/base.py b/jina/orchestrate/flow/base.py index 5c5dfc20abec9..3396dc601ddb0 100644 --- a/jina/orchestrate/flow/base.py +++ b/jina/orchestrate/flow/base.py @@ -1509,7 +1509,9 @@ def _init_table(self): return table def _get_address_table(self, address_table): - address_table.add_row('🔗', 'Protocol: ', f'{self.protocol}') + address_table.add_row( + '🔗', 'Protocol: ', f'{str(self.protocol)}' + ) # str needed for python3.7 address_table.add_row( '🏠', 'Local access: ', From b0228d4896af8b7a7bd549a45a0b74a4a91b6ea5 Mon Sep 17 00:00:00 2001 From: Sami Jaghouar Date: Thu, 17 Mar 2022 15:18:15 +0100 Subject: [PATCH 2/3] Revert "fix: force stringify protocol enum" This reverts commit 22ae1e6c8494c27432140f57bf0e6594a7eaa755. --- jina/orchestrate/flow/base.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/jina/orchestrate/flow/base.py b/jina/orchestrate/flow/base.py index 3396dc601ddb0..5c5dfc20abec9 100644 --- a/jina/orchestrate/flow/base.py +++ b/jina/orchestrate/flow/base.py @@ -1509,9 +1509,7 @@ def _init_table(self): return table def _get_address_table(self, address_table): - address_table.add_row( - '🔗', 'Protocol: ', f'{str(self.protocol)}' - ) # str needed for python3.7 + address_table.add_row('🔗', 'Protocol: ', f'{self.protocol}') address_table.add_row( '🏠', 'Local access: ', From ec9828909006b87564a2ae9bbb2241ae1e2347bb Mon Sep 17 00:00:00 2001 From: Sami Jaghouar Date: Thu, 17 Mar 2022 15:25:32 +0100 Subject: [PATCH 3/3] fix: better enum formatting --- jina/enums.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/jina/enums.py b/jina/enums.py index 8583bed590f5c..980a3a665d00c 100644 --- a/jina/enums.py +++ b/jina/enums.py @@ -16,7 +16,7 @@ parallel_type: any """ -from enum import Enum, EnumMeta, IntEnum +from enum import Enum, EnumMeta, Flag, IntEnum class EnumType(EnumMeta): @@ -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): """