You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ruff (0.8.3) FAST002 produces false positives and suggests incorrect Annotated format with FastAPI params having default values. Following example can not be converted into the Annotated format as FastAPI does not support this format with params having default values. Also --unsafe-fixes fixes it to the format the FastAPI wont accept.
But that is not compatible with FastAPI. It fails deeply with an AssertionError:
deftest_fast002_bug():
fromtypingimportAnnotatedfromfastapiimportFastAPI, Queryapp=FastAPI()
# AssertionError: `Query` default value cannot be set in `Annotated` for 'echo'. Set the default value with `=` instead.@app.get("/test")defhandler(echo: Annotated[str, Query("")]):
returnecho
Used FastAPI 0.115.5.
The rule is pretty nice otherwise so thanks for it! Hopefully we get it fixed.
The text was updated successfully, but these errors were encountered:
Thanks for opening the issue. I think this is a true positive, but the fix is incorrect. The Query default value cannot be used inside Annotated according to FastAPI's documentation. Instead, a regular default value should be used. So the proper fix for your example is to rewrite it to
deftest_fast002_bug():
fromtypingimportAnnotatedfromfastapiimportFastAPI, Queryapp=FastAPI()
# AssertionError: `Query` default value cannot be set in `Annotated` for 'echo'. Set the default value with `=` instead.@app.get("/test")defhandler(echo: Annotated[str, Query()] =""):
returnecho
Ruff (0.8.3)
FAST002
produces false positives and suggests incorrectAnnotated
format with FastAPI params having default values. Following example can not be converted into theAnnotated
format as FastAPI does not support this format with params having default values. Also--unsafe-fixes
fixes it to the format the FastAPI wont accept.Ruff FAST002 violation:
But that is not compatible with FastAPI. It fails deeply with an AssertionError:
Used FastAPI 0.115.5.
The rule is pretty nice otherwise so thanks for it! Hopefully we get it fixed.
The text was updated successfully, but these errors were encountered: