From 8709979679a0cc9fca0d24819f94105e32eea256 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Fri, 6 Oct 2023 11:33:54 -0700 Subject: [PATCH] Re-add backports.strenum --- archinfo/_strenum.py | 52 -------------------------------------------- archinfo/types.py | 6 ++++- setup.cfg | 2 ++ 3 files changed, 7 insertions(+), 53 deletions(-) delete mode 100644 archinfo/_strenum.py diff --git a/archinfo/_strenum.py b/archinfo/_strenum.py deleted file mode 100644 index 933d547a..00000000 --- a/archinfo/_strenum.py +++ /dev/null @@ -1,52 +0,0 @@ -""" -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 archinfo 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 diff --git a/archinfo/types.py b/archinfo/types.py index fea5774c..53318b87 100644 --- a/archinfo/types.py +++ b/archinfo/types.py @@ -1,6 +1,10 @@ +import sys from typing import NewType -from ._strenum import StrEnum +if sys.version_info < (3, 11): + from backports.strenum import StrEnum +else: + from enum import StrEnum class RegisterOffset(int): diff --git a/setup.cfg b/setup.cfg index 45c76346..5ab3f9c5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -18,6 +18,8 @@ classifiers = [options] packages = find: +install_requires = + backports.strenum>=1.2.8;python_version<'3.11' python_requires = >=3.8 [options.extras_require]