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

- **ID:** `python/starlette-typeerror-middleware-must-be-callable`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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

## 无效尝试

- **Passing a class directly instead of an instance** — Class itself is callable, but may not be the intended usage. (50% 失败率)
- **Using a string as middleware** — String is not callable. (90% 失败率)
