Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

compatibility problems #323

Open
dsm054 opened this issue Feb 26, 2022 · 2 comments
Open

compatibility problems #323

dsm054 opened this issue Feb 26, 2022 · 2 comments

Comments

@dsm054
Copy link

dsm054 commented Feb 26, 2022

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
Copy link

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

@LaundroMat
Copy link

LaundroMat commented Dec 12, 2022

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants