Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json Parse for Environment Variables in sub models does not work #26

Closed
5 of 15 tasks
chaimt opened this issue Jan 18, 2023 · 3 comments
Closed
5 of 15 tasks

Json Parse for Environment Variables in sub models does not work #26

chaimt opened this issue Jan 18, 2023 · 3 comments

Comments

@chaimt
Copy link

chaimt commented Jan 18, 2023

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

There is an issue with environment variables using json values for sub modules.
class SubModel(BaseModel):
v1: str = None
l1 : list[str] = None

class Settings(BaseSettings):
sub_model: SubModel = None

class Config:
    env_nested_delimiter = '__'

os.environ['sub_model'] = '{"v1": "aa", "l1": ["a","b","c"]}'
works

os.environ['sub_model__l1'] = '[1,2,3]'
does not work

After debugging the code i have found the issue:
The method explode_env_vars, extracts the value but does not call parse_env_var for it

Example Code

from pydantic import (
    BaseModel,
    BaseSettings,
    Field,
)

import os


class SubModel(BaseModel):
    v1: str = None
    l1 : list[str] = None


class Settings(BaseSettings):
    sub_model: SubModel = None

    class Config:
        env_nested_delimiter = '__'

os.environ['sub_model__l1'] = '[1,2,3]'


global_config = Settings()
print(global_config.dict())

Python, Pydantic & OS Version

pydantic version: 1.10.4
pydantic compiled: False
python version: 3.9.7 (default, Dec 22 2022, 21:29:13)  [Clang 14.0.0 (clang-1400.0.29.202)]
platform: macOS-13.1-x86_64-i386-64bit

Affected Components

@samuelcolvin
Copy link
Member

Sorry for the slow reply.

Can't read your code, so hard to tell, might be the same as #27.

@91fabbai91
Copy link

I face the exact same issue. Is there a fix to put complex datatypes like in nested classes and fill them with the notation with env_nested_delimiter e.g. export BASE_CONFIG__NESTED_CONFIG = '["ListElement0", "ListElement1"]'?

@hramezani hramezani transferred this issue from pydantic/pydantic Apr 26, 2023
@hramezani
Copy link
Member

We improved parsing nested models values in pydantic-settings.

FYI, pydantic-settings now is a separate package and is in alpha state. you can install it by pip install pydantic-settings --pre and test it.

Here is your example in pydantic-settings:

import os
from typing import List, Optional

from pydantic import BaseModel, ConfigDict
from pydantic_settings import BaseSettings


class SubModel(BaseModel):
    v1: Optional[str] = None
    l1 : List[str] = None


class Settings(BaseSettings):
    sub_model: SubModel = None

    model_config = ConfigDict(env_nested_delimiter='__')

os.environ['sub_model__l1'] = '["1","2","3"]'

global_config = Settings()
print(global_config.model_dump())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants