An enum of strings where the case should be ignored.
| 3275 | |
| 3276 | |
| 3277 | class CaselessStrEnum(Enum[G]): |
| 3278 | """An enum of strings where the case should be ignored.""" |
| 3279 | |
| 3280 | def __init__( |
| 3281 | self: CaselessStrEnum[t.Any], |
| 3282 | values: t.Any, |
| 3283 | default_value: t.Any = Undefined, |
| 3284 | **kwargs: t.Any, |
| 3285 | ) -> None: |
| 3286 | super().__init__(values, default_value=default_value, **kwargs) |
| 3287 | |
| 3288 | def validate(self, obj: t.Any, value: t.Any) -> G: |
| 3289 | if not isinstance(value, str): |
| 3290 | self.error(obj, value) |
| 3291 | |
| 3292 | for v in self.values or []: |
| 3293 | assert isinstance(v, str) |
| 3294 | if v.lower() == value.lower(): |
| 3295 | return v # type:ignore[return-value] |
| 3296 | self.error(obj, value) |
| 3297 | |
| 3298 | def _info(self, as_rst: bool = False) -> str: |
| 3299 | """Returns a description of the trait.""" |
| 3300 | none = " or %s" % ("`None`" if as_rst else "None") if self.allow_none else "" |
| 3301 | return f"any of {self._choices_str(as_rst)} (case-insensitive){none}" |
| 3302 | |
| 3303 | def info(self) -> str: |
| 3304 | return self._info(as_rst=False) |
| 3305 | |
| 3306 | def info_rst(self) -> str: |
| 3307 | return self._info(as_rst=True) |
| 3308 | |
| 3309 | |
| 3310 | class FuzzyEnum(Enum[G]): |