Skip to content

synt.stmt.raising ¤

raise_ module-attribute ¤

raise_ = Raise

Alias Raise.

Raise ¤

Bases: Statement

The raise statement.

Examples:

1
2
3
4
5
6
r = raise_()
assert r.into_code() == "raise"
r = raise_(litint(42))
assert r.into_code() == "raise 42"
r = raise_(litint(42)).from_(litstr("Custom exception"))
assert r.into_code() == "raise 42 from 'Custom exception'"
References

Raise.

Source code in synt/stmt/raising.py
class Raise(Statement):
    r"""The `raise` statement.

    Examples:
        ```python
        r = raise_()
        assert r.into_code() == "raise"
        r = raise_(litint(42))
        assert r.into_code() == "raise 42"
        r = raise_(litint(42)).from_(litstr("Custom exception"))
        assert r.into_code() == "raise 42 from 'Custom exception'"
        ```

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

    exception: Expression | None
    """The exception to raise."""
    cause: Expression | None
    """The origin of the raised exception."""

    def __init__(self, exception: IntoExpression | None = None):
        """Initialize a new `raise` statement.

        Args:
            exception: The exception to raise.
        """
        if exception:
            self.exception = exception.into_expression()
        else:
            self.exception = None
        self.cause = None

    def from_(self, cause: IntoExpression) -> Self:
        """Set the cause of the raised exception.

        Args:
            cause: The origin of the raised exception.

        Raises:
            ValueError: If `exception` is `None`.
        """
        if self.exception is None:
            raise ValueError("Cannot set cause without setting exception.")
        self.cause = cause.into_expression()
        return self

    def indented(self, indent_width: int, indent_atom: str) -> str:
        cause = f" from {self.cause.into_code()}" if self.cause is not None else ""
        exc = f" {self.exception.into_code()}" if self.exception is not None else ""
        return f"{indent_width * indent_atom}raise{exc}{cause}"

exception instance-attribute ¤

exception: Expression | None

The exception to raise.

cause instance-attribute ¤

cause: Expression | None = None

The origin of the raised exception.

__init__ ¤

__init__(exception: IntoExpression | None = None)

Initialize a new raise statement.

Parameters:

Name Type Description Default
exception IntoExpression | None

The exception to raise.

None
Source code in synt/stmt/raising.py
def __init__(self, exception: IntoExpression | None = None):
    """Initialize a new `raise` statement.

    Args:
        exception: The exception to raise.
    """
    if exception:
        self.exception = exception.into_expression()
    else:
        self.exception = None
    self.cause = None

from_ ¤

from_(cause: IntoExpression) -> Self

Set the cause of the raised exception.

Parameters:

Name Type Description Default
cause IntoExpression

The origin of the raised exception.

required

Raises:

Type Description
ValueError

If exception is None.

Source code in synt/stmt/raising.py
def from_(self, cause: IntoExpression) -> Self:
    """Set the cause of the raised exception.

    Args:
        cause: The origin of the raised exception.

    Raises:
        ValueError: If `exception` is `None`.
    """
    if self.exception is None:
        raise ValueError("Cannot set cause without setting exception.")
    self.cause = cause.into_expression()
    return self

indented ¤

indented(indent_width: int, indent_atom: str) -> str
Source code in synt/stmt/raising.py
def indented(self, indent_width: int, indent_atom: str) -> str:
    cause = f" from {self.cause.into_code()}" if self.cause is not None else ""
    exc = f" {self.exception.into_code()}" if self.exception is not None else ""
    return f"{indent_width * indent_atom}raise{exc}{cause}"