Skip to content

Commit 0809a78

Browse files
committed
split to multiple pages
1 parent 1def2b0 commit 0809a78

13 files changed

+452
-449
lines changed

source/connect/mongoclient.txt

Lines changed: 35 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

@@ -226,28 +242,31 @@ To use multiprocessing with {+driver-short+}, write code similar to the followin
226242
Do not copy an instance of the ``MongoClient`` class from the parent process to a child
227243
process.
228244

229-
Type Hints
245+
.. _pymongo-type-hints-client:
246+
247+
Type Hints
230248
----------
231249

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
233251

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:
236254

237255
.. code-block:: python
238256

239257
client: MongoClient = MongoClient()
240258

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:
245262

246263
.. code-block:: python
247264

248265
from typing import Any, Dict
249266
client: MongoClient[Dict[str, Any]] = MongoClient()
250267

268+
.. include:: /includes/type-hints/tip-more-info.rst
269+
251270
Troubleshooting
252271
---------------
253272

@@ -312,6 +331,10 @@ process.
312331
multithreaded contexts with ``fork()``, see `Issue 6721 <http://bugs.python.org/issue6721>`__
313332
in the Python Issue Tracker.
314333

334+
.. include:: /includes/type-hints/troubleshooting-client-type.rst
335+
336+
.. include:: /includes/type-hints/troubleshooting-incompatible-type.rst
337+
315338
API Documentation
316339
-----------------
317340

source/databases-collections.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,81 @@ To learn more about supported retryable read operations, see :manual:`Retryable
321321
in the {+mdb-server+} manual. To learn more about supported retryable write
322322
operations, see :manual:`Retryable Writes </core/retryable-writes/>` in the {+mdb-server+} manual.
323323

324+
.. _pymongo-databases-collection-type-hints:
325+
326+
Type Hints
327+
----------
328+
329+
.. include:: /includes/type-hints/intro.rst
330+
331+
.. include:: /includes/type-hints/tip-more-info.rst
332+
333+
.. note:: TypedDict Requires Python 3.8+
334+
335+
.. include:: /includes/type-hints/typeddict-availability.rst
336+
337+
Database
338+
~~~~~~~~
339+
340+
If all documents in a database match a well-defined schema, you can specify a type hint
341+
that uses a Python class to represent the documents' structure. By including this class
342+
in the type hint for your ``Database`` object, you can ensure that all documents you
343+
store or retrieve have the required structure. This provides more accurate type
344+
checking and code completion than the default ``Dict[str, Any]`` type.
345+
346+
First, define a class to represent a document from the database. The class must inherit
347+
from the ``TypedDict`` class and must contain the same fields as the documents in the
348+
database. After you define your class, include its name as the generic type for the
349+
``Database`` type hint.
350+
351+
The following example defines a ``Movie`` class and uses it as the
352+
generic type for a ``Database`` type hint:
353+
354+
.. code-block:: python
355+
:emphasize-lines: 5-7, 10
356+
357+
from typing import TypedDict
358+
from pymongo import MongoClient
359+
from pymongo.database import Database
360+
361+
class Movie(TypedDict):
362+
name: str
363+
year: int
364+
365+
client: MongoClient = MongoClient()
366+
database: Database[Movie] = client["test_database"]
367+
368+
Collection
369+
~~~~~~~~~~
370+
371+
Adding a generic type to a ``Collection`` type hint is similar to adding a generic type
372+
to a ``Database`` type hint. First, define a class that inherits from the ``TypedDict`` class
373+
and represents the structure of the
374+
documents in the collection. Then, include the class name as the generic type for the
375+
``Collection`` type hint, as shown in the following example:
376+
377+
.. code-block:: python
378+
:emphasize-lines: 5-7,11
379+
380+
from typing import TypedDict
381+
from pymongo import MongoClient
382+
from pymongo.collection import Collection
383+
384+
class Movie(TypedDict):
385+
name: str
386+
year: int
387+
388+
client: MongoClient = MongoClient()
389+
database = client["test_database"]
390+
collection: Collection[Movie] = database["test_collection"]
391+
324392
Troubleshooting
325393
---------------
326394

395+
.. include:: /includes/type-hints/troubleshooting-client-type.rst
396+
397+
.. include:: /includes/type-hints/troubleshooting-incompatible-type.rst
398+
327399
.. include:: /includes/troubleshooting/read-write-options.rst
328400

329401
API Documentation

0 commit comments

Comments
 (0)