diff --git a/Quaternion/Quaternion.py b/Quaternion/Quaternion.py index f73c872..947ffd6 100644 --- a/Quaternion/Quaternion.py +++ b/Quaternion/Quaternion.py @@ -62,7 +62,8 @@ class QuatDescr: """Descriptor for an attribute that is a Quat. - This allows setting the attribute with any QuatLike value. + This allows setting the attribute with any QuatLike value. If not set, the + attribute is ``None``. Examples -------- @@ -75,6 +76,9 @@ class QuatDescr: >>> obj = MyClass(att=[10, 20, 30]) >>> obj.att + >>> obj = MyClass() + >>> obj.att is None + True """ def __set_name__(self, owner, name): @@ -83,10 +87,12 @@ def __set_name__(self, owner, name): def __get__(self, obj, type): if obj is None: return None - return getattr(obj, self._name) + return getattr(obj, self._name, None) def __set__(self, obj, value): - setattr(obj, self._name, Quat(value)) + if value is not None: + value = Quat(value) + setattr(obj, self._name, value) @numba.njit(cache=True) diff --git a/Quaternion/tests/test_all.py b/Quaternion/tests/test_all.py index 63f91db..c678148 100644 --- a/Quaternion/tests/test_all.py +++ b/Quaternion/tests/test_all.py @@ -715,13 +715,16 @@ def test_quat_mult(): assert np.allclose(q01_0, q01_1, rtol=0, atol=1e-10) -def test_cxotime_descr(): +def test_quat_descriptor(): from dataclasses import dataclass @dataclass class MyClass: quat: Quat = QuatDescr() + obj = MyClass() + assert obj.quat is None + obj = MyClass(quat=[10, 20, 30]) assert isinstance(obj.quat, Quat) assert obj.quat.ra == 10