Skip to content

Commit 14c0847

Browse files
committed
Allow python-only attributes in a HybridClass by defining them with _extra_fields_in_to_dict
1 parent ac3901b commit 14c0847

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

xobjects/hybrid_class.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ def to_dict(self, copy_to_cpu=True):
292292

293293
skip_fields = set(getattr(obj, "_skip_in_to_dict", []))
294294
additional_fields = set(getattr(obj, "_store_in_to_dict", []))
295+
extra_fields = set(getattr(obj, "_extra_fields_in_to_dict", []))
295296
fields_to_store = (set(obj._fields) - skip_fields) | additional_fields
297+
fields_to_store |= extra_fields
296298

297299
defaults = {}
298300
for field in obj._XoStruct._fields:
@@ -344,7 +346,21 @@ def copy(self, _context=None, _buffer=None, _offset=None):
344346
xobject = self._XoStruct(
345347
self._xobject, _context=_context, _buffer=_buffer, _offset=_offset
346348
)
347-
return self.__class__(_xobject=xobject)
349+
# Get the python-only attributes
350+
additional_fields = set(getattr(self, "_extra_fields_in_to_dict", []))
351+
python_kwargs = {}
352+
for field in additional_fields:
353+
vv = getattr(self, field)
354+
if hasattr(vv, "copy"):
355+
try:
356+
python_kwargs[field] = vv.copy(
357+
_context=_context, _buffer=_buffer, _offset=_offset
358+
)
359+
except TypeError:
360+
python_kwargs[field] = vv.copy()
361+
else:
362+
python_kwargs[field] = vv
363+
return self.__class__(_xobject=xobject, **python_kwargs)
348364

349365
@property
350366
def _buffer(self):

0 commit comments

Comments
 (0)