33from __future__ import annotations
44
55import logging
6- from enum import Enum
7- from typing import TypeVar , cast
8-
9- _EnumT = TypeVar ("_EnumT" , bound = Enum )
6+ from typing import Any , Self , cast
107
118
129class UnknownEnumMixin :
@@ -19,13 +16,14 @@ class UnknownEnumMixin:
1916 __missing_message__ = "Unsupported value %s has been returned for %s"
2017
2118 @classmethod
22- def _missing_ (cls , value ): # type: ignore[override]
19+ def _missing_ (cls , value : object ) -> Self :
2320 """Return `UNKNOWN` and log unrecognized values."""
2421 message = getattr (cls , "__missing_message__" , cls .__missing_message__ )
2522 logging .getLogger (cls .__module__ ).warning (message , value , cls )
26- return cls .UNKNOWN
23+ # Type checker cannot infer UNKNOWN exists on Self, but all subclasses define it
24+ return cast (Self , cls .UNKNOWN ) # type: ignore[attr-defined]
2725
2826 @classmethod
29- def from_value (cls : type [_EnumT ], value : object ) -> _EnumT :
27+ def from_value (cls : type [Self ], value : object ) -> Self :
3028 """Return enum for `value`, falling back to `UNKNOWN`."""
31- return cast (_EnumT , cls (value ))
29+ return cast (Self , cast ( Any , cls ) (value ))
0 commit comments