Skip to content

Allow customisation of the dotevn setting source to filter variables - #832

Merged
hramezani merged 8 commits into
pydantic:mainfrom
CaselIT:dotevn_filtering
Apr 2, 2026
Merged

hramezani merged 8 commits into
pydantic:mainfrom
CaselIT:dotevn_filtering

Conversation

@CaselIT

@CaselIT CaselIT commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

This PR adds a new setting option for the dotenv setting source to filter what variables are provided to the model.

The setting env_filtering is added:

  • the default of None keeps the current behaviour
  • 'match_prefix': only the variable that match the prefix will be passed to the model. Useful when used in conjunction with env_prefix to "scope" a single dotenv file to a specific model.
  • 'only_existing': only the variables that have a corresponding field will be passed to the model. When using this option the dotenv setting source will behave like the env settings source.

Fixes: #800

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an env_filtering option to the dotenv settings source to control which .env variables are passed into the settings model, addressing env-prefix related “extra fields” errors reported in #800.

Changes:

  • Introduce DotenvFiltering and a new env_filtering setting in SettingsConfigDict.
  • Implement dotenv-source filtering modes: only_existing and match_prefix.
  • Add tests and documentation describing the new behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_settings.py Adds coverage for the new env_filtering modes.
pydantic_settings/sources/types.py Defines DotenvFiltering literal type.
pydantic_settings/sources/providers/dotenv.py Implements filtering behavior in DotEnvSettingsSource.
pydantic_settings/sources/init.py Re-exports DotenvFiltering from the sources package.
pydantic_settings/main.py Adds env_filtering to SettingsConfigDict.
docs/index.md Documents env_filtering and updates the dotenv/env_prefix note.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pydantic_settings/sources/providers/dotenv.py Outdated
Comment thread docs/index.md Outdated
Comment thread tests/test_settings.py
@hramezani

Copy link
Copy Markdown
Contributor

Thanks @CaselIT for the PR.

Copilot left some comments. Please check them.

@CaselIT

CaselIT commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

the handling of env_nested_delimiter was already kinda problematic before this patch. I've fixed the reported comments, but I haven't fixed what I would argue is another bug. Let me know if you agree, otherwise I can look into fixing it too

Comment thread tests/test_settings.py
a: A

s = Settings()
exp = {'a': {'x': 'bar', 'b': 'y'}, 'b': 'b', 'c@a': '1', 'c@b': '2'}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these c@a and c@b is what I would argue is another bug, but it was already like this so I left the behaviour in plance

@CaselIT

CaselIT commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

also I haven't found if you have a changelog or similar, if you want me to add a note let me know where should I

@hramezani

Copy link
Copy Markdown
Contributor

also I haven't found if you have a changelog or similar, if you want me to add a note let me know where should I

we don't have a changelog file. We will generate it during release.

Comment thread docs/index.md Outdated
@hramezani

Copy link
Copy Markdown
Contributor

Could you please add a test for match_prefix + extra='forbid' + nested delimiter scenario?

@CaselIT

CaselIT commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

updated. let me know if you want me to do something regarding the other behaviour mentioned here #832 (comment)

@hramezani

Copy link
Copy Markdown
Contributor

updated. let me know if you want me to do something regarding the other behaviour mentioned here #832 (comment)

It would be great if you could fix this. Better to fix it in separate PR

@CaselIT

CaselIT commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

I'll create an issue in the meantime. just to confirm, this should be exp = {'a': {'x': 'bar', 'b': 'y'}, 'b': 'b', 'c': {'a': '1', 'b': '2'}} independently from this PR, right?

@hramezani

hramezani commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

I'll create an issue in the meantime. just to confirm, this should be exp = {'a': {'x': 'bar', 'b': 'y'}, 'b': 'b', 'c': {'a': '1', 'b': '2'}} independently from this PR, right?

After checking more, I think this probably is not an issue. becuase c is not defined as a field. The merge is happening for a becase it is defined as a field in the model.

@CaselIT

CaselIT commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

You are probably right, the docs says

Sub model has to inherit from pydantic.BaseModel, Otherwise pydantic-settings will initialize sub model, collects values for sub model fields separately, and you may get unexpected results.

since there is no c BaseModel this is likely in line with "unexpected results"

Maybe an additional doc line may be worth adding?

@hramezani

Copy link
Copy Markdown
Contributor

You are probably right, the docs says

Sub model has to inherit from pydantic.BaseModel, Otherwise pydantic-settings will initialize sub model, collects values for sub model fields separately, and you may get unexpected results.

since there is no c BaseModel this is likely in line with "unexpected results"

Maybe an additional doc line may be worth adding?

yes, would be good

@CaselIT

CaselIT commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

done

@hramezani

Copy link
Copy Markdown
Contributor

Thanks @CaselIT

Let's merge this. It will be available in the next release!

Please help us in case of issues after the release.

@hramezani
hramezani merged commit bb6536b into pydantic:main Apr 2, 2026
19 checks passed
@CaselIT

CaselIT commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

great!

Please help us in case of issues after the release.

will try, ping me if something comes up related to this

@hramezani hramezani mentioned this pull request Apr 20, 2026
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

Successfully merging this pull request may close these issues.

Behavioural change in env_prefix behaviour compared with v1

3 participants