type_bridge.models.utils¶
utils
¶
Utility functions and dataclasses for TypeDB model classes.
MatchClauseInfo
dataclass
¶
Information needed to build a TypeQL match clause for an instance.
Used by TypeDBManager to build CRUD queries for both entities and relations.
Attributes:
| Name | Type | Description |
|---|---|---|
main_clause |
str
|
The primary match clause (e.g., "$e isa person, has Name "Alice"") |
extra_clauses |
list[str]
|
Additional match clauses (e.g., role player matches for relations) |
var_name |
str
|
The main variable name (e.g., "$e" or "$r") |
WriteQueryInfo
dataclass
¶
Information needed to build a TypeQL write query for an instance.
Supports both entities (which only need insert/put pattern) and relations (which need match clause for role players + insert/put pattern).
Attributes:
| Name | Type | Description |
|---|---|---|
match_clause |
str | None
|
Optional match clause (for relations with role players) |
write_pattern |
str
|
The insert/put pattern |
FieldInfo
dataclass
¶
Information extracted from a field type annotation.
Attributes:
| Name | Type | Description |
|---|---|---|
attr_type |
type[Attribute] | None
|
The Attribute subclass (e.g., Name, Age) |
card_min |
int | None
|
Minimum cardinality (None means use default) |
card_max |
int | None
|
Maximum cardinality (None means unbounded) |
is_key |
bool
|
Whether this field is marked as @key |
is_unique |
bool
|
Whether this field is marked as @unique |
ModelAttrInfo
dataclass
¶
Metadata for an attribute owned by an Entity or Relation.
Attributes:
| Name | Type | Description |
|---|---|---|
typ |
type[Attribute]
|
The Attribute subclass (e.g., Name, Age) |
flags |
AttributeFlags
|
The AttributeFlags with key/unique/card annotations |
attribute_wrapper
¶
Create a validator that wraps raw values in the attribute class.
Returns a Pydantic BeforeValidator that converts raw values (str, int, etc.) into instances of the specified Attribute class.
Source code in type_bridge/models/utils.py
extract_metadata
¶
Extract attribute type, cardinality, and key/unique metadata from a type annotation.
Handles: - Optional[Name] → FieldInfo(Name, 0, 1, False, False) - Key[Name] → FieldInfo(Name, 1, 1, True, False) - Unique[Email] → FieldInfo(Email, 1, 1, False, True) - Name → FieldInfo(Name, 1, 1, False, False) - list[Tag] → FieldInfo(Tag, None, None, False, False) - cardinality set by Flag(Card(...))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_type
|
type
|
The type annotation from annotations |
required |
Returns:
| Type | Description |
|---|---|
FieldInfo
|
FieldInfo with extracted metadata |
Source code in type_bridge/models/utils.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
get_base_type_for_attribute
¶
Get the base Python type for an Attribute class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr_cls
|
type[Attribute]
|
The Attribute subclass (e.g., Name which inherits from String) |
required |
Returns:
| Type | Description |
|---|---|
type | None
|
The corresponding base Python type (str, int, float, bool, datetime) |
Source code in type_bridge/models/utils.py
get_ast_value_type
¶
Get the AST value type for an Attribute class.
This function determines the correct TypeQL literal type for an attribute, including proper handling of DateTimeTZ vs DateTime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr_cls
|
type[Attribute]
|
The Attribute subclass |
required |
Returns:
| Type | Description |
|---|---|
AstValueType
|
The AST value type string ("string", "long", "double", "boolean", |
AstValueType
|
"datetime", "datetime-tz", or "date") |
Source code in type_bridge/models/utils.py
validate_type_name
¶
Validate that a type name doesn't conflict with TypeDB built-ins or TypeQL keywords.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The type name to validate |
required |
class_name
|
str
|
The Python class name (for error messages) |
required |
context
|
Literal['entity', 'relation', 'attribute', 'role']
|
The type context ("entity", "relation", "attribute", "role") |
'entity'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If type name conflicts with a TypeDB built-in type |
ReservedWordError
|
If type name is a TypeQL reserved word |