prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Improve type of `MiddlewareType`
  ### Is there an existing issue for this?
  
  - [X] I have searched the existing issues
  
  ### Is your feature request related to a problem? Please describe.
  
  When using a custom Request class and type hinting the middleware with that custom Request class, type checkers complain that the argument types of the middleware function is invalid. 
  
  ```python
  
  from sanic import Request, Sanic
  
  class MyRequest(Request):
      ...
  
  async def some_middleware(request: MyRequest) -> None:
      ...
  
  app = Sanic("trial-app")
  
  # This raises a type error.
  app.register_middleware(some_middleware, "request")
  
  # Pyright Error
  # Argument of type "(request: MyRequest) -> Coroutine[Any, Any, None]" cannot be assigned to parameter
  # "middleware" of type "MiddlewareType | Middleware" in function "register_middleware"
  # Type "(request: MyRequest) -> Coroutine[Any, Any, None]" cannot be assigned to type "MiddlewareType | Middleware"
  #  Type "(request: MyRequest) -> Coroutine[Any, Any, None]" cannot be assigned to type "RequestMiddlewareType"
  #      Parameter 1: type "Request" cannot be assigned to type "MyRequest"
  #        "Request" is incompatible with "MyRequest"
  #    Type "(request: MyRequest) -> Coroutine[Any, Any, None]" cannot be assigned to type "ResponseMiddlewareType"
  #      Function accepts too many positional parameters; expected 1 but received 2
  #        Parameter 1: type "Request" cannot be assigned to type "MyRequest"
  #          "Request" is incompatible with "MyRequest"
  
  ```
  
  ### Describe the solution you'd like
  
  Using a subclass of Request shouldn't raise this error by the type checkers.
  
  ### Additional context
  
  I think the fix is to make the `Request` type in `MiddlewareType` in [`handler_types`](https://github.com/sanic-org/sanic/blob/main/sanic/models/handler_types.py) a generic with the generic being bound to `Request` like it's done for the `Sanic` type. 
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `sanic/models/handler_types.py`
  Content:
  ```
  1 from asyncio.events import AbstractEventLoop
  2 from typing import Any, Callable, Coroutine, Optional, TypeVar, Union
  3 
  4 import sanic
  5 
  6 from sanic.request import Request
  7 from sanic.response import BaseHTTPResponse, HTTPResponse
  8 
  9 
  10 Sanic = TypeVar("Sanic", bound="sanic.Sanic")
  11 
  12 MiddlewareResponse = Union[
  13     Optional[HTTPResponse], Coroutine[Any, Any, Optional[HTTPResponse]]
  14 ]
  15 RequestMiddlewareType = Callable[[Request], MiddlewareResponse]
  16 ResponseMiddlewareType = Callable[
  17     [Request, BaseHTTPResponse], MiddlewareResponse
  18 ]
  19 ErrorMiddlewareType = Callable[
  20     [Request, BaseException], Optional[Coroutine[Any, Any, None]]
  21 ]
  22 MiddlewareType = Union[RequestMiddlewareType, ResponseMiddlewareType]
  23 ListenerType = Union[
  24     Callable[[Sanic], Optional[Coroutine[Any, Any, None]]],
  25     Callable[[Sanic, AbstractEventLoop], Optional[Coroutine[Any, Any, None]]],
  26 ]
  27 RouteHandler = Callable[..., Coroutine[Any, Any, Optional[HTTPResponse]]]
  28 SignalHandler = Callable[..., Coroutine[Any, Any, None]]
  29
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/sanic/models/handler_types.py b/sanic/models/handler_types.py
  --- a/sanic/models/handler_types.py
  +++ b/sanic/models/handler_types.py
  @@ -3,11 +3,12 @@
   
   import sanic
   
  -from sanic.request import Request
  +from sanic import request
   from sanic.response import BaseHTTPResponse, HTTPResponse
   
   
   Sanic = TypeVar("Sanic", bound="sanic.Sanic")
  +Request = TypeVar("Request", bound="request.Request")
   
   MiddlewareResponse = Union[
       Optional[HTTPResponse], Coroutine[Any, Any, Optional[HTTPResponse]]
  
verification_info: {"golden_diff": "diff --git a/sanic/models/handler_types.py b/sanic/models/handler_types.py\n--- a/sanic/models/handler_types.py\n+++ b/sanic/models/handler_types.py\n@@ -3,11 +3,12 @@\n \n import sanic\n \n-from sanic.request import Request\n+from sanic import request\n from sanic.response import BaseHTTPResponse, HTTPResponse\n \n \n Sanic = TypeVar(\"Sanic\", bound=\"sanic.Sanic\")\n+Request = TypeVar(\"Request\", bound=\"request.Request\")\n \n MiddlewareResponse = Union[\n     Optional[HTTPResponse], Coroutine[Any, Any, Optional[HTTPResponse]]\n", "issue": "Improve type of `MiddlewareType`\n### Is there an existing issue for this?\n\n- [X] I have searched the existing issues\n\n### Is your feature request related to a problem? Please describe.\n\nWhen using a custom Request class and type hinting the middleware with that custom Request class, type checkers complain that the argument types of the middleware function is invalid. \r\n\r\n```python\r\n\r\nfrom sanic import Request, Sanic\r\n\r\nclass MyRequest(Request):\r\n    ...\r\n\r\nasync def some_middleware(request: MyRequest) -> None:\r\n    ...\r\n\r\napp = Sanic(\"trial-app\")\r\n\r\n# This raises a type error.\r\napp.register_middleware(some_middleware, \"request\")\r\n\r\n# Pyright Error\r\n# Argument of type \"(request: MyRequest) -> Coroutine[Any, Any, None]\" cannot be assigned to parameter\r\n# \"middleware\" of type \"MiddlewareType | Middleware\" in function \"register_middleware\"\r\n# Type \"(request: MyRequest) -> Coroutine[Any, Any, None]\" cannot be assigned to type \"MiddlewareType | Middleware\"\r\n#  Type \"(request: MyRequest) -> Coroutine[Any, Any, None]\" cannot be assigned to type \"RequestMiddlewareType\"\r\n#      Parameter 1: type \"Request\" cannot be assigned to type \"MyRequest\"\r\n#        \"Request\" is incompatible with \"MyRequest\"\r\n#    Type \"(request: MyRequest) -> Coroutine[Any, Any, None]\" cannot be assigned to type \"ResponseMiddlewareType\"\r\n#      Function accepts too many positional parameters; expected 1 but received 2\r\n#        Parameter 1: type \"Request\" cannot be assigned to type \"MyRequest\"\r\n#          \"Request\" is incompatible with \"MyRequest\"\r\n\r\n```\n\n### Describe the solution you'd like\n\nUsing a subclass of Request shouldn't raise this error by the type checkers.\n\n### Additional context\n\nI think the fix is to make the `Request` type in `MiddlewareType` in [`handler_types`](https://github.com/sanic-org/sanic/blob/main/sanic/models/handler_types.py) a generic with the generic being bound to `Request` like it's done for the `Sanic` type. \n", "before_files": [{"content": "from asyncio.events import AbstractEventLoop\nfrom typing import Any, Callable, Coroutine, Optional, TypeVar, Union\n\nimport sanic\n\nfrom sanic.request import Request\nfrom sanic.response import BaseHTTPResponse, HTTPResponse\n\n\nSanic = TypeVar(\"Sanic\", bound=\"sanic.Sanic\")\n\nMiddlewareResponse = Union[\n    Optional[HTTPResponse], Coroutine[Any, Any, Optional[HTTPResponse]]\n]\nRequestMiddlewareType = Callable[[Request], MiddlewareResponse]\nResponseMiddlewareType = Callable[\n    [Request, BaseHTTPResponse], MiddlewareResponse\n]\nErrorMiddlewareType = Callable[\n    [Request, BaseException], Optional[Coroutine[Any, Any, None]]\n]\nMiddlewareType = Union[RequestMiddlewareType, ResponseMiddlewareType]\nListenerType = Union[\n    Callable[[Sanic], Optional[Coroutine[Any, Any, None]]],\n    Callable[[Sanic, AbstractEventLoop], Optional[Coroutine[Any, Any, None]]],\n]\nRouteHandler = Callable[..., Coroutine[Any, Any, Optional[HTTPResponse]]]\nSignalHandler = Callable[..., Coroutine[Any, Any, None]]\n", "path": "sanic/models/handler_types.py"}], "after_files": [{"content": "from asyncio.events import AbstractEventLoop\nfrom typing import Any, Callable, Coroutine, Optional, TypeVar, Union\n\nimport sanic\n\nfrom sanic import request\nfrom sanic.response import BaseHTTPResponse, HTTPResponse\n\n\nSanic = TypeVar(\"Sanic\", bound=\"sanic.Sanic\")\nRequest = TypeVar(\"Request\", bound=\"request.Request\")\n\nMiddlewareResponse = Union[\n    Optional[HTTPResponse], Coroutine[Any, Any, Optional[HTTPResponse]]\n]\nRequestMiddlewareType = Callable[[Request], MiddlewareResponse]\nResponseMiddlewareType = Callable[\n    [Request, BaseHTTPResponse], MiddlewareResponse\n]\nErrorMiddlewareType = Callable[\n    [Request, BaseException], Optional[Coroutine[Any, Any, None]]\n]\nMiddlewareType = Union[RequestMiddlewareType, ResponseMiddlewareType]\nListenerType = Union[\n    Callable[[Sanic], Optional[Coroutine[Any, Any, None]]],\n    Callable[[Sanic, AbstractEventLoop], Optional[Coroutine[Any, Any, None]]],\n]\nRouteHandler = Callable[..., Coroutine[Any, Any, Optional[HTTPResponse]]]\nSignalHandler = Callable[..., Coroutine[Any, Any, None]]\n", "path": "sanic/models/handler_types.py"}]}