Skip to content

Commit d6ebd93

Browse files
committed
Add code to catch new/unknown metric
1 parent 0e797f9 commit d6ebd93

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

tests/test_enums.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ def test_hash() -> None:
5353
def test_at_random(choice: mock.MagicMock) -> None:
5454
_ = wom.Metric.at_random()
5555
choice.assert_called_once_with(tuple(wom.Metric))
56+
57+
58+
def test_base_enum_missing() -> None:
59+
"""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
62+
assert wom.Metric.Unknown.value == "unknown"
63+
assert wom.Metric.Unknown != wom.Metric.Vardorvis
64+
assert wom.Metric.Attack.value == "attack"

wom/enums.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
from __future__ import annotations
2525

2626
import random
27+
import sys
2728
import typing as t
2829
from enum import Enum
30+
from enum import EnumMeta
2931

3032
T = t.TypeVar("T", bound="BaseEnum")
3133

@@ -40,7 +42,20 @@
4042
)
4143

4244

43-
class BaseEnum(Enum):
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):
4459
"""The base enum all library enums inherit from."""
4560

4661
def __str__(self) -> str:
@@ -77,6 +92,9 @@ class Period(BaseEnum):
7792
Month = "month"
7893
Year = "year"
7994

95+
# Unknown metric
96+
Unknown = "unknown"
97+
8098

8199
class Metric(BaseEnum):
82100
"""Represents all metrics including skills, bosses, activities, and
@@ -198,6 +216,9 @@ class Metric(BaseEnum):
198216
Ehp = "ehp"
199217
Ehb = "ehb"
200218

219+
# Unknown Metric
220+
Unknown = "unknown"
221+
201222

202223
ComputedMetrics: t.FrozenSet[Metric] = frozenset({Metric.Ehp, Metric.Ehb})
203224
"""Set containing all the types of computed metrics."""
@@ -228,6 +249,7 @@ class Metric(BaseEnum):
228249
Metric.Runecrafting,
229250
Metric.Hunter,
230251
Metric.Construction,
252+
Metric.Unknown,
231253
}
232254
)
233255
"""Set containing skills."""
@@ -250,6 +272,7 @@ class Metric(BaseEnum):
250272
Metric.PvpArena,
251273
Metric.SoulWarsZeal,
252274
Metric.GuardiansOfTheRift,
275+
Metric.Unknown,
253276
}
254277
)
255278
"""Set containing activities."""
@@ -321,6 +344,7 @@ class Metric(BaseEnum):
321344
Metric.Wintertodt,
322345
Metric.Zalcano,
323346
Metric.Zulrah,
347+
Metric.Unknown,
324348
}
325349
)
326350
"""Set containing bosses."""

0 commit comments

Comments
 (0)