object-mapper
object-mapper copied to clipboard
Mapping to and from a class with a custom metaclass is not allowed
The following the case fails:
from unittest import TestCase
from mapper.object_mapper import ObjectMapper
class MyMetaClass(type):
pass
class MyFromClass(metaclass=MyMetaClass):
pass
class MyToClass:
pass
class Test(TestCase):
def test_mapping_class_with_custom_metaclass(self):
mapper = ObjectMapper()
mapper.create_map(MyFromClass, MyToClass)
This behavior prevents to map to and from classes with a custom metaclass (i.e. Django models). Is it intended or the check in ObjectMapper.create_map is just too strict?
A more specific example is the use of classes inherited from an abstract class.
from abc import ABC, abstractmethod
from mapper.object_mapper import ObjectMapper
class Interface(ABC):
@abstractmethod
def foo(self):
pass
class FirstImplementation(Interface):
def __init__(self):
self.member = 1
def foo(self):
print('foo')
class SecondImplementation(Interface):
def __init__(self):
self.member = 1
def foo(self):
print('foo')
mapper = ObjectMapper()
mapper.create_map(FirstImplementation, SecondImplementation)
The above code raises:
mapper.object_mapper_exception.ObjectMapperException: type_from must be a type