Skip to content

Commit

Permalink
Vendor a copy of strenum until packaging is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Sep 12, 2023
1 parent 2499947 commit 26d8444
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
52 changes: 52 additions & 0 deletions archinfo/_strenum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
This file is copied from here: https://github.com/clbarnes/backports.strenum/blob/main/backports/strenum/strenum.py
It is licensed under the Python Software Foundation License Version 2.
This file should be replaced with backports.strenum once its packaging issues
are resolved. See here: https://github.com/clbarnes/backports.strenum/issues/9
Alternatively, if archifo is sooner updated to Python 3.11, this file can be
removed and replaced with the standard library version.
"""
import sys
from enum import Enum
from typing import Any, List, Type, TypeVar

if sys.version_info <= (3, 11):
_S = TypeVar("_S", bound="StrEnum")

class StrEnum(str, Enum):
"""
Enum where members are also (and must be) strings
"""

def __new__(cls: Type[_S], *values: str) -> _S:
if len(values) > 3:
raise TypeError(f"too many arguments for str(): {values!r}")
if len(values) == 1:
# it must be a string
if not isinstance(values[0], str):
raise TypeError(f"{values[0]!r} is not a string")
if len(values) >= 2:
# check that encoding argument is a string
if not isinstance(values[1], str):
raise TypeError(f"encoding must be a string, not {values[1]!r}")
if len(values) == 3:
# check that errors argument is a string
if not isinstance(values[2], str):
raise TypeError("errors must be a string, not %r" % (values[2]))
value = str(*values)
member = str.__new__(cls, value)
member._value_ = value
return member

__str__ = str.__str__

@staticmethod
def _generate_next_value_(name: str, start: int, count: int, last_values: List[Any]) -> str:
"""
Return the lower-cased version of the member name.
"""
return name.lower()

else:
from enum import StrEnum
6 changes: 1 addition & 5 deletions archinfo/types.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import sys
from typing import NewType

if sys.version_info >= (3, 11):
from enum import StrEnum
else:
from backports.strenum import StrEnum
from ._strenum import StrEnum


class RegisterOffset(int):
Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ classifiers =

[options]
packages = find:
install_requires =
backports.strenum;python_version<'3.11'
python_requires = >=3.8

[options.extras_require]
Expand Down

0 comments on commit 26d8444

Please sign in to comment.