@@ -33,6 +33,12 @@ while connected to MongoDB.
33
33
This guide shows you how to create a connection string and use a ``MongoClient`` object
34
34
to connect to MongoDB.
35
35
36
+ .. tip:: Type Hints
37
+
38
+ If your application uses Python 3.5 or later, you can add type hints to your
39
+ ``MongoClient`` objects. To learn how to add type hints, see
40
+ :ref:`Type Hints <pymongo-type-hints-client>`.
41
+
36
42
.. _pymongo_connection_uri:
37
43
38
44
Connection URI
@@ -132,11 +138,21 @@ constructor accepts. All parameters are optional.
132
138
133
139
* - ``document_class``
134
140
- The default class that the client uses to decode BSON documents returned by queries.
135
- This parameter supports the ``bson.raw_bson.RawBSONDocument`` type, as well as
136
- subclasses of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``.
141
+ This parameter supports the following types:
137
142
138
- If you specify ``bson.son.SON`` as the document class, you must also specify types
139
- for the key and value.
143
+ - ``bson.raw_bson.RawBSONDocument``.
144
+ - A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``.
145
+ If you specify ``bson.son.SON`` as the document class, you must also specify types
146
+ for the key and value.
147
+ - A subclass of the ``TypedDict`` type. To pass a ``TypedDict`` subclass for this
148
+ parameter, you must also include the class in a type hint for your ``MongoClient``
149
+ object, as shown in the following example:
150
+
151
+ .. code-block:: python
152
+
153
+ client: MongoClient[MyTypedDict] = MongoClient()
154
+
155
+ .. include:: /includes/type-hints/typeddict-availability.rst
140
156
141
157
**Data type:** ``Type[_DocumentType]``
142
158
@@ -226,28 +242,31 @@ To use multiprocessing with {+driver-short+}, write code similar to the followin
226
242
Do not copy an instance of the ``MongoClient`` class from the parent process to a child
227
243
process.
228
244
229
- Type Hints
245
+ .. _pymongo-type-hints-client:
246
+
247
+ Type Hints
230
248
----------
231
249
232
- If you're using Python v3.5 or later, you can add type hints to your Python code.
250
+ .. include:: /includes/ type- hints/intro.rst
233
251
234
- The following code example shows how to declare a type hint for a ``MongoClient``
235
- object:
252
+ To use type hints in your {+driver-short+} application, you must add a type annotation to your
253
+ ``MongoClient`` object, as shown in the following example :
236
254
237
255
.. code-block:: python
238
256
239
257
client: MongoClient = MongoClient()
240
258
241
- In the previous example, the code doesn't specify a type for the documents that the
242
- ``MongoClient`` object will work with. To specify a document type,
243
- include the ``Dict[str, Any]`` type when you
244
- create the ``MongoClient`` object, as shown in the following example:
259
+ For more accurate type information, you can include the generic document type
260
+ ``Dict[str, Any]`` in your type annotation. This type matches all documents in MongoDB.
261
+ The following example shows how to add this type hint:
245
262
246
263
.. code-block:: python
247
264
248
265
from typing import Any, Dict
249
266
client: MongoClient[Dict[str, Any]] = MongoClient()
250
267
268
+ .. include:: /includes/type-hints/tip-more-info.rst
269
+
251
270
Troubleshooting
252
271
---------------
253
272
@@ -312,6 +331,10 @@ process.
312
331
multithreaded contexts with ``fork()``, see `Issue 6721 <http://bugs.python.org/issue6721>`__
313
332
in the Python Issue Tracker.
314
333
334
+ .. include:: /includes/type-hints/troubleshooting-client-type.rst
335
+
336
+ .. include:: /includes/type-hints/troubleshooting-incompatible-type.rst
337
+
315
338
API Documentation
316
339
-----------------
317
340
0 commit comments