Skip to content

synt.expr.unary_op ¤

awaited module-attribute ¤

awaited = await_

Alias await_.

starred module-attribute ¤

starred = unpack

Alias unpack.

unpack_seq module-attribute ¤

unpack_seq = unpack

Alias unpack.

double_starred module-attribute ¤

double_starred = unpack_kv

Alias unpack_kv.

unpack_dict module-attribute ¤

unpack_dict = unpack_kv

Alias unpack_kv.

neg module-attribute ¤

neg = negative

Alias negative.

bool_not module-attribute ¤

bool_not = not_

Alias not_.

bit_not module-attribute ¤

bit_not = invert

Alias invert.

UnaryOpType ¤

Bases: IntEnum

Unary operator type.

References

expr.ExprPrecedence

Source code in synt/expr/unary_op.py
class UnaryOpType(IntEnum):
    r"""Unary operator type.

    References:
        [`expr.ExprPrecedence`][synt.expr.expr.ExprPrecedence]
    """

    Await = 0
    """Await expression operator `await`.

    Notes:
        `await` is a Python hard keyword, but synt treats it as a unary operator.
    """

    Yield = 1
    """Yield expression operator `yield`.

    Notes:
        `yield` is a Python hard keyword, but synt treats it as a unary operator.
    """

    YieldFrom = 2
    """Yield-from expression operator `yield from`.

    Notes:
        `yield from` is a Python hard keyword group, but synt treats it as a single unary operator.
    """

    Starred = 3
    """Tuple/list extractor operator `*`."""

    DoubleStarred = 4
    """Dict/mapping extractor operator `**`."""

    Positive = 5
    """Positive operator `+`."""

    Neg = 6
    """Negative operator `-`."""

    BitNot = 7
    """Bitwise NOT operator `~`."""

    BoolNot = 8
    """Boolean NOT operator `not`."""

    def to_precedence(self) -> expr.ExprPrecedence:
        """Get the operator's backend expression's precedence."""
        match self:
            case (
                UnaryOpType.Starred
                | UnaryOpType.DoubleStarred
                | UnaryOpType.Yield
                | UnaryOpType.YieldFrom
            ):
                return expr.ExprPrecedence.Atom
            case UnaryOpType.Await:
                return expr.ExprPrecedence.Await
            case UnaryOpType.Positive | UnaryOpType.Neg | UnaryOpType.BitNot:
                return expr.ExprPrecedence.Unary
            case UnaryOpType.BoolNot:
                return expr.ExprPrecedence.BoolNot
            case _:
                raise ValueError(
                    f"Unexpected unary operator type: {self} [{self.value}]"
                )

    def into_code(self) -> str:
        """Converts the operator into a string representation.

        Raises:
            ValueError: If the operator is not recognized.
        """
        match self:
            case UnaryOpType.Starred:
                return "*"
            case UnaryOpType.DoubleStarred:
                return "**"
            case UnaryOpType.Yield:
                return "yield"
            case UnaryOpType.YieldFrom:
                return "yield from"
            case UnaryOpType.Await:
                return "await"
            case UnaryOpType.Positive:
                return "+"
            case UnaryOpType.Neg:
                return "-"
            case UnaryOpType.BitNot:
                return "~"
            case UnaryOpType.BoolNot:
                return "not"

Await class-attribute instance-attribute ¤

Await = 0

Await expression operator await.

Notes

await is a Python hard keyword, but synt treats it as a unary operator.

Yield class-attribute instance-attribute ¤

Yield = 1

Yield expression operator yield.

Notes

yield is a Python hard keyword, but synt treats it as a unary operator.

YieldFrom class-attribute instance-attribute ¤

YieldFrom = 2

Yield-from expression operator yield from.

Notes

yield from is a Python hard keyword group, but synt treats it as a single unary operator.

Starred class-attribute instance-attribute ¤

Starred = 3

Tuple/list extractor operator *.

DoubleStarred class-attribute instance-attribute ¤

DoubleStarred = 4

Dict/mapping extractor operator **.

Positive class-attribute instance-attribute ¤

Positive = 5

Positive operator +.

Neg class-attribute instance-attribute ¤

Neg = 6

Negative operator -.

BitNot class-attribute instance-attribute ¤

BitNot = 7

Bitwise NOT operator ~.

BoolNot class-attribute instance-attribute ¤

BoolNot = 8

Boolean NOT operator not.

to_precedence ¤

to_precedence() -> ExprPrecedence

Get the operator's backend expression's precedence.

Source code in synt/expr/unary_op.py
def to_precedence(self) -> expr.ExprPrecedence:
    """Get the operator's backend expression's precedence."""
    match self:
        case (
            UnaryOpType.Starred
            | UnaryOpType.DoubleStarred
            | UnaryOpType.Yield
            | UnaryOpType.YieldFrom
        ):
            return expr.ExprPrecedence.Atom
        case UnaryOpType.Await:
            return expr.ExprPrecedence.Await
        case UnaryOpType.Positive | UnaryOpType.Neg | UnaryOpType.BitNot:
            return expr.ExprPrecedence.Unary
        case UnaryOpType.BoolNot:
            return expr.ExprPrecedence.BoolNot
        case _:
            raise ValueError(
                f"Unexpected unary operator type: {self} [{self.value}]"
            )

into_code ¤

into_code() -> str

Converts the operator into a string representation.

Raises:

Type Description
ValueError

If the operator is not recognized.

Source code in synt/expr/unary_op.py
def into_code(self) -> str:
    """Converts the operator into a string representation.

    Raises:
        ValueError: If the operator is not recognized.
    """
    match self:
        case UnaryOpType.Starred:
            return "*"
        case UnaryOpType.DoubleStarred:
            return "**"
        case UnaryOpType.Yield:
            return "yield"
        case UnaryOpType.YieldFrom:
            return "yield from"
        case UnaryOpType.Await:
            return "await"
        case UnaryOpType.Positive:
            return "+"
        case UnaryOpType.Neg:
            return "-"
        case UnaryOpType.BitNot:
            return "~"
        case UnaryOpType.BoolNot:
            return "not"

UnaryOp ¤

Bases: Expression

Unary operation.

Source code in synt/expr/unary_op.py
class UnaryOp(expr.Expression):
    r"""Unary operation."""

    expression: expr.Expression
    """Internal expression."""
    op_type: UnaryOpType
    """Operator type."""
    expr_type = expr.ExprType.UnaryOp

    def __init__(self, op: UnaryOpType, e: expr.IntoExpression):
        """Initialize a unary operation.

        Args:
            op: Unary operator type.
            e: Internal expression.
        """
        self.expression = e.into_expression()
        self.op_type = op

        if self.expression.precedence > self.precedence:
            self.expression = self.expression.wrapped()

    def into_code(self) -> str:
        return f"{self.op_type.into_code()} {self.expression.into_code()}"

    @property
    def precedence(self) -> ExprPrecedence:
        """expr.Expression precedence.

        References:
            [Unary.to_precedence][synt.expr.unary_op.UnaryOpType.to_precedence].
        """
        return self.op_type.to_precedence()

expr_type class-attribute instance-attribute ¤

expr_type = UnaryOp

expression instance-attribute ¤

expression: Expression = into_expression()

Internal expression.

op_type instance-attribute ¤

op_type: UnaryOpType = op

Operator type.

precedence property ¤

precedence: ExprPrecedence

expr.Expression precedence.

References

Unary.to_precedence.

__init__ ¤

__init__(op: UnaryOpType, e: IntoExpression)

Initialize a unary operation.

Parameters:

Name Type Description Default
op UnaryOpType

Unary operator type.

required
e IntoExpression

Internal expression.

required
Source code in synt/expr/unary_op.py
def __init__(self, op: UnaryOpType, e: expr.IntoExpression):
    """Initialize a unary operation.

    Args:
        op: Unary operator type.
        e: Internal expression.
    """
    self.expression = e.into_expression()
    self.op_type = op

    if self.expression.precedence > self.precedence:
        self.expression = self.expression.wrapped()

into_code ¤

into_code() -> str
Source code in synt/expr/unary_op.py
def into_code(self) -> str:
    return f"{self.op_type.into_code()} {self.expression.into_code()}"

await_ ¤

await_(e: IntoExpression) -> UnaryOp

Create an await expression.

Examples:

await_expr = await_(litint(10))
assert await_expr.into_code() == "await 10"
Source code in synt/expr/unary_op.py
def await_(e: expr.IntoExpression) -> UnaryOp:
    r"""Create an `await` expression.

    Examples:
        ```python
        await_expr = await_(litint(10))
        assert await_expr.into_code() == "await 10"
        ```
    """
    return UnaryOp(UnaryOpType.Await, e)

unpack ¤

unpack(e: IntoExpression) -> UnaryOp

Sequence unpacking operation.

Examples:

unpacked_expr = unpack(list_(litint(1), litint(2), litint(3)))
assert unpacked_expr.into_code() == "* [1, 2, 3]"
Source code in synt/expr/unary_op.py
def unpack(e: expr.IntoExpression) -> UnaryOp:
    r"""Sequence unpacking operation.

    Examples:
        ```python
        unpacked_expr = unpack(list_(litint(1), litint(2), litint(3)))
        assert unpacked_expr.into_code() == "* [1, 2, 3]"
        ```
    """
    return UnaryOp(UnaryOpType.Starred, e)

unpack_kv ¤

unpack_kv(e: IntoExpression) -> UnaryOp

K-V pair unpacking operation.

Examples:

unpacked_expr = unpack_kv(dict_(kv(litint(1), litstr('a'))))
assert unpacked_expr.into_code() == "** {1: 'a'}"
Source code in synt/expr/unary_op.py
def unpack_kv(e: expr.IntoExpression) -> UnaryOp:
    r"""K-V pair unpacking operation.

    Examples:
        ```python
        unpacked_expr = unpack_kv(dict_(kv(litint(1), litstr('a'))))
        assert unpacked_expr.into_code() == "** {1: 'a'}"
        ```
    """
    return UnaryOp(UnaryOpType.DoubleStarred, e)

positive ¤

positive(e: IntoExpression) -> UnaryOp

Positive operation.

Examples:

positive_expr = positive(litint(10))
assert positive_expr.into_code() == "+ 10"
Source code in synt/expr/unary_op.py
def positive(e: expr.IntoExpression) -> UnaryOp:
    r"""Positive operation.

    Examples:
        ```python
        positive_expr = positive(litint(10))
        assert positive_expr.into_code() == "+ 10"
        ```
    """
    return UnaryOp(UnaryOpType.Positive, e)

negative ¤

negative(e: IntoExpression) -> UnaryOp

Negative operation.

Examples:

positive_expr = negative(litint(10))
assert positive_expr.into_code() == "- 10"
Source code in synt/expr/unary_op.py
def negative(e: expr.IntoExpression) -> UnaryOp:
    r"""Negative operation.

    Examples:
        ```python
        positive_expr = negative(litint(10))
        assert positive_expr.into_code() == "- 10"
        ```
    """
    return UnaryOp(UnaryOpType.Positive, e)

not_ ¤

Boolean NOT operation.

Examples:

not_expr = not_(id_("foo"))
assert not_expr.into_code() == "not foo"
Source code in synt/expr/unary_op.py
def not_(e: expr.IntoExpression) -> UnaryOp:
    r"""Boolean NOT operation.

    Examples:
        ```python
        not_expr = not_(id_("foo"))
        assert not_expr.into_code() == "not foo"
        ```
    """
    return UnaryOp(UnaryOpType.BoolNot, e)

invert ¤

invert(e: IntoExpression) -> UnaryOp

Bitwise NOT operation.

Examples:

not_expr = invert(id_("foo"))
assert not_expr.into_code() == "~ foo"
Source code in synt/expr/unary_op.py
def invert(e: expr.IntoExpression) -> UnaryOp:
    r"""Bitwise NOT operation.

    Examples:
        ```python
        not_expr = invert(id_("foo"))
        assert not_expr.into_code() == "~ foo"
        ```
    """
    return UnaryOp(UnaryOpType.BitNot, e)

yield_ ¤

yield_(e: Expression) -> UnaryOp

Yield operation.

Examples:

yield_expr = yield_(litint(10))
assert yield_expr.into_code() == "yield 10"
Source code in synt/expr/unary_op.py
def yield_(e: expr.Expression) -> UnaryOp:
    r"""Yield operation.

    Examples:
        ```python
        yield_expr = yield_(litint(10))
        assert yield_expr.into_code() == "yield 10"
        ```
    """
    return UnaryOp(UnaryOpType.Yield, e)

yield_from ¤

yield_from(e: Expression) -> UnaryOp

Yield from operation.

Examples:

yield_expr = yield_from(list_(litint(10), litint(42)))
assert yield_expr.into_code() == "yield from [10, 42]"
Source code in synt/expr/unary_op.py
def yield_from(e: expr.Expression) -> UnaryOp:
    r"""Yield from operation.

    Examples:
        ```python
        yield_expr = yield_from(list_(litint(10), litint(42)))
        assert yield_expr.into_code() == "yield from [10, 42]"
        ```
    """
    return UnaryOp(UnaryOpType.YieldFrom, e)