type_bridge.models.entity¶
entity
¶
Entity class for TypeDB entities.
Entity
¶
Bases: TypeDBType
Base class for TypeDB entities with Pydantic validation.
Entities own attributes defined as Attribute subclasses. Use TypeFlags to configure type name and abstract status. Supertype is determined automatically from Python inheritance.
This class inherits from TypeDBType and Pydantic's BaseModel, providing: - Automatic validation of attribute values - JSON serialization/deserialization - Type checking and coercion - Field metadata via Pydantic's Field()
Example
class Name(String): pass
class Age(Integer): pass
class Person(Entity): flags = TypeFlags(name="person") name: Name = Flag(Key) age: Age
Abstract entity¶
class AbstractPerson(Entity): flags = TypeFlags(abstract=True) name: Name
Inheritance (Person sub abstract-person)¶
class ConcretePerson(AbstractPerson): age: Age
__init_subclass__
¶
Called when Entity subclass is created.
Source code in type_bridge/models/entity.py
to_schema_definition
classmethod
¶
Generate TypeQL schema definition for this entity.
Returns:
| Type | Description |
|---|---|
str | None
|
TypeQL schema definition string, or None if this is a base class |
Source code in type_bridge/models/entity.py
to_ast
¶
Generate AST InsertClause for this instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
str
|
Variable name to use |
'$e'
|
Returns:
| Type | Description |
|---|---|
InsertClause
|
InsertClause containing statements |
Source code in type_bridge/models/entity.py
get_match_clause_info
¶
Get match clause info for this entity instance.
Prefers IID-based matching when available (most precise). Falls back to @key attribute matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var_name
|
str
|
Variable name to use in the match clause |
'$e'
|
Returns:
| Type | Description |
|---|---|
MatchClauseInfo
|
MatchClauseInfo with the match clause |
Raises:
| Type | Description |
|---|---|
ValueError
|
If entity has neither _iid nor key attributes |
Source code in type_bridge/models/entity.py
get_match_pattern
¶
Get an AST EntityPattern for matching this entity instance.
Prefers IID-based matching when available (most precise). Falls back to @key attribute matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var_name
|
str
|
Variable name to use in the pattern |
'$e'
|
Returns:
| Type | Description |
|---|---|
EntityPattern
|
EntityPattern AST node |
Raises:
| Type | Description |
|---|---|
ValueError
|
If entity has neither _iid nor key attributes |
Source code in type_bridge/models/entity.py
to_dict
¶
Serialize the entity to a primitive dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include
|
set[str] | None
|
Optional set of field names to include. |
None
|
exclude
|
set[str] | None
|
Optional set of field names to exclude. |
None
|
by_alias
|
bool
|
When True, use attribute TypeQL names instead of Python field names. |
False
|
exclude_unset
|
bool
|
When True, omit fields that were never explicitly set. |
False
|
Source code in type_bridge/models/entity.py
from_dict
classmethod
¶
Construct an Entity from a plain dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
External data to hydrate the Entity. |
required |
field_mapping
|
dict[str, str] | None
|
Optional mapping of external keys to internal field names. |
None
|
strict
|
bool
|
When True, raise on unknown fields; otherwise ignore them. |
True
|
Source code in type_bridge/models/entity.py
__repr__
¶
Developer-friendly string representation of entity.
Source code in type_bridge/models/entity.py
__str__
¶
User-friendly string representation of entity.