Skip to content

synt.stmt.assertion ¤

assert_ module-attribute ¤

assert_ = Assert

Alias Assert.

Assert ¤

Bases: Statement

The assert statement.

Examples:

assert_stmt = assert_(TRUE, litstr("Condition is true"))
assert assert_stmt.into_code() == "assert True, 'Condition is true'"
References

Assert.

Source code in synt/stmt/assertion.py
class Assert(Statement):
    r"""The `assert` statement.

    Examples:
        ```python
        assert_stmt = assert_(TRUE, litstr("Condition is true"))
        assert assert_stmt.into_code() == "assert True, 'Condition is true'"
        ```

    References:
        [`Assert`](https://docs.python.org/3/library/ast.html#ast.Assert).
    """

    test: Expression
    """The expression to assert."""
    msg: Expression | None
    """The assert message."""

    def __init__(self, test: IntoExpression, msg: IntoExpression | None = None):
        """Initialize the assertion.

        Args:
            test: The expression to assert.
            msg: The assert message.
        """
        self.test = test.into_expression()
        if msg:
            self.msg = msg.into_expression()
        else:
            self.msg = None

    def indented(self, indent_width: int, indent_atom: str) -> str:
        msg = f", {self.msg.into_code()}" if self.msg else ""
        return f"{indent_width * indent_atom}assert {self.test.into_code()}{msg}"

msg instance-attribute ¤

msg: Expression | None

The assert message.

test instance-attribute ¤

test: Expression = into_expression()

The expression to assert.

__init__ ¤

1
2
3
__init__(
    test: IntoExpression, msg: IntoExpression | None = None
)

Initialize the assertion.

Parameters:

Name Type Description Default
test IntoExpression

The expression to assert.

required
msg IntoExpression | None

The assert message.

None
Source code in synt/stmt/assertion.py
def __init__(self, test: IntoExpression, msg: IntoExpression | None = None):
    """Initialize the assertion.

    Args:
        test: The expression to assert.
        msg: The assert message.
    """
    self.test = test.into_expression()
    if msg:
        self.msg = msg.into_expression()
    else:
        self.msg = None

indented ¤

indented(indent_width: int, indent_atom: str) -> str
Source code in synt/stmt/assertion.py
def indented(self, indent_width: int, indent_atom: str) -> str:
    msg = f", {self.msg.into_code()}" if self.msg else ""
    return f"{indent_width * indent_atom}assert {self.test.into_code()}{msg}"