Skip to content

Commit aa7a8f1

Browse files
committed
renaming mro to inheritance
1 parent 66b29b7 commit aa7a8f1

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
* Added `mro` field to `__jsondump__` of `compas.datastructures.Datastructure` to allow for deserialization to closest available superclass of custom datastructures.
12+
* Added `inheritance` field to `__jsondump__` of `compas.datastructures.Datastructure` to allow for deserialization to closest available superclass of custom datastructures.
1313

1414
### Changed
1515

src/compas/data/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __dtype__(self):
7676
return "{}/{}".format(".".join(self.__class__.__module__.split(".")[:2]), self.__class__.__name__)
7777

7878
@classmethod
79-
def __cls_dtype__(cls):
79+
def __clstype__(cls):
8080
return "{}/{}".format(".".join(cls.__module__.split(".")[:2]), cls.__name__)
8181

8282
@property

src/compas/data/encoders.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
numpy_support = False
4141

4242

43-
def cls_from_dtype(dtype, mro=None): # type: (...) -> Type[Data]
43+
def cls_from_dtype(dtype, inheritance=None): # type: (...) -> Type[Data]
4444
"""Get the class object corresponding to a COMPAS data type specification.
4545
4646
Parameters
4747
----------
4848
dtype : str
4949
The data type of the COMPAS object in the following format:
5050
'{}/{}'.format(o.__class__.__module__, o.__class__.__name__).
51-
mro : list[str], optional
52-
The MRO of the class, all superclasses of the class that can be used if given dtype is not found.
51+
inheritance : list[str], optional
52+
The inheritance chain of this class, a list of superclasses that can be used if given dtype is not found.
5353
5454
Returns
5555
-------
@@ -66,12 +66,12 @@ def cls_from_dtype(dtype, mro=None): # type: (...) -> Type[Data]
6666
6767
"""
6868

69-
if mro is None:
70-
full_mro = [dtype]
69+
if inheritance is None:
70+
full_inheritance = [dtype]
7171
else:
72-
full_mro = [dtype] + mro
72+
full_inheritance = [dtype] + inheritance
7373

74-
for dtype in full_mro:
74+
for dtype in full_inheritance:
7575
mod_name, attr_name = dtype.split("/")
7676
try:
7777
module = __import__(mod_name, fromlist=[attr_name])
@@ -81,7 +81,7 @@ def cls_from_dtype(dtype, mro=None): # type: (...) -> Type[Data]
8181
except AttributeError:
8282
continue
8383

84-
raise ValueError("No class found in MRO: {}".format(mro))
84+
raise ValueError("No class found in inheritance chain: {}".format(full_inheritance))
8585

8686

8787
class DataEncoder(json.JSONEncoder):
@@ -236,7 +236,7 @@ def object_hook(self, o):
236236
return o
237237

238238
try:
239-
cls = cls_from_dtype(o["dtype"], o.get("mro", None))
239+
cls = cls_from_dtype(o["dtype"], o.get("inheritance", None))
240240

241241
except ValueError:
242242
raise DecoderError(

src/compas/datastructures/datastructure.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,25 @@ def __init__(self, attributes=None, name=None):
2121
self._aabb = None
2222
self._obb = None
2323

24-
def __get_mro__(self):
25-
mro = []
24+
@property
25+
def __inheritance__(self):
26+
"""Get the inheritance chain of the datastructure.
27+
Until one level above the Datastructure class (eg. Mesh, Graph, ...).
28+
29+
Returns
30+
-------
31+
list[str]
32+
The inheritance chain of the datastructure.
33+
34+
"""
35+
inheritance = []
2636
for cls in self.__class__.__mro__:
2737
if cls == self.__class__:
2838
continue
2939
if cls == Datastructure:
3040
break
31-
mro.append(cls.__cls_dtype__())
32-
return mro
41+
inheritance.append(cls.__clstype__())
42+
return inheritance
3343

3444
def __jsondump__(self, minimal=False):
3545
"""Return the required information for serialization with the COMPAS JSON serializer.
@@ -47,7 +57,7 @@ def __jsondump__(self, minimal=False):
4757
state = {
4858
"dtype": self.__dtype__,
4959
"data": self.__data__,
50-
"mro": self.__get_mro__(),
60+
"inheritance": self.__inheritance__,
5161
}
5262
if minimal:
5363
return state

tests/compas/datastructures/test_datastructure.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ def test_mro_fallback(level2):
115115
return
116116

117117
assert level2.__jsondump__()["dtype"] == "test_datastructure/Level2"
118-
# Level2 should serialize Level1 into the mro
119-
assert level2.__jsondump__()["mro"] == ["test_datastructure/Level1"]
118+
# Level2 should serialize Level1 into the inheritance
119+
assert level2.__jsondump__()["inheritance"] == ["test_datastructure/Level1"]
120120

121121
dumped = json_dumps(level2)
122122
loaded = json_loads(dumped)
123123

124124
# The loaded object should be deserialized as the closes available class: Level1
125125
assert loaded.__class__ == Level1
126126
assert loaded.__jsondump__()["dtype"] == "test_datastructure/Level1"
127-
assert loaded.__jsondump__()["mro"] == []
127+
assert loaded.__jsondump__()["inheritance"] == []
128128

129129
# level1 attributes should still be available
130130
assert loaded.level1_attr == "level1"
@@ -139,16 +139,16 @@ def test_mro_fallback_multi_level(level3):
139139
return
140140

141141
assert level3.__jsondump__()["dtype"] == "test_datastructure/Level3"
142-
# Level3 should serialize Level2 and Level1 into the mro
143-
assert level3.__jsondump__()["mro"] == ["test_datastructure/Level2", "test_datastructure/Level1"]
142+
# Level3 should serialize Level2 and Level1 into the inheritance
143+
assert level3.__jsondump__()["inheritance"] == ["test_datastructure/Level2", "test_datastructure/Level1"]
144144

145145
dumped = json_dumps(level3)
146146
loaded = json_loads(dumped)
147147

148148
# The loaded object should be deserialized as the closes available class: Level1
149149
assert loaded.__class__ == Level1
150150
assert loaded.__jsondump__()["dtype"] == "test_datastructure/Level1"
151-
assert loaded.__jsondump__()["mro"] == []
151+
assert loaded.__jsondump__()["inheritance"] == []
152152

153153
# level1 attributes should still be available
154154
assert loaded.level1_attr == "level1"
@@ -161,13 +161,13 @@ def test_mro_fallback_multi_level(level3):
161161
def test_custom_mesh(custom_mesh):
162162
# This test should pass both Python and IronPython
163163
assert custom_mesh.__jsondump__()["dtype"].endswith("CustomMesh")
164-
assert custom_mesh.__jsondump__()["mro"] == ["compas.datastructures/Mesh"]
164+
assert custom_mesh.__jsondump__()["inheritance"] == ["compas.datastructures/Mesh"]
165165
assert custom_mesh.__jsondump__()["data"]["custom_mesh_attr"] == "custom_mesh"
166166

167167
dumped = json_dumps(custom_mesh)
168168
loaded = json_loads(dumped)
169169

170170
assert loaded.__class__ == Mesh
171171
assert loaded.__jsondump__()["dtype"] == "compas.datastructures/Mesh"
172-
assert loaded.__jsondump__()["mro"] == []
172+
assert loaded.__jsondump__()["inheritance"] == []
173173
assert not hasattr(loaded, "custom_mesh_attr")

0 commit comments

Comments
 (0)