@@ -33,6 +33,12 @@ while connected to MongoDB.
3333This guide shows you how to create a connection string and use a ``MongoClient`` object
3434to connect to MongoDB.
3535
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+
3642.. _pymongo_connection_uri:
3743
3844Connection URI
@@ -132,11 +138,21 @@ constructor accepts. All parameters are optional.
132138
133139 * - ``document_class``
134140 - 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:
137142
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
140156
141157 **Data type:** ``Type[_DocumentType]``
142158
@@ -176,28 +192,38 @@ constructor accepts. All parameters are optional.
176192 a process, the child process *does* need its own ``MongoClient`` object.
177193 To learn more, see the :ref:`FAQ <pymongo-faq>` page.
178194
179- Type Hints
195+ .. _pymongo-type-hints-client:
196+
197+ Type Hints
180198----------
181199
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
183201
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 :
186204
187205.. code-block:: python
188206
189207 client: MongoClient = MongoClient()
190208
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:
195212
196213.. code-block:: python
197214
198215 from typing import Any, Dict
199216 client: MongoClient[Dict[str, Any]] = MongoClient()
200217
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+
201227API Documentation
202228-----------------
203229
0 commit comments