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

AttributeError: 'Function' object has no attribute 'items' #244

Open
DeoLeung opened this issue Mar 5, 2019 · 3 comments
Open

AttributeError: 'Function' object has no attribute 'items' #244

DeoLeung opened this issue Mar 5, 2019 · 3 comments

Comments

@DeoLeung
Copy link

DeoLeung commented Mar 5, 2019

from geoalchemy2 import WKBElement
import simplejson as json
json.dumps({'a': [WKBElement('abcde')]})

I tried to use the default as parser to due with WKBElement, but the code is not going into the parser, how could I do the customized serialization?

@etrepum
Copy link
Member

etrepum commented Mar 5, 2019

I'm not familiar with that library. What is the output of print(repr(WKBElement.__bases__))? What versions of simplejson, geoalchemy2, sqlalchemy, and python are you using?

@DeoLeung
Copy link
Author

DeoLeung commented Mar 5, 2019

(<class 'geoalchemy2.elements._SpatialElement'>, <class 'sqlalchemy.sql.functions.Function'>)
simplejson                3.16.0
GeoAlchemy2               0.5.0
python 3.7.2
SQLAlchemy                1.2.18

Thank you~

@etrepum
Copy link
Member

etrepum commented Mar 5, 2019

I'm afraid that this is awkward with recent versions of simplejson because of the way that sqlalchemy.sql.functions.Function implements __getattr__. The issue is that for any unknown attribute, Function.attribute returns a callable, which for certain properties (such as _asdict and for_json in this case), simplejson thinks the result of that function should be a dict-like or json serializable object but it isn't.

The workaround in this case is to turn off the namedtuple_as_object and for_json options.

json.dumps({'a': [WKBElement('abcde')]}, namedtuple_as_object=False, for_json=False)

This will ensure that these instances will get passed to default.

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

2 participants