leosussan / fastapi-gino-arq-uvicorn

High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (w/ Redis and PostgreSQL).
MIT License
420 stars 44 forks source link

sqlalchemy_utils generic_repr breaks repr for gino models #16

Open asfaltboy opened 4 years ago

asfaltboy commented 4 years ago

While playing around with the gino ORM on the shell, I kept encountering the NoInspectionAvailable error. Eventually, I traced this back to @generic_repr decorator and removing it fixed the problem. Here is the full stack trace:


In [1]: from app.settings.globals import ARQ_BACKGROUND_FUNCTIONS, DATABASE_CONFIG
   ...: from app.models.orm.user import User
   ...: from app.application import app, db
   ...: await db.set_bind(DATABASE_CONFIG.url)
   ...: await User.get(1)
Out[1]: -------------------------------------------------
NoInspectionAvailable                     Traceback (most recent call last)
  /my_env/lib/python3.8/site-packages/IPython/core/formatters.py(702)__call__()
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

  /my_env/lib/python3.8/site-packages/IPython/lib/pretty.py(394)pretty()
    392                         if cls is not object \
    393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
    395
    396             return _default_pprint(obj, self, cycle)

  /my_env/lib/python3.8/site-packages/IPython/lib/pretty.py(700)_repr_pprint()
    698     """A pprint that just redirects to the normal repr function."""
    699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
    701     lines = output.splitlines()
    702     with p.group():

  /my_env/lib/python3.8/site-packages/sqlalchemy_utils/models.py(89)<lambda>()
     87     if len(fields) == 1 and callable(fields[0]):
     88         target = fields[0]
---> 89         target.__repr__ = lambda self: _generic_repr_method(self, fields=None)
     90         return target
     91     else:

  /my_env/lib/python3.8/site-packages/sqlalchemy_utils/models.py(42)_generic_repr_method()
     40
     41 def _generic_repr_method(self, fields):
---> 42     state = sa.inspect(self)
     43     field_reprs = []
     44     if not fields:

> /my_env/lib/python3.8/site-packages/sqlalchemy/inspection.py(71)inspect()
     69
     70     if raiseerr and (reg is None or ret is None):
---> 71         raise exc.NoInspectionAvailable(
     72             "No inspection system is "
     73             "available for object of type %s" % type_

NoInspectionAvailable: No inspection system is available for object of type <class 'app.models.orm.user.User'>```
leosussan commented 4 years ago

Thank you for this! I'm going to update this repo w/ some updates that'll include a fix to this, will check it out.