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

Can't use Request / Response subclasses with set_error_serializer #2397

Closed
davetapley opened this issue Oct 30, 2024 · 1 comment
Closed

Comments

@davetapley
Copy link
Contributor

davetapley commented Oct 30, 2024

def set_error_serializer(self, serializer: ErrorSerializer) -> None:

ErrorSerializer = Callable[['Request', 'Response', 'HTTPError'], None]

Isn't quite right if you use request_type= and/or response_type= in App initializer.
The set_error_serializer implementation gets the subclasses at run time, but:

Argument of type "(request: Request, response: Response, error: HTTPError) -> None" cannot be assigned to parameter "serializer" of type "ErrorSerializer" in function "set_error_serializer"
  Type "(request: Request, response: Response, error: HTTPError) -> None" is not assignable to type "ErrorSerializer"
    Parameter 1: type "Request" is incompatible with type "Request"
      "falcon.request.Request" is not assignable to "my.Request"
    Parameter 2: type "Response" is incompatible with type "Response"
      "falcon.response.Response" is not assignable to "my.Response"

Pylance[reportArgumentType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType)

I think an elegant solution would be to replace:

falcon/falcon/app.py

Lines 267 to 268 in 6d7a45b

_request_type: Type[Request]
_response_type: Type[Response]

With e.g.:

_REQ = TypeVar('_REQ', bound=Request)
_RES = TypeVar('_RES', bound=Response)

...

 _request_type: _REQ
 _response_type: _RES

...

    def set_error_serializer(self, serializer: ErrorSerializer[_REQ, _RES]) -> None:

Then:

ErrorSerializer = Callable[['Request', 'Response', 'HTTPError'], None]

Becomes:

ErrorSerializer = Callable[[_REQ, _RES, 'HTTPError'], None]

Thoughts?

@davetapley
Copy link
Contributor Author

Whoops, dupe of #2372

@davetapley davetapley closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants