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

tickets/DM-11328 #52

Merged
merged 1 commit into from
Jun 15, 2018
Merged
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
16 changes: 14 additions & 2 deletions python/lsst/utils/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import sys
import types

import numpy as np

__all__ = ("continueClass", "inClass", "TemplateMeta")


Expand Down Expand Up @@ -265,8 +267,18 @@ def __call__(self, *args, **kwds):
# the abstract base class.
# If the ABC defines a "TEMPLATE_PARAMS" attribute, we use those strings
# as the kwargs we should intercept to find the right type.
key = tuple(kwds.pop(p, d) for p, d in zip(self.TEMPLATE_PARAMS,
self.TEMPLATE_DEFAULTS))

# Generate a type mapping key from input keywords. If the type returned
# from the keyword lookup is a numpy dtype object, fetch the underlying
# type of the dtype
key = []
for p, d in zip(self.TEMPLATE_PARAMS, self.TEMPLATE_DEFAULTS):
tempKey = kwds.pop(p, d)
if isinstance(tempKey, np.dtype):
tempKey = tempKey.type
key.append(tempKey)
key = tuple(key)

# indices are only tuples if there are multiple elements
cls = self._registry.get(key[0] if len(key) == 1 else key, None)
if cls is None:
Expand Down
12 changes: 8 additions & 4 deletions tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,14 @@ def testInheritanceHooks(self):

def testConstruction(self):
self.register()
f = self.Example(dtype=np.float32)
self.assertIsInstance(f, self.Example)
self.assertIsInstance(f, self.ExampleF)
self.assertNotIsInstance(f, self.ExampleD)
f1 = self.Example(dtype=np.float32)
# Test that numpy dtype objects resolve to their underlying type
f2 = self.Example(dtype=np.dtype(np.float32))
for f in (f1, f2):
self.assertIsInstance(f, self.Example)
self.assertIsInstance(f, self.ExampleF)
self.assertNotIsInstance(f, self.ExampleD)

with self.assertRaises(TypeError):
self.Example()
with self.assertRaises(TypeError):
Expand Down