Skip to content
Become a
Sponsor or follow @15r10nk on
or
for updates

# `` `inline_snapshot`

## `` `Category = Literal['update', 'fix', 'create', 'trim']` `module-attribute`

See categories

## `` `Snapshot`

Can be used to annotate function arguments which accept snapshot values.

You can annotate function arguments with `Snapshot[T]` to declare that a snapshot-value can be passed as function argument. `Snapshot[T]` is a type alias for `T`, which allows you to pass `int` values instead of `int` snapshots.

Example:

    from typing import Optional
    from inline_snapshot import snapshot, Snapshot

    # required snapshots

    def check_in_bounds(value, lower: Snapshot[int], upper: Snapshot[int]):
        assert lower <= value <= upper

    def test_numbers():
        for c in "hello world":
            check_in_bounds(ord(c), snapshot(32), snapshot(119))

        # use with normal values
        check_in_bounds(5, 0, 10)

    # optional snapshots

    def check_container(
        value,
        *,
        value_repr: Optional[Snapshot[str]] = None,
        length: Optional[Snapshot[int]] = None
    ):
        if value_repr is not None:
            assert repr(value) == value_repr

        if length is not None:
            assert len(value) == length

    def test_container():
        check_container([1, 2], value_repr=snapshot("[1, 2]"), length=snapshot(2))

        check_container({1, 1}, length=snapshot(1))

Source code in `.pybuild/cpython3_3.13_inline-snapshot/build/inline_snapshot/_types.py`

    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69

    class Snapshot(Generic[T]):
        """Can be used to annotate function arguments which accept snapshot
        values.

        You can annotate function arguments with `Snapshot[T]` to declare that a snapshot-value can be passed as function argument.
        `Snapshot[T]` is a type alias for `T`, which allows you to pass `int` values instead of `int` snapshots.

        Example:
        <!-- inline-snapshot: create fix trim first_block outcome-passed=2 -->
        ``` python
        from typing import Optional
        from inline_snapshot import snapshot, Snapshot

        # required snapshots

        def check_in_bounds(value, lower: Snapshot[int], upper: Snapshot[int]):
            assert lower <= value <= upper

        def test_numbers():
            for c in "hello world":
                check_in_bounds(ord(c), snapshot(32), snapshot(119))

            # use with normal values
            check_in_bounds(5, 0, 10)

        # optional snapshots

        def check_container(
            value,
            *,
            value_repr: Optional[Snapshot[str]] = None,
            length: Optional[Snapshot[int]] = None
        ):
            if value_repr is not None:
                assert repr(value) == value_repr

            if length is not None:
                assert len(value) == length

        def test_container():
            check_container([1, 2], value_repr=snapshot("[1, 2]"), length=snapshot(2))

            check_container({1, 1}, length=snapshot(1))
        ```
        """