object-mapper icon indicating copy to clipboard operation
object-mapper copied to clipboard

Mapping to and from a class with a custom metaclass is not allowed

Open toro2k opened this issue 5 years ago • 1 comments

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?

toro2k avatar Aug 28 '19 09:08 toro2k

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

AlirezaRoshanzamir avatar Nov 04 '20 14:11 AlirezaRoshanzamir