Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs with parameters that default to None to use Optional #1561

Closed
9 tasks done
tiangolo opened this issue Jun 12, 2020 · 11 comments
Closed
9 tasks done

Update docs with parameters that default to None to use Optional #1561

tiangolo opened this issue Jun 12, 2020 · 11 comments
Labels
confirmed feature New feature or request good first issue Good for newcomers reviewed

Comments

@tiangolo
Copy link
Owner

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • 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 #1377 🎉

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.

@tiangolo tiangolo added feature New feature or request confirmed good first issue Good for newcomers labels Jun 12, 2020
@kx-chen
Copy link
Contributor

kx-chen commented Jun 13, 2020

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

@BCasaleiro
Copy link

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

@Illwatermelon
Copy link

Thanks a lot!

@meysam81
Copy link

meysam81 commented Oct 3, 2020

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

@mdaj06
Copy link

mdaj06 commented Oct 11, 2020

@tiangolo Is this available to take up?

@Darkflame72
Copy link

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

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Dec 13, 2020

This issue was solved on #1644

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

@azimovMichael
Copy link

azimovMichael commented Mar 29, 2021

Is this still up for grabs? @tiangolo

@M549233
Copy link

M549233 commented May 3, 2021

@azimovMichael

@Shaman20
Copy link

Shaman20 commented May 9, 2022

Can I work on this issue?

@tiangolo
Copy link
Owner Author

Ah, I forgot to close my own issue! Haha. This is done now. ✅

Thanks for the interest and discussion everyone! ☕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed feature New feature or request good first issue Good for newcomers reviewed
Projects
None yet
Development

No branches or pull requests

12 participants