-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbase.py
More file actions
39 lines (29 loc) · 1.47 KB
/
base.py
File metadata and controls
39 lines (29 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Shared enum helpers for consistent parsing and logging."""
from __future__ import annotations
import logging
from typing import Self, cast
class UnknownEnumMixin:
"""Mixin for enums that need an `UNKNOWN` fallback.
Define `UNKNOWN` on the enum and optionally override
`__missing_message__` to customize the log message.
"""
__missing_message__ = "Unsupported value %s has been returned for %s"
def __init_subclass__(cls, **kwargs: object) -> None:
"""Validate that concrete enum subclasses define an `UNKNOWN` member."""
super().__init_subclass__(**kwargs)
# _member_map_ is only present on concrete Enum subclasses.
member_map: dict[str, object] | None = getattr(cls, "_member_map_", None)
if member_map is not None and "UNKNOWN" not in member_map:
raise TypeError(
f"{cls.__name__} uses UnknownEnumMixin but does not define "
f"an UNKNOWN member"
)
@classmethod
def _missing_(cls, value: object) -> Self: # type: ignore[override]
"""Return `UNKNOWN` and log unrecognized values.
Intentionally overrides the Enum base `_missing_` to provide an UNKNOWN fallback.
"""
message = cls.__missing_message__
logging.getLogger(cls.__module__).warning(message, value, cls)
# Type checker cannot infer UNKNOWN exists on Self, but all subclasses define it
return cast("Self", cls.UNKNOWN) # type: ignore[attr-defined]