Comments (8)
@kschwab I think it's better to create new issue with your question or maybe I should reopen this one again?
from pydantic-settings.
Agree @kschwab. Let's define it as str
from pydantic-settings.
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.
@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.
@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.
@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.
@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.
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)
- Create default templates (feature request) HOT 4
- Example from docs doesn't work can't import from pydantic.env_settings import SettingsSourceCallable HOT 1
- Can my custom PydanticBaseSettingsSource get values already retrieved from other PydanticBaseSettingsSources? HOT 2
- Comma delimited string as list of strings HOT 1
- Easily list the names of the env var required to set a field HOT 2
- Environment Variable Overrides Init Arguments in BaseSettings with Aliases HOT 5
- environment variables case sensitivity on windows HOT 3
- Can't read Pydantic Settings from stdin HOT 3
- Python 3.9 (and earlier I guess), and non PEP-484 type definitions with injector HOT 5
- Typing for multiple PathTypes (and support Traversable) for SettingsConfigDict json_file, toml_file, yaml_file, etc HOT 2
- Validation error for 3 levels of nested dicts in v2.3.0 HOT 7
- Field alias AttributeError: 'AliasChoices' object has no attribute 'lower' - CliSettingsSource HOT 3
- CLI help interacts badly with strings containing `'%'` HOT 4
- CliSettingSource fails to handle `AliasChoices` and `AliasPath` HOT 3
- Error when using validator in nested model with case_sensitive=True and using upper case HOT 7
- Settings parsing breaks with complex type HOT 8
- Feature request: add a reload method to re-parse env variables HOT 7
- What's the recommended way to access settings loaded from previous sources? HOT 1
- Env does not override nested value when other sources are used HOT 2
- 2.3.4: `pyupgrade --py39-plus` generated changes causes pytest fails HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydantic-settings.