Skip to content

[Bug]: precedence of data read by pydantic-settings #118

@HardMax71

Description

@HardMax71

Bug Description

pydantic-settings reads first from env params, not from specified file. -> need to fix.

Also maybe toml instead of env?

Steps to Reproduce

Example:

import os
from pydantic_settings import BaseSettings, SettingsConfigDict

os.environ["S_THEORY"] = "Right"

with open(".env", "w") as f:
    f.write("S_THEORY=Wrong")

class Config(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")

    s_theory: str

conf = Config()

print(f"Inside Pydantic:  {conf.s_theory}")            # Right
print(f"Inside OS Env:    {os.getenv('S_THEORY')}")    # Right

# Cleanup
os.remove(".env")
del os.environ["S_THEORY"]

Expected Behavior

Should read from given file regardless what is in env

Component

Backend API

Environment

No response

Relevant Logs

Checklist

  • I have searched existing issues to ensure this is not a duplicate
  • I have included steps to reproduce the issue

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions