Skip to content

type_bridge.expressions.arithmetic

arithmetic

Arithmetic expressions for TypeQL queries.

TypeQL supports six infix operators with standard precedence: +, -, *, /, %, ^

These can be used in let assignments and reduce clauses to perform arithmetic on query variables and literals.

ArithmeticExpr dataclass

ArithmeticExpr(left, operator, right)

Bases: Expression

Binary arithmetic expression for TypeQL queries.

Represents an infix operation like $x + $y or $price * 1.1.

Example

expr = ArithmeticExpr("$x", "+", "$y") expr.to_typeql("$unused") '($x + $y)'

In a let assignment:

let $total = ($price * $quantity);

to_ast

to_ast(var)

Arithmetic expressions are values, not match patterns.

Source code in type_bridge/expressions/arithmetic.py
def to_ast(self, var: str) -> list[Pattern]:
    """Arithmetic expressions are values, not match patterns."""
    return []

to_value_ast

to_value_ast(var=None)

Convert to an ArithmeticValue AST node.

Source code in type_bridge/expressions/arithmetic.py
def to_value_ast(self, var: str | None = None) -> Value:
    """Convert to an ArithmeticValue AST node."""
    from type_bridge.query.ast import ArithmeticValue

    left: Value | str = self.left
    right: Value | str = self.right

    # If either operand is itself an ArithmeticExpr (nested), recurse
    # For now, operands are always strings (variables or literals)
    return ArithmeticValue(left=left, operator=self.operator, right=right)

to_typeql

to_typeql(var)

Generate parenthesized TypeQL arithmetic expression.

Parameters:

Name Type Description Default
var str

Ignored (arithmetic expressions use their own operands)

required

Returns:

Type Description
str

Parenthesized TypeQL expression, e.g. "($x + $y)"

Source code in type_bridge/expressions/arithmetic.py
def to_typeql(self, var: str) -> str:
    """Generate parenthesized TypeQL arithmetic expression.

    Args:
        var: Ignored (arithmetic expressions use their own operands)

    Returns:
        Parenthesized TypeQL expression, e.g. "($x + $y)"
    """
    return f"({self.left} {self.operator} {self.right})"

get_attribute_types

get_attribute_types()

Arithmetic expressions don't reference attribute types directly.

Source code in type_bridge/expressions/arithmetic.py
def get_attribute_types(self) -> set[type[Attribute]]:
    """Arithmetic expressions don't reference attribute types directly."""
    return set()

add

add(left, right)

Create an addition expression.

Parameters:

Name Type Description Default
left str | int | float

Variable name or numeric literal

required
right str | int | float

Variable name or numeric literal

required

Returns:

Type Description
ArithmeticExpr

ArithmeticExpr representing left + right

Example

add("x", "y").to_typeql("") '($x + $y)' add("price", 10).to_typeql("") '($price + 10)'

Source code in type_bridge/expressions/arithmetic.py
def add(left: str | int | float, right: str | int | float) -> ArithmeticExpr:
    """Create an addition expression.

    Args:
        left: Variable name or numeric literal
        right: Variable name or numeric literal

    Returns:
        ArithmeticExpr representing ``left + right``

    Example:
        >>> add("x", "y").to_typeql("")
        '($x + $y)'
        >>> add("price", 10).to_typeql("")
        '($price + 10)'
    """
    return ArithmeticExpr(_normalize_operand(left), "+", _normalize_operand(right))

sub

sub(left, right)

Create a subtraction expression.

Parameters:

Name Type Description Default
left str | int | float

Variable name or numeric literal

required
right str | int | float

Variable name or numeric literal

required

Returns:

Type Description
ArithmeticExpr

ArithmeticExpr representing left - right

Source code in type_bridge/expressions/arithmetic.py
def sub(left: str | int | float, right: str | int | float) -> ArithmeticExpr:
    """Create a subtraction expression.

    Args:
        left: Variable name or numeric literal
        right: Variable name or numeric literal

    Returns:
        ArithmeticExpr representing ``left - right``
    """
    return ArithmeticExpr(_normalize_operand(left), "-", _normalize_operand(right))

mul

mul(left, right)

Create a multiplication expression.

Parameters:

Name Type Description Default
left str | int | float

Variable name or numeric literal

required
right str | int | float

Variable name or numeric literal

required

Returns:

Type Description
ArithmeticExpr

ArithmeticExpr representing left * right

Source code in type_bridge/expressions/arithmetic.py
def mul(left: str | int | float, right: str | int | float) -> ArithmeticExpr:
    """Create a multiplication expression.

    Args:
        left: Variable name or numeric literal
        right: Variable name or numeric literal

    Returns:
        ArithmeticExpr representing ``left * right``
    """
    return ArithmeticExpr(_normalize_operand(left), "*", _normalize_operand(right))

div

div(left, right)

Create a division expression.

Parameters:

Name Type Description Default
left str | int | float

Variable name or numeric literal

required
right str | int | float

Variable name or numeric literal

required

Returns:

Type Description
ArithmeticExpr

ArithmeticExpr representing left / right

Source code in type_bridge/expressions/arithmetic.py
def div(left: str | int | float, right: str | int | float) -> ArithmeticExpr:
    """Create a division expression.

    Args:
        left: Variable name or numeric literal
        right: Variable name or numeric literal

    Returns:
        ArithmeticExpr representing ``left / right``
    """
    return ArithmeticExpr(_normalize_operand(left), "/", _normalize_operand(right))

mod

mod(left, right)

Create a modulo expression.

Parameters:

Name Type Description Default
left str | int | float

Variable name or numeric literal

required
right str | int | float

Variable name or numeric literal

required

Returns:

Type Description
ArithmeticExpr

ArithmeticExpr representing left % right

Source code in type_bridge/expressions/arithmetic.py
def mod(left: str | int | float, right: str | int | float) -> ArithmeticExpr:
    """Create a modulo expression.

    Args:
        left: Variable name or numeric literal
        right: Variable name or numeric literal

    Returns:
        ArithmeticExpr representing ``left % right``
    """
    return ArithmeticExpr(_normalize_operand(left), "%", _normalize_operand(right))

pow_

pow_(left, right)

Create an exponentiation expression.

Parameters:

Name Type Description Default
left str | int | float

Variable name or numeric literal

required
right str | int | float

Variable name or numeric literal

required

Returns:

Type Description
ArithmeticExpr

ArithmeticExpr representing left ^ right

Source code in type_bridge/expressions/arithmetic.py
def pow_(left: str | int | float, right: str | int | float) -> ArithmeticExpr:
    """Create an exponentiation expression.

    Args:
        left: Variable name or numeric literal
        right: Variable name or numeric literal

    Returns:
        ArithmeticExpr representing ``left ^ right``
    """
    return ArithmeticExpr(_normalize_operand(left), "^", _normalize_operand(right))