Skip to content

serialize and deserilize mro#1460

Merged
Licini merged 5 commits into
mainfrom
mro
May 7, 2025
Merged

serialize and deserilize mro#1460
Licini merged 5 commits into
mainfrom
mro

Conversation

@Licini
Copy link
Copy Markdown
Contributor

@Licini Licini commented May 6, 2025

This is to add the ability to deserializing a datastructure implementation like Mesh, Graph, Cellnetwork etc. to its closest available parent class.

For example we want to be able to serialize a FormDiagram object using compas_tna into a json. And open same json in another environment without compas_tna installed, while loading the same object as its closest available class Mesh.

An example behaviour:

from compas.datastructures import Mesh
from compas import json_dumps
from compas import json_loads

# Some custom class that inherits from Mesh but is not available when deserializing
class MyCustomMesh(Mesh):
    pass

mesh = MyCustomMesh()
serialized = json_dumps(mesh)

# delete the access to our custom class
del MyCustomMesh

# fallback to Mesh if MyCustomMesh is not available
loaded = json_loads(serialized)
assert loaded.__class__ == Mesh

This is done through inserting an mro (method resolving order) field into the json dump state, so the decoder can later pickup and use as a backup if dtype is not directly found

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2025

Codecov Report

Attention: Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 61.92%. Comparing base (d773b3e) to head (ff7344e).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/compas/data/encoders.py 86.66% 2 Missing ⚠️
src/compas/datastructures/datastructure.py 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1460      +/-   ##
==========================================
+ Coverage   61.91%   61.92%   +0.01%     
==========================================
  Files         208      208              
  Lines       22334    22365      +31     
==========================================
+ Hits        13827    13850      +23     
- Misses       8507     8515       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/compas/data/data.py Outdated
return "{}/{}".format(".".join(self.__class__.__module__.split(".")[:2]), self.__class__.__name__)

@classmethod
def __cls_dtype__(cls):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__clstype__ or __ctype__ perhaps?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok clstype then, as ctype is a exiting function (C compatible data types)

Comment thread src/compas/data/encoders.py Outdated


def cls_from_dtype(dtype): # type: (...) -> Type[Data]
def cls_from_dtype(dtype, mro=None): # type: (...) -> Type[Data]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would not call this "mro". method resolution order refers to the mechanism that Python uses to determine which method should be called in a multiple inheritance scenario. but that is not what we do here...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, we use "inheritance" then

self._aabb = None
self._obb = None

def __get_mro__(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__clstree__ or __inheritance__ or something like that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, __inheritance__ and as property

@Licini
Copy link
Copy Markdown
Contributor Author

Licini commented May 7, 2025

@tomvanmele how does this look now?

@gonzalocasas
Copy link
Copy Markdown
Member

Very cool change! Could you please revise the data serialization doc page, and add this information? https://compas.dev/compas/latest/userguide/advanced.serialisation.html

@Licini Licini merged commit c1ac434 into main May 7, 2025
17 checks passed
@Licini Licini deleted the mro branch July 4, 2025 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants