You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You will be able to pass anything you want to app.state I've starlette is
on 0.12.9
Le jeu. 5 sept. 2019 à 5:14 PM, Artsiom Dolatau <notifications@github.com>
a écrit :
I typically use a subclass of pydantic.BaseSettings so I can read config from the environment, then create an lru_cached function that returns a config instance, then wherever I need access to a config setting I just call the lru_cached function to get the config and read the setting off of it.
No there are no convenience functions like that.
Le mar. 17 sept. 2019 à 7:53 PM, Sandeep Srinivasa <notifications@github.com>
a écrit :
@sandys does pydantic BaseSettings not work for you? It allows you to pass values at instantiation time or via environment variable (and is type safe to boot!).
A more complete example:
fromfunctoolsimportlru_cachefromtypingimportListfrompydanticimportBaseSettingsclassAPISettings(BaseSettings):
api_v1_route: str="/api/v1"openapi_route: str="/api/v1/openapi.json"backend_cors_origins_str: str=""# Should be a comma-separated list of originsdebug: bool=Falsedebug_exceptions: bool=Falsedisable_superuser_dependency: bool=Falseinclude_admin_routes: bool=False@propertydefbackend_cors_origins(self) ->List[str]:
return [x.strip() forxinself.backend_cors_origins_str.split(",") ifx]
classConfig:
env_prefix=""@lru_cache()defget_api_settings() ->APISettings:
returnAPISettings() # reads variables from environment
If you want something more fundamentally different as a function of the production vs. staging vs. testing environment (e.g., rather than setting each environment variable differently), you could just put some logic inside get_api_settings based on the environment variable value.
Accessing the config in your app is then as simple as, for example, debug = get_api_settings().debug.
thanks for replying.
im not sure how to use it. i read on other issues by @tiangolo that
app.state should be used. So I'm not entirely sure what is the right way to
configure and setup a global configuration object in fastapi.
You should not use app.state yet -- I don't think it's even supported currently (@euri10 has a pull request to add support).
However, even if it were supported, I would advise against using app.state for configuration if possible -- it is intended for storing application state, which may change over time, and was created so that that state could be accessed and modified from requests. I am having a hard time imagining a scenario in which accessing/modifying the application configuration in that way is a good idea -- I think in most cases it would be an anti-pattern.
Also, it is not mypy / completion-friendly, which I think is especially useful for config settings.
On Wed, 18 Sep, 2019, 00:57 dmontagu, ***@***.***> wrote:
You should not use app.state yet -- I don't think it's even supported
currently ***@***.*** <https://github.com/euri10> has a pull request to add
support).
However, even if it *were* supported, I would advise against using
app.state for *configuration* if possible -- it is intended for storing
application *state*, which may change over time.
Also, it is not mypy / completion-friendly, which I think is especially
useful for config settings.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#508>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAASYU4FTIM6CV5V7GGLYX3QKEVR3ANCNFSM4IT7DHBA>
.
Without going into all the nuances of everything my utility functions are doing, this is roughly what it looks like when I'm creating my server using configuration:
Classy and very elegant example of leveraging pydantic @dmontagu as always (I think the checks on whether the db is connected are unnecessary 😉)!
Another option is to use Starlette Config.
@dmontagu@euri10 which one would you both recommend ? Starlette Config or Pydantic ? Since fastapi is fundamentally based on Starlette, would Starlette Config be the more long-term safer way of doing this ?
@dmontagu@euri10 which one would you both recommend ? Starlette Config or Pydantic ? Since fastapi is fundamentally based on Starlette, would Starlette Config be the more long-term safer way of doing this ?
I wouldn't recommend Config based only on the fact that FastAPI is based on Starlette since the BaseSettings class is from pydantic and FastAPI relies on it at least as much as it does on Starlette.
FastAPI is not opinionated, find what works best for you, if the real question about safety is : do you think one or the other has more or less chances to be maintained over a long period of time, then I think both have equal chances, if not more for pydantic BaseSettings, but that's pure guess.
@sandys I personally think pydantic's is more flexible, powerful, plays better with autocompletion/IDEs, and the list goes on. If you are already using fastapi, I'd definitely recommend it; might as well take advantage of the dependencies you are already including.
I now use pydantic in most of my projects (even if they aren't based on fastapi) because of how useful BaseModel (and BaseSettings) are.
The starlette config has no nice way of handling type-safety or autocompletion. It's not an unreasonable choice, and if you were only using pydantic for the settings I might drop it in favor of fewer dependencies (then again, I might not, but I'm biased :D). But I think pydantic has a lot to offer here.
Pydantic is very close to a 1.0 release right now (I think the target is some time in the next month); I think it will be safe to depend on pydantic in the long term.
Activity
euri10 commentedon Sep 5, 2019
dmontagu commentedon Sep 5, 2019
I typically use a subclass of
pydantic.BaseSettings
so I can read config from the environment, then create anlru_cache
d function that returns a config instance, then wherever I need access to a config setting I just call thelru_cache
d function to get the config and read the setting off of it.sandys commentedon Sep 17, 2019
i have the same question here - what is the equivalent of app.config in flask ? https://flask.palletsprojects.com/en/1.1.x/config/
to specific, are there any convenience functions similar to
we need to load different configurations based on production or staging environment.
euri10 commentedon Sep 17, 2019
dmontagu commentedon Sep 17, 2019
@sandys does pydantic
BaseSettings
not work for you? It allows you to pass values at instantiation time or via environment variable (and is type safe to boot!).A more complete example:
If you want something more fundamentally different as a function of the production vs. staging vs. testing environment (e.g., rather than setting each environment variable differently), you could just put some logic inside
get_api_settings
based on the environment variable value.Accessing the config in your app is then as simple as, for example,
debug = get_api_settings().debug
.sandys commentedon Sep 17, 2019
dmontagu commentedon Sep 17, 2019
You should not use
app.state
yet -- I don't think it's even supported currently (@euri10 has a pull request to add support).However, even if it were supported, I would advise against using
app.state
for configuration if possible -- it is intended for storing application state, which may change over time, and was created so that that state could be accessed and modified from requests. I am having a hard time imagining a scenario in which accessing/modifying the application configuration in that way is a good idea -- I think in most cases it would be an anti-pattern.Also, it is not mypy / completion-friendly, which I think is especially useful for config settings.
sandys commentedon Sep 17, 2019
dmontagu commentedon Sep 17, 2019
Is the example I posted above not clear enough?
Without going into all the nuances of everything my utility functions are doing, this is roughly what it looks like when I'm creating my server using configuration:
(Placing the setup in a function like this makes it easy to modify the configuration and re-instantiate the app with the new configuration in tests.)
dmontagu commentedon Sep 17, 2019
Also, pydantic has docs about using BaseSettings that you might find useful.
euri10 commentedon Sep 17, 2019
Classy and very elegant example of leveraging pydantic @dmontagu as always (I think the checks on whether the db is connected are unnecessary 😉)!
Another option is to use Starlette Config.
sandys commentedon Sep 20, 2019
@dmontagu @euri10 which one would you both recommend ? Starlette Config or Pydantic ? Since fastapi is fundamentally based on Starlette, would Starlette Config be the more long-term safer way of doing this ?
euri10 commentedon Sep 20, 2019
I wouldn't recommend
Config
based only on the fact that FastAPI is based on Starlette since theBaseSettings
class is from pydantic and FastAPI relies on it at least as much as it does on Starlette.FastAPI is not opinionated, find what works best for you, if the real question about safety is : do you think one or the other has more or less chances to be maintained over a long period of time, then I think both have equal chances, if not more for pydantic BaseSettings, but that's pure guess.
dmontagu commentedon Sep 20, 2019
@sandys I personally think pydantic's is more flexible, powerful, plays better with autocompletion/IDEs, and the list goes on. If you are already using fastapi, I'd definitely recommend it; might as well take advantage of the dependencies you are already including.
I now use pydantic in most of my projects (even if they aren't based on fastapi) because of how useful
BaseModel
(andBaseSettings
) are.The starlette config has no nice way of handling type-safety or autocompletion. It's not an unreasonable choice, and if you were only using pydantic for the settings I might drop it in favor of fewer dependencies (then again, I might not, but I'm biased :D). But I think pydantic has a lot to offer here.
Pydantic is very close to a 1.0 release right now (I think the target is some time in the next month); I think it will be safe to depend on pydantic in the long term.
21 remaining items