getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.87k stars 491 forks source link

TypeError when instantiating Starlette middleware with positional arguments #3246

Open phooijenga opened 3 months ago

phooijenga commented 3 months ago

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.7.1

Steps to Reproduce

I have a FastAPI app with some middleware, one of which I want to pass a positional argument, like this:

app = FastAPI(
    middleware=[
        Middleware(RequestLoggerMiddleware, "app.request"),
    ],
)

This works fine when Sentry is not enabled, but causes a TypeError when it is. The problem does not occur when I use a keyword argument instead.

Expected Result

I expect the app to work the same way with and without Sentry enabled.

Actual Result

File "/app/app.py", line 103, in <module>
  middleware=[Middleware(RequestLoggerMiddleware, "app.request")],
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: patch_middlewares.<locals>._sentry_middleware_init() takes 2 positional arguments but 3 were given
sentrivana commented 3 months ago

Hey @phooijenga, thanks for reporting, this shouldn't happen. Please use keyword args until we fix this.

antonpirker commented 2 months ago

Hey @phooijenga,

can you tell me what version of FastAPI and Starlette your are using and can you show me what RequestLoggerMiddleware looks like?

This would help a lot!

phooijenga commented 2 months ago

It still happens with the latest versions available. I've put together a Gist with a self-contained reproduction.

antonpirker commented 2 months ago

Amazing @phooijenga !

I will have a look!