Skip to content

Commit 8f7ba00

Browse files
committed
fixes
1 parent 0809a78 commit 8f7ba00

10 files changed

+123
-71
lines changed

source/connect/mongoclient.txt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ 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-
4236
.. _pymongo_connection_uri:
4337

4438
Connection URI
@@ -125,7 +119,7 @@ constructor accepts. All parameters are optional.
125119
:wikipedia:`round-robin DNS <Round-robin_DNS>` addresses.
126120

127121
**Data type:** ``Union[str, Sequence[str]]``
128-
**Default value:** ``"localhost"``
122+
| **Default value:** ``"localhost"``
129123

130124
* - ``port``
131125
- The port number {+mdb-server+} is running on.
@@ -134,7 +128,7 @@ constructor accepts. All parameters are optional.
134128
instead of using this parameter.
135129

136130
**Data type:** ``int``
137-
**Default value:** ``27017``
131+
| **Default value:** ``27017``
138132

139133
* - ``document_class``
140134
- The default class that the client uses to decode BSON documents returned by queries.
@@ -257,16 +251,14 @@ To use type hints in your {+driver-short+} application, you must add a type anno
257251
client: MongoClient = MongoClient()
258252

259253
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:
254+
``Dict[str, Any]`` in your type annotation. This data type matches all documents in MongoDB.
255+
The following example shows how to include this data type in your type annotation:
262256

263257
.. code-block:: python
264258

265259
from typing import Any, Dict
266260
client: MongoClient[Dict[str, Any]] = MongoClient()
267261

268-
.. include:: /includes/type-hints/tip-more-info.rst
269-
270262
Troubleshooting
271263
---------------
272264

@@ -341,4 +333,4 @@ API Documentation
341333
To learn more about creating a ``MongoClient`` object in {+driver-short+},
342334
see the following API documentation:
343335

344-
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__
336+
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__

source/databases-collections.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,7 @@ Type Hints
328328

329329
.. include:: /includes/type-hints/intro.rst
330330

331-
.. include:: /includes/type-hints/tip-more-info.rst
332-
333-
.. note:: TypedDict Requires Python 3.8+
331+
.. note:: TypedDict in Python 3.7 and Earlier
334332

335333
.. include:: /includes/type-hints/typeddict-availability.rst
336334

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
See the following resources for more information about type hints:
1+
.. tip:: Learn More About Type Hints
22

3-
- `typing <https://docs.python.org/3.5/library/typing.html>`__ module (Python 3.5)
4-
- `mypy <https://mypy.readthedocs.io/en/stable/getting_started.html>`__
5-
- `typing_extensions <https://pypi.org/project/typing-extensions/>`__ package
3+
See the following resources for more information about type hints:
4+
5+
- `typing <https://docs.python.org/3.5/library/typing.html>`__ module (Python 3.5+)
6+
- `mypy <https://mypy.readthedocs.io/en/stable/getting_started.html>`__
7+
- `typing_extensions <https://pypi.org/project/typing-extensions/>`__ package

source/includes/type-hints/troubleshooting-client-type.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Client Type Annotations
22
~~~~~~~~~~~~~~~~~~~~~~~
33

44
If you don't add a type annotation for your ``MongoClient`` object,
5-
you might see the following mypy error:
5+
your type checker might show an error similar to the following:
66

77
.. code-block:: python
88

source/includes/type-hints/troubleshooting-incompatible-type.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Incompatible Type
22
~~~~~~~~~~~~~~~~~
33

4-
If you use the generic form of the ``MongoClient`` class, you
5-
might see the following mypy error:
4+
If you use the generic form of the ``MongoClient`` class, your type checker might
5+
show an error similar to the following:
66

77
.. code-block:: python
88
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
The ``typing`` module, which contains the ``TypedDict`` class, is available only in
2-
Python 3.8 and later. To use the ``TypedDict`` class in earlier versions of Python,
3-
install the ``typing_extensions`` package.
1+
The examples in this section use the ``TypedDict`` class from the ``typing`` module. This
2+
module is available only in Python 3.8 and later. To use the ``TypedDict`` class in
3+
earlier versions of Python, install the ``typing_extensions`` package.

source/includes/write/bulk-write.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
)
1111
# end-bulk-insert-one
1212

13+
# start-bulk-insert-one-typed
14+
class Restaurant (TypedDict):
15+
name: str
16+
cuisine: str
17+
borough: str
18+
restaurant_id: str
19+
20+
operation = pymongo.InsertOne(Restaurant(
21+
name="Mongo's Deli", cuisine="Sandwiches", borough="Manhattan", restaurant_id="1234"))
22+
# end-bulk-insert-one-typed
23+
1324
# start-bulk-update-one
1425
operation = UpdateOne(
1526
namespace="sample_restaurants.restaurants",
@@ -39,6 +50,19 @@
3950
)
4051
# end-bulk-replace-one
4152

53+
# start-bulk-replace-one-typed
54+
class Restaurant (TypedDict):
55+
name: str
56+
cuisine: str
57+
borough: str
58+
restaurant_id: str
59+
60+
operation = pymongo.ReplaceOne(
61+
{ "restaurant_id": "1234" },
62+
Restaurant(name="Mongo's Pizza", cuisine="Pizza", borough="Brooklyn", restaurant_id="5678")
63+
)
64+
# end-bulk-replace-one-typed
65+
4266
# start-bulk-delete-one
4367
operation = DeleteOne(
4468
namespace="sample_restaurants.restaurants",

source/run-command.txt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,9 @@ collections.
226226
Type Hints
227227
----------
228228

229-
When you use the ``Database.command()`` method to run a database command, you can instruct
230-
the method to decode the returned BSON to a specific document type. To do so,
231-
construct a ``CodecOptions`` object and pass the name of the class the documents should
232-
be decoded to. The class can be one of the following types:
229+
The ``Database.command()`` method can decode the returned BSON documents to instances
230+
of a specific class. To specify this class, construct a ``CodecOptions`` object and pass
231+
the class name. The class can be one of the following types:
233232

234233
- ``bson.raw_bson.RawBSONDocument``.
235234
- A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``.
@@ -239,11 +238,13 @@ be decoded to. The class can be one of the following types:
239238
parameter, you must also include the class in a type hint for your ``CodecOptions``
240239
object.
241240

242-
The following example shows how to specify the document type for the result of the
243-
``ping`` command:
241+
.. include:: /includes/type-hints/typeddict-availability.rst
242+
243+
The following example decodes the BSON returned by the ``ping`` command to instances
244+
of the ``RawBSONDocument`` class:
244245

245246
.. code-block:: python
246-
:emphasize-lines: 3, 5
247+
:emphasize-lines: 3, 6-7
247248

248249
from pymongo import MongoClient
249250
from bson.raw_bson import RawBSONDocument
@@ -253,9 +254,11 @@ The following example shows how to specify the document type for the result of t
253254
options = CodecOptions(RawBSONDocument)
254255
result = client.admin.command("ping", codec_options=options)
255256

256-
To pass a ``TypedDict`` subclass, use the following syntax:
257+
To decode BSON to a subclass of the ``TypedDict`` class, specify the class name in
258+
the ``CodecOptions`` type hint, as shown in the following example:
257259

258260
.. code-block:: python
261+
:emphasize-lines: 4, 6-8, 11
259262

260263
from pymongo import MongoClient
261264
from bson.raw_bson import RawBSONDocument
@@ -270,8 +273,6 @@ To pass a ``TypedDict`` subclass, use the following syntax:
270273
options: CodecOptions[Movie] = CodecOptions(Movie)
271274
result = client.admin.command("ping", codec_options=options)
272275

273-
.. include:: /includes/type-hints/tip-more-info.rst
274-
275276
Troubleshooting
276277
---------------
277278

source/write/bulk-write.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ The following example creates an instance of ``InsertOne``:
9595
:language: python
9696
:copyable:
9797

98+
You can also create an instance of ``InsertOne`` by passing an instance of a custom class
99+
to the constructor. This provides additional type safety if you're using a type-checking
100+
tool. The instance you pass must inherit from the ``TypedDict`` class.
101+
102+
.. include:: /includes/type-hints/typeddict-availability.rst
103+
104+
The following example constructs an ``InsertOne`` instance by using a custom
105+
class for added type safety:
106+
107+
.. literalinclude:: /includes/write/bulk-write.py
108+
:start-after: start-bulk-insert-one-typed
109+
:end-before: end-bulk-insert-one-typed
110+
:language: python
111+
:copyable:
112+
98113
To insert multiple documents, create an instance of ``InsertOne`` for each document.
99114

100115
.. include:: /includes/write/unique-id-note.rst
@@ -157,6 +172,21 @@ The following example creates an instance of ``ReplaceOne``:
157172
:language: python
158173
:copyable:
159174

175+
You can also create an instance of ``ReplaceOne`` by passing an instance of a custom class
176+
to the constructor. This provides additional type safety if you're using a type-checking
177+
tool. The instance you pass must inherit from the ``TypedDict`` class.
178+
179+
.. include:: /includes/type-hints/typeddict-availability.rst
180+
181+
The following example constructs a ``ReplaceOne`` instance by using a custom
182+
class for added type safety:
183+
184+
.. literalinclude:: /includes/write/bulk-write.py
185+
:start-after: start-bulk-replace-one-typed
186+
:end-before: end-bulk-replace-one-typed
187+
:language: python
188+
:copyable:
189+
160190
To replace multiple documents, you must create an instance of ``ReplaceOne`` for each document.
161191

162192
Delete Operations

source/write/insert.txt

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ The following example inserts a document into the ``restaurants`` collection:
6060

6161
sample_restaurants.restaurants.insert_one({"name" : "Mongo's Burgers"})
6262

63+
You can also pass an instance of a custom class to the ``insert_one()`` method.
64+
This provides additional type safety if you're using a type-checking
65+
tool. The instance you pass must inherit from the ``TypedDict`` class.
66+
67+
.. include:: /includes/type-hints/typeddict-availability.rst
68+
69+
The following example passes an instance of the ``Restaurant`` class to the ``insert_one()``
70+
method for added type safety:
71+
72+
.. code-block:: python
73+
:copyable: true
74+
75+
class Restaurant(TypedDict):
76+
name: str
77+
78+
sample_restaurants.restaurants.insert_one(Movie(name="Mongo's Burgers")
79+
6380
Insert Multiple Documents
6481
-------------------------
6582

@@ -78,6 +95,28 @@ The following example inserts a list of documents into the ``restaurants`` colle
7895

7996
sample_restaurants.restaurants.insert_many(document_list)
8097

98+
You can also pass a list of instances of a custom class to the ``insert_many()`` method.
99+
This provides additional type safety if you're using a type-checking
100+
tool. The instances you pass must inherit from the ``TypedDict`` class.
101+
102+
.. include:: /includes/type-hints/typeddict-availability.rst
103+
104+
The following example calls the ``insert_many()`` method and passes a list that contains
105+
instances of the ``Restaurant`` class. This adds type safety to the insert operation.
106+
107+
.. code-block:: python
108+
:copyable: true
109+
110+
class Restaurant(TypedDict):
111+
name: str
112+
113+
document_list = [
114+
Restaurant(name="Mongo's Burgers"),
115+
Restaurant(name="Mongo's Pizza")
116+
]
117+
118+
sample_restaurants.restaurants.insert_many(document_list)
119+
81120
Modify Insert Behavior
82121
----------------------
83122

@@ -140,41 +179,6 @@ document-level validation.
140179

141180
sample_restaurants.restaurants.insert_many(document_list, bypass_document_validation = True)
142181

143-
Type Hints
144-
----------
145-
146-
.. include:: /includes/type-hints/intro.rst
147-
148-
When you call the ``insert_one()`` or ``insert_many()`` method, you can pass one or more
149-
instances of a custom class that represents the documents in the collection.
150-
151-
To create
152-
a custom type, define a class that inherits from the ``TypedDict`` class. In the class,
153-
add a property for each field in the document.
154-
155-
After you define the class, you can insert instances of the class. The following example
156-
defines a ``Movie`` class to represent documents from the
157-
``sample_mflix.movies`` collection.
158-
Each ``Movie`` object contains two key-value pairs: ``name``, a string key with a string
159-
value, and ``year``, a string key with an integer value.
160-
The code then uses the ``insert_one()`` method to insert a ``Movie`` object.
161-
162-
.. code-block:: python
163-
:emphasize-lines: 10
164-
165-
from typing import TypedDict
166-
167-
class Movie(TypedDict):
168-
name: str
169-
year: int
170-
171-
client: MongoClient = MongoClient()
172-
database = client["test_database"]
173-
collection: Collection[Movie] = database["test_collection"]
174-
inserted = collection.insert_one(Movie(name="Jurassic Park", year=1993))
175-
176-
.. include:: /includes/type-hints/tip-more-info.rst
177-
178182
Troubleshooting
179183
---------------
180184

@@ -308,6 +312,7 @@ The following code example shows how to implement this solution:
308312
The ``NotRequired`` class is available only in Python 3.11 and later.
309313
To use ``NotRequired`` in earlier versions of Python, install the ``typing_extensions``
310314
package instead.
315+
311316
Additional Information
312317
----------------------
313318

0 commit comments

Comments
 (0)