Skip to content

Commit

Permalink
MOTOR-1335 AsyncIOMotorClient is not suscriptable (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Jul 9, 2024
1 parent 5146708 commit 13ca542
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/requirements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Use `the PyMongo compatibility matrix`_ to determine what MongoDB version is
supported by PyMongo. Use the compatibility matrix above to determine what
MongoDB version Motor supports.

.. _the PyMongo compatibility matrix: https://mongodb.com/docs/drivers/pymongo#mongodb-compatibility
.. _the PyMongo compatibility matrix: https://mongodb.com/docs/drivers/pymongo#compatibility

Motor and Tornado
`````````````````
Expand Down
20 changes: 20 additions & 0 deletions motor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def __repr__(self):


class AgnosticBaseProperties(AgnosticBase):
# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls

codec_options = ReadOnlyProperty()
read_preference = ReadOnlyProperty()
read_concern = ReadOnlyProperty()
Expand Down Expand Up @@ -1376,6 +1381,11 @@ def get_io_loop(self):
class AgnosticBaseCursor(AgnosticBase):
"""Base class for AgnosticCursor and AgnosticCommandCursor"""

# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls

_async_close = AsyncRead(attr_name="close")
_refresh = AsyncRead()
address = ReadOnlyProperty()
Expand Down Expand Up @@ -1940,6 +1950,11 @@ class AgnosticChangeStream(AgnosticBase):

resume_token = ReadOnlyProperty()

# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls

def __init__(
self,
target,
Expand Down Expand Up @@ -2131,6 +2146,11 @@ class AgnosticClientEncryption(AgnosticBase):
get_key_by_alt_name = AsyncCommand()
remove_key_alt_name = AsyncCommand()

# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls

def __init__(
self,
kms_providers,
Expand Down
2 changes: 1 addition & 1 deletion synchro/synchrotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def want_method(method, classname):

# Run the tests from the pymongo target dir with our custom plugin.
os.chdir(sys.argv[1])
code = pytest.main(sys.argv[2:], plugins=[SynchroPytestPlugin()])
code = pytest.main(sys.argv[2:] + ["-p", "no:warnings"], plugins=[SynchroPytestPlugin()])

if code != 0:
sys.exit(code)
Expand Down
17 changes: 17 additions & 0 deletions test/check_runtime_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Any, Dict

from motor.motor_asyncio import (
AsyncIOMotorChangeStream,
AsyncIOMotorClient,
AsyncIOMotorClientEncryption,
AsyncIOMotorCollection,
AsyncIOMotorCursor,
AsyncIOMotorDatabase,
)

client: AsyncIOMotorClient[Dict[str, Any]]
db: AsyncIOMotorDatabase[Dict[str, Any]]
cur: AsyncIOMotorCursor[Dict[str, Any]]
coll: AsyncIOMotorCollection[Dict[str, Any]]
cs: AsyncIOMotorChangeStream[Dict[str, Any]]
enc: AsyncIOMotorClientEncryption[Dict[str, Any]]
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ commands =
mypy --install-types --non-interactive motor
mypy --install-types --non-interactive test/test_typing.py
pytest test/test_mypy_fails.py
python test/check_runtime_types.py

0 comments on commit 13ca542

Please sign in to comment.