-
-
Notifications
You must be signed in to change notification settings - Fork 92
Failing single underscore nested delimiter in BaseSettings with submodules #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note that in my real use case, my classes had default values which hid the error and made it be completely silent. I could not find a way to print the needed env vars and so I asked this question. Also, after experimentation I found out that if the attributes/submodels have no underscores of their own (here |
Yes. the problem was the I've tested your example in the new As you may know, we are preparing |
Hi @hramezani, thanks for your answer. Should I not expect a fix in the V1 version and I should prepare for a migration to V2 instead? |
Hi @petroslamb, I think it's not a bug. this not working because you have a field with BTW, |
@hramezani I very much appreciate your prompt responses. If I may comment on this, I think I did not find this exception in the documentation, which made it silent and hard to debug for me. Also, I suspect this will happen for every delimiter that is also included the attributes. And the single underscore is as common a case as they come. |
I think there is no way to fix this. Actually, it's not a bug and the user has to care about the
Yeah, actually env variables names are strings, and Maybe we can say something about this common problem in the documentation. @petroslamb You can open a PR and explain the problem in doc if you have time. |
The
Python uses underscores to name vars a lot, so it's highly problematic if ExampleThis is our config that fails to be (fully) loaded from the environment:
The fields without an
The rest of them, having an
|
@antonakospanos it does work with
As I mentioned above, we can add a note to the documentation about this. |
Properties like
If we concat the It seems like sth that can be supported by adjusting the implementation to check all the information provided (env_prefix, nested config name, env_nested_delimeter). |
The config name is I am still thinking the behavior of |
@samuelcolvin your input on this? |
Is this fixed or planned to be fixed in the context of v2? Any update please? |
Unless I'm missing something, if you set the delimiter to I don't think it's reasonable to expect Pydantic V2 to know not to split |
It would be really nice if it worked because the current behavior is a bit inconvenient to work around. I want to start using pydantic-settings, but I don't want to have to change the names of my existing env vars, and I like the elegant solution that pydantic-settings offers If I understand correctly, is pydantic-settings starting with the env vars, splitting those up by the delimiter, and looking for fields that match in the settings class? Would the problem be solved if instead, it worked backwards? Starting from the class, grab field names, and look for env vars that match the field_name+delimiter+nested_field_name combination? I'm sure I'm missing something obvious, but I wanted to throw the question out there because on the surface, it does seem possible to support behavior like this |
Any workaround for this that doesn't require picking In addition to @jcarbelbide's suggestion, another potentially simpler / less invasive option that would cover most (even if not all) use cases would be to add a new EDIT: digging into the source, it looks like changing just one line does the trick for depth=1: - _, *keys, last_key = env_name_without_prefix.split(self.env_nested_delimiter)
+ _, *keys, last_key = env_name_without_prefix.split(self.env_nested_delimiter, 1) @hramezani what do you think? I could submit a PR for adding |
@gsakkis, I will be happy to review a simple change that will fix your problem. please remember to include proper test and doc. |
Initial Checks
Description
Hi,
I am following the example for nesting a
BaseSettings
class withBaseModels
and I am usingenv_nested_delimiter = '_'
.Running the example code I get:
While it looks possible to use the single underscore delimiter, the values are not picked up and I am not sure which ones the settings object is looking for in this example.
Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: