Skip to content

PYTHON-1690: Fix error message when insert_many is given a single RawBSONDocument instead of a list #580

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 6 commits into from
Mar 22, 2021

Conversation

guanlinzhou
Copy link
Contributor

@guanlinzhou guanlinzhou commented Mar 18, 2021

Changed type checking from abc.Iterable to abc.Sequence in order to better match 'list-like' types

TypeError                                 
Traceback (most recent call last)
<ipython-input-6-9084eaf77999> in <module>
----> 1 client.test.test.insert_many(RawBSONDocument(bson.BSON.encode({'_id':2})))

~/mongo-python-driver/pymongo/collection.py in insert_many(self, documents, ordered, bypass_document_validation, session)
    688         """
    689         if not isinstance(documents, abc.Sequence) or not documents:
--> 690             raise TypeError("documents must be a non-empty list")
    691         inserted_ids = []
    692         def gen():

TypeError: documents must be a non-empty list

@guanlinzhou guanlinzhou reopened this Mar 18, 2021
@guanlinzhou
Copy link
Contributor Author

@prashantmital @ShaneHarvey thoughts on whether this requires a testing change? I was looking for tests for invalid input types and couldn't find any existing ones

@ShaneHarvey
Copy link
Member

Yeah it would be great to add a test in test_collection.py which uses with self.assertRaisesRegex(... on various invalid types.

@guanlinzhou
Copy link
Contributor Author

Yeah it would be great to add a test in test_collection.py which uses with self.assertRaisesRegex(... on various invalid types.

added tests in the most recent commit

Copy link
Contributor

@prashantmital prashantmital left a comment

Choose a reason for hiding this comment

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

LGTM 🥳

Note that normally if you would have backported this commit to a branch that tested against Python 2.7, your tests would have failed because you are using assertRaisesRegex where Py 2 called that same method assertRaisesRegexp. Fortunately, our test setup code has a nifty tweak in place to prevent this from becoming a problem - see

# Use assertRaisesRegex if available, otherwise use Python 2.7's

@ShaneHarvey
Copy link
Member

Please run the tests before pushing a backport to v3.12. I think the abc.Mapping usage may be different in that branch.

@guanlinzhou guanlinzhou merged commit 94f4de1 into mongodb:master Mar 22, 2021
@guanlinzhou guanlinzhou deleted the PYTHON-1690 branch March 22, 2021 18:13
guanlinzhou added a commit that referenced this pull request Mar 22, 2021
…BSONDocument instead of a list (#580)

(cherry picked from commit 94f4de1)
guanlinzhou added a commit to guanlinzhou/mongo-python-driver that referenced this pull request Mar 22, 2021
…BSONDocument instead of a list (mongodb#580)

(cherry picked from commit 94f4de1)
(cherry picked from commit 0ba1f7e)
guanlinzhou added a commit to guanlinzhou/mongo-python-driver that referenced this pull request Mar 22, 2021
…BSONDocument instead of a list (mongodb#580)

(cherry picked from commit 94f4de1)
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.

3 participants