type_bridge.typeql.annotations¶
annotations
¶
Shared TypeQL annotation formatting utilities.
This module centralizes the formatting logic for TypeQL annotations to avoid duplication across the codebase.
format_card_annotation
¶
Format a @card(min..max) annotation string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_val
|
int | None
|
Minimum cardinality (None means unspecified, defaults to 0) |
required |
max_val
|
int | None
|
Maximum cardinality (None means unbounded) |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Formatted annotation string like "@card(1..5)" or "@card(2..)", |
str | None
|
or None if both min and max are None. |
Examples:
>>> format_card_annotation(1, 5)
'@card(1..5)'
>>> format_card_annotation(2, None)
'@card(2..)'
>>> format_card_annotation(0, 1)
'@card(0..1)'
>>> format_card_annotation(None, None)
None
Source code in type_bridge/typeql/annotations.py
format_type_annotations
¶
Format type-level annotations (@abstract, @independent).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
abstract
|
bool
|
Whether to include @abstract annotation |
False
|
independent
|
bool
|
Whether to include @independent annotation |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of annotation strings (may be empty) |
Examples:
>>> format_type_annotations(abstract=True)
['@abstract']
>>> format_type_annotations(abstract=True, independent=True)
['@abstract', '@independent']
>>> format_type_annotations()
[]
Source code in type_bridge/typeql/annotations.py
escape_annotation_string
¶
Render a TypeQL string literal for @doc / @meta values.
Escapes backslashes, quotes, and control characters exactly the way
TypeDB's schema export renders them, mirroring the Rust core's
escaped_string_literal so both lowering paths emit identical text.
Examples:
>>> escape_annotation_string('plain')
'"plain"'
>>> escape_annotation_string('line1\nline2')
'"line1\\nline2"'
Source code in type_bridge/typeql/annotations.py
format_doc_meta_annotations
¶
Format TypeDB 3.12+ @doc / @meta annotations.
Emits @doc before @meta with meta keys sorted, matching the
canonical annotation order of TypeDB's schema export (and the Rust
core's append_doc_meta_annotations).
Examples:
>>> format_doc_meta_annotations("a person", {"icon": "p.png"})
['@doc("a person")', '@meta("icon", "p.png")']
>>> format_doc_meta_annotations(None, {})
[]