Fastapi: Update docs with parameters that default to None to use Optional

Created on 12 Jun 2020  路  7Comments  路  Source: tiangolo/fastapi

First check

  • [x] I added a very descriptive title to this issue.
  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] I searched the FastAPI documentation, with the integrated search.
  • [x] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [x] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [x] I already checked if it is not related to FastAPI but to Pydantic.
  • [x] I already checked if it is not related to FastAPI but to Swagger UI.
  • [x] I already checked if it is not related to FastAPI but to ReDoc.
  • [x] After submitting this, I commit to:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained minimal, reproducible, example with my use case:

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/")
def read_root(q: str = Query(None)):
    return {"q": q.lower()}

Description

  • Open the editor with that code, similar to many examples in the docs.
  • It's quite probable that the editor won't complain that q can be None and that q.lower() could raise an error.

The solution you would like

I would like the examples in the docs to use Optional:

from typing import Optional

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/")
def read_root(q: Optional[str] = Query(None)):
    return {"q": q.lower()}

By using Optional the editor would be able to warn that q.lower() could raise an error if q is None.

Describe alternatives you've considered

To leave it as is.

Environment

  • OS: [e.g. Linux / Windows / macOS]: All (docs)
  • FastAPI Version [e.g. 0.3.0]: 0.56.0

To know the FastAPI version use:

python -c "import fastapi; print(fastapi.__version__)"
  • Python version: All (docs)

To know the Python version use:

python --version

Additional context

I was waiting to first introduce Optional in the docs, but it was done in https://github.com/tiangolo/fastapi/pull/1377 :tada:

The new docs are here: https://fastapi.tiangolo.com/python-types/#optional

Now it's a good moment to update all the examples that have a default of None in all the docs to also use Optional.

confirmed enhancement good first issue

Most helpful comment

I'll be working on this with @chrisngyn and @YKo20010

All 7 comments

I'll be working on this with @chrisngyn and @YKo20010

I believe this was already solved with #1644 being merged to master. Should this issue be closed @tiangolo?

Thanks a lot!

@tiangolo
Is this issue still accepting contributors?
You mentioned here that you merged another branch.

@tiangolo Is this available to take up?

@tiangolo is this fixed or do you want someone to take it up a quick search through the docs makes it look all solved?

This issue was solved on https://github.com/tiangolo/fastapi/pull/1644

To confirm I've made a quick search and the docs are following the request on this issue. :nerd_face:

Was this page helpful?
0 / 5 - 0 ratings