hiroaki-yamamoto / mongoengine-goodjson

More human-readable json serializer/deserializer for MongoEngine
MIT License
62 stars 19 forks source link

compatibility problems #323

Open dsm054 opened 2 years ago

dsm054 commented 2 years ago

In modern versions of pymongo, they've abandoned support for Python 2, and removed the py3 compatibility libraries. This means that in mongoengine-goodjson 1.1.8,

from bson import (
    ObjectId, DBRef, RE_TYPE, Regex, MinKey, MaxKey, Timestamp, Code, Binary,
    PY3, SON
)
from bson.py3compat import text_type, string_type

will fail because of PY3 and bson.py3compat no longer existing. For example:

>>> import pymongo
>>> pymongo.__version__
'4.0'
>>> import mongoengine_goodjson
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/appuser/conda/envs/esg_web/lib/python3.10/site-packages/mongoengine_goodjson/__init__.py", line 6, in <module>
    from .encoder import GoodJSONEncoder
  File "/home/appuser/conda/envs/esg_web/lib/python3.10/site-packages/mongoengine_goodjson/encoder.py", line 16, in <module>
    from bson import (
ImportError: cannot import name 'PY3' from 'bson' (/home/appuser/conda/envs/esg_web/lib/python3.10/site-packages/bson/__init__.py)

At this point I think losing support for 2.7 entirely would be more than justified, but if it's desired to preserve that, we can simply embed the few lines that are actually being used:

PY3 = sys.version_info[0] == 3
string_type, text_type = (str, str) if PY3 else (basestring, unicode)

and leave everything else alone.

To work around this, right now I'm having to do something like

try:
    bson.PY3
except AttributeError:
    bson.PY3 = True
try:
    import bson.py3compat
except ImportError:
    mod = types.ModuleType('bson.py3compat')
    mod.string_type = str
    mod.text_type = str
    bson.py3compat = mod
    sys.modules['bson.py3compat'] = mod

on the client side (as well as a collections.Iterable = collections.abc.Iterable elsewhere, to deal with that issue) but obviously that's not optimal.

arankaren commented 2 years ago

@dsm054 exactly, mongoengine-goodjson is support <=3.12.3 versions of pymongo :cry:

LaundroMat commented 1 year ago

For what it's worth, I'm using the current master branch and it works fine for my use case (I'm only using the Document document). I just had to change my import statement to

 from mongoengine_goodjson.document import Document as gjDocument

and have my documents inherit from gjDocument.