Repository: avocado-framework/avocado

Issue:
Settings registration should execute the key_type under default values
When registering a new namespace with the Settings module, the `key_type` is not applied to the default value. This may cause problems if the default value is anything different from a string.


Discussion:
This seems to be easy @willianrampazzo . I will try to do this next week.
@willianrampazzo actually, `default` value is expecting something already on the `key_type` format. key_type is a function to convert from command line arguments and config files.

Can you give me a "problem example" please?
> @willianrampazzo actually, `default` value is expecting something already on the `key_type` format. key_type is a function to convert from command line arguments and config files.
> 
> Can you give me a "problem example" please?

Here is a real-world example:

Here we have the `key_type` https://github.com/avocado-framework/avocado/blob/master/avocado/plugins/xunit.py#L198. I had to apply the same function to the `default` value on line https://github.com/avocado-framework/avocado/blob/master/avocado/plugins/xunit.py#L199 because before it was registering the `default` value as `str` and was breaking the comparison at line https://github.com/avocado-framework/avocado/blob/master/avocado/plugins/xunit.py#L78 (it was comparing `int` from `log_size` with `str` from `max_log_size`).

Here is the change I did: https://github.com/avocado-framework/avocado/commit/c46c9b4259caa18d44b5433a33b60938d6322af3

If we force the `key_type` to the `default` value during the registration, this would save us some debugging time in case of problems.
@willianrampazzo I see.

The problem that I'm thinking right now is that the `key_type` is intended to be used during "arg parse" and "config parse" only. And most of the methods used in key_type was already expecting this.

In order to apply the `key_type` metho to `default` values too, we need:

1. Pass to default the values as they are parsed inside the config files (`"foo,bar"` instead of ['foo', 'bar']); OR

2. Make adjustments on the `key_type` methods so will be ready to receive and ignore conversions if the value is already on the desired format.

Not sure if you got the idea.
> @willianrampazzo I see.
> 
> The problem that I'm thinking right now is that the `key_type` is intended to be used during "arg parse" and "config parse" only. And most of the methods used in key_type was already expecting this.
> 
> In order to apply the `key_type` metho to `default` values too, we need:
> 
> 1. Pass to default the values as they are parsed inside the config files (`"foo,bar"` instead of ['foo', 'bar']); OR
> 2. Make adjustments on the `key_type` methods so will be ready to receive and ignore conversions if the value is already on the desired format.
> 
> Not sure if you got the idea.

Actually, no, sorry. I did not understand what you meant by item number 1.
When using the argparse or configparse we use lists as "foo,bar" instead of ['foo', 'bar'], right?
> When using the argparse or configparse we use lists as "foo,bar" instead of ['foo', 'bar'], right?

Right!
@willianrampazzo sorry if wasn't clear.

Take the "show" example (`avocado/core/__init__.py:35`), or we need to change the lambda to support multiple formats or we need to change the default.

Makes sense?
> @willianrampazzo sorry if wasn't clear.
> 
> Take the "show" example (`avocado/core/__init__.py:35`), or we need to change the lambda to support multiple formats or we need to change the default.
> 
> Makes sense?

Okay, now I got it. I had the use case of just one argument at a time for an option. In this case, I think we should better document the use of the `default` argument during the registration. It should say we need to use the "processed" value for it.

Resolving patch:
diff --git a/avocado/core/future/settings.py b/avocado/core/future/settings.py
--- a/avocado/core/future/settings.py
+++ b/avocado/core/future/settings.py
@@ -502,8 +502,11 @@ def register_option(self, section, key, default, help_msg, key_type=str,
             What is the key name of your option inside that section.
 
         default : typeof(key_type)
-            What is the default value of your option if the key is not present
-            inside the section on all configuration files.
+            The default value of an option. It sets the option value when the
+            key is not defined in any configuration files or via command-line.
+            The default value should be "processed". It means the value should
+            match the type of key_type. Due to some internal limitations, the
+            Settings module will not apply key_type to the default value.
 
         help_msg : str
             The help message that will be displayed at command-line (-h) and


Test patch: