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

The variable is still rturned even if it is not defined in configuration #527

Open
sshishov opened this issue Jul 4, 2024 · 0 comments
Open

Comments

@sshishov
Copy link

sshishov commented Jul 4, 2024

Let assume that we have this environment variables set on environment:

export VAR1=var1 VAR2=var2

Then we have the following piece of code:

from environ import Env

my_env = Env(VAR1=str)

print(my_env('VAR1'))  # var1
print(my_env('VAR2'))  # I would like it to fail as this variable is not defined in configuration, but it returns var2

I expect that the second call should fail with some error because this variable is not defined in the configuration.

If it is expected behavior, then I would like to have this configuration to enforce it.

Why? Because sometimes user do mistakes, define configuration as 1 variable and then use variable with typo when they access it, in this case the default casting is not applied which can lead to some issues.

UPDATE with example with boolean and casting:

export VAR1=True VAR2=False VAR3=True VAR4=False
from environ import Env

my_env = Env(VAR1=bool, VAR2=bool)

# usual print everything looks the same
print([str(value) for value in [my_env('VAR1'), my_env('VAR2'), my_env('VAR3'), my_env('VAR4')]]) 
# ['True', 'False', 'True', 'False']

# repr produces the differences in the type
print([repr(value) for value in [my_env('VAR1'), my_env('VAR2'), my_env('VAR3'), my_env('VAR4')]]) 
# ['True', 'False', "'True'", "'False'"]

# when will be used in `if` clauses, unexpected behavior can happen
print([bool(value) for value in [my_env('VAR1'), my_env('VAR2'), my_env('VAR3'), my_env('VAR4')]]) 
# [True, False, True, True]


Best regards,
    Sergei
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

1 participant