Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"exclude" filters don't appear to work correctly with msgspec structs #784

Closed
BenGatewood opened this issue Aug 14, 2023 · 4 comments
Closed
Labels
feature request New feature or request

Comments

@BenGatewood
Copy link

Describe the bug

When I try and exclude attributes from a custom class that inherits from msgspec.Struct the syntax/approach from the docs doesn't seem to produce the desired behaviour and the excluded attributes remain in the snapshot

To reproduce


from syrupy.filters import props


class Base(msgspec.Struct):
    foo: str
    bar: int


def test_base(snapshot):
    b = Base(
        foo="baz",
        bar=5,
    )

    assert b == snapshot(exclude=props("bar"))

Expected behavior

From the above example, I would expect a snapshot like:

# name: test_base
  Base(foo='baz')
# ---

Environment (please complete the following information):

  • OS: Mac OSX 13.5
  • Syrupy Version: 4.0.8
  • Python Version: 3.11

Additional context

Thanks for looking!

@noahnu noahnu added the bug Something isn't working label Aug 14, 2023
@noahnu
Copy link
Collaborator

noahnu commented Aug 15, 2023

Custom classes are serialized by calling repr, see: https://github.com/tophat/syrupy/blob/2ed84ea8e2b9dfa01cd4788f9787809ffc6b2648/src/syrupy/extensions/amber/serializer.py#L373

Since repr returns a string, we don't have a way to modify the result of calling repr to apply an exclude. You could extend the serializer to support msgspec.Struct:

import pytest
from typing import Any
from syrupy.filters import props
from syrupy.extensions.amber import AmberSnapshotExtension, AmberDataSerializer

import msgspec

class MySpec(msgspec.Struct):
    a: str
    b: int


class ExtendedAmberDataSerializer(AmberDataSerializer):
    @classmethod
    def serialize_unknown(cls, data: Any, *, depth: int = 0, **kwargs: Any) -> str:
        if isinstance(data, (msgspec.Struct,)):
            return cls.serialize_custom_iterable(
                data=data,
                resolve_entries=(
                    cls.sort(data.__struct_fields__),
                    lambda o, p: getattr(o, str(p)),
                    None
                ),
                separator="=",
                **kwargs
            )
        return super().serialize_unknown(data, depth=depth, **kwargs)


class ExtendedAmberSnapshotExtension(AmberSnapshotExtension):
    serializer_class = ExtendedAmberDataSerializer


@pytest.fixture
def snapshot(snapshot):
    return snapshot.use_extension(ExtendedAmberSnapshotExtension)

def test_case(snapshot):
    assert MySpec(a="word", b=3) == snapshot(exclude=props("a"))

I acknowledge the serializer can be improved here.. I think we could expose an alternative to serialize_dict to handle repr a bit better


Note that "serialize_custom_iterable" requires syrupy v4.1.0 (just released today).

@noahnu noahnu added feature request New feature or request and removed bug Something isn't working labels Aug 15, 2023
@noahnu
Copy link
Collaborator

noahnu commented Aug 16, 2023

Updated my example using serialize_custom_iterable which I've exposed in v4.1.0.

@BenGatewood
Copy link
Author

Nice one - thanks! I'll have a go with this method for the moment

@noahnu
Copy link
Collaborator

noahnu commented Aug 17, 2023

Will close out this issue then. Feel free to comment and I'll re-open if there are other ways we can make this simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants