Skip to content

Commit 788fa51

Browse files
committed
Add remark about slots usage in ValueObject
1 parent 6431074 commit 788fa51

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

  • src/app/domain/value_objects

src/app/domain/value_objects/base.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,24 @@
99
class ValueObject:
1010
"""
1111
Base class for immutable value objects (VO) in domain.
12-
Defined by its attributes, which must themselves be immutable.
12+
Defined by its instance attributes, which must themselves be immutable.
1313
For simple cases where only type distinction is required,
1414
consider using `typing.NewType` instead of subclassing this class.
15+
16+
Warning: due to mismatch between typing rules and runtime behavior
17+
for `Final[ClassVar[T]]`, using `__slots__` in subclasses is not
18+
recommended — such class constants are turned into `member_descriptor`
19+
at runtime instead of preserving their declared values.
20+
21+
Note: I considered using plain `ClassVar[T]` for class constants
22+
(as `ClassVar[Final[T]]` currently has no semantic meaning anyway)
23+
which would immediately resolve the slots compatibility and `fields()`
24+
behavior issues. However, I chose to prioritize correct typing
25+
semantics with `Final[ClassVar[T]]` and observe how this inconsistency
26+
between typing rules and runtime behavior gets resolved.
27+
28+
https://github.com/python/cpython/issues/89547
29+
https://github.com/python/mypy/issues/19607
1530
"""
1631

1732
def __post_init__(self) -> None:

0 commit comments

Comments
 (0)