Skip to content

Nodes

A node is an immutable, hashable dataclass that stores expression structure. Nodes are not evaluated directly — pass them to a VisitorDispatcher with matching visitors.

dynamic_expressions.nodes.Node dataclass

Base class for expression AST nodes.

Subclass this type to define new node kinds. Each node must be hashable because the dispatcher caches evaluation results keyed by the node identity.

Literal

dynamic_expressions.nodes.LiteralNode dataclass

Constant value embedded in an expression tree.

Pair with LiteralVisitor. When value is a tuple, nested Node instances are recursively evaluated during the visitation.

Attributes:

Name Type Description
value Any

Constant payload, or a collection that may contain nested nodes.

Any Of

dynamic_expressions.nodes.AnyOfNode dataclass

Logical OR over a sequence of child expressions.

Evaluates to a True value if at least one child expression is truthy. Pair with AnyOfVisitor.

Attributes:

Name Type Description
expressions tuple[Node, ...]

Child nodes evaluated in order until one of them is truthy.

All Of

dynamic_expressions.nodes.AllOfNode dataclass

Logical AND over a sequence of child expressions.

Evaluates to a True value only when every child expression is truthy. Pair with AllOfVisitor.

Attributes:

Name Type Description
expressions tuple[Node, ...]

Child nodes evaluated in order; all must be truthy.

Unary Expression

dynamic_expressions.nodes.UnaryExpressionNode dataclass

Unary operator applied to a single child expression.

Pair with UnaryExpressionVisitor. Supported operators are listed in UnaryExpressionOperator.

Attributes:

Name Type Description
operator UnaryExpressionOperator

Unary operator name, for example "not" or "-".

value Node

Operand is evaluated before the operator is applied.

Supported operators:

Operator Meaning
+ Unary plus (operator.pos)
- Negation
~ Bitwise invert
abs Absolute value
not Logical not

Binary Expression

dynamic_expressions.nodes.BinaryExpressionNode dataclass

Binary operator applied to two child expressions.

Pair with BinaryExpressionVisitor. Supported operators are listed in BinaryExpressionOperator.

Attributes:

Name Type Description
operator BinaryExpressionOperator

Binary operator name, for example "=" or "+".

left Node

Left-hand operand.

right Node

Right-hand operand.

Supported operators:

Operator Meaning
= Equality
!= Inequality
<, <=, >, >= Comparison
in Membership test (left in right)
+, -, *, /, //, %, ^ Arithmetic
&, \| Bitwise and / or
getitem Subscript access
getattr Dot-separated attribute path

Operand order for in

Passing the container as left and the search value as right still works but emits a DeprecationWarning. Prefer left=<search value>, right=<container>.

Coalesce

dynamic_expressions.nodes.CoalesceNode dataclass

Return the first truthy child expression.

Similar to SQL COALESCE. Pair with CoalesceVisitor.

Attributes:

Name Type Description
items tuple[Node, ...]

Candidates evaluated in order; the first truthy result wins.

Match

dynamic_expressions.nodes.MatchNode dataclass

Pattern match over CaseNode branches.

Returns the value of the first case whose condition is truthy, or the optional default branch. Pair with MatchVisitor.

Attributes:

Name Type Description
cases tuple[CaseNode, ...]

Branches tested in order.

default Node | None

Fallback node is returned if no case matches.

Case

dynamic_expressions.nodes.CaseNode dataclass

Single branch inside a MatchNode.

Do not evaluate this node directly. It is handled only as part of MatchNode by MatchVisitor.

Attributes:

Name Type Description
expression Node

Condition evaluated to decide whether this branch matches.

value Node

Result returned when the condition is truthy.

From Context

dynamic_expressions.nodes.FromContextNode dataclass

Read a field from the evaluation context.

The field name may use dot notation ("user.name") to traverse nested attributes. Pair with FromContextNodeVisitor.

Attributes:

Name Type Description
field_name str

Attribute path is resolved from the dispatcher context.