type_bridge.crud.types¶
types
¶
Type resolution and attribute handling utilities.
wrap_attribute_value
¶
Wrap raw values in Attribute instances with consistent behavior.
This is the unified attribute wrapping function used by all hydration paths. It ensures consistent handling of: - Multi-value attributes (lists) - Single-value attributes - Type coercion (via _pydantic_validate when available) - Null handling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The raw value to wrap (or list of values for multi-value attrs) |
required |
attr_info
|
ModelAttrInfo
|
ModelAttrInfo containing attribute class and flags |
required |
use_pydantic_validate
|
bool
|
If True, use _pydantic_validate() for type coercion (e.g., parsing ISO datetime strings). Default True. |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
Wrapped attribute value(s), or None/empty list for missing values |
Examples:
>>> wrap_attribute_value("Alice", name_attr_info)
Name("Alice")
>>> wrap_attribute_value(["tag1", "tag2"], tags_attr_info)
[Tag("tag1"), Tag("tag2")]
Source code in type_bridge/crud/types.py
is_multi_value_attribute
¶
Check if attribute is multi-value based on cardinality.
Multi-value attributes have either: - Unbounded cardinality (card_max is None) - Maximum cardinality > 1
Single-value attributes have: - Maximum cardinality == 1 (including 0..1 and 1..1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flags
|
AttributeFlags
|
AttributeFlags instance containing cardinality information |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if multi-value (card_max is None or > 1), False if single-value |
Examples:
>>> flags = AttributeFlags(card_min=0, card_max=1)
>>> is_multi_value_attribute(flags)
False
>>> flags = AttributeFlags(card_min=0, card_max=5)
>>> is_multi_value_attribute(flags)
True
>>> flags = AttributeFlags(card_min=2, card_max=None)
>>> is_multi_value_attribute(flags)
True
Source code in type_bridge/crud/types.py
build_metadata_fetch
¶
Build a fetch clause that retrieves only IID and type metadata.
Uses TypeQL 3.8.0 built-in functions iid() and label() to fetch the internal ID and type label. This is used for queries that need to identify entities/relations without fetching all attributes.
Note: TypeQL grammar doesn't allow mixing "key": value entries with $e.* in the same fetch clause, so metadata-only fetch is separate from attribute fetch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
str
|
Variable name (with or without $) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Fetch clause string like 'fetch { "_iid": iid($e), "_type": label($e) }' |
Example
build_metadata_fetch("e") 'fetch {\n "_iid": iid($e), "_type": label($e)\n}'
Source code in type_bridge/crud/types.py
hydrate_attributes
¶
Hydrate attributes from TypeDB fetch result.
Extracts attribute values from a raw TypeDB result dictionary, mapping TypeDB attribute names to Python field names. Handles multi-value attributes and collects key values for deduplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_class
|
type[Entity]
|
The entity class to extract attributes for |
required |
raw_data
|
dict[str, Any]
|
Raw attribute data from TypeDB fetch result |
required |
wrap_values
|
bool
|
If True, wrap attributes in Attribute instances using the unified wrap_attribute_value() helper |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Tuple of (attrs_dict, key_values_tuple): |
tuple[tuple[str, Any], ...]
|
|
tuple[dict[str, Any], tuple[tuple[str, Any], ...]]
|
|
Examples:
>>> result = {"name": "Alice", "age": 30}
>>> attrs, keys = hydrate_attributes(Person, result)
>>> attrs
{"name": "Alice", "age": 30}
>>> keys
(("name", "Alice"),) # if name is a key attribute
Source code in type_bridge/crud/types.py
extract_entity_key
¶
Extract the first key attribute from an entity for matching.
This is used to build match clauses based on key attributes when IID is not available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
Any
|
The entity instance |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str, Any] | None
|
Tuple of (field_name, attr_typeql_name, raw_value) if a key attribute |
tuple[str, str, Any] | None
|
with a non-None value is found, otherwise None. |
Examples: