python
type_error
ai_generated
true
TypeError: 'str' object cannot be interpreted as an integer
ID: python/starlette-route-parameter-type-mismatch
80%Fix Rate
82%Confidence
0Evidence
2024-10-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
A route parameter expected as an integer is passed as a string without conversion.
generic中文
预期为整数的路由参数以字符串形式传递,未进行转换。
Workarounds
-
90% success Convert the parameter to int explicitly
async def get_item(item_id: str): item_id = int(item_id) -
95% success Use Starlette's path converter for int
@app.route('/items/{item_id:int}') async def get_item(item_id: int): ...
Dead Ends
Common approaches that don't work:
-
Using str() on the parameter
80% fail
Doesn't convert to int; keeps it as string.
-
Ignoring the type and using the string directly
70% fail
May cause errors in logic expecting an integer.