From 13ca542d5effd3483980dee219132db6d85fcb62 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 9 Jul 2024 11:17:36 -0500 Subject: [PATCH] MOTOR-1335 AsyncIOMotorClient is not suscriptable (#293) --- doc/requirements.rst | 2 +- motor/core.py | 20 ++++++++++++++++++++ synchro/synchrotest.py | 2 +- test/check_runtime_types.py | 17 +++++++++++++++++ tox.ini | 1 + 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/check_runtime_types.py diff --git a/doc/requirements.rst b/doc/requirements.rst index 89fc9d37..ad6c6bd3 100644 --- a/doc/requirements.rst +++ b/doc/requirements.rst @@ -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 ````````````````` diff --git a/motor/core.py b/motor/core.py index 7f22af5a..b0c42e30 100644 --- a/motor/core.py +++ b/motor/core.py @@ -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() @@ -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() @@ -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, @@ -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, diff --git a/synchro/synchrotest.py b/synchro/synchrotest.py index 1b2f7ebc..eeb8e01c 100644 --- a/synchro/synchrotest.py +++ b/synchro/synchrotest.py @@ -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) diff --git a/test/check_runtime_types.py b/test/check_runtime_types.py new file mode 100644 index 00000000..ba015a9f --- /dev/null +++ b/test/check_runtime_types.py @@ -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]] diff --git a/tox.ini b/tox.ini index 0dcee475..229856ec 100644 --- a/tox.ini +++ b/tox.ini @@ -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