Skip to content

Commit

Permalink
Allow quat descriptor attribute to be None
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Dec 17, 2023
1 parent 3e50019 commit cfc32a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions Quaternion/Quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand All @@ -75,6 +76,9 @@ class QuatDescr:
>>> obj = MyClass(att=[10, 20, 30])
>>> obj.att
<Quat q1=0.26853582 q2=-0.14487813 q3=0.12767944 q4=0.94371436>
>>> obj = MyClass()
>>> obj.att is None
True
"""

def __set_name__(self, owner, name):
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion Quaternion/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cfc32a2

Please sign in to comment.