idank / explainshell

match command-line arguments to their help text
GNU General Public License v3.0
12.98k stars 778 forks source link

pymongo: use projection=[] instead of fields={} #323

Open cben opened 4 months ago

cben commented 4 months ago

With current pymongo==3.13.0, was causing exception:

> podman-compose exec web env PYTHONPATH=. python explainshell/manager.py --log info --verify
...
INFO:explainshell.store:creating store, db = 'explainshell', host = 'mongodb://db'
Traceback (most recent call last):
  File "explainshell/manager.py", line 201, in <module>
    sys.exit(main(args.files, args.db, args.host, args.overwrite, args.drop, args.verify))
  File "explainshell/manager.py", line 165, in main
    ok = s.verify()
  File "/opt/webapp/explainshell/store.py", line 372, in verify
    manpages = set([m['_id'] for m in self.manpage.find(fields={'_id':1})])
  File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 1696, in find
    return Cursor(self, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'fields'
exit code: 1

Now works:

> podman-compose exec web env PYTHONPATH=. python explainshell/manager.py --log info --verify
...
INFO:explainshell.store:creating store, db = 'explainshell', host = 'mongodb://db'
exit code: 0

I tested the other calls too; updatemanpage unreachable from current CLI but tested same call in another code path.

Not sure when pymongo API changed; oldest doc I found online https://pymongo.readthedocs.io/en/3.5.1/api/pymongo/collection.html#pymongo.collection.Collection.find which doesn't mention fields= only projection=. Probably broken since 8406b3189797a5b14059ce5dc8ffe2e12b4e9f28?

Change from {x: 1} to ['x'] style is cosmetic, reads less magic to me, but projection arg supports both.

idank commented 4 months ago

Thanks! Shouldn't the dependency be updated as well?

cben commented 4 months ago

This updates the code to match documented functionality for pymongo==3.13.0 currently in requirements.txt.

The dependency has already been bumped in commit 8406b3189797a5b14059ce5dc8ffe2e12b4e9f28 and I'm pretty convinced these code paths have been broken since :-D

Oh yes here it is in the docs:

Changed in version 3.0: Changed the parameter names spec, fields, timeout, and partial to filter, projection, no_cursor_timeout, and allow_partial_results respectively. ...

Also here in pymongo 2->3 migration guide: https://pymongo.readthedocs.io/en/3.12.1/migrate-to-pymongo3.html#fields-renamed-projection