Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dismiss type check during create_map #23

Open
CristhianMotoche opened this issue Nov 2, 2019 · 4 comments
Open

Dismiss type check during create_map #23

CristhianMotoche opened this issue Nov 2, 2019 · 4 comments

Comments

@CristhianMotoche
Copy link

Hello!

I've been looking for a library like this one and I think it can be pretty useful. I want to use it to convert an object that is mapped from a table of my database (I'm using Tortoise ORM for this):

from tortoise.models import Model
from tortoise import fields

class SessionTable(Model):
    __tablename__ = 'sessions'

    id = fields.IntField(pk=True)
    key = fields.CharField(max_length=8)

to a simple plain object that I use in my use cases (business logic):

@dataclass
class Session:
   id:int
   key:str

Then, my mapper should be something like:

mapper = ObjectMapper()
mapper.create_map(SessionTable, Session)
mapper.create_map(Session, SessionTable)
...
instance_session = mapper.map(session_table)
instance_sessiontable = mapper.map(session)

But, SessionTable isn't a type:

>>> type(SessionTable)
<class 'tortoise.models.ModelMeta'>

so, it fails with ObjectMapperException: type_from must be a type and ObjectMapperException: type_to must be a type during the map creation create_map.

Does it make sense to add a flag to avoid those asserts?

def create_map(self, type_from, type_to, mapping=None, allow_from_any = False, allow_to_any = False):
    ...
    if allow_from_any or (type(type_from) is not type):
       ...
    if allow_to_any or (type(type_to) is not type):
       ...

Although, I'm not sure if that would work since all data members need a default value. BTW, is it possible to solve that issue? If it is, I'd like to give it a try.

@CristhianMotoche CristhianMotoche changed the title Dismiss type check during map Dismiss type check during create_map Nov 2, 2019
@asyncee
Copy link

asyncee commented Feb 4, 2020

I double this, faced same issue with pydantic and sqlalchemy. Is there are a change for this to be fixed?

@michaelwiles
Copy link

Yes Please! Same here

@leoalvs
Copy link

leoalvs commented Jan 16, 2021

I faced this issue, solved using 1.0.7 version

@tylertjburns
Copy link

tylertjburns commented Aug 3, 2021

i changed the lines from:

         if type(type_from) is not type:
            raise ObjectMapperException("type_from must be a type")

         if type(type_to) is not type:
            raise ObjectMapperException("type_to must be a type")

to:

      if (not isinstance(type_from, type)):
            raise ObjectMapperException("type_from must be a type")

      if (not isinstance(type_to, type)):
          raise ObjectMapperException("type_to must be a type")

this worked well for me.

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

No branches or pull requests

5 participants