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

More generic serialization of function types #808

Closed
ManiacDC opened this issue Sep 12, 2023 · 4 comments · Fixed by #823
Closed

More generic serialization of function types #808

ManiacDC opened this issue Sep 12, 2023 · 4 comments · Fixed by #823
Labels
feature request New feature or request released

Comments

@ManiacDC
Copy link
Contributor

ManiacDC commented Sep 12, 2023

Is your feature request related to a problem? Please describe.

When trying to compare snapshots with attributes that contain functions, the function repr is returned as
<function request_response.<locals>.app at 0x7fbf103dc0e0>

The "at 0x..." part changes with each run of the test, I'm assuming it's a memory location.

Describe the solution you'd like

I'd like the at 0x... part to be removed.

Describe alternatives you've considered

I've worked around this using a matcher:

    def function_matcher(data, path):
        if isinstance(data, FunctionType):
            return f"<function '{data.__qualname__}'>"
        else:
            return data

Additional context

This could be implemented in the ambr and json snapshot extensions.

In ambr (serializer.py):

...
        elif isinstance(data, FunctionType):
            serialize_method = cls.serialize_function
...
        
    @classmethod
    def serialize_function(cls, data: FunctionType, *, depth: int = 0, **kwargs: Any) -> str:
        return cls.__serialize_plain(data=f"<{FunctionType.__name__} {data.__qualname__}>", depth=depth)

In json:

        if isinstance(data, FunctionType):
            return f"<{FunctionType.__name__} '{data.__qualname__}'>"
@noahnu noahnu added the feature request New feature or request label Sep 14, 2023
@noahnu
Copy link
Collaborator

noahnu commented Sep 14, 2023

<...> is the old amber syntax and didn't support syntax highlighting too well.

In the latest version of syrupy, classes are serialized as MyClass(...). We can probably do something similar for functions.

Would you be interested in opening a PR and contributing?

@ManiacDC
Copy link
Contributor Author

Sure, I'll try to get to that soon.

@ManiacDC
Copy link
Contributor Author

@noahnu sorry for the delay, I've opened a PR to address this issue.

@tophat-opensource-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 4.6.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
feature request New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants