haive.core.common.mixins.identifier¶

Identifier mixin for unique identification of objects.

This module provides a mixin class that adds UUID-based identification and human-readable naming to Pydantic models. The mixin handles validation, generation, and utility methods for working with identifiers.

Uses Pydantic v2 patterns with field_validator and computed fields.

Usage:

from haive.core.common.mixins.identifier import IdentifierMixin

class MyComponent(IdentifierMixin, BaseModel):

# Other fields content: str

def __init__(self, **data):

super().__init__(**data) # Now the component has an ID and optional name

# Create with auto-generated ID component = MyComponent(content=”Hello”) print(component.id) # UUID string print(component.short_id) # First 8 chars of UUID

# Create with custom name named_component = MyComponent(content=”Hello”, name=”GreetingComponent”) print(named_component.display_name) # “GreetingComponent”

Classes¶

IdentifierMixin

Mixin that adds unique identification to any Pydantic model.

Module Contents¶

class haive.core.common.mixins.identifier.IdentifierMixin(/, **data)[source]¶

Bases: pydantic.BaseModel

Mixin that adds unique identification to any Pydantic model.

This mixin provides both UUID-based identification and human-readable naming capabilities. It automatically generates UUIDs, validates provided IDs, and offers convenience methods for working with the identifiers.

Parameters:

data (Any)

id¶

A UUID string that uniquely identifies the object.

name¶

An optional human-readable name for the object.

short_id¶

First 8 characters of the UUID (computed).

Return type:

str

display_name¶

User-friendly name for display (computed).

Return type:

str

uuid_obj¶

UUID object representation of the ID (computed).

Return type:

uuid.UUID

has_custom_name¶

Whether a custom name is set (computed).

Return type:

bool

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

clear_name()[source]¶

Clear the name.

Return type:

None

identifier_info()[source]¶

Get comprehensive identifier information.

Returns:

A dictionary containing all identifier-related information.

Return type:

dict[str, str]

initialize_uuid_obj()[source]¶

Initialize UUID object after model validation.

Returns:

Self, with the _uuid_obj private attribute initialized.

Return type:

Self

matches_id(id_or_name)[source]¶

Check if this object matches the given ID or name.

This method checks if the provided string matches this object’s full ID, short ID, or name (case-insensitive).

Parameters:

id_or_name (str) – The ID or name string to check against.

Returns:

True if there’s a match, False otherwise.

Return type:

bool

regenerate_id()[source]¶

Generate a new ID and return it.

This method creates a new UUID, updates the ID field, and returns the new ID string.

Returns:

The newly generated UUID string.

Return type:

str

set_name(name)[source]¶

Set the name with validation.

Parameters:

name (str) – The new name to set.

Return type:

None

classmethod validate_id(v)[source]¶

Ensure ID is a valid UUID string.

Parameters:

v (str) – The ID string to validate.

Returns:

The validated ID string, or a new UUID if invalid.

Return type:

str

classmethod validate_name(v)[source]¶

Validate and clean the name field.

Parameters:

v (str | None) – The name string to validate.

Returns:

The cleaned name string, or None if empty after cleaning.

Return type:

str | None

property display_name: str¶

Display name (uses name if available, otherwise short_id).

Returns:

The human-readable name if set, otherwise “Object-{short_id}”.

Return type:

str

property has_custom_name: bool¶

Whether this object has a custom name (not auto-generated).

Returns:

True if a non-empty name is set, False otherwise.

Return type:

bool

property short_id: str¶

Short version of the ID (first 8 characters).

Returns:

The first 8 characters of the UUID string.

Return type:

str

property uuid_obj: uuid.UUID¶

UUID object representation of the ID.

Returns:

The UUID object corresponding to the ID string.

Return type:

uuid.UUID