Skip to content

Commit 437d9dc

Browse files
committed
fix try_enum and typevars
1 parent b39d524 commit 437d9dc

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

discord/enums.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25-
2625
from __future__ import annotations
2726

2827
from enum import Enum as EnumBase
2928
import types
3029
from typing import Any, Self, TypeVar, Union
3130

31+
E = TypeVar("E", bound="Enum")
32+
3233
__all__ = (
3334
"Enum",
3435
"ChannelType",
@@ -987,22 +988,9 @@ def __int__(self):
987988
return self.value
988989

989990

990-
T = TypeVar("T")
991-
992-
993-
def create_unknown_value(cls: type[T], val: Any) -> T:
994-
value_cls = cls._enum_value_cls_ # type: ignore
995-
name = f"unknown_{val}"
996-
return value_cls(name=name, value=val)
997-
998-
999-
def try_enum(cls: type[T], val: Any) -> T:
991+
def try_enum(cls: type[E], val: Any) -> E:
1000992
"""A function that tries to turn the value into enum ``cls``.
1001993
1002994
If it fails it returns a proxy invalid value instead.
1003995
"""
1004-
1005-
try:
1006-
return cls._enum_value_map_[val] # type: ignore
1007-
except (KeyError, TypeError, AttributeError):
1008-
return create_unknown_value(cls, val)
996+
return cls(val)

0 commit comments

Comments
 (0)