Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1458 - Allow for custom parsing of environment variables via par…
…se_env_var in Config object (#4406) * Fix #1458 - Allow for custom parsing of environment variables via env_parse * Add docs for env_parse usage * Add changes file for #3977 * fixup: remove stray print statement * Revert env_parse property on field * Add parse_env_var classmethod in nested Config * Update documentation for parse_env_var * Update changes file. * fixup: linting in example * Rebase and remove quotes around imported example * fix example * my suggestions * remove unnecessary Field(env_parse=_parse_custom_dict) Co-authored-by: Samuel Colvin <s@muelcolvin.com>
- Loading branch information
1 parent
8dade7e
commit fe7c9da
Showing
5 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow for custom parsing of environment variables via `parse_env_var` in `Config`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
from typing import Any, List | ||
|
||
from pydantic import BaseSettings | ||
|
||
|
||
class Settings(BaseSettings): | ||
numbers: List[int] | ||
|
||
class Config: | ||
@classmethod | ||
def parse_env_var(cls, field_name: str, raw_val: str) -> Any: | ||
if field_name == 'numbers': | ||
return [int(x) for x in raw_val.split(',')] | ||
return cls.json_loads(raw_val) | ||
|
||
|
||
os.environ['numbers'] = '1,2,3' | ||
print(Settings().dict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters