Skip to content

Settings - case sensitivity - misleading example in docs #1105

Closed

Description

@tribals

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 is True, the environment variable must be in all-caps, so in this example redis_host could only be modified via export 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

Activity

  1. samuelcolvin commented on Dec 18, 2019

    @samuelcolvin
    Member

    Thank you for reporting this, I guess we forgot to update the documentation when the functionality changed.

    PR welcome to correct the docs.

  2. tribals commented on Dec 18, 2019

    @tribals
    ContributorAuthor

    I will submit PR, but just need to clarify current behavior/any drawbacks? Does it just turns case sensitivity on without any "smarty" logic? So, attribute names must match environment variable names closely?

  3. samuelcolvin commented on Dec 18, 2019

    @samuelcolvin
    Member

    No, the other logic for working out environment variable names to look up is unchanged.

    Just that if not self.case_sensitive everything is converted to lowercase before the lookup.

    https://github.com/samuelcolvin/pydantic/blob/de6bd1b4694dc7b04c9626e39fd494965f230b7c/pydantic/env_settings.py#L67-L88

  4. tribals commented on Dec 18, 2019

    @tribals
    ContributorAuthor

    But, if there is no explicit binding to environment variable (through Field.env), then variable name is taken from attribute's name, and follow its case as is, is it correct?

  5. samuelcolvin commented on Dec 18, 2019

    @samuelcolvin
    Member

    well it's cls.env_prefix + field.name but env_prefix is an empty list by default, so yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions