Giter Club home page Giter Club logo

Comments (3)

hramezani avatar hramezani commented on June 11, 2024 2

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})

from pydantic-settings.

PrieJos avatar PrieJos commented on June 11, 2024

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. 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.

... 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

from pydantic-settings.

hramezani avatar hramezani commented on June 11, 2024

@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.

from pydantic-settings.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.