sanic icon indicating copy to clipboard operation
sanic copied to clipboard

mypy error type for async get function in HTTPMethodView

Open FirePanda169 opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

Mypy throws an error overriding the signature of the get method, as it is described only for synchronous execution.

error: Signature of "get" incompatible with supertype "HTTPMethodView"  [override]
note:      Superclass:
note:          Callable[..., Any] | None
note:      Subclass:
note:          def get(self, request: Request[Any, Any]) -> Coroutine[Any, Any, HTTPResponse]

I suggest changing the description of the get method /sanic/views.py to:

get: Union[Callable[..., Any], Coroutine[..., Any], None]

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Linux

Sanic Version

23.12.0

Additional context

No response

FirePanda169 avatar Feb 14 '24 08:02 FirePanda169

PR welcome :wink:

ahopkins avatar Apr 01 '24 13:04 ahopkins

That did not solve the problem for me. mypy still complains and I have no idea why.

It works if None is removed from the type definition.

 get: Union[Callable[..., Any], Coroutine[..., Any]]

I cannot explain this behavior.

hac-v avatar Apr 10 '24 15:04 hac-v

@ahopkins The problem is here when we are using mypy and try to implement get method https://github.com/sanic-org/sanic/blob/da1c6465852c8fc0b77ee2618d845665292045e0/sanic/views.py#L118

If there is no handler defined then server is having Attribute error anyways. Example:

AttributeError: type object 'MyView' has no attribute 'get'

Proposed solution:

- get: Optional[Callable[..., Any]] +get: Callable[..., Any]

  • Kindly suggest better solution or if there is a way to implement get in class based view if we are using mypy.

swtnk avatar Jul 20 '24 04:07 swtnk