Skip to content

Commit 13ca542

Browse files
authored
MOTOR-1335 AsyncIOMotorClient is not suscriptable (#293)
1 parent 5146708 commit 13ca542

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

doc/requirements.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Use `the PyMongo compatibility matrix`_ to determine what MongoDB version is
6767
supported by PyMongo. Use the compatibility matrix above to determine what
6868
MongoDB version Motor supports.
6969

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

7272
Motor and Tornado
7373
`````````````````

motor/core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def __repr__(self):
9090

9191

9292
class AgnosticBaseProperties(AgnosticBase):
93+
# Allow the use of generics at runtime.
94+
@classmethod
95+
def __class_getitem__(cls, key: str) -> object:
96+
return cls
97+
9398
codec_options = ReadOnlyProperty()
9499
read_preference = ReadOnlyProperty()
95100
read_concern = ReadOnlyProperty()
@@ -1376,6 +1381,11 @@ def get_io_loop(self):
13761381
class AgnosticBaseCursor(AgnosticBase):
13771382
"""Base class for AgnosticCursor and AgnosticCommandCursor"""
13781383

1384+
# Allow the use of generics at runtime.
1385+
@classmethod
1386+
def __class_getitem__(cls, key: str) -> object:
1387+
return cls
1388+
13791389
_async_close = AsyncRead(attr_name="close")
13801390
_refresh = AsyncRead()
13811391
address = ReadOnlyProperty()
@@ -1940,6 +1950,11 @@ class AgnosticChangeStream(AgnosticBase):
19401950

19411951
resume_token = ReadOnlyProperty()
19421952

1953+
# Allow the use of generics at runtime.
1954+
@classmethod
1955+
def __class_getitem__(cls, key: str) -> object:
1956+
return cls
1957+
19431958
def __init__(
19441959
self,
19451960
target,
@@ -2131,6 +2146,11 @@ class AgnosticClientEncryption(AgnosticBase):
21312146
get_key_by_alt_name = AsyncCommand()
21322147
remove_key_alt_name = AsyncCommand()
21332148

2149+
# Allow the use of generics at runtime.
2150+
@classmethod
2151+
def __class_getitem__(cls, key: str) -> object:
2152+
return cls
2153+
21342154
def __init__(
21352155
self,
21362156
kms_providers,

synchro/synchrotest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def want_method(method, classname):
341341

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

346346
if code != 0:
347347
sys.exit(code)

test/check_runtime_types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Any, Dict
2+
3+
from motor.motor_asyncio import (
4+
AsyncIOMotorChangeStream,
5+
AsyncIOMotorClient,
6+
AsyncIOMotorClientEncryption,
7+
AsyncIOMotorCollection,
8+
AsyncIOMotorCursor,
9+
AsyncIOMotorDatabase,
10+
)
11+
12+
client: AsyncIOMotorClient[Dict[str, Any]]
13+
db: AsyncIOMotorDatabase[Dict[str, Any]]
14+
cur: AsyncIOMotorCursor[Dict[str, Any]]
15+
coll: AsyncIOMotorCollection[Dict[str, Any]]
16+
cs: AsyncIOMotorChangeStream[Dict[str, Any]]
17+
enc: AsyncIOMotorClientEncryption[Dict[str, Any]]

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,4 @@ commands =
161161
mypy --install-types --non-interactive motor
162162
mypy --install-types --non-interactive test/test_typing.py
163163
pytest test/test_mypy_fails.py
164+
python test/check_runtime_types.py

0 commit comments

Comments
 (0)