Skip to content

Commit 68c188f

Browse files
committed
Move to _missing_
1 parent d6ebd93 commit 68c188f

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

tests/test_enums.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def test_at_random(choice: mock.MagicMock) -> None:
5757

5858
def test_base_enum_missing() -> None:
5959
"""A test for the BaseEnum.__missing__ method."""
60-
assert wom.Metric["new_fake_metric"] == wom.Metric.Unknown
61-
assert wom.Metric["another_fake_metric"] == wom.Metric.Unknown
60+
assert wom.Metric("new_fake_metric") == wom.Metric.Unknown
61+
assert wom.Metric("another_fake_metric") == wom.Metric.Unknown
6262
assert wom.Metric.Unknown.value == "unknown"
6363
assert wom.Metric.Unknown != wom.Metric.Vardorvis
6464
assert wom.Metric.Attack.value == "attack"

wom/enums.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import sys
2828
import typing as t
2929
from enum import Enum
30-
from enum import EnumMeta
3130

3231
T = t.TypeVar("T", bound="BaseEnum")
3332

@@ -42,20 +41,7 @@
4241
)
4342

4443

45-
class MetaEnum(EnumMeta):
46-
def __getitem__(cls: MetaEnum, name: str) -> t.Any:
47-
try:
48-
return super().__getitem__(name)
49-
except KeyError:
50-
print(
51-
f"{name!r} is not a valid {cls.__name__} variant. "
52-
"Please report this issue on github at https://github.com/Jonxslays/wom.py/issues/new",
53-
file=sys.stderr,
54-
)
55-
return cls.__getitem__("Unknown")
56-
57-
58-
class BaseEnum(Enum, metaclass=MetaEnum):
44+
class BaseEnum(Enum):
5945
"""The base enum all library enums inherit from."""
6046

6147
def __str__(self) -> str:
@@ -82,6 +68,15 @@ def at_random(cls: t.Type[T]) -> T:
8268
"""
8369
return random.choice(tuple(cls))
8470

71+
@classmethod
72+
def _missing_(cls: t.Type[T], value: object) -> t.Optional[T]:
73+
print(
74+
f"{value!r} is not a valid {cls.__name__} variant. "
75+
"Please report this issue on github at https://github.com/Jonxslays/wom.py/issues/new",
76+
file=sys.stderr,
77+
)
78+
return cls.__getitem__("Unknown")
79+
8580

8681
class Period(BaseEnum):
8782
"""A period of time used by the API."""

0 commit comments

Comments
 (0)