Skip to content

Commit

Permalink
Add code to catch new/unknown metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharpienero committed Feb 22, 2025
1 parent 0e797f9 commit d6ebd93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ def test_hash() -> None:
def test_at_random(choice: mock.MagicMock) -> None:
_ = wom.Metric.at_random()
choice.assert_called_once_with(tuple(wom.Metric))


def test_base_enum_missing() -> None:
"""A test for the BaseEnum.__missing__ method."""
assert wom.Metric["new_fake_metric"] == wom.Metric.Unknown
assert wom.Metric["another_fake_metric"] == wom.Metric.Unknown
assert wom.Metric.Unknown.value == "unknown"
assert wom.Metric.Unknown != wom.Metric.Vardorvis
assert wom.Metric.Attack.value == "attack"
26 changes: 25 additions & 1 deletion wom/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
from __future__ import annotations

import random
import sys
import typing as t
from enum import Enum
from enum import EnumMeta

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

Expand All @@ -40,7 +42,20 @@
)


class BaseEnum(Enum):
class MetaEnum(EnumMeta):
def __getitem__(cls: MetaEnum, name: str) -> t.Any:
try:
return super().__getitem__(name)
except KeyError:
print(
f"{name!r} is not a valid {cls.__name__} variant. "
"Please report this issue on github at https://github.com/Jonxslays/wom.py/issues/new",
file=sys.stderr,
)
return cls.__getitem__("Unknown")


class BaseEnum(Enum, metaclass=MetaEnum):
"""The base enum all library enums inherit from."""

def __str__(self) -> str:
Expand Down Expand Up @@ -77,6 +92,9 @@ class Period(BaseEnum):
Month = "month"
Year = "year"

# Unknown metric
Unknown = "unknown"


class Metric(BaseEnum):
"""Represents all metrics including skills, bosses, activities, and
Expand Down Expand Up @@ -198,6 +216,9 @@ class Metric(BaseEnum):
Ehp = "ehp"
Ehb = "ehb"

# Unknown Metric
Unknown = "unknown"


ComputedMetrics: t.FrozenSet[Metric] = frozenset({Metric.Ehp, Metric.Ehb})
"""Set containing all the types of computed metrics."""
Expand Down Expand Up @@ -228,6 +249,7 @@ class Metric(BaseEnum):
Metric.Runecrafting,
Metric.Hunter,
Metric.Construction,
Metric.Unknown,
}
)
"""Set containing skills."""
Expand All @@ -250,6 +272,7 @@ class Metric(BaseEnum):
Metric.PvpArena,
Metric.SoulWarsZeal,
Metric.GuardiansOfTheRift,
Metric.Unknown,
}
)
"""Set containing activities."""
Expand Down Expand Up @@ -321,6 +344,7 @@ class Metric(BaseEnum):
Metric.Wintertodt,
Metric.Zalcano,
Metric.Zulrah,
Metric.Unknown,
}
)
"""Set containing bosses."""

0 comments on commit d6ebd93

Please sign in to comment.