Skip to content

synt.stmt.assign ¤

assign module-attribute ¤

assign = Assignment

Alias Assignment.

Assignment ¤

Bases: Statement

Assignment statement.

Examples:

1
2
3
4
5
6
ass = tup(id_("a"), id_("b")).assign(tup(litint(1), litstr("foo")))
assert ass.into_code() == "a, b = (1, 'foo')" # automatically unpack
ass = id_("a").expr().ty(id_("str")).assign(litstr("foo"))
assert ass.into_code() == "a: str = 'foo'" # explicitly typing
ass = id_("a").expr().ty(id_("str"))
assert ass.into_code() == "a: str" # only typing
References

Assign
AnnAssign
AugAssign.

Source code in synt/stmt/assign.py
class Assignment(Statement):
    r"""Assignment statement.

    Examples:
        ```python
        ass = tup(id_("a"), id_("b")).assign(tup(litint(1), litstr("foo")))
        assert ass.into_code() == "a, b = (1, 'foo')" # automatically unpack
        ass = id_("a").expr().ty(id_("str")).assign(litstr("foo"))
        assert ass.into_code() == "a: str = 'foo'" # explicitly typing
        ass = id_("a").expr().ty(id_("str"))
        assert ass.into_code() == "a: str" # only typing
        ```

    References:
        [`Assign`](https://docs.python.org/3/library/ast.html#ast.Assign)<br/>
        [`AnnAssign`](https://docs.python.org/3/library/ast.html#ast.AnnAssign)<br/>
        [`AugAssign`](https://docs.python.org/3/library/ast.html#ast.AugAssign).
    """

    target: Expression
    """Target of the assignment."""
    target_ty: Expression | None
    """The type of the target."""
    value: Expression | None
    """The value to be assigned."""

    def __init__(self, target: IntoExpression):
        """Initialize a new assignment statement.

        Args:
            target: The variable to be assigned to.
        """
        self.target = target.into_expression()
        self.target_ty = None
        self.value = None

    def type(self, ty: IntoExpression) -> Self:
        """Set the target's type.

        Args:
            ty: The type of the target variable.
        """
        self.target_ty = ty.into_expression()
        return self

    def assign(self, v: IntoExpression) -> Self:
        """Set the target's value.

        Args:
            v: The value of the assignment.
        """
        self.value = v.into_expression()
        return self

    def indented(self, indent_width: int, indent_atom: str) -> str:
        if is_tuple(self.target):  # implicit tuple, like `a, b = foo`.
            target = self.target.into_code_implicit()
        else:
            target = self.target.into_code()
        tty = f": {self.target_ty.into_code()}" if self.target_ty is not None else ""
        ass = f" = {self.value.into_code()}" if self.value is not None else ""
        return f"{indent_atom * indent_width}{target}{tty}{ass}"

target instance-attribute ¤

target: Expression = into_expression()

Target of the assignment.

target_ty instance-attribute ¤

target_ty: Expression | None = None

The type of the target.

value instance-attribute ¤

value: Expression | None = None

The value to be assigned.

__init__ ¤

__init__(target: IntoExpression)

Initialize a new assignment statement.

Parameters:

Name Type Description Default
target IntoExpression

The variable to be assigned to.

required
Source code in synt/stmt/assign.py
def __init__(self, target: IntoExpression):
    """Initialize a new assignment statement.

    Args:
        target: The variable to be assigned to.
    """
    self.target = target.into_expression()
    self.target_ty = None
    self.value = None

type ¤

type(ty: IntoExpression) -> Self

Set the target's type.

Parameters:

Name Type Description Default
ty IntoExpression

The type of the target variable.

required
Source code in synt/stmt/assign.py
def type(self, ty: IntoExpression) -> Self:
    """Set the target's type.

    Args:
        ty: The type of the target variable.
    """
    self.target_ty = ty.into_expression()
    return self

assign ¤

assign(v: IntoExpression) -> Self

Set the target's value.

Parameters:

Name Type Description Default
v IntoExpression

The value of the assignment.

required
Source code in synt/stmt/assign.py
def assign(self, v: IntoExpression) -> Self:
    """Set the target's value.

    Args:
        v: The value of the assignment.
    """
    self.value = v.into_expression()
    return self

indented ¤

indented(indent_width: int, indent_atom: str) -> str
Source code in synt/stmt/assign.py
def indented(self, indent_width: int, indent_atom: str) -> str:
    if is_tuple(self.target):  # implicit tuple, like `a, b = foo`.
        target = self.target.into_code_implicit()
    else:
        target = self.target.into_code()
    tty = f": {self.target_ty.into_code()}" if self.target_ty is not None else ""
    ass = f" = {self.value.into_code()}" if self.value is not None else ""
    return f"{indent_atom * indent_width}{target}{tty}{ass}"