mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
fe7c9da6c1
* 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>
20 lines
441 B
Python
20 lines
441 B
Python
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())
|