-
-
Notifications
You must be signed in to change notification settings - Fork 290
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] Default value on empty string #993
Comments
@LukeSavefrogs I'm not entirely sure if this is expected, because casting empty string to But as you pointed out, this would probably be covered if the ideas in #973 are carried on, which would be great. There is an undergoing refactor on validation implementation of the default setting, so we should await for that, anyway. In the meantime, I've found a small hack that can be used as a workaround as well. You can use Dynaconf(
...,
Validator(
"files.output.kml",
when=Validator("files.output.kml", eq=""),
cast=lambda value: "output.kml", # hack to use default when str is empty
default="output.kml", # applies when the value was not set at all
)
) |
Thank you @pedro-psb!
I confirm that indeed the proposed hack works:
To be able to use Dynaconf(
...,
Validator(
"files.output.kml",
when=Validator("files.output.kml", eq=""),
cast=lambda value: Path("output.kml") if str(value).strip() == "" else Path(value),
default="output.kml", # applies when the value was not set at all
)
) This way, even if the key is not defined, |
We must add the workaround example as a test_functional so we ensure it keeps working. |
Sorry I'm a bit confused, shouldn't the actual solution to use the If so, this is a bug right? |
@LukeSavefrogs Empty values in this case are key-value pairs with no pairs in yaml: When this happens, YAML parser assign a |
I've removed this as a bug because of what I've explained in the previous post about
Also, one possible way to improve user experience in this use case is to add an "interpret-empty-str-as-none" option. I personally don't like it, as we already have the |
Instead of a new flag which could be very confusing how about a What do you think? |
It's an interesting idea to have a more broad custom parsing step. I see the general use cases where you can't directly instruct the person writing the conf files about all dynaconf-specific idiosyncrasies and want to use a custom interpretation layer in between to match their specific needs.
But "before" should really be completely out of the validation (just enforcing, not sure if you didn't already mean that). We are currently trying to remove any side-effect from the validation process in order to make the codebase more decoupled and maintainable, as validation side-effects have been a particularly noticeable source of bugs. Conceptually it's purpose is to validate a fixed state, so calls to it shouldn't change state. I believe this should be placed the nearest possible from parsers, like an adapter layer for the user, which would be mostly his responsibility. And it should make it easier for him to debug and reason about later. We already have a basic hooking system, so this could be like a |
Another possible solution would be to add an access hook to the other end of the flow, on the access layer. When accessing any given key, we would run registered hooks that can implement this fallback logic on empty string values. This fallback logic on access is related to what we are trying to do on #988. It's easier to implement when using Also, it may be helpful to have a look at #975 for hooking. Although it was implemented for a specific django use-case, we could try generalizing it. |
Problem
I have a nested structure whose value i need to set to a specific string when empy string or
None
is provided.Take the following for example:
With the following configuration file (saved as
config.toml
):kml
key is not present in the config file, the default is given to the setting as espectedkml
key is set to an empty string, the default is completely ignored, even if I passedapply_default_on_none=True
, while I would expect it to printoutput.kml
It is somehow related to #973, since if that issue was solved I could simply put a
condition=lambda value: value is not None and value.strip() != ""
parameter to theValidator
and then use the default value since aValidationError
would occur.What I expected
From the documentation:
Reading this I expected the default value to kick in even on empty strings if i set
apply_default_on_none=True
on theValidator
or on theDynaconf
class (I tried both but got the same result).Workaround
To work around this issue I had to check manually if the setting was still empty:
And the same for all the other keys I have to make sure exist.
The text was updated successfully, but these errors were encountered: