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

Any response type for Respose.serializer() #141

Closed
peterschutt opened this issue Jun 15, 2022 · 2 comments
Closed

Any response type for Respose.serializer() #141

peterschutt opened this issue Jun 15, 2022 · 2 comments
Labels
Enhancement This is a new feature or request

Comments

@peterschutt
Copy link
Contributor

peterschutt commented Jun 15, 2022

Response.serializer() passed to orjson.dumps() in Response.render():

    def render(self, content: Any) -> bytes:
        """Renders content into bytes"""
        try:
            if self.media_type == MediaType.JSON:
                return dumps(content, default=self.serializer, option=OPT_SERIALIZE_NUMPY | OPT_OMIT_MICROSECONDS)

Response.serializer() return type is dict[str, Any] but should be Any as overriding serializer() is the way to extend orjson for any type. Currently I need a #type: ignore for a subclass:

class Response(_Response):
    @staticmethod
    def serializer(value: Any) -> Any:  # type:ignore[override]
        if isinstance(value, pgproto.UUID):
            return str(value)
        return _Response.serializer(value)

Otherwise mypy gives us:

src/app/core/response.py:8: error: Signature of "serializer" incompatible with supertype "Response"  [override]
src/app/core/response.py:8: note:      Superclass:
src/app/core/response.py:8: note:          def serializer(value: Any) -> Dict[str, Any]
src/app/core/response.py:8: note:      Subclass:
src/app/core/response.py:8: note:          def serializer(value) -> Any
@Goldziher
Copy link
Contributor

fair enough, also wouldn't affect existing users negatively so go ahead.

@Goldziher Goldziher added the Enhancement This is a new feature or request label Jun 15, 2022
@peterschutt
Copy link
Contributor Author

I'm not getting the error now, so perhaps an xy problem. I'll reopen if I come across it again.

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

No branches or pull requests

2 participants