Settings - case sensitivity - misleading example in docs #1105
Description
Bug
Please complete:
- OS: Linux
- Python version
import sys; print(sys.version)
: 3.6.9 - Pydantic version
import pydantic; print(pydantic.VERSION)
: 1.2
There is an example in docs about case sensitivity of environment variable names: https://pydantic-docs.helpmanual.io/usage/settings/#environment-variable-names.
I cite it here for convenience:
Case-sensitivity can be turned on through the Config:
from pydantic import BaseSettings
class Settings(BaseSettings):
redis_host = 'localhost'
class Config:
case_sensitive = True
When
case_sensitive
isTrue
, the environment variable must be in all-caps, so in this exampleredis_host
could only be modified viaexport REDIS_HOST
.
This example is simply incorrect. It leads that you can name Settings
attrs using "snake_case", and export env variable named "UPPER_CASE", and Settings
will catch them and populate proper attr with value taken from this env variable. This is not the case. In fact, if you want case sensitive environment variable names, you must name Settings
attributes all "UPPER_CASE" as well.
Proof
Let's take this example "as is" and try to override settings attribute using environment variable:
import os
from pydantic import BaseSettings
class Settings(BaseSettings):
redis_host = 'localhost'
class Config:
case_sensitive = True
Settings().redis_host
'localhost' # default
os.environ['REDIS_HOST'] = 'myredis' # according to example, we must name env var all UPPER_CASE
Settings().redis_host
'localhost' # still default
os.environ['redis_host'] = 'myredis'
Settings().redis_host
'myredis' # now it's working