Skip to content

type_bridge.expressions.base

base

Base expression class for TypeQL query building.

Expression

Bases: ABC

Base class for all query expressions.

Expressions represent query constraints that can be composed using boolean operators and converted to TypeQL patterns.

to_ast abstractmethod

to_ast(var)

Convert this expression to AST patterns.

Parameters:

Name Type Description Default
var str

The variable name to use in the pattern (e.g., "$e")

required

Returns:

Type Description
list[Pattern]

List of AST patterns

Source code in type_bridge/expressions/base.py
@abstractmethod
def to_ast(self, var: str) -> list["Pattern"]:
    """Convert this expression to AST patterns.

    Args:
        var: The variable name to use in the pattern (e.g., "$e")

    Returns:
        List of AST patterns
    """
    ...

to_value_ast

to_value_ast(var=None)

Convert this expression to an AST Value (if applicable).

Most expressions are patterns (filters) and cannot be converted to values. FunctionCall expressions and Literal wrappers are values.

Parameters:

Name Type Description Default
var str | None

Optional variable name for aggregate expressions that need context.

None

Raises:

Type Description
NotImplementedError

If expression cannot be converted to a value.

Source code in type_bridge/expressions/base.py
def to_value_ast(self, var: str | None = None) -> "Value":
    """Convert this expression to an AST Value (if applicable).

    Most expressions are patterns (filters) and cannot be converted to values.
    FunctionCall expressions and Literal wrappers are values.

    Args:
        var: Optional variable name for aggregate expressions that need context.

    Raises:
        NotImplementedError: If expression cannot be converted to a value.
    """
    raise NotImplementedError(f"{type(self).__name__} cannot be converted to an AST Value.")

to_typeql

to_typeql(var)

Deprecated: Use to_ast() instead.

Source code in type_bridge/expressions/base.py
def to_typeql(self, var: str) -> str:
    """Deprecated: Use to_ast() instead."""
    from type_bridge.query.compiler import QueryCompiler

    compiler = QueryCompiler()
    patterns = self.to_ast(var)
    # We need to compile each pattern and join them?
    # Compiler has _compile_pattern.
    # But _compile_pattern is private.
    # compiler.compile() takes a Clause.
    # We can create a temporary MatchClause?
    from type_bridge.query.ast import MatchClause

    return compiler.compile(MatchClause(patterns=patterns)).replace("match\n", "").strip(";")

get_attribute_types

get_attribute_types()

Get all attribute types referenced by this expression.

Returns:

Type Description
set[type[Attribute]]

Set of attribute types used in this expression

Note

Default implementation returns attr_type if present. BooleanExpr overrides to recursively collect from operands.

Source code in type_bridge/expressions/base.py
def get_attribute_types(self) -> set[type["Attribute"]]:
    """Get all attribute types referenced by this expression.

    Returns:
        Set of attribute types used in this expression

    Note:
        Default implementation returns attr_type if present.
        BooleanExpr overrides to recursively collect from operands.
    """
    # Check if this expression has attr_type attribute
    attr_type = getattr(self, "attr_type", None)
    if attr_type is not None:
        return {attr_type}
    return set()

and_

and_(other)

Combine this expression with another using AND logic.

Parameters:

Name Type Description Default
other Expression

Another expression to AND with this one

required

Returns:

Type Description
BooleanExpr

BooleanExpr representing the conjunction

Source code in type_bridge/expressions/base.py
def and_(self, other: "Expression") -> "BooleanExpr":
    """Combine this expression with another using AND logic.

    Args:
        other: Another expression to AND with this one

    Returns:
        BooleanExpr representing the conjunction
    """
    from type_bridge.expressions.boolean import BooleanExpr

    return BooleanExpr(operation="and", operands=[self, other])

or_

or_(other)

Combine this expression with another using OR logic.

Parameters:

Name Type Description Default
other Expression

Another expression to OR with this one

required

Returns:

Type Description
BooleanExpr

BooleanExpr representing the disjunction

Source code in type_bridge/expressions/base.py
def or_(self, other: "Expression") -> "BooleanExpr":
    """Combine this expression with another using OR logic.

    Args:
        other: Another expression to OR with this one

    Returns:
        BooleanExpr representing the disjunction
    """
    from type_bridge.expressions.boolean import BooleanExpr

    return BooleanExpr(operation="or", operands=[self, other])

not_

not_()

Negate this expression using NOT logic.

Returns:

Type Description
BooleanExpr

BooleanExpr representing the negation

Source code in type_bridge/expressions/base.py
def not_(self) -> "BooleanExpr":
    """Negate this expression using NOT logic.

    Returns:
        BooleanExpr representing the negation
    """
    from type_bridge.expressions.boolean import BooleanExpr

    return BooleanExpr(operation="not", operands=[self])