pypi fastapi 0.78.0

latest releases: 0.111.0, 0.111.0.dev1, 0.110.3...
2 years ago

Features

  • ✨ Add support for omitting ... as default value when declaring required parameters with:

  • Path()

  • Query()

  • Header()

  • Cookie()

  • Body()

  • Form()

  • File()

New docs at Tutorial - Query Parameters and String Validations - Make it required. PR #4906 by @tiangolo.

Up to now, declaring a required parameter while adding additional validation or metadata needed using ... (Ellipsis).

For example:

from fastapi import Cookie, FastAPI, Header, Path, Query

app = FastAPI()


@app.get("/items/{item_id}")
def main(
    item_id: int = Path(default=..., gt=0),
    query: str = Query(default=..., max_length=10),
    session: str = Cookie(default=..., min_length=3),
    x_trace: str = Header(default=..., title="Tracing header"),
):
    return {"message": "Hello World"}

...all these parameters are required because the default value is ... (Ellipsis).

But now it's possible and supported to just omit the default value, as would be done with Pydantic fields, and the parameters would still be required.

✨ For example, this is now supported:

from fastapi import Cookie, FastAPI, Header, Path, Query

app = FastAPI()


@app.get("/items/{item_id}")
def main(
    item_id: int = Path(gt=0),
    query: str = Query(max_length=10),
    session: str = Cookie(min_length=3),
    x_trace: str = Header(title="Tracing header"),
):
    return {"message": "Hello World"}

To declare parameters as optional (not required), you can set a default value as always, for example using None:

from typing import Union
from fastapi import Cookie, FastAPI, Header, Path, Query

app = FastAPI()


@app.get("/items/{item_id}")
def main(
    item_id: int = Path(gt=0),
    query: Union[str, None] = Query(default=None, max_length=10),
    session: Union[str, None] = Cookie(default=None, min_length=3),
    x_trace: Union[str, None] = Header(default=None, title="Tracing header"),
):
    return {"message": "Hello World"}

Docs

  • 📝 Add docs recommending Union over Optional and migrate source examples. New docs at Python Types Intro - Using Union or Optional. PR #4908 by @tiangolo.
  • 🎨 Fix default value as set in tutorial for Path Operations Advanced Configurations. PR #4899 by @tiangolo.
  • 📝 Add documentation for redefined path operations. PR #4864 by @madkinsz.
  • 📝 Updates links for Celery documentation. PR #4736 by @sammyzord.
  • ✏ Fix example code with sets in tutorial for body nested models. PR #3030 by @hitrust.
  • ✏ Fix links to Pydantic docs. PR #4670 by @kinuax.
  • 📝 Update docs about Swagger UI self-hosting with newer source links. PR #4813 by @Kastakin.
  • 📝 Add link to external article: Building the Poll App From Django Tutorial With FastAPI And React. PR #4778 by @jbrocher.
  • 📝 Add OpenAPI warning to "Body - Fields" docs with extra schema extensions. PR #4846 by @ml-evs.

Translations

  • 🌐 Fix code examples in Japanese translation for docs/ja/docs/tutorial/testing.md. PR #4623 by @hirotoKirimaru.

Internal

  • ♻ Refactor dict value extraction to minimize key lookups fastapi/utils.py. PR #3139 by @ShahriyarR.
  • ✅ Add tests for required nonable parameters and body fields. PR #4907 by @tiangolo.
  • 👷 Fix installing Material for MkDocs Insiders in CI. PR #4897 by @tiangolo.
  • 👷 Add pre-commit CI instead of custom GitHub Action. PR #4896 by @tiangolo.
  • 👷 Add pre-commit GitHub Action workflow. PR #4895 by @tiangolo.
  • 📝 Add dark mode auto switch to docs based on OS preference. PR #4869 by @ComicShrimp.
  • 🔥 Remove un-used old pending tests, already covered in other places. PR #4891 by @tiangolo.
  • 🔧 Add Python formatting hooks to pre-commit. PR #4890 by @tiangolo.
  • 🔧 Add pre-commit with first config and first formatting pass. PR #4888 by @tiangolo.
  • 👷 Disable CI installing Material for MkDocs in forks. PR #4410 by @dolfinus.

Don't miss a new fastapi release

NewReleases is sending notifications on new releases.