Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Use StrEnum #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/edi_energy_scraper/epoch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@
this module contains the epoch enum
"""

from enum import Enum
import sys

if sys.version_info.major == 3 and sys.version_info.minor >= 11:
from enum import StrEnum

class Epoch(str, Enum): # pylint: disable=too-few-public-methods
# We have to use the builtin / std lib enum.StrEnum in Python >= 3.11, because the behaviour of (str,Enum) changed:
# class Foo(str, Enum):
# MEMBER = "MEMBER"
# f"{a_str_enum_member}" results in "MEMBER" for Python < v3.11 but "Foo.MEMBER" in Python >= v3.11
else:
from enum import Enum

class StrEnum(str, Enum): # type:ignore[no-redef]
"""
An enum class of which each member has a string representation.
This is a workaround for Python <v3.11 because enum.StrEnum was introduced in Python 3.11.
"""

# We'll live with this class for Python <v3.11. The unit test for python 3.9-3.11 ensure that this works.


class Epoch(StrEnum): # pylint: disable=too-few-public-methods
"""
An Epoch describes the time range in which documents are valid.
It's relative to the current time, meaning that CURRENT documents are valid now, PAST documents are not valid
Expand All @@ -16,9 +34,3 @@ class Epoch(str, Enum): # pylint: disable=too-few-public-methods
PAST = "past" #: documents that are not valid anymore and have been archived
CURRENT = "current" #: documents that are currently valid valid_from <= now < valid_to
FUTURE = "future" #: documents that will become valid in the future (most likely with the next format version)

def __str__(self):
"""
this is required because the behaviour of "StrEnum"s changed in python 3.11
"""
return self.value
Loading