python type_error ai_generated true

类型错误:'Middleware' 对象不可调用

TypeError: 'Middleware' object is not callable

ID: python/starlette-middleware-type-error

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-06-15首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

中间件类未实例化或未作为可调用对象使用。

English

A middleware class is not instantiated or not used as a callable.

generic

解决方案

  1. 90% 成功率 Instantiate the middleware before adding it to the app.
    from starlette.middleware import Middleware
    middleware = Middleware(SomeMiddleware)
    app.add_middleware(middleware)
  2. 95% 成功率 Use the middleware class directly with app.add_middleware.
    app.add_middleware(SomeMiddleware)

无效尝试

常见但无效的做法:

  1. Passing the middleware class directly without instantiation. 80% 失败

    The middleware must be an instance or a callable; a class is not callable in this context.

  2. Importing the middleware incorrectly from a wrong module. 60% 失败

    The imported object may not be a middleware at all.