Skip to content

Commit

Permalink
Weaken enum typing
Browse files Browse the repository at this point in the history
Warns on enum extension, raises IncompleteEnum if CoercionLevel.STRICT
  • Loading branch information
ManicJamie committed Jun 3, 2024
1 parent dfa23c4 commit fa4f201
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ classifiers = [
"License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)"
]
dependencies = [
"aiohttp"
"aiohttp",
"aenum"
]
dynamic = ["version"]

Expand Down
27 changes: 26 additions & 1 deletion src/speedruncompy/datatypes/enums.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
from enum import IntEnum, StrEnum
import enum
from aenum import extend_enum
from ._impl import _log
from .. import config, exceptions

class StrEnum(enum.StrEnum):
"""WARN: this is a forgiving enum type, which dynamically adds missing fields at runtime."""
@classmethod
def _missing_(cls, value):
if config.COERCION >= config.CoercionLevel.STRICT:
raise exceptions.IncompleteEnum(cls, value)
_log.error(f"Enum {cls.__name__} missing value {value}. Adding missing value...")
_log.warning("""If you did not construct this enum, Speedrun.com has updated this field. If speedruncompy is up-to-date, please contact the library author.""")
extend_enum(cls, str(value), (value, ))
return list(cls)[-1] # type: ignore

class IntEnum(enum.IntEnum):
"""WARN: this is a forgiving enum type, which dynamically adds missing fields at runtime."""
@classmethod
def _missing_(cls, value):
if config.COERCION >= config.CoercionLevel.STRICT:
raise exceptions.IncompleteEnum(cls, value)
_log.error(f"Enum {cls.__name__} missing value {value}. Adding missing value...")
_log.warning("""If you did not construct this enum, Speedrun.com has updated this field. If speedruncompy is up-to-date, please contact the library author.""")
extend_enum(cls, str(value), (value, ))
return list(cls)[-1] # type: ignore

class ItemType(IntEnum):
"""The type of the item this object is referencing (eg. a comment on either a run or a thread)"""
Expand Down
3 changes: 3 additions & 0 deletions src/speedruncompy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
class IncompleteDatatype(Exception):
"""A speedruncompy datatype is missing non-optional fields"""

class IncompleteEnum(Exception):
"""A speedruncompy enum is missing a value"""

class SrcpyException(Exception):
"""speedruncompy found an issue with your request during initialisation (eg. bad arguments)"""

Expand Down
1 change: 0 additions & 1 deletion test/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ async def test_API_Context_Manager_Errors(self):
assert client._session is session # still same session

assert client._session is None # session closed


@pytest.mark.skip(reason="Test stub")
def test_Authflow(self):
Expand Down

0 comments on commit fa4f201

Please sign in to comment.