From b69af18e94e6a5429db43ceeaf1c1aecef40142e Mon Sep 17 00:00:00 2001 From: ivan-borovets <130386813+ivan-borovets@users.noreply.github.com> Date: Thu, 4 Sep 2025 04:26:09 +0500 Subject: [PATCH] Add remark about slots usage in ValueObject --- src/app/domain/value_objects/base.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/app/domain/value_objects/base.py b/src/app/domain/value_objects/base.py index faa3036..27bd16a 100644 --- a/src/app/domain/value_objects/base.py +++ b/src/app/domain/value_objects/base.py @@ -9,9 +9,24 @@ class ValueObject: """ Base class for immutable value objects (VO) in domain. - Defined by its attributes, which must themselves be immutable. + Defined by its instance attributes, which must themselves be immutable. For simple cases where only type distinction is required, consider using `typing.NewType` instead of subclassing this class. + + Warning: due to mismatch between typing rules and runtime behavior + for `Final[ClassVar[T]]`, using `__slots__` in subclasses is not + recommended — such class constants are turned into `member_descriptor` + at runtime instead of preserving their declared values. + + Note: I considered using plain `ClassVar[T]` for class constants + (as `ClassVar[Final[T]]` currently has no semantic meaning anyway) + which would immediately resolve the slots compatibility and `fields()` + behavior issues. However, I chose to prioritize correct typing + semantics with `Final[ClassVar[T]]` and observe how this inconsistency + between typing rules and runtime behavior gets resolved. + + https://github.com/python/cpython/issues/89547 + https://github.com/python/mypy/issues/19607 """ def __post_init__(self) -> None: