Skip to content

Commit

Permalink
List are now formatted one line per element.
Browse files Browse the repository at this point in the history
isinstance(obj, list) was not working because traitlets.List and derivatives are not considered list type.

config_files trait is also commented because it crashed with the empty list as default.
  • Loading branch information
ccossou committed Nov 24, 2023
1 parent 265ba7b commit 5146849
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ctapipe/core/config_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ def _trait_to_str(trait, help=True, indent_level=0):
trait_value = f"'{trait_value}'"

# Make sure that list of values end up on multiple lines (In particular quality criteria)
# Since traitlets.List derivative can't be compared with it, nor list/tuple, I have to check if "List" is in the
# type (converted as str)
value_repr = ""
if isinstance(trait_value, list):
trait_value_type = str(type(trait_value))
trait_value_is_list = ("List" in trait_value_type) or isinstance(trait_value, list)
if trait_value_is_list:
for v in trait_value:
# tuples are not correctly handled by yaml, so we convert them to list here. They'll be converted to tuple
# when reading again.
Expand All @@ -92,7 +96,9 @@ def _trait_to_str(trait, help=True, indent_level=0):

# Automatically comment all parameters that are unvalid
commented = ""
if trait_value == traitlets.Undefined:
if trait_value in (traitlets.Undefined, None):
commented = "#"
elif trait_type.startswith("List") and len(trait_value) == 0:
commented = "#"

trait_repr += f"{indent_str*indent_level}{commented}{trait.name}: {value_repr}\n"
Expand Down

0 comments on commit 5146849

Please sign in to comment.