Skip to content

PYTHON-4590 - Add type guards to async API methods #1820

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 8 commits into from
Sep 4, 2024

Conversation

NoahStapp
Copy link
Contributor

No description provided.

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.

Do we need to do this? Doesn't the user already get an error if they pass in the wrong type? Could you give some examples?

"""A proxy for SocketInfo.write_command that handles event publishing."""
if not isinstance(client, AsyncMongoClient):
raise TypeError(
f"AsyncMongoClient required but {client} is an instance of {type(client)}"
Copy link
Member

Choose a reason for hiding this comment

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

Could this ever be reached? Wouldn't something else fail beforehand?

@NoahStapp
Copy link
Contributor Author

Do we need to do this? Doesn't the user already get an error if they pass in the wrong type? Could you give some examples?

The idea is that the current behavior looks like this:

>>> d = Database(AsyncMongoClient(), "test")
>>> d.list_collections()
<coroutine object AsyncMongoClient._retryable_read at 0x103023c40>

This change gives us this:

>>> d = Database(AsyncMongoClient(), "test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nstapp/Github/mongo-python-driver/pymongo/synchronous/database.py", line 128, in __init__
    raise TypeError(f"MongoClient required but {client} is an instance of {type(client)}")
TypeError: MongoClient required but AsyncMongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True) is an instance of <class 'pymongo.asynchronous.mongo_client.AsyncMongoClient'>

@NoahStapp NoahStapp requested a review from ShaneHarvey August 30, 2024 15:08
@@ -176,6 +176,7 @@
if TYPE_CHECKING:
from types import TracebackType

from pymongo.asynchronous.mongo_client import AsyncMongoClient
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this is no longer used

@NoahStapp NoahStapp requested a review from blink1073 August 30, 2024 19:15
if not isinstance(name, str):
raise TypeError("name must be an instance of str")

if not isinstance(client, AsyncMongoClient):
raise TypeError(
f"AsyncMongoClient required but {client} is an instance of {type(client)}"
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to include both the repr(client) and the type(client)? It could be safer to only add the type to avoid adding topology/hostname info into these errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I think type(client) makes more sense in this instance.

@@ -231,6 +231,12 @@ def __init__(
)
if not isinstance(name, str):
raise TypeError("name must be an instance of str")
from pymongo.synchronous.database import Database

if not isinstance(database, Database):
Copy link
Member

Choose a reason for hiding this comment

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

I'm paranoid this might break apps that Mock pymongo classes but it should be fine. Anything that's mocking should inherit from our base classes anyway (instead of trying to ducktype).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, we'll have to watch out for bug reports with this.

blink1073
blink1073 previously approved these changes Sep 3, 2024

if not isinstance(database, AsyncDatabase):
raise TypeError(
f"AsyncCollection requires an AsyncDatabase, {database} is an instance of {type(database)}"
Copy link
Member

Choose a reason for hiding this comment

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

We should make the same {client} error message change to all the Database ones as well since Database's repr includes the client.

@NoahStapp NoahStapp merged commit 5a49ccc into mongodb:master Sep 4, 2024
31 of 34 checks passed
@NoahStapp NoahStapp deleted the PYTHON-4590 branch September 4, 2024 12:58
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