Skip to content

Commit

Permalink
Added object, class, and function type validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Helveg committed Feb 23, 2023
1 parent 6f840d8 commit d5d772e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
56 changes: 39 additions & 17 deletions bsb/config/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ def type_handler(value, _parent=None, _key=None):
return type_handler


class object_(TypeHandler):
"""
Type validator. Attempts to import the value, absolute, or relative to the
`module_path` entries.
:param module_path: List of the modules that should be searched when doing a
relative import.
:type module_path: list[str]
:raises: TypeError when value can't be cast.
:returns: Type validator function
:rtype: Callable
"""

def __init__(self, module_path=None):
self._module_path = module_path

def __call__(self, value):
msg = f"Could not import object {value}."
try:
obj = _load_object(value, self._module_path)
obj._cfg_inv = value
except Exception:
raise TypeError(msg)
return obj

def __inv__(self, value):
return getattr(value, "_cfg_inv", value)

def __name__(self):
return "object"


class class_(TypeHandler):
"""
Type validator. Attempts to import the value as the name of a class, relative to
Expand Down Expand Up @@ -155,10 +187,10 @@ def __name__(self):
return "class"


class function_(TypeHandler):
class function_(object_):
"""
Type validator. Attempts to import the value, absolute, or relative to the
`module_path` entries.
`module_path` entries, and verifies that it is callable.
:param module_path: List of the modules that should be searched when doing a
relative import.
Expand All @@ -168,27 +200,17 @@ class function_(TypeHandler):
:rtype: Callable
"""

def __init__(self, module_path=None):
self._module_path = module_path

def __call__(self, value):
msg = f"Could not import {value} as a callable function."
try:
obj = _load_object(value, self._module_path)
except Exception:
raise TypeError(msg)
else:
if not callable(obj):
raise TypeError(msg)
return obj
obj = super().__call__(value)
if not callable(obj):
raise TypeError(f"Could not import {value} as a callable function.")
return obj

def __inv__(self, value):
if not inspect.isclass(value):
value = type(value)
return f"{value.__module__}.{value.__name__}"

def __name__(self):
return "class"
return "function"


def str(strip=False, lower=False, upper=False):
Expand Down
4 changes: 1 addition & 3 deletions bsb/simulators/neuron/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def create(self, id, pos, morpho, rot, additional):

@config.node
class ArborizedModel(NeuronCell, classmap_entry="arborize"):
model = config.attr(
type=types.class_(), required=lambda s: not ("relay" in s and s["relay"])
)
model = config.attr(type=types.object_(), required=True)
_schematics = {}

def create(self, id, pos, morpho, rot, additional):
Expand Down

0 comments on commit d5d772e

Please sign in to comment.