Skip to content

PYTHON-2957 Support 'let' option for multiple CRUD commands #804

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

Merged
merged 20 commits into from
Nov 30, 2021

Conversation

juliusgeo
Copy link
Contributor

No description provided.

@juliusgeo juliusgeo marked this pull request as ready for review November 29, 2021 21:01
Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Just a couple comments

@@ -619,6 +619,9 @@ def _update(self, sock_info, criteria, document, upsert=False,
command = SON([('update', self.name),
('ordered', ordered),
('updates', [update_doc])])
if let:
common.validate_is_document_type("let", let)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the more generic validate_is_mapping instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we add a test to make sure that validation works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -795,6 +798,8 @@ def update_one(self, filter, update, upsert=False,
MongoDB 4.2 and above.
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.
- `let` (optional): Specifies a document with a list of variables
that can then be accessed using the form `$$<variable_name>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm my first impression is that this is very vague. Are there any docs that we can link to? Or any documentation guidance in the CRUD spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this with the docs from the CRUD spec?:

Map of parameter names and values. Values must be constant or closed
expressions that do not reference document fields. Parameters can then be
accessed as variables in an aggregate expression context (e.g. "$$var").

I think this version more clearly describes the api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -752,7 +755,7 @@ def replace_one(self, filter, replacement, upsert=False,
def update_one(self, filter, update, upsert=False,
bypass_document_validation=False,
collation=None, array_filters=None, hint=None,
session=None):
session=None, let=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all the helpers that didn't support "let" before, can you add a versionchanged:: 4.1 section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -2236,6 +2254,8 @@ def __find_and_modify(self, filter, projection, sort, upsert=None,
cmd = SON([("findAndModify", self.__name),
("query", filter),
("new", return_document)])
if let:
cmd["let"] = let
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a validate_is_mapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@client_context.require_version_min(5, 0, 0)
def test_helpers_with_let(self):
c = self.db.test
helpers = [("delete_many", ({}, {})), ("delete_one", ({}, {})),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor this to use the methods instead of strings, eg:

helpers = [(c.delete_many,...

It makes it easier to find usages of the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

for helper, args in helpers:
with self.assertRaises(TypeError) as cm:
getattr(c, helper)(*args, let=let)
self.assertIn("let must be an instance of dict",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -795,6 +798,8 @@ def update_one(self, filter, update, upsert=False,
MongoDB 4.2 and above.
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.
- `let` (optional): Specifies a document with a list of variables
that can then be accessed using the form `$$<variable_name>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this with the docs from the CRUD spec?:

Map of parameter names and values. Values must be constant or closed
expressions that do not reference document fields. Parameters can then be
accessed as variables in an aggregate expression context (e.g. "$$var").

I think this version more clearly describes the api.

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one last thing. Can you add this to the 4.1 changelog?

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@juliusgeo juliusgeo merged commit 046d789 into mongodb:master Nov 30, 2021
juliusgeo added a commit to juliusgeo/mongo-python-driver that referenced this pull request Apr 5, 2022
juliusgeo added a commit to juliusgeo/mongo-python-driver that referenced this pull request Apr 7, 2022
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

Successfully merging this pull request may close these issues.

2 participants