Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-41049: Allow derived types in ConfigDictField items #107

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 23.12.0
rev: 23.12.1
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.10
language_version: python3.11
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.8
rev: v0.1.14
hooks:
- id: ruff
- repo: https://github.com/numpy/numpydoc
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/pex/config/configDictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __setitem__(self, k, x, at=None, label="setitem", setHistory=True):

# validate itemtype
dtype = self._field.itemtype
if type(x) is not self._field.itemtype and x != self._field.itemtype:
if not isinstance(x, self._field.itemtype) and x != self._field.itemtype:
msg = "Value {} at key {!r} is of incorrect type {}. Expected type {}".format(
x,
k,
Expand All @@ -89,7 +89,7 @@ def __setitem__(self, k, x, at=None, label="setitem", setHistory=True):
if x == dtype:
self._dict[k] = dtype(__name=name, __at=at, __label=label)
else:
self._dict[k] = dtype(__name=name, __at=at, __label=label, **x._storage)
self._dict[k] = type(x)(__name=name, __at=at, __label=label, **x._storage)
if setHistory:
self.history.append(("Added item at key %s" % k, at, label))
else:
Expand Down
5 changes: 3 additions & 2 deletions python/lsst/pex/config/dictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ def __init__(self, config, field, value, at, label, setHistory=True):
for k in value:
# do not set history per-item
self.__setitem__(k, value[k], at=at, label=label, setHistory=False)
except TypeError:
msg = f"Value {value} is of incorrect type {_typeStr(value)}. Mapping type expected."
except TypeError as exc:
msg = (f"Setting {k} raised an exception:\n{exc}\nPossible cause: "
f"Value {value} is of incorrect type {_typeStr(value)}. Mapping type expected.")
raise FieldValidationError(self._field, self._config, msg)
if setHistory:
self._history.append((dict(self._dict), at, label))
Expand Down
28 changes: 19 additions & 9 deletions tests/test_Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ class OuterConfig(InnerConfig, pexConfig.Config):

i = pexConfig.ConfigField("Outer.i", InnerConfig)

def __init__(self):
pexConfig.Config.__init__(self)
self.i.f = 5.0
# This does not work - it breaks usage as a type even in ConfigField
# def __init__(self):
# pexConfig.Config.__init__(self)
# self.i.f = 5.0

def setDefaults(self):
self.i = InnerConfig(f=5.0)

def validate(self):
pexConfig.Config.validate(self)
Expand All @@ -101,6 +105,7 @@ class Complex(pexConfig.Config):
"""A complex config for testing."""

c = pexConfig.ConfigField("an inner config", InnerConfig)
d = pexConfig.ConfigDictField[str, InnerConfig](doc="a configdictfield of inner", default={})
r = pexConfig.ConfigChoiceField(
"a registry field", typemap=GLOBAL_REGISTRY, default="AAA", optional=False
)
Expand All @@ -120,14 +125,19 @@ def setUp(self):
self.simple = Simple()
self.inner = InnerConfig()
self.outer = OuterConfig()
self.comp = Complex()
self.comp_kwargs = {
"d": {"outer": self.outer},
}
self.comp = Complex(**self.comp_kwargs)
self.deprecation = Deprecation()

def tearDown(self):
del self.simple
del self.inner
del self.outer
del self.comp_kwargs
del self.comp
del self.deprecation

def testFieldTypeAnnotationRuntime(self):
# test parsing type annotation for runtime dtype
Expand Down Expand Up @@ -341,7 +351,7 @@ def testSave(self):
roundtrip_path = os.path.join(tmpdir, "roundtrip.test")
self.comp.save(roundtrip_path)

roundTrip = Complex()
roundTrip = Complex(**self.comp_kwargs)
roundTrip.load(roundtrip_path)
self.assertEqual(self.comp.c.f, roundTrip.c.f)
self.assertEqual(self.comp.r.name, roundTrip.r.name)
Expand All @@ -351,7 +361,7 @@ def testSave(self):
roundtrip_path = os.path.join(tmpdir, "roundtrip_open.test")
with open(roundtrip_path, "w") as outfile:
self.comp.saveToStream(outfile)
roundTrip = Complex()
roundTrip = Complex(**self.comp_kwargs)
with open(roundtrip_path) as infile:
roundTrip.loadFromStream(infile)
self.assertEqual(self.comp.c.f, roundTrip.c.f)
Expand All @@ -362,7 +372,7 @@ def testSave(self):
roundtrip_path = os.path.join(tmpdir, "roundtrip_def.test")
with open(roundtrip_path, "w") as outfile:
self.comp.saveToStream(outfile, root="root")
roundTrip = Complex()
roundTrip = Complex(**self.comp_kwargs)
with self.assertRaises(NameError):
roundTrip.load(roundtrip_path)
roundTrip.load(roundtrip_path, root="root")
Expand All @@ -374,7 +384,7 @@ def testSave(self):
saved_string += "config.c.f = parameters.value"
namespace = SimpleNamespace(value=7)
extraLocals = {"parameters": namespace}
roundTrip = Complex()
roundTrip = Complex(**self.comp_kwargs)
roundTrip.loadFromString(saved_string, extraLocals=extraLocals)
self.assertEqual(namespace.value, roundTrip.c.f)
self.assertEqual(self.comp.r.name, roundTrip.r.name)
Expand Down Expand Up @@ -528,7 +538,7 @@ def testYaml(self):
self.assertEqual(self.comp.c.f, comp.c.f)

def testCompare(self):
comp2 = Complex()
comp2 = Complex(**self.comp_kwargs)
inner2 = InnerConfig()
simple2 = Simple()
self.assertTrue(self.comp.compare(comp2))
Expand Down
Loading