Skip to content

synt.expr.subscript ¤

slice_ module-attribute ¤

slice_ = Slice

Alias Slice.

Notes

slice is a built-in type in Python, so it's renamed to slice_ with a suffix.

Subscript ¤

Bases: Expression

Subscript operation.

References

Subscription.

Source code in synt/expr/subscript.py
class Subscript(expr.Expression):
    r"""Subscript operation.

    References:
        [Subscription](https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-subscription).
    """

    target: expr.Expression
    """Target of the operation."""
    slices: list[Slice | expr.Expression]
    """Slices to index the target."""

    expr_type = expr.ExprType.Subscript
    precedence = expr.ExprPrecedence.Call

    def __init__(
        self, target: expr.IntoExpression, slices: list[Slice | expr.IntoExpression]
    ):
        """Initialize a `Subscript` operation.

        Args:
            target: Target of the operation.
            slices: Slices to index the target.
        """
        self.target = target.into_expression()
        if (
            self.target.precedence > self.precedence
        ):  # special rule here, attr call doesn't need a wrap
            self.target = self.target.wrapped()
        self.slices = [
            s if isinstance(s, Slice) else s.into_expression() for s in slices
        ]

    def into_code(self) -> str:
        slice_text = ", ".join(x.into_code() for x in self.slices)
        return f"{self.target.into_code()}[{slice_text}]"

expr_type class-attribute instance-attribute ¤

expr_type = Subscript

precedence class-attribute instance-attribute ¤

precedence = Call

target instance-attribute ¤

target: Expression = into_expression()

Target of the operation.

slices instance-attribute ¤

1
2
3
4
slices: list[Slice | Expression] = [
    s if isinstance(s, Slice) else into_expression()
    for s in slices
]

Slices to index the target.

__init__ ¤

1
2
3
4
__init__(
    target: IntoExpression,
    slices: list[Slice | IntoExpression],
)

Initialize a Subscript operation.

Parameters:

Name Type Description Default
target IntoExpression

Target of the operation.

required
slices list[Slice | IntoExpression]

Slices to index the target.

required
Source code in synt/expr/subscript.py
def __init__(
    self, target: expr.IntoExpression, slices: list[Slice | expr.IntoExpression]
):
    """Initialize a `Subscript` operation.

    Args:
        target: Target of the operation.
        slices: Slices to index the target.
    """
    self.target = target.into_expression()
    if (
        self.target.precedence > self.precedence
    ):  # special rule here, attr call doesn't need a wrap
        self.target = self.target.wrapped()
    self.slices = [
        s if isinstance(s, Slice) else s.into_expression() for s in slices
    ]

into_code ¤

into_code() -> str
Source code in synt/expr/subscript.py
def into_code(self) -> str:
    slice_text = ", ".join(x.into_code() for x in self.slices)
    return f"{self.target.into_code()}[{slice_text}]"

Slice ¤

Bases: IntoCode

Slice constructor.

1
2
3
4
5
foo[
    5     # lower
    :10   # upper
    :2    # step (optional)
]

Examples:

1
2
3
4
sl = slice_(litint(5), litint(10))
assert sl.into_code() == "5:10"
sl = slice_(litint(5), litint(10), id_("a"))
assert sl.into_code() == "5:10:a"
References

Slice.

Source code in synt/expr/subscript.py
class Slice(code.IntoCode):
    r"""Slice constructor.

    ```python
    foo[
        5     # lower
        :10   # upper
        :2    # step (optional)
    ]
    ```

    Examples:
        ```python
        sl = slice_(litint(5), litint(10))
        assert sl.into_code() == "5:10"
        sl = slice_(litint(5), litint(10), id_("a"))
        assert sl.into_code() == "5:10:a"
        ```

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

    lower: expr.Expression
    """Lower bound of the slice."""
    upper: expr.Expression
    """Upper bound of the slice."""
    step: expr.Expression | None
    """Step of the slice."""

    def __init__(
        self,
        lower: expr.IntoExpression,
        upper: expr.IntoExpression,
        step: expr.IntoExpression | None = None,
    ):
        """Initialize the slice.

        Args:
            lower: Lower bound of the slice.
            upper: Upper bound of the slice.
            step: Step of the slice.
        """
        self.lower = lower.into_expression()
        self.upper = upper.into_expression()
        self.step = step.into_expression() if step else None

    def into_code(self) -> str:
        if self.step:
            return f"{self.lower.into_code()}:{self.upper.into_code()}:{self.step.into_code()}"
        else:
            return f"{self.lower.into_code()}:{self.upper.into_code()}"

lower instance-attribute ¤

lower: Expression = into_expression()

Lower bound of the slice.

upper instance-attribute ¤

upper: Expression = into_expression()

Upper bound of the slice.

step instance-attribute ¤

1
2
3
step: Expression | None = (
    into_expression() if step else None
)

Step of the slice.

__init__ ¤

1
2
3
4
5
__init__(
    lower: IntoExpression,
    upper: IntoExpression,
    step: IntoExpression | None = None,
)

Initialize the slice.

Parameters:

Name Type Description Default
lower IntoExpression

Lower bound of the slice.

required
upper IntoExpression

Upper bound of the slice.

required
step IntoExpression | None

Step of the slice.

None
Source code in synt/expr/subscript.py
def __init__(
    self,
    lower: expr.IntoExpression,
    upper: expr.IntoExpression,
    step: expr.IntoExpression | None = None,
):
    """Initialize the slice.

    Args:
        lower: Lower bound of the slice.
        upper: Upper bound of the slice.
        step: Step of the slice.
    """
    self.lower = lower.into_expression()
    self.upper = upper.into_expression()
    self.step = step.into_expression() if step else None

into_code ¤

into_code() -> str
Source code in synt/expr/subscript.py
def into_code(self) -> str:
    if self.step:
        return f"{self.lower.into_code()}:{self.upper.into_code()}:{self.step.into_code()}"
    else:
        return f"{self.lower.into_code()}:{self.upper.into_code()}"