Skip to content

Allow disabling loading field from certain sources via annotation #521

@jvllmr

Description

@jvllmr

In one of my newer projects I plan to use pydantic-settings paired with pydanclick for a declarative way to load settings from cli args and other sources such as pyproject.toml at the same time.

When thinking about it I noticed there could be cases where I don't want users to configure things from a specific source. For instance a --clean or --clear for creating a clean build in a build tool should only be possible via cli args in my opinion. Taking in this arg from other sources could lead to potentially destructive behaviour.

I think using an annotation would be the best way to implement this. I imagined it as something like this:

from pydantic_settings import BaseSettings, SettingsFieldOpts
from typing import Annotated

class BuildCmdSettings(BaseSettings):
    clean: Annotated[bool, SettingsFieldOpts(disable_sources=["PyprojectTomlConfigSettingsSource"])] = False

SettingsFieldOpts would be a new type which could hold more options for a field if needed in the future.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions