Skip to content

Commit

Permalink
make renum object-like rather than str-like
Browse files Browse the repository at this point in the history
  • Loading branch information
Zephyrkul committed Apr 11, 2024
1 parent 2b5adc3 commit ce75208
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions renum/renum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from contextvars import ContextVar
from enum import Enum
from typing import TYPE_CHECKING, Any, ClassVar, Iterator, overload
from typing import TYPE_CHECKING, Any, Iterator, overload

if TYPE_CHECKING:
from typing_extensions import Self
Expand All @@ -14,8 +14,13 @@
_matches: ContextVar[dict[renum, re.Match[str]]] = ContextVar("_match")


class renum(str, Enum):
_pattern_: ClassVar[re.Pattern[str]]
class renum(Enum):
if TYPE_CHECKING:
_pattern_: re.Pattern[str]
_value_: str

@property
def value(self) -> str: ...

@staticmethod
def _generate_next_value_(
Expand All @@ -25,7 +30,7 @@ def _generate_next_value_(

def __new__(cls, *values: Any) -> Self:
value = str(*values)
member = str.__new__(cls, value)
member = object.__new__(cls)
member._value_ = value
return member

Expand All @@ -36,8 +41,10 @@ def __init_subclass__(cls, flags: int | re.RegexFlag = 0, **kwargs: Any) -> None
flags=flags,
)

__str__ = str.__str__ # type: ignore
__format__ = str.__format__ # type: ignore
def __str__(self) -> str:
return self.value

__hash__ = object.__hash__

@property
def last_match(self) -> re.Match[str] | None:
Expand Down

0 comments on commit ce75208

Please sign in to comment.