From af0bcda68b1e90601b002e50ddeb76e79bf03a4f Mon Sep 17 00:00:00 2001 From: Alexander Tikhonov Date: Sun, 21 Apr 2024 22:31:03 +0300 Subject: [PATCH] Change Alias to regular class so that we could support multiple names in future --- mashumaro/types.py | 15 +++++++++++++-- tests/test_types.py | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/mashumaro/types.py b/mashumaro/types.py index ae4d477d..dc0a90c1 100644 --- a/mashumaro/types.py +++ b/mashumaro/types.py @@ -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) diff --git a/tests/test_types.py b/tests/test_types.py index bdad3e36..b0beb779 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -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") @@ -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')"