Skip to content

Commit 6431074

Browse files
committed
Drop dataclass usage in base entity
1 parent 0ce057e commit 6431074

4 files changed

Lines changed: 31 additions & 37 deletions

File tree

src/app/domain/entities/base.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from dataclasses import dataclass
21
from typing import Any, TypeVar
32

43
from app.domain.exceptions.base import DomainError
@@ -7,29 +6,19 @@
76
T = TypeVar("T", bound=ValueObject)
87

98

10-
@dataclass(eq=False)
119
class Entity[T: ValueObject]:
1210
"""
1311
raises DomainError
1412
1513
Base class for domain entities, defined by a unique identity (`id`).
1614
- `id`: Identity that remains constant throughout the entity's lifecycle.
1715
- Entities are mutable, but are compared solely by their `id`.
18-
- Subclasses must set `eq=False` to inherit the equality behavior.
19-
- Add `kw_only=True` in subclasses to enforce named arguments for clarity & safety.
2016
"""
2117

22-
id_: T
23-
24-
def __post_init__(self) -> None:
25-
"""
26-
:raises DomainError:
27-
28-
Hook for additional initialization and ensuring invariants.
29-
Subclasses can override this method to implement custom logic, while
30-
still calling `super().__post_init__()` to preserve base checks.
31-
"""
18+
def __init__(self, *, id_: T) -> None:
19+
""":raises DomainError:"""
3220
self.__forbid_base_class_instantiation()
21+
self.id_ = id_
3322

3423
def __forbid_base_class_instantiation(self) -> None:
3524
""":raises DomainError:"""

src/app/domain/entities/user.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
from dataclasses import dataclass
2-
31
from app.domain.entities.base import Entity
42
from app.domain.enums.user_role import UserRole
53
from app.domain.value_objects.user_id import UserId
64
from app.domain.value_objects.user_password_hash import UserPasswordHash
75
from app.domain.value_objects.username import Username
86

97

10-
@dataclass(eq=False, kw_only=True)
118
class User(Entity[UserId]):
12-
username: Username
13-
password_hash: UserPasswordHash
14-
role: UserRole
15-
is_active: bool
9+
def __init__(
10+
self,
11+
*,
12+
id_: UserId,
13+
username: Username,
14+
password_hash: UserPasswordHash,
15+
role: UserRole,
16+
is_active: bool,
17+
) -> None:
18+
super().__init__(id_=id_)
19+
self.username = username
20+
self.password_hash = password_hash
21+
self.role = role
22+
self.is_active = is_active

tests/app/unit/factories/named_entity.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ class NamedEntityId(ValueObject):
99
value: int
1010

1111

12-
@dataclass(eq=False)
1312
class NamedEntity(Entity[NamedEntityId]):
14-
name: str
13+
def __init__(self, *, id_: NamedEntityId, name: str) -> None:
14+
super().__init__(id_=id_)
15+
self.name = name
1516

1617

17-
@dataclass(eq=False)
1818
class NamedEntitySubclass(NamedEntity):
19-
value: int
19+
def __init__(self, *, id_: NamedEntityId, name: str, value: int) -> None:
20+
super().__init__(id_=id_, name=name)
21+
self.value = value
2022

2123

2224
def create_named_entity_id(
@@ -29,12 +31,12 @@ def create_named_entity(
2931
id_: int = 42,
3032
name: str = "name",
3133
) -> NamedEntity:
32-
return NamedEntity(NamedEntityId(id_), name)
34+
return NamedEntity(id_=NamedEntityId(id_), name=name)
3335

3436

3537
def create_named_entity_subclass(
3638
id_: int = 42,
3739
name: str = "name",
3840
value: int = 314,
3941
) -> NamedEntitySubclass:
40-
return NamedEntitySubclass(NamedEntityId(id_), name, value)
42+
return NamedEntitySubclass(id_=NamedEntityId(id_), name=name, value=value)

tests/app/unit/factories/tagged_entity.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@ class TaggedEntityId(ValueObject):
99
value: int
1010

1111

12-
@dataclass(eq=False)
1312
class TaggedEntity(Entity[TaggedEntityId]):
14-
tag: str
13+
def __init__(self, *, id_: TaggedEntityId, tag: str) -> None:
14+
super().__init__(id_=id_)
15+
self.tag = tag
1516

1617

17-
def create_tagged_entity_id(
18-
id_: int = 54,
19-
) -> TaggedEntityId:
18+
def create_tagged_entity_id(id_: int = 54) -> TaggedEntityId:
2019
return TaggedEntityId(id_)
2120

2221

23-
def create_tagged_entity(
24-
id_: int = 54,
25-
tag: str = "tag",
26-
) -> TaggedEntity:
27-
return TaggedEntity(TaggedEntityId(id_), tag)
22+
def create_tagged_entity(id_: int = 54, tag: str = "tag") -> TaggedEntity:
23+
return TaggedEntity(id_=TaggedEntityId(id_), tag=tag)

0 commit comments

Comments
 (0)