Closed PrieJos closed 4 months ago
Thanks @PrieJos for reporting this.
This is the final collected values from sources by pydantic-settings
{'MYAPP_FOO': 'value-from-osenv', 'MYAPP_BAR': '2', 'foo': 'value-from-init', 'bar': 3}
It will be passed to pydantic
for validation. As populate_by_name
is enabled, pydantic picks the values that you mentioned. There is no strict rule to tell pydantic which value has to be picked when populate_by_name
is enabled and values are provided for both field name and alias.
So, you can fix the problem by providing init values by alias name like:
settings = Settings(**{'MYAPP_FOO':"value-from-init", 'MYAPP_BAR':3})
Thanks @hramezani for the reply.
So then as far as I understood you, the behavior you described here...
This is the final collected values from sources by
pydantic-settings
{'MYAPP_FOO': 'value-from-osenv', 'MYAPP_BAR': '2', 'foo': 'value-from-init', 'bar': 3}
It will be passed to
pydantic
for validation. Aspopulate_by_name
is enabled, pydantic picks the values that you mentioned. There is no strict rule to tell pydantic which value has to be picked whenpopulate_by_name
is enabled and values are provided for both field name and alias.
... is somehow not fully consistent with the excerpt of the documentation I pasted in the ticket whenever aliases are used. Or at least, it is not clear enough in my opinion. So maybe a side note on the documentation warning about this will help. 🙏🏻
On the other hand, this solution you provided I checked it already and as you mentioned is returning the expected output.
So, you can fix the problem by providing init values by alias name like:
settings = Settings(**{'MYAPP_FOO':"value-from-init", 'MYAPP_BAR':3})
However I opted to write my own PydanticBaseSettingsSource
that replaces the out-of-the-box InitSettingsSource
implementation which is basically doing your proposed solution behind the scene.
Thanks!
Cheers Jose M. Prieto
@PrieJos probably the documentation can be more clear about this. You can make a PR and improve it if you want and we appreciate it. BTW, I am closing the issue for now.
Dear Pydantic team,
I am using
pydantic-settings
in one of my projects to implement all the settings load logic. But I ran into the following issue which I think it might be a bug or at least something not consistent with what the documentation says.So, according to the section Field value priority in the documentation, you can read as follows:
That default priority is perfectly fine for me. However as far as I could check, the following code is not consistent with that:
While I was expecting to get the following output out of it:
The truth is that this is not the case. What I'm getting back is:
That means the explicit init arguments I am giving when instantiating
Settings
object are not taking into account even though I set the configuration parameterpopulate_by_name=True
. However if you replace field name by the alias as follows, then it works as expected:Did I miss some configuration or is it a real bug?
BTW - The versions that are installed in my python environment are:
pydantic 2.6.3
pydantic-settings 2.2.1
Thanks in advanced for your support.
Cheers, Jose M. Prieto