-
Notifications
You must be signed in to change notification settings - Fork 9
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
Class
property
#161
Comments
So the thing that would keep this from being an Yeah, I can see that making sense, though I agree the use cases are probably a bit limited. |
A draft of what I'm thinking is here: #163 Not sure if there's anything interesting we could be doing with |
This would really useful! Within SimPEG - we would use it for solvers. We use a class Class(properties.Property):
class_info = "a property that is an uninstantiated class"
def validate(self, instance, value):
if inspect.isclass(value) is False:
extra = (
"Expected an uninstantiated class. The provided value is not"
)
self.error(instance, value, TypeError, extra)
self._parent_module = value.__module__
return value
def serializer(self, value, **kwargs):
return "{}.{}".format(self._parent_module, value.__name__)
def deserializer(self, value, **kwargs):
name = value.split(".")
try:
module = sys.modules[".".join(name[:-1])]
except KeyError:
raise ImportError(
"{} not found. Please install {}".format(
".".join(value, name[0])
)
)
return getattr(module, name[-1]) I took a crack at implementing the serialization / deserialization piece, and then pretty quickly ended up rather deep in On the properties/properties/basic.py Lines 174 to 177 in 096b070
Also in the sphinx docs (this seems redundant if it is already going through the default setter?) properties/properties/basic.py Lines 687 to 691 in 096b070
This one on properties/properties/base/base.py Lines 397 to 398 in 096b070
Here is the current pass at a (rough) implementation within SimPEG: and the associated class that inherits I would be happy to help iterate or test out a pr if that is of interest. |
I think there is some value here? I don't think it's exceptionally common for a
HasProperties
instance to need aClass
property value (as opposed to anInstance
) but I think it's certainly a possibility.Traitlets has this, for what it's worth: https://github.com/ipython/traitlets/blob/4.3.2/traitlets/traitlets.py#L1527
The text was updated successfully, but these errors were encountered: