Skip to content

Commit

Permalink
Add back support of kwargs param when cls param is a `ClassDefini…
Browse files Browse the repository at this point in the history
…tion`

This time with detection of unsupported fields provided by
`kwargs`
  • Loading branch information
candleindark committed Oct 1, 2024
1 parent cf4c1a1 commit 634286f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions linkml_runtime/utils/schema_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass, field
from dataclasses import dataclass, fields
from typing import Dict, List, Union, Optional

from linkml_runtime.linkml_model import (ClassDefinition, EnumDefinition,
Expand Down Expand Up @@ -71,8 +71,7 @@ def add_class(
:param replace_if_present: if True, replace existing class if present
:param use_attributes: Whether to specify the given slots as an inline
definition of slots, attributes, in the class definition
:param kwargs: additional ClassDefinition properties (ignored if `cls` is a
`ClassDefinition`)
:param kwargs: additional ClassDefinition properties
:return: builder
:raises ValueError: if class already exists and replace_if_present=False
"""
Expand All @@ -83,8 +82,21 @@ def add_class(

if isinstance(cls, str):
cls = ClassDefinition(cls, **kwargs)
if isinstance(cls, dict):
elif isinstance(cls, dict):
cls = ClassDefinition(**{**cls, **kwargs})
else:
# Ensure that `cls` is a `ClassDefinition` object
if not isinstance(cls, ClassDefinition):
msg = (
f"cls must be a string, dict, or ClassDefinition, "
f"not {type(cls)!r}"
)
raise TypeError(msg)

cls_as_dict = {f.name: getattr(cls, f.name) for f in fields(cls)}

cls = ClassDefinition(**{**cls_as_dict, **kwargs})

if cls.name in self.schema.classes and not replace_if_present:
raise ValueError(f"Class {cls.name} already exists")
self.schema.classes[cls.name] = cls
Expand Down

0 comments on commit 634286f

Please sign in to comment.