type_bridge.attribute.flags¶
flags
¶
Flag system for TypeDB attribute annotations.
TypeNameCase
¶
Bases: Enum
Type name case formatting options for Entity and Relation types.
TypeFlags
dataclass
¶
Metadata flags for Entity and Relation classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
TypeDB type name (if None, uses class name with case formatting) |
None
|
abstract
|
bool
|
Whether this is an abstract type |
False
|
base
|
bool
|
Whether this is a Python base class that should not appear in TypeDB schema |
False
|
case
|
TypeNameCase
|
Case formatting for auto-generated type names (default: CLASS_NAME) |
CLASS_NAME
|
Example
class Person(Entity): flags = TypeFlags(name="person") name: Name
class PersonName(Entity): flags = TypeFlags() # → PersonName (default CLASS_NAME) name: Name
class PersonName(Entity): flags = TypeFlags(case=TypeNameCase.SNAKE_CASE) # → person_name name: Name
class AbstractPerson(Entity): flags = TypeFlags(abstract=True) name: Name
class BaseEntity(Entity): flags = TypeFlags(base=True) # Python base class only # Children skip this in TypeDB hierarchy
Initialize TypeFlags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
TypeDB type name (if None, uses class name with case formatting) |
None
|
abstract
|
bool
|
Whether this is an abstract type |
False
|
base
|
bool
|
Whether this is a Python base class that should not appear in TypeDB schema |
False
|
case
|
TypeNameCase
|
Case formatting for auto-generated type names (default: CLASS_NAME) |
CLASS_NAME
|
Source code in type_bridge/attribute/flags.py
Card
¶
Cardinality marker for multi-value attribute ownership.
IMPORTANT: Card() should only be used with list[Type] annotations. For optional single values, use Optional[Type] instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min
|
int | None
|
Minimum cardinality (default: None, which means unspecified) |
None
|
max
|
int | None
|
Maximum cardinality (default: None, which means unbounded) |
None
|
Examples:
tags: list[Tag] = Flag(Card(min=2)) # @card(2..) - at least two jobs: list[Job] = Flag(Card(1, 5)) # @card(1..5) - one to five ids: list[ID] = Flag(Key, Card(min=1)) # @key @card(1..)
INCORRECT - use Optional[Type] instead:¶
age: Age = Flag(Card(min=0, max=1)) # ❌ Wrong!¶
age: Optional[Age] # ✓ Correct
Initialize cardinality marker.
Supports both positional and keyword arguments: - Card(1, 5) → min=1, max=5 - Card(min=2) → min=2, max=None (unbounded) - Card(max=5) → min=0, max=5 (defaults min to 0) - Card(min=0, max=10) → min=0, max=10
Source code in type_bridge/attribute/flags.py
AttributeFlags
dataclass
¶
AttributeFlags(is_key=False, is_unique=False, card_min=None, card_max=None, has_explicit_card=False, name=None, case=None)
Metadata for attribute ownership and type configuration.
Represents TypeDB ownership annotations like @key, @card(min..max), @unique, and allows overriding the attribute type name with explicit name or case formatting.
Example
class Person(Entity): name: Name = Flag(Key) # @key (implies @card(1..1)) email: Email = Flag(Unique) # @unique @card(1..1) age: Optional[Age] # @card(0..1) - no Flag needed tags: list[Tag] = Flag(Card(min=2)) # @card(2..) jobs: list[Job] = Flag(Card(1, 5)) # @card(1..5)
Override attribute type name explicitly¶
class Name(String): flags = AttributeFlags(name="name")
Or use case formatting¶
class PersonName(String): flags = AttributeFlags(case=TypeNameCase.SNAKE_CASE) # -> person_name
to_typeql_annotations
¶
Convert to TypeQL annotations like @key, @card(0..5).
Rules: - @key implies @card(1..1), so never output @card with @key - @unique with @card(1..1) is redundant, so omit @card in that case - Otherwise, always output @card if cardinality is specified
Returns:
| Type | Description |
|---|---|
list[str]
|
List of TypeQL annotation strings |
Source code in type_bridge/attribute/flags.py
format_type_name
¶
Format a class name according to the specified case style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
str
|
The Python class name |
required |
case
|
TypeNameCase
|
The case formatting style to apply |
required |
Returns:
| Type | Description |
|---|---|
str
|
The formatted type name |
Examples:
>>> format_type_name("PersonName", TypeNameCase.LOWERCASE)
'personname'
>>> format_type_name("PersonName", TypeNameCase.CLASS_NAME)
'PersonName'
>>> format_type_name("PersonName", TypeNameCase.SNAKE_CASE)
'person_name'
Source code in type_bridge/attribute/flags.py
Flag
¶
Create attribute flags for Key, Unique, and Card markers.
Usage
field: Type = Flag(Key) # @key (implies @card(1..1)) field: Type = Flag(Unique) # @unique @card(1..1) field: list[Type] = Flag(Card(min=2)) # @card(2..) field: list[Type] = Flag(Card(1, 5)) # @card(1..5) field: Type = Flag(Key, Unique) # @key @unique field: list[Type] = Flag(Key, Card(min=1)) # @key @card(1..)
For optional single values, use Optional[Type] instead: field: Optional[Type] # @card(0..1) - no Flag needed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*annotations
|
Any
|
Variable number of Key, Unique, or Card marker instances |
()
|
Returns:
| Type | Description |
|---|---|
Annotated[Any, AttributeFlags]
|
AttributeFlags instance with the specified flags |
Example
class Person(Entity): flags = TypeFlags(name="person") name: Name = Flag(Key) # @key (implies @card(1..1)) email: Email = Flag(Key, Unique) # @key @unique age: Optional[Age] # @card(0..1) tags: list[Tag] = Flag(Card(min=2)) # @card(2..) jobs: list[Job] = Flag(Card(1, 5)) # @card(1..5)