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

feat(serializer): add snapshot regex value matcher and bypass custom repr helper #791

Merged
merged 12 commits into from Aug 21, 2023

Conversation

iamogbz
Copy link
Collaborator

@iamogbz iamogbz commented Aug 19, 2023

Description

  • Adds function bypass_custom_repr to help with serializing objects with custom third party class repr implementations
  • Adds the replacer parameter to the path_type matcher signature to allow custom replacement actions on matched property values
  • Adds matcher helper path_value to support regex match replacement on data values
  • Deprecates the regex flag from the matcher to simplify the api, since the matcher and string compare serve equivalent functions

Related Issues

Checklist

  • This PR has sufficient documentation.
  • This PR has sufficient test coverage.
  • This PR title satisfies semantic convention.

Additional Comments

No additional comments

@iamogbz iamogbz requested a review from noahnu August 19, 2023 18:30
@iamogbz iamogbz marked this pull request as ready for review August 19, 2023 18:51
poetry.lock Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly a mismatch in poetry versions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran inv install again, jic.

Comment on lines 357 to 361
def bypass_custom_repr(cls, data: Any) -> "tuple[Any, ...]":
attr_names = list(cls.object_attrs(data))
return collections.namedtuple(data.__class__.__name__, attr_names)(
**{prop: getattr(data, prop) for prop in attr_names}
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about how the namedtuple serialization is exactly what we want when it came to #784.

Regarding the name of the method, this does not really "bypass" custom repr, it's just a utility for constructing a named tuple based on an object's attrs. If it's "bypassing" some serialization, I'd expect it to somehow "communicate" with the serializer itself. I.e. a model where it sets a marker on an object so that the serializer knows to handle it differently.

Instead, I'd rename this to more explicitly be a utility that constructs a named tuple from a custom object. Perhaps def make_named_tuple or def create_named_tuple / def convert_obj_to_named_tuple.

If the idea is to use this in conjunction with the replacer to provide a more elegant solution to the msgspec serializer issue, we'd want a mechanism to apply the replacer to all snapshots. It gets verbose having to specify the replacer per use.

Another way I was thinking of approaching the problem in the long run was to expose some static "registry" / map of custom class types -> serializers to register with the class / or to override.

e.g.

class Serializer(AmberDataSerializer):
    custom_classes = { msgspec.Struct: some_serializer }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name to object_as_named_tuple_class.

It gets verbose having to specify the replacer per use.

True, the replacer is a low hanging fruit that allows both functionality I wanted to add here. I like the custom_classes idea, it could even be used in the __matcher assertion property.

Another option would be to allow a change to the _matcher property that won't be reverted after each assertion.

@pytest.fixture
def snapshot_bypass_my_custom_repr(snapshot):
  snapshot._matcher = path_type(
    types=(MyCustomReprClass,),
    replacer=lambda data, _: AmberDataSerializer.object_as_named_tuple(data),
  )
  return snapshot

def test_snapshot_object_as_named_tuple_class(snapshot_bypass_my_custom_repr):
    assert MyCustomReprClass() == snapshot_bypass_my_custom_repr(
        exclude=props("prop1"),
    )

src/syrupy/matchers.py Outdated Show resolved Hide resolved
src/syrupy/matchers.py Outdated Show resolved Hide resolved
@iamogbz iamogbz requested a review from noahnu August 20, 2023 19:17
@noahnu noahnu merged commit 3ac2ce8 into main Aug 21, 2023
14 checks passed
@noahnu noahnu deleted the custom-matcher branch August 21, 2023 11:51
tophat-opensource-bot pushed a commit that referenced this pull request Aug 21, 2023
# [4.2.0](v4.1.1...v4.2.0) (2023-08-21)

### Features

* **serializer:** add snapshot regex value matcher and bypass custom repr helper ([#791](#791)) ([3ac2ce8](3ac2ce8))
@tophat-opensource-bot
Copy link
Collaborator

🎉 This PR is included in version 4.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

3 participants