From 1a2ac9e4c3cffb5ab20591c25a4a3c21241e0c9d Mon Sep 17 00:00:00 2001 From: Palina Date: Wed, 9 Oct 2024 16:44:06 +0800 Subject: [PATCH] Fix `Option` printing (#855) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andrei Văcaru <16517508+anvacaru@users.noreply.github.com> --- src/kontrol/options.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/kontrol/options.py b/src/kontrol/options.py index 8dd057ff5..3bf0b4d2f 100644 --- a/src/kontrol/options.py +++ b/src/kontrol/options.py @@ -477,13 +477,18 @@ def __str__(self) -> str: The first line collects all attributes that are directly set on the instance. The loop is required to iterate over all parent classes and fetch attributes set by the `default` method. + Default values from parent classes will only be used if the attribute is not explicitly set. + :return: String representation of the instance. """ options_dict = {**self.__dict__} for parent in self.__class__.__bases__: if hasattr(parent, 'default'): - options_dict.update(parent.default()) + parent_defaults = parent.default() + for key, value in parent_defaults.items(): + if key not in options_dict: + options_dict[key] = value options_str = ', '.join(f'{key}: {value}' for key, value in options_dict.items()) return f'ProveOptions({options_str})' @@ -881,13 +886,18 @@ def __str__(self) -> str: The first line collects all attributes that are directly set on the instance. The loop is required to iterate over all parent classes and fetch attributes set by the `default` method. + Default values from parent classes will only be used if the attribute is not explicitly set. + :return: String representation of the instance. """ options_dict = {**self.__dict__} for parent in self.__class__.__bases__: if hasattr(parent, 'default'): - options_dict.update(parent.default()) + parent_defaults = parent.default() + for key, value in parent_defaults.items(): + if key not in options_dict: + options_dict[key] = value options_str = ', '.join(f'{key}: {value}' for key, value in options_dict.items()) return f'BuildOptions({options_str})'