Skip to content

Commit

Permalink
Update documentation for parse_env_var
Browse files Browse the repository at this point in the history
  • Loading branch information
acmiyaguchi committed Aug 19, 2022
1 parent 5debc6c commit bd892a3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
14 changes: 10 additions & 4 deletions docs/examples/settings_with_custom_parsing.py
@@ -1,16 +1,22 @@
# output-json
import os
from typing import List
from typing import Any, List

from pydantic import BaseSettings, Field
from pydantic import BaseSettings


def parse_list(s: str) -> List[int]:
return [int(x.strip()) for x in s.split(',')]


class Settings(BaseSettings):
numbers: List[int] = Field(env_parse=parse_list)
numbers: List[int]

class Config:
@classmethod
def parse_env_var(cls, field_name: str, raw_val: str) -> Any:
if field_name == "numbers":
return parse_list(raw_val)
return cls.json_loads(raw_val)


os.environ['numbers'] = '1,2,3'
Expand Down
17 changes: 0 additions & 17 deletions docs/examples/settings_with_custom_parsing_validator.py

This file was deleted.

10 changes: 1 addition & 9 deletions docs/usage/settings.md
Expand Up @@ -100,20 +100,12 @@ Nested environment variables take precedence over the top-level environment vari
(e.g. in the example above, `SUB_MODEL__V2` trumps `SUB_MODEL`).

You may also populate a complex type by providing your own parsing function to
`env_parse` in the field extras.
the `parse_env_var` classmethod in the Config object.

```py
{!.tmp_examples/settings_with_custom_parsing.py!}
```

You might choose to pass the environment string value through to pydantic and
transform in a validator instead.

```py
{!.tmp_examples/settings_with_custom_parsing_validator.py!}
```


## Dotenv (.env) support

!!! note
Expand Down

0 comments on commit bd892a3

Please sign in to comment.