Giter Club home page Giter Club logo

Comments (8)

eremeevfd avatar eremeevfd commented on September 20, 2024 1

@kschwab I think it's better to create new issue with your question or maybe I should reopen this one again?

from pydantic-settings.

hramezani avatar hramezani commented on September 20, 2024 1

Agree @kschwab. Let's define it as str

from pydantic-settings.

hramezani avatar hramezani commented on September 20, 2024

Thanks @eremeevfd for the issue.

It worked in Pydantic 1.* and I've read that we can use ENV_VAR=None|none|null to parse as None in python.

Where did you read it?

You already provided a value(None as a string) for env variable, So, pydantic-settings will pick that.
If you don't provide any value for the env, it will use the default value which is None in you case.

from pydantic-settings.

eremeevfd avatar eremeevfd commented on September 20, 2024

@hramezani Okay, I'm sorry, I think I've mixed topics of boolean parsing (true/True/false/False) with None parsing and somehow thought that null/None from envvar should be parse to None type also 😆
Everything works as expected and worked this way!

from pydantic-settings.

kschwab avatar kschwab commented on September 20, 2024

@hramezani I was going to submit a similar question, but as inverse of the case presented above:

import os

from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    ENV_VAR: str | None = 'Default string'

if __name__ == '__main__':

    os.environ['ENV_VAR'] = 'None' # or 'null'
    print(type(Settings().ENV_VAR) == type(None))
    '''
    False
    '''

The use case I was thinking of was Optional[int] = 2 where it's very clear that 'None' (str) should be coerced into None (NoneType). For the above Optional[str] case, adding a env_none_str spec in the SettingsConfigDict for environment variables seems like a good solution.

If env_none_str=None (the default), no coercion will take place. If env_none_str="None", environment variables with "None" string values will be coerced into None NoneTypes.

Thoughts?

from pydantic-settings.

hramezani avatar hramezani commented on September 20, 2024

@kschwab I would suggest adding a new config flag named e.g. parse_env_none or something like this.
The default value should be False and it means string'None' should be considered as string.(to don't introduce breaking change).
By setting the flag to True, string 'None' should be parse as None

from pydantic-settings.

kschwab avatar kschwab commented on September 20, 2024

@hramezani thanks for the feedback.

My rationale behind using a str flag instead of a bool flag was when considering compliance with e.g. a JSON spec null value. It may be desirable that users want "null" (str) to be coerced into None instead of only "None" (str) to None. i.e., the None str parse value is user defined. Defining the flag itself as None would be the default so as to not introduce breaking changes.

I actually don't have a strong preference either way. A bool flag is simpler and likely the dominant case. Let me know how you would like to proceed. I'll take a stab at initial PR for your feedback.

from pydantic-settings.

kschwab avatar kschwab commented on September 20, 2024

@eremeevfd

As an update, once PR #206 merges, the below will now work:

import os

from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    ENV_VAR: str | None = 'Default string'

if __name__ == '__main__':

    os.environ['ENV_VAR'] = 'None'
    print(type(Settings().ENV_VAR) == type(None))
    '''
    False
    '''

    print(type(Settings(_env_parse_none_str='None').ENV_VAR) == type(None))
    '''
    True
    '''

    os.environ['ENV_VAR'] = 'null'
    print(type(Settings(_env_parse_none_str='null').ENV_VAR) == type(None))
    '''
    True
    '''

from pydantic-settings.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.