Skip to content

Commit 9dc395a

Browse files
committed
split to multiple pages
1 parent c4e5620 commit 9dc395a

13 files changed

+453
-447
lines changed

source/connect/mongoclient.txt

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ while connected to MongoDB.
3333
This guide shows you how to create a connection string and use a ``MongoClient`` object
3434
to 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

3844
Connection 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+
201227
API Documentation
202228
-----------------
203229

source/databases-collections.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,81 @@ within 35 milliseconds of the closest member's ping time.
253253
:manual:`localThreshold </reference/program/mongos/#std-option-mongos.--localThreshold>`
254254
command-line option.
255255

256+
.. _pymongo-databases-collection-type-hints:
257+
258+
Type Hints
259+
----------
260+
261+
.. include:: /includes/type-hints/intro.rst
262+
263+
.. include:: /includes/type-hints/tip-more-info.rst
264+
265+
.. note:: TypedDict Requires Python 3.8+
266+
267+
.. include:: /includes/type-hints/typeddict-availability.rst
268+
269+
Database
270+
~~~~~~~~
271+
272+
If all documents in a database match a well-defined schema, you can specify a type hint
273+
that uses a Python class to represent the documents' structure. By including this class
274+
in the type hint for your ``Database`` object, you can ensure that all documents you
275+
store or retrieve have the required structure. This provides more accurate type
276+
checking and code completion than the default ``Dict[str, Any]`` type.
277+
278+
First, define a class to represent a document from the database. The class must inherit
279+
from the ``TypedDict`` class and must contain the same fields as the documents in the
280+
database. After you define your class, include its name as the generic type for the
281+
``Database`` type hint.
282+
283+
The following example defines a ``Movie`` class and uses it as the
284+
generic type for a ``Database`` type hint:
285+
286+
.. code-block:: python
287+
:emphasize-lines: 5-7, 10
288+
289+
from typing import TypedDict
290+
from pymongo import MongoClient
291+
from pymongo.database import Database
292+
293+
class Movie(TypedDict):
294+
name: str
295+
year: int
296+
297+
client: MongoClient = MongoClient()
298+
database: Database[Movie] = client["test_database"]
299+
300+
Collection
301+
~~~~~~~~~~
302+
303+
Adding a generic type to a ``Collection`` type hint is similar to adding a generic type
304+
to a ``Database`` type hint. First, define a class that inherits from the ``TypedDict`` class
305+
and represents the structure of the
306+
documents in the collection. Then, include the class name as the generic type for the
307+
``Collection`` type hint, as shown in the following example:
308+
309+
.. code-block:: python
310+
:emphasize-lines: 5-7,11
311+
312+
from typing import TypedDict
313+
from pymongo import MongoClient
314+
from pymongo.collection import Collection
315+
316+
class Movie(TypedDict):
317+
name: str
318+
year: int
319+
320+
client: MongoClient = MongoClient()
321+
database = client["test_database"]
322+
collection: Collection[Movie] = database["test_collection"]
323+
256324
Troubleshooting
257325
---------------
258326

327+
.. include:: /includes/type-hints/troubleshooting-client-type.rst
328+
329+
.. include:: /includes/type-hints/troubleshooting-incompatible-type.rst
330+
259331
.. include:: /includes/troubleshooting/read-write-options.rst
260332

261333
API Documentation

0 commit comments

Comments
 (0)