Ellipsis error when using starlette
Hi team,
As discussed with @stroxler , I have an issue with Pyrefly check when using the app.add_middleware(TimerMiddleware) function. Pyrefly returns Expected Ellipsis to be a ParamSpec value in function starlette.applications.Starlette.add_middleware [bad-argument-type] , while the signature of the function from starlette is class _MiddlewareFactory(Protocol[P]): def __call__(self, app: ASGIApp, /, *args: P.args, **kwargs: P.kwargs) -> ASGIApp: ... # pragma: no cover. Please find a sample of code you can use for reproductability:
from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware
import time
app = FastAPI()
# Custom middleware class
class TimerMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
start_time = time.time()
response = await call_next(request)
duration = time.time() - start_time
print(f"{request.method} {request.url.path} took {duration:.4f}s")
return response
# Register middleware using add_middleware
app.add_middleware(TimerMiddleware)
# Sample endpoint
@app.get("/hello")
async def hello():
return {"message": "Hello with class-based middleware!"}
Could you please advise on how to handle such a case? Thanks in advance for your help and let me know if you need more information.
Cheers, Thibault
I've also encountered a similar issue with just this:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
The error message is
Expected `Ellipsis` to be a ParamSpec value in function `starlette.applications.Starlette.add_middleware` [bad-argument-type]
Minimal repro in the sandbox: https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgDRQAKRIREAygtQMYBQPDUALyNmrDtwAUAIgZSAlDwAm1YFGABGCcABchUuSrUA2g3qoYAXXoAqZmgDOuhgDo79m9YDWAdzdPnPm5yUAC0AHyYKDDaPFBQINQwAK4gKGoStiAOHoFZ9sEA1OmZ2VDWXr55CjzAAEy6xGSUNEbObWZRFnwaWrX06nJAA
Whoops removing myself as assignee - I was going to do this during sprints, then I got pulled into other things
This will be fixed shortly and included in the next release.