Skip to content

BaseEntity

Base class for entities, domain objects with unique identity that live within an aggregate. Entities are always accessed through their parent aggregate and cannot exist independently.

See Entities guide for practical usage and Entities concept for design rationale.

Bases: Element, BaseModel, OptionsMixin

Base class for entities -- domain objects with unique identity that live within an aggregate.

Entities are mutable with identity-based equality: two entities are equal if they share the same identifier, regardless of attribute values. Field mutations are validated via Pydantic's validate_assignment and trigger pre/post invariant checks automatically.

Fields are declared using standard Python type annotations with optional Field constraints. An identity field is auto-injected unless one is explicitly declared with identifier=True.

Entities track their lifecycle state (new, persisted, changed, destroyed) via the state_ property, and can raise events that are registered on the aggregate root.

Meta Options

Option Type Description
part_of type The aggregate class this entity belongs to. Required.
provider str The persistence provider name (default: "default").
schema_name str The storage table/collection name.
auto_add_id_field bool Whether to auto-inject an id field (default: True).
Source code in src/protean/core/entity.py
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
def __init__(self, *args: Any, **kwargs: Any) -> None:
    # Pop internal kwargs that should not reach Pydantic
    owner = kwargs.pop("_owner", None)
    root = kwargs.pop("_root", None)
    # _version is a PrivateAttr; accept it in kwargs for backward
    # compatibility (e.g. repository hydration) but set it after init.
    _version_value = kwargs.pop("_version", None)

    # Pop association/VO descriptor kwargs and shadow field kwargs before
    # Pydantic init.  Shadow fields (e.g. order_id, billing_address_street)
    # are set dynamically during domain resolution and must not be passed
    # to Pydantic's __init__ which rejects them with extra="forbid".
    descriptor_kwargs: dict[str, Any] = {}
    shadow_kwargs: dict[str, Any] = {}

    # Build the set of known shadow field names from descriptors in
    # __container_fields__.  This prevents silently swallowing truly
    # unknown kwargs that Pydantic should reject.
    _shadow_field_names: set[str] = set()
    for fobj in getattr(type(self), _FIELDS, {}).values():
        if isinstance(fobj, Reference):
            attr_name = fobj.get_attribute_name()
            if attr_name:
                _shadow_field_names.add(attr_name)
        elif isinstance(fobj, ValueObject):
            for sf in fobj.embedded_fields.values():
                if sf.attribute_name is not None:
                    _shadow_field_names.add(sf.attribute_name)

    for name in list(kwargs):
        if name in _shadow_field_names:
            shadow_kwargs[name] = kwargs.pop(name)
        elif self._get_class_descriptor(type(self), name) is not None:
            descriptor_kwargs[name] = kwargs.pop(name)

    # Support template dict pattern: Entity({"key": "val"}, key2="val2")
    # Keyword args take precedence over template dict values.
    if args:
        merged: dict[str, Any] = {}
        for template in args:
            if not isinstance(template, dict):
                raise AssertionError(
                    f"Positional argument {template} passed must be a dict. "
                    f"This argument serves as a template for loading common "
                    f"values.",
                )
            # Also separate descriptor and shadow kwargs from template dicts
            for tname in list(template):
                if tname in _shadow_field_names:
                    shadow_kwargs[tname] = template.pop(tname)
                elif self._get_class_descriptor(type(self), tname) is not None:
                    descriptor_kwargs[tname] = template.pop(tname)
            merged.update(template)
        merged.update(kwargs)
        kwargs = merged

    # Collect all validation errors (Pydantic + required descriptors) before raising
    collected_errors: dict[str, list[str]] = {}

    # Push init context onto the thread-local stack so that
    # model_post_init (called by Pydantic inside super().__init__())
    # can retrieve descriptor/shadow kwargs.  Pydantic wipes __dict__
    # during validation, so we cannot stash data on the instance.
    stack: list[dict[str, Any]] = getattr(_init_context, "stack", [])
    depth_before_init = len(stack)
    stack.append(
        {
            "descriptor_kwargs": descriptor_kwargs,
            "shadow_kwargs": shadow_kwargs,
            "owner": owner,
            "root": root,
            "_version": _version_value,
        }
    )
    _init_context.stack = stack

    try:
        super().__init__(**kwargs)
    except PydanticValidationError as e:
        collected_errors.update(convert_pydantic_errors(e))
    finally:
        # ``model_post_init`` pops this call's entry on a successful init.
        # When construction raises before it runs (Pydantic field validation
        # fails, or an abstract class is rejected), the entry is left behind,
        # and a caller that catches the error in a loop (the snapshot loader
        # reloading a stale aggregate) leaks one entry per attempt. Truncate
        # to the pre-push depth so this call's entry is balanced for any
        # construction exception, without popping a parent or double-popping
        # one ``model_post_init`` already removed.
        del stack[depth_before_init:]

    # Check required descriptor fields (ValueObject, Reference, etc.)
    for field_name, field_obj in getattr(type(self), _FIELDS, {}).items():
        if (
            isinstance(field_obj, (ValueObject, Reference))
            and getattr(field_obj, "required", False)
            and field_name not in descriptor_kwargs
        ):
            # Check if shadow fields are present (e.g. region_id for region Reference)
            has_shadow = False
            if isinstance(field_obj, ValueObject):
                has_shadow = any(
                    sf.attribute_name in shadow_kwargs
                    for sf in field_obj.embedded_fields.values()
                )
            elif isinstance(field_obj, Reference):
                shadow_name = field_obj.get_attribute_name()
                has_shadow = shadow_name in shadow_kwargs

            if not has_shadow:
                collected_errors.setdefault(field_name, []).append("is required")

    if collected_errors:
        raise ValidationError(collected_errors)

state_ property writable

state_: _EntityState

Access entity lifecycle state.

__track_id_field classmethod

__track_id_field() -> None

Find the field marked identifier=True and record its name.

Source code in src/protean/core/entity.py
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
@classmethod
def __track_id_field(cls) -> None:
    """Find the field marked ``identifier=True`` and record its name."""
    id_fields = [
        field
        for _, field in getattr(cls, _FIELDS, {}).items()
        if getattr(field, "identifier", False)
    ]

    if len(id_fields) > 1:
        raise NotSupportedError(
            {
                "_entity": [
                    (
                        f"Multiple identifier fields found in entity {cls.__name__}. "
                        "Only one identifier field is allowed."
                    )
                ]
            }
        )
    elif len(id_fields) == 1:
        setattr(cls, _ID_FIELD_NAME, id_fields[0].field_name)

defaults

defaults() -> None

Placeholder for defaults.

Override in subclass when an attribute's default depends on other attribute values.

Source code in src/protean/core/entity.py
830
831
832
833
834
def defaults(self) -> None:
    """Placeholder for defaults.

    Override in subclass when an attribute's default depends on other attribute values.
    """

can_transition_to

can_transition_to(
    field_name: str, target_value: Any
) -> bool

Check whether a status field can transition to the given value.

Returns True if the transition is valid, False otherwise. Always returns True for non-status fields or status fields without a transitions map.

Example::

if order.can_transition_to("status", OrderStatus.SHIPPED):
    order.ship(tracking_number="ABC123")
Source code in src/protean/core/entity.py
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
def can_transition_to(self, field_name: str, target_value: Any) -> bool:
    """Check whether a status field can transition to the given value.

    Returns ``True`` if the transition is valid, ``False`` otherwise.
    Always returns ``True`` for non-status fields or status fields
    without a transitions map.

    Example::

        if order.can_transition_to("status", OrderStatus.SHIPPED):
            order.ship(tracking_number="ABC123")
    """

    fields_dict = getattr(self.__class__, _FIELDS, {})
    field_obj = fields_dict.get(field_name)
    if field_obj is None or not isinstance(field_obj, ResolvedField):
        return True
    transitions = field_obj.transitions
    if not transitions:
        return True

    current_value = getattr(self, field_name, None)
    target = target_value.value if isinstance(target_value, Enum) else target_value
    current = (
        current_value.value if isinstance(current_value, Enum) else current_value
    )

    if current is None:
        return True

    if current not in transitions:
        return False

    return target in transitions[current]

raise_

raise_(event: Any) -> None

Raise an event in the aggregate cluster.

The event is always registered on the aggregate root, irrespective of where it is raised in the entity cluster.

Source code in src/protean/core/entity.py
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
def raise_(self, event: Any) -> None:
    """Raise an event in the aggregate cluster.

    The event is always registered on the aggregate root, irrespective
    of where it is raised in the entity cluster.
    """
    if event.meta_.part_of != self._root.__class__:
        raise ConfigurationError(
            f"Event `{event.__class__.__name__}` is not associated with"
            f" aggregate `{self._root.__class__.__name__}`"
        )

    # Delegate to the root aggregate's raise_ (BaseAggregate overrides this)
    self._root.raise_(event)

to_dict

to_dict() -> dict[str, Any]

Return entity data as a dictionary.

Reference fields are skipped (they are navigation, not data). ValueObject fields are included only when non-None.

Source code in src/protean/core/entity.py
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
def to_dict(self) -> dict[str, Any]:
    """Return entity data as a dictionary.

    Reference fields are skipped (they are navigation, not data).
    ValueObject fields are included only when non-None.
    """
    result: dict[str, Any] = {}
    for fname, field_obj in getattr(self, _FIELDS, {}).items():
        if isinstance(field_obj, Reference):
            continue

        value = getattr(self, fname, None)

        if isinstance(field_obj, ValueObject):
            # Present, not truthy: ``as_dict`` returns ``None`` only
            # when the VO is ``None``, so an all-default VO is still included.
            dict_value = field_obj.as_dict(value)
            if dict_value is not None:
                result[fname] = dict_value
        elif isinstance(field_obj, Association):
            # HasOne/HasMany: delegate to descriptor's as_dict
            result[fname] = field_obj.as_dict(value)
        else:
            result[fname] = field_obj.as_dict(value)
    return result

from_value_object classmethod

from_value_object(vo: BaseValueObject) -> Self

Construct an entity instance from a value object.

This is the inverse of value_object_from_entity() -- it converts a VO payload (typically carried in a command or event) back into an entity instance::

items = [OrderItem.from_value_object(item) for item in command.items]

None values on identifier/unique fields are stripped so that auto-generated identity fields receive their default rather than failing validation. Explicit None on other fields is preserved.

Source code in src/protean/core/entity.py
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
@classmethod
def from_value_object(cls, vo: "BaseValueObject") -> Self:
    """Construct an entity instance from a value object.

    This is the inverse of ``value_object_from_entity()`` -- it converts
    a VO payload (typically carried in a command or event) back into an
    entity instance::

        items = [OrderItem.from_value_object(item) for item in command.items]

    ``None`` values on identifier/unique fields are stripped so that
    auto-generated identity fields receive their default rather than
    failing validation.  Explicit ``None`` on other fields is preserved.
    """
    data = vo.to_dict()

    # Strip None only for identifier/unique fields so auto-generated
    # identity defaults kick in, while preserving intentional Nones.
    id_or_unique = set()
    for fname, field_obj in getattr(cls, _FIELDS, {}).items():
        if isinstance(field_obj, ResolvedField) and (
            field_obj.identifier or field_obj.unique
        ):
            id_or_unique.add(fname)

    return cls(
        **{k: v for k, v in data.items() if not (v is None and k in id_or_unique)}
    )

__deepcopy__

__deepcopy__(
    memo: dict[int, Any] | None = None,
) -> BaseEntity

Deep copy that handles circular _root/_owner references.

Pydantic's default deepcopy recurses infinitely when pydantic_private contains back-references to the entity itself (e.g. _root and _owner on aggregate roots).

Source code in src/protean/core/entity.py
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> "BaseEntity":
    """Deep copy that handles circular _root/_owner references.

    Pydantic's default __deepcopy__ recurses infinitely when
    __pydantic_private__ contains back-references to the entity
    itself (e.g. _root and _owner on aggregate roots).
    """
    if memo is None:
        memo = {}

    # Short-circuit if we've already been copied (prevents infinite loop)
    existing: BaseEntity | None = memo.get(id(self))
    if existing is not None:
        return existing

    cls = type(self)
    new_obj = cls.__new__(cls)
    memo[id(self)] = new_obj

    # Deep-copy __dict__ (Pydantic model fields and extras)
    object.__setattr__(new_obj, "__dict__", copy.deepcopy(self.__dict__, memo))
    object.__setattr__(
        new_obj,
        "__pydantic_extra__",
        copy.deepcopy(self.__pydantic_extra__, memo),
    )
    object.__setattr__(
        new_obj,
        "__pydantic_fields_set__",
        copy.copy(self.__pydantic_fields_set__),
    )

    # Deep-copy __pydantic_private__, with memo to break cycles
    private = getattr(self, "__pydantic_private__", None)
    if private is None:
        object.__setattr__(new_obj, "__pydantic_private__", None)
    else:
        object.__setattr__(
            new_obj,
            "__pydantic_private__",
            copy.deepcopy(private, memo),
        )

    return new_obj