Utils
fieldz_kb.lpg.utils
Shared utility functions for fieldz_kb LPG backends.
This module provides helper functions for type classification, relationship naming, and field introspection.
Classes:
| Name | Description |
|---|---|
TypeAttributes |
Describes how a type maps to a node property or relationship. |
Functions:
| Name | Description |
|---|---|
get_array_type_from_field |
Determine the collection type for a field. |
get_type_attributes |
Classify a type as primitive, array, or relationship and extract attributes. |
make_node_class_name_from_type |
Return a node class name for a given Python type. |
make_relationship_type_from_field_name |
Generate a relationship type string from a field name. |
TypeAttributes
dataclass
Describes how a type maps to a node property or relationship.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
One of "primitive", "array", or "relationship". |
required |
target_types
|
frozenset
|
Frozenset of target type tuples. |
required |
many
|
bool
|
Whether the field represents a to-many relationship. |
required |
ordered
|
bool
|
Whether ordering matters for this field. |
required |
get_array_type_from_field
Determine the collection type for a field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Field
|
The fieldz field descriptor. |
required |
module
|
str | None
|
Module name for resolving forward references. |
None
|
Returns:
| Type | Description |
|---|---|
type
|
The collection type (list, tuple, set, frozenset). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no appropriate collection type can be determined. |
Source code in src/fieldz_kb/lpg/utils.py
get_type_attributes
get_type_attributes(type_: tuple) -> TypeAttributes
Classify a type as primitive, array, or relationship and extract attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
tuple
|
A tuple of (type_origin, type_args) from get_types_from_type_hint. |
required |
Returns:
| Type | Description |
|---|---|
TypeAttributes
|
A TypeAttributes describing how the type maps to a node property. |
Source code in src/fieldz_kb/lpg/utils.py
make_node_class_name_from_type
Return a node class name for a given Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
type
|
The Python type. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The class name string. |
make_relationship_type_from_field_name
Generate a relationship type string from a field name.
For plural field names (many=True), singularizes the name segments. For example: 'employees' -> 'HAS_EMPLOYEE', 'userProfiles' -> 'HAS_USER_PROFILE'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
The field name to convert. |
required |
many
|
bool
|
Whether the field represents a to-many relationship. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A relationship type string like 'HAS_FIELD_NAME'. |