Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions labtech/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ def deserialize_enum(self, serialized: dict[str, jsonable]) -> Enum:
return enum_cls[name]

def serialize_class(self, cls: type) -> jsonable:
if '<locals>' in cls.__qualname__:
raise SerializationError(
(f'Unable to serialize class "{cls.__qualname__}" because it was defined in a function. '
'Please ensure all task and parameter classes are defined at the top-level of a module.')
)

if '>' in cls.__qualname__:
# This should never happen, but is here for safety.
raise SerializationError('Unexpected ">" in class __qualname__')

# Handle nested classes by splitting class nesting path by ">".
return f'{cls.__module__}.{cls.__qualname__.replace(".", ">")}'

Expand Down
18 changes: 18 additions & 0 deletions tests/labtech/test_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from labtech.exceptions import SerializationError
from labtech.serialization import Serializer


class TestSerializer:

class TestSerializeClass:

def test_local_object(self):

class Example:
pass

serializer = Serializer()
with pytest.raises(SerializationError, match=f'Unable to serialize class "{Example.__qualname__}" because it was defined in a function.'):
serializer.serialize_class(Example)