Update dependency fastapi to v0.95.1
This MR contains the following updates:
Package | Type | Update | Change |
---|---|---|---|
fastapi | dependencies | minor |
0.92.0 -> 0.95.1
|
Release Notes
tiangolo/fastapi
v0.95.1
Fixes
-
🐛 Fix usingAnnotated
in routers or path operations decorated multiple times. MR #9315 by @sharonyogev.
Docs
-
🌐 🔠 📄 🐢 Translate docs to Emoji 🥳🎉 💥 🤯 🤯. MR #5385 by @LeeeeT. -
📝 Add notification message warning about old versions of FastAPI not supportingAnnotated
. MR #9298 by @grdworkin. -
📝 Fix typo indocs/en/docs/advanced/behind-a-proxy.md
. MR #5681 by @Leommjr. -
✏ Fix wrong import from typing module in Persian translations fordocs/fa/docs/index.md
. MR #6083 by @Kimiaattaei. -
✏ ️ Fix format, remove unnecessary asterisks indocs/en/docs/help-fastapi.md
. MR #9249 by @armgabrielyan. -
✏ Fix typo indocs/en/docs/tutorial/query-params-str-validations.md
. MR #9272 by @nicornk. -
✏ Fix typo/bug in inline code example indocs/en/docs/tutorial/query-params-str-validations.md
. MR #9273 by @tim-habitat. -
✏ Fix typo indocs/en/docs/tutorial/path-params-numeric-validations.md
. MR #9282 by @aadarsh977. -
✏ Fix typo: 'wll' to 'will' indocs/en/docs/tutorial/query-params-str-validations.md
. MR #9380 by @dasstyxx.
Translations
-
🌐 Add French translation fordocs/fr/docs/advanced/index.md
. MR #5673 by @axel584. -
🌐 Add Portuguese translation fordocs/pt/docs/tutorial/body-nested-models.md
. MR #4053 by @luccasmmg. -
🌐 Add Russian translation fordocs/ru/docs/alternatives.md
. MR #5994 by @Xewus. -
🌐 Add Portuguese translation fordocs/pt/docs/tutorial/extra-models.md
. MR #5912 by @LorhanSohaky. -
🌐 Add Portuguese translation fordocs/pt/docs/tutorial/path-operation-configuration.md
. MR #5936 by @LorhanSohaky. -
🌐 Add Russian translation fordocs/ru/docs/contributing.md
. MR #6002 by @stigsanek. -
🌐 Add Korean translation fordocs/tutorial/dependencies/classes-as-dependencies.md
. MR #9176 by @sehwan505. -
🌐 Add Russian translation fordocs/ru/docs/project-generation.md
. MR #9243 by @Xewus. -
🌐 Add French translation fordocs/fr/docs/index.md
. MR #9265 by @frabc. -
🌐 Add Russian translation fordocs/ru/docs/tutorial/query-params-str-validations.md
. MR #9267 by @dedkot01. -
🌐 Add Russian translation fordocs/ru/docs/benchmarks.md
. MR #9271 by @Xewus.
Internal
-
🔧 Update sponsors: remove Jina. MR #9388 by @tiangolo. -
🔧 Update sponsors, add databento, remove Ines's course and StriveWorks. MR #9351 by @tiangolo.
v0.95.0
Highlights
This release adds support for dependencies and parameters using Annotated
and recommends its usage.
This has several benefits, one of the main ones is that now the parameters of your functions with Annotated
would not be affected at all.
If you call those functions in other places in your code, the actual default values will be kept, your editor will help you notice missing required arguments, Python will require you to pass required arguments at runtime, you will be able to use the same functions for different things and with different libraries (e.g. Typer will soon support Annotated
too, then you could use the same function for an API and a CLI), etc.
Because Annotated
is standard Python, you still get all the benefits from editors and tools, like autocompletion, inline errors, etc.
One of the biggest benefits is that now you can create Annotated
dependencies that are then shared by multiple path operation functions, this will allow you to reduce a lot of code duplication in your codebase, while keeping all the support from editors and tools.
For example, you could have code like this:
def get_current_user(token: str):
### authenticate user
return User()
@​app.get("/items/")
def read_items(user: User = Depends(get_current_user)):
...
@​app.post("/items/")
def create_item(*, user: User = Depends(get_current_user), item: Item):
...
@​app.get("/items/{item_id}")
def read_item(*, user: User = Depends(get_current_user), item_id: int):
...
@​app.delete("/items/{item_id}")
def delete_item(*, user: User = Depends(get_current_user), item_id: int):
...
There's a bit of code duplication for the dependency:
user: User = Depends(get_current_user)
...the bigger the codebase, the more noticeable it is.
Now you can create an annotated dependency once, like this:
CurrentUser = Annotated[User, Depends(get_current_user)]
And then you can reuse this Annotated
dependency:
CurrentUser = Annotated[User, Depends(get_current_user)]
@​app.get("/items/")
def read_items(user: CurrentUser):
...
@​app.post("/items/")
def create_item(user: CurrentUser, item: Item):
...
@​app.get("/items/{item_id}")
def read_item(user: CurrentUser, item_id: int):
...
@​app.delete("/items/{item_id}")
def delete_item(user: CurrentUser, item_id: int):
...
...and CurrentUser
has all the typing information as User
, so your editor will work as expected (autocompletion and everything), and FastAPI will be able to understand the dependency defined in Annotated
.
Roughly all the docs have been rewritten to use Annotated
as the main way to declare parameters and dependencies. All the examples in the docs now include a version with Annotated
and a version without it, for each of the specific Python versions (when there are small differences/improvements in more recent versions). There were around 23K new lines added between docs, examples, and tests.
The key updated docs are:
- Python Types Intro:
- Tutorial:
Special thanks to @nzig for the core implementation and to @adriangb for the inspiration and idea with Xpresso!
Features
Docs
-
📝 Tweak tip recommendingAnnotated
in docs. MR #9270 by @tiangolo. -
📝 Update order of examples, latest Python version first, and simplify version tab names. MR #9269 by @tiangolo. -
📝 Update all docs to useAnnotated
as the main recommendation, with new examples and tests. MR #9268 by @tiangolo.
v0.94.1
Fixes
-
🎨 Fix types for lifespan, upgrade Starlette to 0.26.1. MR #9245 by @tiangolo.
v0.94.0
Upgrades
-
⬆ Upgrade python-multipart to support 0.0.6. MR #9212 by @musicinmybrain. -
⬆ ️ Upgrade Starlette version, support newlifespan
with state. MR #9239 by @tiangolo.
Docs
-
📝 Update Sentry link in docs. MR #9218 by @smeubank.
Translations
Internal
-
➕ Addpydantic
to PyPI classifiers. MR #5914 by @yezz123. -
⬆ Bump black from 22.10.0 to 23.1.0. MR #5953 by @dependabot[bot]. -
⬆ Bump types-ujson from 5.6.0.0 to 5.7.0.1. MR #6027 by @dependabot[bot]. -
⬆ Bump dawidd6/action-download-artifact from 2.24.3 to 2.26.0. MR #6034 by @dependabot[bot]. -
⬆ [pre-commit.ci] pre-commit autoupdate. MR #5709 by @pre-commit-ci[bot].
v0.93.0
Features
-
✨ Add support forlifespan
async context managers (supersedingstartup
andshutdown
events). Initial MR #2944 by @uSpike.
Now, instead of using independent startup
and shutdown
events, you can define that logic in a single function with yield
decorated with @asynccontextmanager
(an async context manager).
For example:
from contextlib import asynccontextmanager
from fastapi import FastAPI
def fake_answer_to_everything_ml_model(x: float):
return x * 42
ml_models = {}
@​asynccontextmanager
async def lifespan(app: FastAPI):
### Load the ML model
ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
yield
### Clean up the ML models and release the resources
ml_models.clear()
app = FastAPI(lifespan=lifespan)
@​app.get("/predict")
async def predict(x: float):
result = ml_models["answer_to_everything"](x)
return {"result": result}
Note: This is the recommended way going forward, instead of using startup
and shutdown
events.
Read more about it in the new docs: Advanced User Guide: Lifespan Events.
Docs
Translations
-
🌐 Tamil translations - initial setup. MR #5564 by @gusty1g. -
🌐 Add French translation fordocs/fr/docs/advanced/path-operation-advanced-configuration.md
. MR #9221 by @axel584. -
🌐 Add French translation fordocs/tutorial/debugging.md
. MR #9175 by @frabc. -
🌐 Initiate Armenian translation setup. MR #5844 by @har8. -
🌐 Add French translation fordeployment/manually.md
. MR #3693 by @rjNemo.
Internal
-
👷 Update translation bot messages. MR #9206 by @tiangolo. -
👷 Update translations bot to use Discussions, and notify when a MR is done. MR #9183 by @tiangolo. -
🔧 Update sponsors-badges. MR #9182 by @tiangolo. -
👥 Update FastAPI People. MR #9181 by @github-actions[bot]. -
🔊 Log GraphQL errors in FastAPI People, because it returns 200, with a payload with an error. MR #9171 by @tiangolo. -
💚 Fix/workaround GitHub Actions in Docker with git for FastAPI People. MR #9169 by @tiangolo. -
♻ ️ Refactor FastAPI Experts to use only discussions now that questions are migrated. MR #9165 by @tiangolo. -
⬆ ️ Upgrade analytics. MR #6025 by @tiangolo. -
⬆ ️ Upgrade and re-enable installing Typer-CLI. MR #6008 by @tiangolo.
Configuration
-
If you want to rebase/retry this MR, check this box
This MR has been generated by Renovate Bot.