15r10nk/inline-snapshot

internal assertion error when snapshot is used with qualified name

Closed this issue · 2 comments

Description

Consider the following example from the docs

import inline_snapshot import snapshot


def something():
    return 1548 * 18489


def test_something():
    assert something() == snapshot()

This works well: pytest tests/test_snapshot.py --inline-snapshot=create populates the snapshot.

However, when I change snapshot to the qualified name inline_snapshot.snapshot, an assertion error is raised from inline_snapshot._inline_snapshot. The error does not clearly indicate to the user what is going on. Preferably, I'd like qualified usage to be supported. But if it can't be done, I would expect a proper exception with a clear message.

import inline_snapshot


def something():
    return 1548 * 18489


def test_something():
    assert something() == inline_snapshot.snapshot()

pytest tests/test_snapshot.py --inline-snapshot=create

    def test_something():
>       assert something() == inline_snapshot.snapshot()

tests/test_snapshot.py:9:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../.virtualenvs/sandbox-311/lib/python3.11/site-packages/inline_snapshot/_inline_snapshot.py:618: in __call__
    return self.func(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

obj = <inline_snapshot._sentinels.Undefined object at 0x74aaf5708b50>

    @repr_wrapper
    def snapshot(obj: Any = undefined) -> Any:
        """`snapshot()` is a placeholder for some value.

        `pytest --inline-snapshot=create` will create the value which matches your conditions.

        >>> assert 5 == snapshot()
        >>> assert 5 <= snapshot()
        >>> assert 5 >= snapshot()
        >>> assert 5 in snapshot()

        `snapshot()[key]` can be used to create sub-snapshots.

        The generated value will be inserted as argument to `snapshot()`

        >>> assert 5 == snapshot(5)

        `snapshot(value)` has the semantic of an noop which returns `value`.
        """
        if not _active:
            if obj is undefined:
                raise AssertionError(
                    "your snapshot is missing a value run pytest with --inline-snapshot=create"
                )
            else:
                return obj

        frame = inspect.currentframe()
        assert frame is not None
        frame = frame.f_back
        assert frame is not None
        frame = frame.f_back
        assert frame is not None

        expr = Source.executing(frame)

        module = inspect.getmodule(frame)
        if module is not None and module.__file__ is not None:
            _files_with_snapshots.add(module.__file__)

        key = id(frame.f_code), frame.f_lasti

        if key not in snapshots:
            node = expr.node
            if node is None:
                # we can run without knowing of the calling expression but we will not be able to fix code
                snapshots[key] = Snapshot(obj, None)
            else:
                assert isinstance(node, ast.Call)
>               assert isinstance(node.func, ast.Name)
E               AssertionError: assert False
E                +  where False = isinstance(<ast.Attribute object at 0x74aaf530bb80>, <class 'ast.Name'>)
E                +    where <ast.Attribute object at 0x74aaf530bb80> = <ast.Call object at 0x74aaf530bbb0>.func
E                +    and   <class 'ast.Name'> = ast.Name

../../../.virtualenvs/sandbox-311/lib/python3.11/site-packages/inline_snapshot/_inline_snapshot.py:680: AssertionError

Version information

  • Python 3.11.8
  • inline-snapshot 0.8.1

Thank you, this was actually an old limitation which I was able to remove now.

fixed in 0.8.2