-
Notifications
You must be signed in to change notification settings - Fork 37
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
Conversation
poetry.lock
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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} | ||
) |
There was a problem hiding this comment.
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 }
There was a problem hiding this comment.
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"),
)
# [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))
🎉 This PR is included in version 4.2.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Description
bypass_custom_repr
to help with serializing objects with custom third party classrepr
implementationsreplacer
parameter to thepath_type
matcher signature to allow custom replacement actions on matched property valuespath_value
to support regex match replacement on data valuesregex
flag from the matcher to simplify the api, since the matcher and string compare serve equivalent functionsRelated Issues
Checklist
Additional Comments
No additional comments