-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-3084 MongoClient/Database/Collection should not implement Iterable #909
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
Conversation
@@ -124,7 +124,22 @@ def test_getattr(self): | |||
self.assertEqual(coll2.write_concern, coll4.write_concern) | |||
|
|||
def test_iteration(self): | |||
self.assertRaises(TypeError, next, self.db) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice the old code was accidentally testing Database, not collection.
break | ||
# Non-string indices will start failing in PyMongo 5. | ||
self.assertEqual(coll[0].name, "coll.0") | ||
self.assertEqual(coll[{}].name, "coll.{}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened https://jira.mongodb.org/browse/PYTHON-3184 for this.
Ah it turns out this approach does not work quite the same on PyPy. I'm not surprised since it feels like an undefined behavior hack:
|
With further research it appears that setting
https://docs.python.org/3/reference/datamodel.html#special-method-names This is exactly the behavior we want so I'll adjust the assertions to pass on PyPy. |
self.assertRaises(TypeError, iterate) | ||
client = self.client | ||
if "PyPy" in sys.version: | ||
msg = "'NoneType' object is not callable" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened https://foss.heptapod.net/pypy/pypy/-/issues/3716 for PyPy to consider improving this error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
As noted in PYTHON-3084, this was the only way to get the typing.Iterable instance check to fail. We could also remove
__next__
+next
(from client/Database only) but I kept them around so that users get better error messagesTypeError: 'MongoClient' object is not iterable
vsTypeError: name must be an instance of str
.