|
36 | 36 |
|
37 | 37 | if importlib.metadata.version("click") < "8.2": |
38 | 38 | 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 | | - |
71 | 39 | else: |
72 | 40 | from click.parser import ( # type: ignore[attr-defined, no-redef, unused-ignore] |
73 | 41 | _split_opt as split_opt, |
74 | 42 | ) |
75 | 43 |
|
76 | | - class EnumChoice(Choice): # type: ignore[no-redef, type-arg, unused-ignore] |
77 | | - """An enum-based choice type for click >= 8.2. |
78 | 44 |
|
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. |
80 | 47 |
|
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. |
82 | 50 |
|
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) |
99 | 73 |
|
100 | 74 |
|
101 | 75 | class _OptionHighlighter(RegexHighlighter): |
|
0 commit comments