Skip to content
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

Loading env variable with empty prefix fails #180

Open
ykrasik opened this issue Sep 22, 2024 · 6 comments
Open

Loading env variable with empty prefix fails #180

ykrasik opened this issue Sep 22, 2024 · 6 comments

Comments

@ykrasik
Copy link

ykrasik commented Sep 22, 2024

Hi,

@dataclass
class Test:
    my_var: str

dataconf.env("").on(Test)

The above will fail in the environment contains a key of the format xxx_{int}=... i.e. a string, an underscore and a number that can be parsed as an int.
This line then considers p[1] to be an int, but focus does not contain the key p[0] at all and we get a Key error.

For example, my env contains 'EFC_5748': '1'. The above line is executed with these values:
p = ['efc', 5748]
focus = ret

focus does not contain the key efc

@zifeo
Copy link
Owner

zifeo commented Sep 23, 2024

@ykrasik Does it work when setting a prefix? The prefix was never tested to be empty and the _ will be automatically added in order to avoid already set environment variables per mistake.

@ykrasik
Copy link
Author

ykrasik commented Sep 23, 2024

It does, yes. I guess it is pretty much expected to have a prefix, with the unexpected keys validation coming afterwards.

@zifeo
Copy link
Owner

zifeo commented Sep 23, 2024

@ykrasik thanks for getting back, we should indeed manage that case explicitly 👍

@ykrasik
Copy link
Author

ykrasik commented Sep 24, 2024

That would be great. I'll find some workaround in the meantime - we don't use a prefix for env variable overrides, I might just parse it using pydantic settings and add as a dict.

@zifeo
Copy link
Owner

zifeo commented Sep 25, 2024

@ykrasik just to make it clear, I am not sure I will add support to handle the prefix-less case, but at least have an error message explaining why prefix are required for security. I could also add maybe an explicit flag if you can share more about why prefixes do not work on your end?

@ykrasik
Copy link
Author

ykrasik commented Sep 25, 2024

No particular reason, just currently we didn't have prefixes and I was trying to introduce this library in a backwards compatible manner to what we have now. I might add prefixes to the env vars if we don't have too many of them, or just work around it

@ykrasik ykrasik changed the title Loading env variable with certain format fails Loading env variable with empty prefix fails Sep 28, 2024
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

No branches or pull requests

2 participants