Skip to content

Commit

Permalink
Add keyCheck user callback function to ConfigDictField
Browse files Browse the repository at this point in the history
  • Loading branch information
enourbakhsh committed Dec 11, 2024
1 parent d603f9c commit f377f1d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion python/lsst/pex/config/configDictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ def __setitem__(self, k, x, at=None, label="setitem", setHistory=True):
)
raise FieldValidationError(self._field, self._config, msg)

# validate key using keycheck
if self._field.keyCheck is not None and not self._field.keyCheck(k):
msg = f"Key {k!r} is not a valid key"
raise FieldValidationError(self._field, self._config, msg)

if at is None:
at = getCallStack()
name = _joinNamePath(self._config._name, self._field.name, k)
Expand Down Expand Up @@ -127,6 +132,8 @@ class ConfigDictField(DictField):
Default is `True`.
dictCheck : `~collections.abc.Callable` or `None`, optional
Callable to check a dict.
keyCheck : `~collections.abc.Callable` or `None`, optional
Callable to check a key.
itemCheck : `~collections.abc.Callable` or `None`, optional
Callable to check an item.
deprecated : None or `str`, optional
Expand All @@ -140,7 +147,8 @@ class ConfigDictField(DictField):
- ``keytype`` or ``itemtype`` arguments are not supported types
(members of `ConfigDictField.supportedTypes`.
- ``dictCheck`` or ``itemCheck`` is not a callable function.
- ``dictCheck``, ``keyCheck`` or ``itemCheck`` is not a callable
function.
See Also
--------
Expand Down Expand Up @@ -172,6 +180,7 @@ def __init__(
default=None,
optional=False,
dictCheck=None,
keyCheck=None,
itemCheck=None,
deprecated=None,
):
Expand All @@ -191,12 +200,15 @@ def __init__(
raise ValueError(f"'itemtype' {_typeStr(itemtype)} is not a supported type")
if dictCheck is not None and not hasattr(dictCheck, "__call__"):
raise ValueError("'dictCheck' must be callable")
if keyCheck is not None and not hasattr(keyCheck, "__call__"):
raise ValueError("'keyCheck' must be callable")
if itemCheck is not None and not hasattr(itemCheck, "__call__"):
raise ValueError("'itemCheck' must be callable")

self.keytype = keytype
self.itemtype = itemtype
self.dictCheck = dictCheck
self.keyCheck = keyCheck
self.itemCheck = itemCheck

def rename(self, instance):
Expand All @@ -210,6 +222,9 @@ def validate(self, instance):
value = self.__get__(instance)
if value is not None:
for k in value:
if self.keyCheck is not None and not self.keyCheck(k):
msg = f"Key {k!r} is not a valid key"
raise FieldValidationError(self, instance, msg)
item = value[k]
item.validate()
if self.itemCheck is not None and not self.itemCheck(item):
Expand Down

0 comments on commit f377f1d

Please sign in to comment.