@@ -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
@@ -176,28 +192,38 @@ constructor accepts. All parameters are optional.
176
192
a process, the child process *does* need its own ``MongoClient`` object.
177
193
To learn more, see the :ref:`FAQ <pymongo-faq>` page.
178
194
179
- Type Hints
195
+ .. _pymongo-type-hints-client:
196
+
197
+ Type Hints
180
198
----------
181
199
182
- If you're using Python v3.5 or later, you can add type hints to your Python code.
200
+ .. include:: /includes/ type- hints/intro.rst
183
201
184
- The following code example shows how to declare a type hint for a ``MongoClient``
185
- object:
202
+ To use type hints in your {+driver-short+} application, you must add a type annotation to your
203
+ ``MongoClient`` object, as shown in the following example :
186
204
187
205
.. code-block:: python
188
206
189
207
client: MongoClient = MongoClient()
190
208
191
- In the previous example, the code doesn't specify a type for the documents that the
192
- ``MongoClient`` object will work with. To specify a document type,
193
- include the ``Dict[str, Any]`` type when you
194
- create the ``MongoClient`` object, as shown in the following example:
209
+ For more accurate type information, you can include the generic document type
210
+ ``Dict[str, Any]`` in your type annotation. This type matches all documents in MongoDB.
211
+ The following example shows how to add this type hint:
195
212
196
213
.. code-block:: python
197
214
198
215
from typing import Any, Dict
199
216
client: MongoClient[Dict[str, Any]] = MongoClient()
200
217
218
+ .. include:: /includes/type-hints/tip-more-info.rst
219
+
220
+ Troubleshooting
221
+ ---------------
222
+
223
+ .. include:: /includes/type-hints/troubleshooting-client-type.rst
224
+
225
+ .. include:: /includes/type-hints/troubleshooting-incompatible-type.rst
226
+
201
227
API Documentation
202
228
-----------------
203
229
0 commit comments