# 断言错误：路由函数必须是异步的

- **ID:** `python/starlette-missing-route-handler`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Starlette中的路由处理器被定义为同步函数而不是异步函数。

## 版本兼容性

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

## 解决方案

1. **Define the route handler as async** (95% 成功率)
   ```
   @app.route('/')
async def homepage(request):
    return PlainTextResponse('Hello')
   ```
2. **Use a sync wrapper if absolutely necessary** (50% 成功率)
   ```
   from starlette.responses import PlainTextResponse
@app.route('/')
def sync_homepage(request):
    return PlainTextResponse('Hello')
   ```

## 无效尝试

- **Adding @app.route without async** — Starlette requires async handlers; sync functions cause assertion errors. (90% 失败率)
- **Wrapping the function in a decorator that makes it async** — Complex and may not work correctly with Starlette's internals. (70% 失败率)
