Skip to content

Commit

Permalink
Deprecate class Id and NewId class factory (#5)
Browse files Browse the repository at this point in the history
Current implementation of identity is complex and requires at least one extra call to get a real value. Deprecate this in honor of using NewType with bind type.
  • Loading branch information
NikBelyaev committed Sep 4, 2023
1 parent 737c97d commit 2eff243
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ddd_framework/domain/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from abc import ABC
from datetime import datetime
from typing import Any, Protocol, TypeVar
Expand Down Expand Up @@ -44,6 +45,8 @@ def from_raw_id(cls, raw_id: str | int) -> Id:

def NewId(name: str, base: type[Id] = Id) -> type[Id]:
"""Create a new identifier's type."""
warnings.warn('Id class will be removed in the next update', DeprecationWarning, stacklevel=2)

return make_class(name, attrs={}, bases=(base,))


Expand All @@ -55,6 +58,8 @@ def structure_id(value: Any, _klass: type[Id]) -> Id:

cattrs.register_structure_hook(Id, structure_id)

EntityId = Any


# endregion

Expand All @@ -64,11 +69,11 @@ def structure_id(value: Any, _klass: type[Id]) -> Id:
class Entity:
"""Represent an entity."""

id: Id | None = field(default=None)
id: EntityId | Id | None = field(default=None)

def __hash__(self) -> int:
# TODO: Fix "Item "None" of "Id | None" has no attribute "id""
return hash(self.id.id) # type: ignore
return hash(self.id.id) if isinstance(self.id, Id) else hash(self.id)


# endregion
Expand Down

0 comments on commit 2eff243

Please sign in to comment.