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

Add documentation about configuration and env vars #1118

Merged
merged 7 commits into from
Apr 2, 2020

Conversation

alexmitelman
Copy link
Contributor

In this pull request I add a new documentation page that describes the way to work with environment variables.

Related to this #190 and this #399 issues.

@codecov
Copy link

codecov bot commented Mar 15, 2020

Codecov Report

Merging #1118 into master will not change coverage by %.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1118   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          224       225    +1     
  Lines         6728      6733    +5     
=========================================
+ Hits          6728      6733    +5     
Impacted Files Coverage Δ
...ts/test_tutorial/test_metadata/test_tutorial001.py 100.00% <100.00%> (ø)
tests/test_tutorial/test_settings/test_app02.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e1cd45...9f9e2eb. Read the comment docs.

@tiangolo tiangolo merged commit d4d5b21 into tiangolo:master Apr 2, 2020
@tiangolo
Copy link
Owner

tiangolo commented Apr 2, 2020

Thanks for starting this! 🚀 🍰

I have been intending to add these docs for awhile 😅 So I updated it with what I had in mind and to include a couple of extra explanations.

Thanks for your contribution! 🎉 🍰

@leosussan
Copy link

leosussan commented May 15, 2020

Question to @alexmitelman : any benefit to using Pydantic vs. Starlette's baked-in configuration stuff?

@alexmitelman
Copy link
Contributor Author

alexmitelman commented May 17, 2020

Question to @alexmitelman : any benefit to using Pydantic vs. Starlette's baked-in configuration stuff?

IMHO, Starlette solution looks more low level, while using Pydantic you just declare class as usual.
Let's compare:

Starlette

from starlette.config import Config


config = Config(".env")

DEBUG = config('DEBUG', cast=bool, default=False)

Pydantic

from pydantic import BaseSettings


class Settings(BaseSettings):
    debug: bool = False

    class Config:
        env_file = '.env'

@leosussan
Copy link

leosussan commented May 30, 2020

@alexmitelman that's a description of syntax, not a functional differentiation. It also does not really explain the strengths / benefits of that syntactical difference, or if there is one.

I've now used both. IMO - given FastAPI's reliance on simplicity, I think Starlette's setup should be the preferred structure for the following three reasons:

  1. Starlette's config does not require the installation of a secondary dependency (Pydantic requires python-dotenv).
  2. Starlette's config caches the variable by default (no need for a custom lru_cache implementation).
  3. Starlette's suggested config.py file structure - simple, flat - is consistent with the .env file format.

That said - I've very recently taken to using Pydantic for settings management in larger projects for the following three reasons:

  1. Pydantic is better for codebases with multiple runtimes (it wouldn't make sense to install Starlette on a worker just to gain access to its' config class for the sake of code sharing).
  2. Pydantic lends itself well to the creation of constructed globals (e.g. DATABASE_URI as the combination of DATABASE_PORT, DATABASE_HOST, etc etc).
  3. Pydantic has a much larger library of data types to cast / validate globals against.

Those three reasons are compelling (don't get me wrong, Pydantic is amazing; I use it in everything) but I'd imagine the first three would make Starlette's config preferable in most situations.

@mths0x5f
Copy link

I was surprised by this change, @leosussan summarized what I needed to know.

hyahiaoui pushed a commit to hyahiaoui/fastapi that referenced this pull request Aug 1, 2020
* Add doc and example for env var config

* Syntax highlight for .env file

* Add test for configuration docs

* 📝 Update settings docs, add more examples

* ✅ Add tests for settings

* 🚚 Rename "Application Configuration" to "Metadata and Docs URLs"

to disambiguate between that and settings

* 🔥 Remove replaced example file

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants