From cf4c1a1a5563c16f2f025f12baef640491489451 Mon Sep 17 00:00:00 2001 From: Isaac To Date: Mon, 30 Sep 2024 12:55:49 -0700 Subject: [PATCH] Turn `None` values for `slots` and `use_attributes` to empty list and dict This can simplify the code --- linkml_runtime/utils/schema_builder.py | 38 ++++++++++++++------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/linkml_runtime/utils/schema_builder.py b/linkml_runtime/utils/schema_builder.py index 6412be5f..2bc6a0f8 100644 --- a/linkml_runtime/utils/schema_builder.py +++ b/linkml_runtime/utils/schema_builder.py @@ -76,6 +76,11 @@ def add_class( :return: builder :raises ValueError: if class already exists and replace_if_present=False """ + if slots is None: + slots = [] + if slot_usage is None: + slot_usage = {} + if isinstance(cls, str): cls = ClassDefinition(cls, **kwargs) if isinstance(cls, dict): @@ -84,25 +89,22 @@ def add_class( raise ValueError(f"Class {cls.name} already exists") self.schema.classes[cls.name] = cls if use_attributes: - if slots is not None: - for s in slots: - if isinstance(s, SlotDefinition): - cls.attributes[s.name] = s - else: - raise ValueError( - f"If use_attributes=True then slots must be SlotDefinitions" - ) + for s in slots: + if isinstance(s, SlotDefinition): + cls.attributes[s.name] = s + else: + raise ValueError( + f"If use_attributes=True then slots must be SlotDefinitions" + ) else: - if slots is not None: - for s in slots: - cls.slots.append(s.name if isinstance(s, SlotDefinition) else s) - if isinstance(s, str) and s in self.schema.slots: - # top-level slot already exists - continue - self.add_slot(s, replace_if_present=replace_if_present) - if slot_usage: - for k, v in slot_usage.items(): - cls.slot_usage[k] = v + for s in slots: + cls.slots.append(s.name if isinstance(s, SlotDefinition) else s) + if isinstance(s, str) and s in self.schema.slots: + # top-level slot already exists + continue + self.add_slot(s, replace_if_present=replace_if_present) + for k, v in slot_usage.items(): + cls.slot_usage[k] = v return self def add_slot(