python type_error ai_generated true

类型错误:中间件必须是可调用的

TypeError: Middleware must be callable

ID: python/starlette-typeerror-middleware-must-be-callable

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-09-01首次发现

版本兼容性

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

根因分析

传递了一个不可调用的对象(例如没有__call__方法的类实例)作为中间件。

English

Passing a non-callable object (e.g., a class instance without __call__) as middleware.

generic

解决方案

  1. 90% 成功率 Pass a middleware class or function
    app.add_middleware(MyMiddleware)
  2. 85% 成功率 Ensure middleware class has __call__ method
    class MyMiddleware:
        async def __call__(self, scope, receive, send):
            # implementation
            pass

无效尝试

常见但无效的做法:

  1. Passing a class directly instead of an instance 50% 失败

    Class itself is callable, but may not be the intended usage.

  2. Using a string as middleware 90% 失败

    String is not callable.