Skip to content

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

Merged
merged 5 commits into from
Mar 28, 2022

Conversation

ShaneHarvey
Copy link
Member

@ShaneHarvey ShaneHarvey commented Mar 28, 2022

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 messages TypeError: 'MongoClient' object is not iterable vs TypeError: name must be an instance of str.

@@ -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)
Copy link
Member Author

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.{}")
Copy link
Member Author

Choose a reason for hiding this comment

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

@ShaneHarvey
Copy link
Member Author

ShaneHarvey commented Mar 28, 2022

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:

FAIL: test_iteration (test_client.ClientUnitTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/mongo-python-driver/mongo-python-driver/test/test_client.py", line 216, in test_iteration
    for _ in client:  # type: ignore[misc] # error: "None" not callable  [misc]
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/work/mongo-python-driver/mongo-python-driver/test/test_client.py", line 217, in test_iteration
    break
AssertionError: "'MongoClient' object is not iterable" does not match "'NoneType' object is not callable"

@ShaneHarvey
Copy link
Member Author

ShaneHarvey commented Mar 28, 2022

With further research it appears that setting __iter__ = None is actually well defined behavior:

Setting a special method to None indicates that the corresponding operation is not available. For example, if a class sets iter() to None, the class is not iterable, so calling iter() on its instances will raise a TypeError (without falling back to getitem()). 2

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"
Copy link
Member Author

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.

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM!

@ShaneHarvey ShaneHarvey merged commit 75fa14d into mongodb:master Mar 28, 2022
@ShaneHarvey ShaneHarvey deleted the PYTHON-3084 branch March 29, 2022 00:55
juliusgeo pushed a commit to juliusgeo/mongo-python-driver that referenced this pull request Apr 5, 2022
juliusgeo pushed 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