Skip to content

Commit

Permalink
Format codes in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed May 15, 2023
1 parent cfec546 commit 6dfb0c8
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,17 @@ from typing import Any, List, Tuple, Type

from pydantic.fields import FieldInfo

from pydantic_settings import BaseSettings, EnvSettingsSource, PydanticBaseSettingsSource
from pydantic_settings import (
BaseSettings,
EnvSettingsSource,
PydanticBaseSettingsSource,
)


class MyCustomSource(EnvSettingsSource):
def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool) -> Any:
def prepare_field_value(
self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool
) -> Any:
if field_name == 'numbers':
return [int(x) for x in value.split(',')]
return json.loads(value)
Expand Down Expand Up @@ -323,7 +329,10 @@ from pydantic_settings import BaseSettings
class Settings(BaseSettings):
...

model_config = ConfigDict(env_file=('.env', '.env.prod')) # `.env.prod` takes priority over `.env`
model_config = ConfigDict(
# `.env.prod` takes priority over `.env`
env_file=('.env', '.env.prod')
)
```

You can also use the keyword argument override to tell Pydantic not to load any file at all (even if one is set in
Expand Down Expand Up @@ -484,21 +493,31 @@ class JsonConfigSettingsSource(PydanticBaseSettingsSource):
when reading `config.json`
"""

def get_field_value(self, field: FieldInfo, field_name: str) -> Tuple[Any, str, bool]:
def get_field_value(
self, field: FieldInfo, field_name: str
) -> Tuple[Any, str, bool]:
encoding = self.config.get('env_file_encoding')
file_content_json = json.loads(Path('tests/example_test_config.json').read_text(encoding))
file_content_json = json.loads(
Path('tests/example_test_config.json').read_text(encoding)
)
fiel_value = file_content_json.get(field_name)
return fiel_value, field_name, False

def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool) -> Any:
def prepare_field_value(
self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool
) -> Any:
return value

def __call__(self) -> Dict[str, Any]:
d: Dict[str, Any] = {}

for field_name, field in self.settings_cls.model_fields.items():
field_value, field_key, value_is_complex = self.get_field_value(field, field_name)
field_value = self.prepare_field_value(field_name, field, field_value, value_is_complex)
field_value, field_key, value_is_complex = self.get_field_value(
field, field_name
)
field_value = self.prepare_field_value(
field_name, field, field_value, value_is_complex
)
if field_value is not None:
d[field_key] = field_value

Expand Down

0 comments on commit 6dfb0c8

Please sign in to comment.