diff --git a/poetry.lock b/poetry.lock index 4c60d24e..12d38d96 100644 --- a/poetry.lock +++ b/poetry.lock @@ -68,6 +68,17 @@ files = [ [package.extras] dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"] +[[package]] +name = "backports-strenum" +version = "1.3.1" +description = "Base class for creating enumerated constants that are also subclasses of str" +optional = false +python-versions = ">=3.8.6,<3.11" +files = [ + {file = "backports_strenum-1.3.1-py3-none-any.whl", hash = "sha256:cdcfe36dc897e2615dc793b7d3097f54d359918fc448754a517e6f23044ccf83"}, + {file = "backports_strenum-1.3.1.tar.gz", hash = "sha256:77c52407342898497714f0596e86188bb7084f89063226f4ba66863482f42414"}, +] + [[package]] name = "beautifulsoup4" version = "4.12.2" @@ -1011,7 +1022,6 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, @@ -1680,4 +1690,4 @@ wandb = ["pandas", "wandb"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.13" -content-hash = "b2f9811050666c2fd310398e92b9b293cfe34f096ac0bc7cd147cb2a3ad6ae22" +content-hash = "2e0db7634a4683316e82c187456e5aa38bfdccaa4dea6c83ef9f82085c841e5d" diff --git a/pyproject.toml b/pyproject.toml index 200e5e62..d8f06a7e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ readme = "README.md" python = ">=3.10,<3.13" typing-extensions = ">=4.5.0" numpy = "^1.23.2" +"backports.strenum" = { version = "^1.3.1", python = "<3.11" } # wandb dependencies pandas = { version = "^1.5.0", optional = true } diff --git a/ranzen/misc.py b/ranzen/misc.py index 25b0dd51..150e624a 100644 --- a/ranzen/misc.py +++ b/ranzen/misc.py @@ -12,6 +12,11 @@ from ranzen.types import Addable, SizedDataset, Subset +if sys.version_info >= (3, 11): + from enum import StrEnum +else: + from backports.strenum import StrEnum + __all__ = [ "AddDict", "Split", @@ -118,65 +123,6 @@ def str_to_enum(str_: str | E, *, enum: type[E]) -> E: ) -if sys.version_info >= (3, 11): - # will be available in python 3.11 - from enum import StrEnum -else: - # - # the following is copied straight from https://github.com/python/cpython/blob/3.11/Lib/enum.py - # - # DO NOT CHANGE THIS CODE! - # - class ReprEnum(Enum): - """ - Only changes the repr(), leaving str() and format() to the mixed-in type. - """ - - _S = TypeVar("_S", bound="StrEnum") - - class StrEnum(str, ReprEnum): - """ - Enum where members are also (and must be) strings - """ - - _value_: str - - def __new__(cls: type[_S], *values: str) -> _S: - "values must already be of type `str`" - if len(values) > 3: - raise TypeError("too many arguments for str(): %r" % (values,)) - if len(values) == 1: - # it must be a string - if not isinstance(values[0], str): # pyright: ignore - raise TypeError("%r is not a string" % (values[0],)) - if len(values) >= 2: - # check that encoding argument is a string - if not isinstance(v1 := values[1], str): # pyright: ignore - raise TypeError("encoding must be a string, not %r" % (v1,)) - if len(values) == 3: - # check that errors argument is a string - if not isinstance(v2 := values[2], str): # pyright: ignore - raise TypeError("errors must be a string, not %r" % (v2)) - value = str(*values) - member = str.__new__(cls, value) - member._value_ = value - return member - - def __str__(self) -> str: - return str.__str__(self) - - def _generate_next_value_( # type: ignore - name: str, - start: int, - count: int, - last_values: list[Any], - ) -> str: - """ - Return the lower-cased version of the member name. - """ - return name.lower() - - _KT = TypeVar("_KT") _VT = TypeVar("_VT", bound=Addable) _VT2 = TypeVar("_VT2", bound=Addable)