Skip to content

env_prefix Ignored in Some Cases in Pydantic Settings #426

@bolshakov

Description

@bolshakov

Environment:

  • Pydantic version: 2.9.2
  • Pydantic settings version: 2.5.2
  • Python version: 3.12.5

Description:

I encountered an issue where the env_prefix seems to be ignored under specific circumstances when using SettingsConfigDict in a nested settings structure.

Code to Reproduce:

from pydantic_settings import BaseSettings, SettingsConfigDict

class InnerSettings(BaseSettings):
    port: int = 5432

class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_prefix="SETTINGS__", env_nested_delimiter="__")
    
    inner_settings: InnerSettings = InnerSettings()
    port: int = 5433

settings = Settings()
print(settings.model_dump())

Steps to Reproduce:

  1. Run the following test cases:
# Execution 1: Without env vars
poetry run python test.py
# Output: {'inner_settings': {'port': 5432}, 'port': 5433}

# Execution 2: With SETTINGS__PORT
SETTINGS__PORT=42 poetry run python test.py
# Output: {'inner_settings': {'port': 5432}, 'port': 42}

# Execution 3: With nested SETTINGS__INNER_SETTINGS__PORT
SETTINGS__INNER_SETTINGS__PORT=42 poetry run python test.py
# Output: {'inner_settings': {'port': 42}, 'port': 5433}

# Execution 4: With PORT and nested SETTINGS__INNER_SETTINGS__PORT
PORT=666 SETTINGS__INNER_SETTINGS__PORT=42 poetry run python test.py
# Output: {'inner_settings': {'port': 42}, 'port': 5433}

# Execution 5: With only PORT
PORT=666 poetry run python test.py
# Output: {'inner_settings': {'port': 666}, 'port': 5433}

# Execution 6: With both PORT and SETTINGS__PORT
PORT=666 SETTINGS__PORT=42 poetry run python test.py
# Output: {'inner_settings': {'port': 666}, 'port': 42}

Expected Behavior:

The env_prefix should correctly apply to nested settings and non-prefixed environment variables should be ignored.

Actual Behavior:

In some cases, the non-prefixed PORT variable overrides the prefixed settings.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions