haive.core.common.mixins.general.id¶

ID mixin for basic identification capabilities.

from typing import Any This module provides a simple mixin for adding UUID-based identification to Pydantic models. It’s a lightweight alternative to the more comprehensive IdentifierMixin when only basic ID capabilities are needed.

Usage:

from pydantic import BaseModel from haive.core.common.mixins.general import IdMixin

class MyComponent(IdMixin, BaseModel):

name: str

def __init__(self, **data):

super().__init__(**data) # Now the component has a UUID string ID

# Create with auto-generated ID component = MyComponent(name=”Test”) print(component.id) # UUID string

# Create with specific ID custom_component = MyComponent.with_id(“custom-id-123”, name=”Custom”) print(custom_component.id) # “custom-id-123”

Classes¶

IdMixin

Mixin for adding basic ID generation and management capabilities.

Module Contents¶

class haive.core.common.mixins.general.id.IdMixin(/, **data)[source]¶

Bases: pydantic.BaseModel

Mixin for adding basic ID generation and management capabilities.

This mixin adds a UUID-based ID field to any Pydantic model, with methods for regenerating the ID and creating instances with specific IDs.

Parameters:

data (Any)

id¶

A UUID string that uniquely identifies the object.

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.

regenerate_id()[source]¶

Generate a new UUID and return it.

This method replaces the current ID with a new UUID.

Returns:

The newly generated UUID string.

Return type:

str

classmethod with_id(id_value, **kwargs)[source]¶

Create an instance with a specific ID.

This class method provides a convenient way to create an instance with a predetermined ID value.

Parameters:
  • id_value (str) – The ID value to use.

  • **kwargs – Additional attributes for the instance.

Returns:

A new instance with the specified ID.