type_bridge.attribute.base¶
base
¶
Base Attribute class for TypeDB attribute types.
Attribute
¶
Bases: ABC
Base class for TypeDB attributes.
Attributes in TypeDB are value types that can be owned by entities and relations.
Attribute instances can store values, allowing type-safe construction: Name("Alice") # Creates Name instance with value "Alice" Age(30) # Creates Age instance with value 30
Type name formatting
You can control how the class name is converted to TypeDB attribute name using the 'case' class variable or 'attr_name' for explicit control.
Example
class Name(String): pass # TypeDB attribute: "Name" (default CLASS_NAME)
class PersonName(String): case = TypeNameCase.SNAKE_CASE # TypeDB attribute: "person_name"
class PersonName(String): attr_name = "full_name" # Explicit override
class Age(Integer): pass
class Person(Entity): name: Name age: Age
Direct instantiation with wrapped types (best practice):¶
person = Person(name=Name("Alice"), age=Age(30))
Initialize attribute with a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to store in this attribute instance |
None
|
Source code in type_bridge/attribute/base.py
__init_subclass__
¶
Called when a subclass is created.
Source code in type_bridge/attribute/base.py
__str__
¶
__repr__
¶
__eq__
¶
Compare attribute with another attribute instance.
For strict type safety, Attribute instances do NOT compare equal to raw values.
To access the raw value, use the .value property.
Examples:
Age(20) == Age(20) # True (same type, same value) Age(20) == Id(20) # False (different types!) Age(20) == 20 # False (not equal to raw value!) Age(20).value == 20 # True (access raw value explicitly)
Source code in type_bridge/attribute/base.py
__hash__
¶
get_attribute_name
classmethod
¶
Get the TypeDB attribute name.
If attr_name is explicitly set, it is used as-is. Otherwise, the class name is formatted according to the case parameter. Default case is CLASS_NAME (preserves class name as-is).
Source code in type_bridge/attribute/base.py
get_value_type
classmethod
¶
is_key
classmethod
¶
is_abstract
classmethod
¶
is_independent
classmethod
¶
get_owners
classmethod
¶
Get all Entity/Relation classes that own this attribute.
Returns:
| Type | Description |
|---|---|
set[type[TypeDBType]]
|
Set of model classes that define this attribute as a field. |
set[type[TypeDBType]]
|
Does not require a database connection (static discovery). |
Source code in type_bridge/attribute/base.py
get_supertype
classmethod
¶
to_schema_definition
classmethod
¶
Generate TypeQL schema definition for this attribute.
Includes support for TypeDB annotations: - @abstract (comes right after attribute name) - @independent (comes right after attribute name, allows standalone existence) - @range(min..max) from range_constraint ClassVar (after value type) - @regex("pattern") from regex ClassVar (after value type) - @values("a", "b", ...) from allowed_values ClassVar (after value type)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_doc_meta
|
bool
|
Emit @doc/@meta head annotations. Migration redefine payloads pass False — TypeDB rejects head annotations in redefine; annotation changes lower to dedicated define/redefine/undefine steps instead. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
TypeQL schema definition string |
Source code in type_bridge/attribute/base.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
gt
classmethod
¶
Create greater-than comparison expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Attribute
|
Value to compare against |
required |
Returns:
| Type | Description |
|---|---|
ComparisonExpr
|
ComparisonExpr for attr > value |
Example
Age.gt(Age(30)) # age > 30
Source code in type_bridge/attribute/base.py
lt
classmethod
¶
Create less-than comparison expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Attribute
|
Value to compare against |
required |
Returns:
| Type | Description |
|---|---|
ComparisonExpr
|
ComparisonExpr for attr < value |
Example
Age.lt(Age(30)) # age < 30
Source code in type_bridge/attribute/base.py
gte
classmethod
¶
Create greater-than-or-equal comparison expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Attribute
|
Value to compare against |
required |
Returns:
| Type | Description |
|---|---|
ComparisonExpr
|
ComparisonExpr for attr >= value |
Example
Salary.gte(Salary(80000.0)) # salary >= 80000
Source code in type_bridge/attribute/base.py
lte
classmethod
¶
Create less-than-or-equal comparison expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Attribute
|
Value to compare against |
required |
Returns:
| Type | Description |
|---|---|
ComparisonExpr
|
ComparisonExpr for attr <= value |
Example
Age.lte(Age(65)) # age <= 65
Source code in type_bridge/attribute/base.py
eq
classmethod
¶
Create equality comparison expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Attribute
|
Value to compare against |
required |
Returns:
| Type | Description |
|---|---|
ComparisonExpr
|
ComparisonExpr for attr == value |
Example
Status.eq(Status("active")) # status == "active"
Source code in type_bridge/attribute/base.py
neq
classmethod
¶
Create not-equal comparison expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Attribute
|
Value to compare against |
required |
Returns:
| Type | Description |
|---|---|
ComparisonExpr
|
ComparisonExpr for attr != value |
Example
Status.neq(Status("deleted")) # status != "deleted"
Source code in type_bridge/attribute/base.py
sum
classmethod
¶
Create sum aggregation expression.
Returns:
| Type | Description |
|---|---|
AggregateExpr
|
AggregateExpr for sum(attr) |
Example
Salary.sum() # sum of all salaries
Source code in type_bridge/attribute/base.py
avg
classmethod
¶
Create average (mean) aggregation expression.
Note
Automatically converts to TypeQL 'mean' function. TypeDB 3.x uses 'mean' instead of 'avg'.
Returns:
| Type | Description |
|---|---|
AggregateExpr
|
AggregateExpr for mean(attr) |
Example
Age.avg() # Generates TypeQL: mean($age)
Source code in type_bridge/attribute/base.py
max
classmethod
¶
Create maximum aggregation expression.
Returns:
| Type | Description |
|---|---|
AggregateExpr
|
AggregateExpr for max(attr) |
Example
Score.max() # maximum score
Source code in type_bridge/attribute/base.py
min
classmethod
¶
Create minimum aggregation expression.
Returns:
| Type | Description |
|---|---|
AggregateExpr
|
AggregateExpr for min(attr) |
Example
Price.min() # minimum price
Source code in type_bridge/attribute/base.py
median
classmethod
¶
Create median aggregation expression.
Returns:
| Type | Description |
|---|---|
AggregateExpr
|
AggregateExpr for median(attr) |
Example
Salary.median() # median salary
Source code in type_bridge/attribute/base.py
std
classmethod
¶
Create standard deviation aggregation expression.
Returns:
| Type | Description |
|---|---|
AggregateExpr
|
AggregateExpr for std(attr) |
Example
Score.std() # standard deviation of scores
Source code in type_bridge/attribute/base.py
__get_pydantic_core_schema__
classmethod
¶
Unified Pydantic schema generation for all attribute types.
This base implementation handles the common patterns: - Serialization: extract _value from attribute instances - Validation: wrap raw values in attribute instances - Literal type support (for types that enable it)
Subclasses can override for completely custom behavior, or override the helper methods (_pydantic_serialize, _pydantic_validate, etc.) for targeted customization.
Source code in type_bridge/attribute/base.py
__class_getitem__
classmethod
¶
build_lookup
classmethod
¶
Build an expression for a lookup operator.
This method centralizes the logic for converting lookup names (e.g., 'gt', 'in') into TypeQL expressions. Subclasses (like String) should override this to handle type-specific lookups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookup
|
str
|
The lookup operator name (e.g., 'exact', 'gt', 'contains') |
required |
value
|
Any
|
The value to filter by |
required |
Returns:
| Type | Description |
|---|---|
Expression
|
Expression object representing the filter |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the lookup operator is not supported by this attribute type |