Commit cb0abc25 authored by Tim Schoof's avatar Tim Schoof
Browse files

Merge branch 'fix_configuration' into 'master'

Fix configuration with values considered as False

See merge request !37
parents 7c5741f0 64df2de1
Pipeline #14433 passed with stage
in 57 seconds
...@@ -155,6 +155,13 @@ def create_cli_parser_for_attribute(attribute, parser, prefix, short_prefix): ...@@ -155,6 +155,13 @@ def create_cli_parser_for_attribute(attribute, parser, prefix, short_prefix):
def create_instance_from_configurable( def create_instance_from_configurable(
type, options, kwargs=None, builder=None): type, options, kwargs=None, builder=None):
"""
Create an instance of type using the given options.
The kwargs argument can be used to overwrite options.
If no builder is given the constructor of type will be used.
"""
if kwargs is None: if kwargs is None:
kwargs = {} kwargs = {}
if builder is None: if builder is None:
...@@ -165,11 +172,25 @@ def create_instance_from_configurable( ...@@ -165,11 +172,25 @@ def create_instance_from_configurable(
def create_instance_from_attribute(attribute, options, kwargs): def create_instance_from_attribute(attribute, options, kwargs):
"""
Insert a value for the attribute into kwargs using the values given in
options.
If the attribute has a complex type, the value will be an instance
that is constructed from the given options.
Does nothing if kwargs already has an entry for the attribute name.
"""
if attribute.name in kwargs: if attribute.name in kwargs:
return return
config_entry = attribute.metadata[CONFIGURABLE_CONFIG_ENTRY] config_entry = attribute.metadata[CONFIGURABLE_CONFIG_ENTRY]
if not config_entry.flatten: if not config_entry.flatten:
options = options.get(attribute.name, {}) if options is not attr.NOTHING:
options = options.get(attribute.name, attr.NOTHING)
# else default options for this attribute will be used
else:
# flatten only works for attributes that have complex types
assert config_entry.proxy is not None or attr.has(attribute.type)
if config_entry.proxy is not None: if config_entry.proxy is not None:
proxy_instance = create_instance_from_configurable( proxy_instance = create_instance_from_configurable(
config_entry.proxy, options) config_entry.proxy, options)
...@@ -179,8 +200,12 @@ def create_instance_from_attribute(attribute, options, kwargs): ...@@ -179,8 +200,12 @@ def create_instance_from_attribute(attribute, options, kwargs):
kwargs[attribute.name] = create_instance_from_configurable( kwargs[attribute.name] = create_instance_from_configurable(
attribute.type, options, builder=config_entry.builder) attribute.type, options, builder=config_entry.builder)
else: else:
if options: # attribute has a simple type
assert not config_entry.flatten
if options is not attr.NOTHING:
# use given option for this attribute
kwargs[attribute.name] = options kwargs[attribute.name] = options
# else default options for this attribute will be used
def parsed_args_to_dict(parsed): def parsed_args_to_dict(parsed):
......
...@@ -473,3 +473,13 @@ def test_create_instance_overwrite_options_via_kwargs(): ...@@ -473,3 +473,13 @@ def test_create_instance_overwrite_options_via_kwargs():
assert instance.simple_attribute is simple_attribute assert instance.simple_attribute is simple_attribute
assert instance.configurable_attribute is configurable_attribute assert instance.configurable_attribute is configurable_attribute
assert instance.extern_attribute is extern_attribute assert instance.extern_attribute is extern_attribute
def test_create_instance_from_configurable_zero():
@Configurable
class Foo:
i = Config("An integer", type=int, default=1)
instance = create_instance_from_configurable(Foo, {"i": 0})
assert instance.i == 0
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment