Skip to content

Commit

Permalink
Change Alias to regular class so that we could support multiple names…
Browse files Browse the repository at this point in the history
… in future
  • Loading branch information
Fatal1ty committed Apr 21, 2024
1 parent 1f84b01 commit af0bcda
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 13 additions & 2 deletions mashumaro/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ def __post_init__(self) -> None:
)


@dataclass(unsafe_hash=True)
class Alias:
name: str
def __init__(self, name: str, /):
self.name = name

def __repr__(self) -> str:
return f"Alias(name='{self.name}')"

def __eq__(self, other: Any) -> bool:
if not isinstance(other, Alias):
return False
return self.name == other.name

def __hash__(self) -> int:
return hash(self.name)
18 changes: 17 additions & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mashumaro import DataClassDictMixin
from mashumaro.core.const import PEP_585_COMPATIBLE
from mashumaro.exceptions import UnserializableField
from mashumaro.types import SerializableType
from mashumaro.types import Alias, SerializableType
from tests.entities import GenericSerializableList, GenericSerializableWrapper

XT = TypeVar("XT")
Expand Down Expand Up @@ -351,3 +351,19 @@ class DataClass(DataClassDictMixin):
obj = DataClass.from_dict({"x": ["1", "2022-05-29", "2.3"]})
# assert obj.x.value == (1, date(2022, 5, 29), 2.3)
assert obj.to_dict() == {"x": [1, "2022-05-29", 2.3]}


def test_alias():
x1 = Alias("x")
x2 = Alias("x")
y = Alias("y")

assert x1 == x2
assert hash(x1) == hash(x2)

assert x1 != y
assert x1 != "x"
assert hash(x1) != hash(y)

assert str(y) == "Alias(name='y')"
assert repr(y) == "Alias(name='y')"

0 comments on commit af0bcda

Please sign in to comment.