Middleware _MiddlewareClass mypy typing PR2381 #2581
-
I have some custom middleware a bit like class TraceTokenLoggerMiddleware:
def __init__(self, app, logger: IAsyncLogger):
self._app = app
self._async_logger = logger
async def __call__(self, scope: dict, receive, send):
# do something
pass Then creating my app: App(
...
middleware=[
Middleware(TraceTokenLoggerMiddleware, logger=async_logger)
]
) My project uses #2381 changed I upgraded to Now when I run
Please advise how to stop this error or if a fix is needed in It seems like I ought to make |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
FWIW if I do define So I suggest |
Beta Was this translation helpful? Give feedback.
-
@pawelrubin and @adriangb , are you able to comment / help please? |
Beta Was this translation helpful? Give feedback.
-
You should be able to fix the issue by providing complete type annotations to your from typing import Any
from starlette.middleware import Middleware
from starlette.types import ASGIApp, Receive, Scope, Send
IAsyncLogger = Any # whatever the type is
class TraceTokenLoggerMiddleware:
def __init__(self, app: ASGIApp, logger: IAsyncLogger) -> None:
self._app = app
self._async_logger = logger
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
# do something
pass
async_logger: IAsyncLogger = NotImplemented
Middleware(TraceTokenLoggerMiddleware, logger=async_logger) # no errors here |
Beta Was this translation helpful? Give feedback.
You should be able to fix the issue by providing complete type annotations to your
TraceTokenLoggerMiddleware
: