Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e882fc2

Browse files
committed
fix parent setter
1 parent 0865e96 commit e882fc2

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

magpylib/_src/input_checks.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ def validate_field_lambda(val, bh):
143143
return val
144144

145145

146+
def check_input_parent(inp):
147+
"""
148+
parent input must be None or Collection.
149+
return - true if its a collection
150+
- false if its None
151+
raise error if neither
152+
"""
153+
if (getattr(inp, "_object_type", "") == "Collection"):
154+
return True
155+
elif inp is None:
156+
return False
157+
158+
raise MagpylibBadUserInput(
159+
"Input `parent` must be `None` or a `Collection` object."
160+
f"Instead received {type(inp)}."
161+
)
162+
146163
#################################################################
147164
#################################################################
148165
# CHECK - FORMAT

magpylib/_src/obj_classes/class_BaseGeo.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from magpylib._src.input_checks import (
1313
check_format_input_orientation,
1414
check_format_input_vector,
15+
check_input_parent,
1516
)
16-
from magpylib._src.exceptions import MagpylibBadUserInput
1717

1818
from magpylib._src.utility import add_iteration_suffix
1919

@@ -142,17 +142,14 @@ def parent(self):
142142

143143
@parent.setter
144144
def parent(self, inp):
145-
if inp is None:
146-
if self._parent is not None:
147-
self._parent.remove(self)
148-
self._parent = None
149-
elif getattr(inp, "_object_type", "") == "Collection":
150-
inp.add(inp)
151-
else:
152-
raise MagpylibBadUserInput(
153-
f"The `parent` property of {type(self).__name__} must be a Collection."
154-
f"Instead received {inp!r} of type {type(inp).__name__}"
155-
)
145+
is_collection = check_input_parent(inp)
146+
147+
if self._parent is not None:
148+
self._parent.remove(self)
149+
self._parent = None
150+
151+
if is_collection:
152+
inp.add(self)
156153

157154
@property
158155
def position(self):

magpylib/_src/obj_classes/class_Collection.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ def add(self, *children, override_parent=False):
190190
191191
Parameters
192192
----------
193-
children: source, `Sensor` or `Collection` objects or arbitrary lists thereof
193+
children: sources, sensors or collections or arbitrary lists thereof
194194
Add arbitrary sources, sensors or other collections to this collection.
195-
Duplicate children will automatically be eliminated.
196195
197196
Returns
198197
-------
@@ -216,7 +215,7 @@ def add(self, *children, override_parent=False):
216215
obj_list = check_duplicates(obj_list)
217216
# assign parent
218217
for obj in obj_list:
219-
if obj._parent is None or override_parent:
218+
if (obj._parent is None) or override_parent:
220219
obj._parent = self
221220
else:
222221
raise ValueError(
@@ -284,7 +283,7 @@ def _remove(parent, child, recursive=True, issearching=False):
284283
raise ValueError(f"""{parent}.remove({child}) : {child!r} not found.""")
285284

286285
def remove(self, child, recursive=True):
287-
"""Remove a specific child from the collection.
286+
"""Remove a specific object from the collection tree.
288287
289288
Parameters
290289
----------

0 commit comments

Comments
 (0)