-
-
Notifications
You must be signed in to change notification settings - Fork 288
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
[bug] Lazy validation fails with TypeError: __call__() takes 2 positional arguments but 3 were given #662
Comments
Hi 👋 I'd like to be assigned to this issue and give it a try. |
Hey, I found the issue. 👋 The problem behind this is within the But within the Then, the issue comes when the method is called and the But when checking the regular implementation, I found that the elements retrieved when the Validator is called are So, I was hesitating about which is going to be the best approach.
|
Hi @GibranHL0 My suggestions on # make the default value for Lazy to be an empty value
11 from dynaconf.utils.functional import empty
169 def __init__(self, value=empty, formatter=Formatters.python_formatter):
# accept a `validator_object` as named parameter and bind it to the context
178 def __call__(self, settings, validator_object=None):
"""LazyValue triggers format lazily."""
self.settings = settings
self.context["_validator_object"] = validator_object
return self.formatter(self.value, **self.context) Then, update the documentation on https://www.dynaconf.com/validation/#computed-values (docs/validation.md) the correct way of using the computed lazy value must be from dynaconf import Dynaconf, Validator
from dynaconf.utils.parse_conf import Lazy
def my_lazy_function(value, **context):
"""
value: default value passed to the validator, defaults to `empty`
context: a dictionary containing
env: All the environment variables
this: The settings instance
"""
# Hypothetical example
if env.get("HOSTNAME") == "localhost":
result = f"{this.server.name}/local"
else:
result = f"{this.server.name}/{value}"
return result
settings = Dynaconf(
validators=[
Validator("FOOBAR", default=Lazy("default_optional_value", formatter=my_lazy_function))
]
)
# When the value is first accessed, then the my_lazy_function will be called
print(settings.FOOBAR) |
Can someone confirm whether this was indeed addressed by #675? |
@justinmayer yes, on latest master it is working as described on the docs part of the issue #675 |
@GibranHL0 Do you have any updates here? Do you need any help? |
The branch with the solution has already been merged #675. |
Describe the bug
Tried to introduce Lazy Validators as described in https://www.dynaconf.com/validation/#computed-values i.e.
First bug (documentation): The above fails with
ImportError: cannot import name 'empty' from 'dynaconf.utils.parse_conf'
--> "empty" seems to be now in
dynaconf.utils.functional.empty
Second bug:
Lazy Validator fails with
TypeError: __call__() takes 2 positional arguments but 3 were given
To Reproduce
Steps to reproduce the behavior:
stacktrace:
Expected behavior
Work as documented in https://www.dynaconf.com/validation/#computed-values
Environment (please complete the following information):
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: