Skip to content

Commit 808f0eb

Browse files
committed
Simplify.
1 parent b4f7baf commit 808f0eb

1 file changed

Lines changed: 26 additions & 52 deletions

File tree

src/_pytask/click.py

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,66 +36,40 @@
3636

3737
if importlib.metadata.version("click") < "8.2":
3838
from click.parser import split_opt
39-
40-
class EnumChoice(Choice): # type: ignore[type-arg, unused-ignore]
41-
"""An enum-based choice type.
42-
43-
The implementation is copied from https://github.com/pallets/click/pull/2210 and
44-
related discussion can be found in https://github.com/pallets/click/issues/605.
45-
46-
In contrast to using :class:`click.Choice`, using this type ensures that the
47-
error message does not show the enum members.
48-
49-
In contrast to the proposed implementation in the PR, this implementation does
50-
not use the members than rather the values of the enum.
51-
52-
"""
53-
54-
def __init__(self, enum_type: type[Enum], case_sensitive: bool = True) -> None:
55-
super().__init__(
56-
choices=[element.value for element in enum_type],
57-
case_sensitive=case_sensitive,
58-
)
59-
self.enum_type = enum_type
60-
61-
def convert(
62-
self, value: Any, param: Parameter | None, ctx: Context | None
63-
) -> Any:
64-
if isinstance(value, Enum):
65-
value = value.value
66-
value = super().convert(value=value, param=param, ctx=ctx)
67-
if value is None:
68-
return None
69-
return self.enum_type(value)
70-
7139
else:
7240
from click.parser import ( # type: ignore[attr-defined, no-redef, unused-ignore]
7341
_split_opt as split_opt,
7442
)
7543

76-
class EnumChoice(Choice): # type: ignore[no-redef, type-arg, unused-ignore]
77-
"""An enum-based choice type for click >= 8.2.
7844

79-
Extracts values from enum members to use as valid choices.
45+
class EnumChoice(Choice): # type: ignore[type-arg, unused-ignore]
46+
"""An enum-based choice type.
8047
81-
"""
48+
The implementation is copied from https://github.com/pallets/click/pull/2210 and
49+
related discussion can be found in https://github.com/pallets/click/issues/605.
8250
83-
def __init__(self, enum_type: type[Enum], case_sensitive: bool = False) -> None:
84-
super().__init__(
85-
choices=[element.value for element in enum_type],
86-
case_sensitive=case_sensitive,
87-
)
88-
self.enum_type = enum_type
89-
90-
def convert(
91-
self, value: Any, param: Parameter | None, ctx: Context | None
92-
) -> Any:
93-
if isinstance(value, Enum):
94-
value = value.value
95-
value = super().convert(value=value, param=param, ctx=ctx)
96-
if value is None:
97-
return None
98-
return self.enum_type(value)
51+
In contrast to using :class:`click.Choice`, using this type ensures that the
52+
error message does not show the enum members.
53+
54+
In contrast to the proposed implementation in the PR, this implementation does
55+
not use the members than rather the values of the enum.
56+
57+
"""
58+
59+
def __init__(self, enum_type: type[Enum], case_sensitive: bool = True) -> None:
60+
super().__init__(
61+
choices=[element.value for element in enum_type],
62+
case_sensitive=case_sensitive,
63+
)
64+
self.enum_type = enum_type
65+
66+
def convert(self, value: Any, param: Parameter | None, ctx: Context | None) -> Any:
67+
if isinstance(value, Enum):
68+
value = value.value
69+
value = super().convert(value=value, param=param, ctx=ctx)
70+
if value is None:
71+
return None
72+
return self.enum_type(value)
9973

10074

10175
class _OptionHighlighter(RegexHighlighter):

0 commit comments

Comments
 (0)