Skip to content

Commit 453d457

Browse files
committed
fixes
1 parent 0da250f commit 453d457

11 files changed

+138
-117
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.
@@ -207,16 +201,14 @@ To use type hints in your {+driver-short+} application, you must add a type anno
207201
client: MongoClient = MongoClient()
208202

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

213207
.. code-block:: python
214208

215209
from typing import Any, Dict
216210
client: MongoClient[Dict[str, Any]] = MongoClient()
217211

218-
.. include:: /includes/type-hints/tip-more-info.rst
219-
220212
Troubleshooting
221213
---------------
222214

@@ -230,4 +222,4 @@ API Documentation
230222
To learn more about creating a ``MongoClient`` object in {+driver-short+},
231223
see the following API documentation:
232224

233-
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__
225+
- `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
@@ -9,6 +9,17 @@
99
)
1010
# end-bulk-insert-one
1111

12+
# start-bulk-insert-one-typed
13+
class Restaurant (TypedDict):
14+
name: str
15+
cuisine: str
16+
borough: str
17+
restaurant_id: str
18+
19+
operation = pymongo.InsertOne(Restaurant(
20+
name="Mongo's Deli", cuisine="Sandwiches", borough="Manhattan", restaurant_id="1234"))
21+
# end-bulk-insert-one-typed
22+
1223
# start-bulk-update-one
1324
operation = pymongo.UpdateOne(
1425
{ "name": "Mongo's Deli" },
@@ -35,6 +46,19 @@
3546
)
3647
# end-bulk-replace-one
3748

49+
# start-bulk-replace-one-typed
50+
class Restaurant (TypedDict):
51+
name: str
52+
cuisine: str
53+
borough: str
54+
restaurant_id: str
55+
56+
operation = pymongo.ReplaceOne(
57+
{ "restaurant_id": "1234" },
58+
Restaurant(name="Mongo's Pizza", cuisine="Pizza", borough="Brooklyn", restaurant_id="5678")
59+
)
60+
# end-bulk-replace-one-typed
61+
3862
# start-bulk-delete-one
3963
operation = pymongo.DeleteOne({ "restaurant_id": "5678" })
4064
# end-bulk-delete-one

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/tools.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,6 @@ daemon in the global application group:
260260
and in the `Multiple Python Sub Interpreters <https://modwsgi.readthedocs.io/en/master/user-guides/application-issues.html#multiple-python-sub-interpreters>`__
261261
section of the mod_wsgi documentation.
262262

263-
Alternative Python Drivers
264-
--------------------------
265-
266-
This section lists alternatives to {+driver-short+}.
267-
268-
- `Motor <https://github.com/mongodb/motor>`__ is a full-featured, non-blocking
269-
MongoDB driver for Python Tornado applications.
270-
271-
- `TxMongo <https://github.com/twisted/txmongo>`__ is an asynchronous Twisted
272-
Python driver for MongoDB.
273-
274-
- `MongoMock <https://github.com/mongomock/mongomock>`__ is a small
275-
library to help test Python code. It uses {+driver-short+} to interact with MongoDB.
276-
277263
Type Checkers
278264
-------------
279265

@@ -293,4 +279,18 @@ in the ``typing`` module documentation.
293279
.. code-block:: python
294280

295281
[mypy-pymongo]
296-
follow_imports = False
282+
follow_imports = False
283+
284+
Alternative Python Drivers
285+
--------------------------
286+
287+
This section lists alternatives to {+driver-short+}.
288+
289+
- `Motor <https://github.com/mongodb/motor>`__ is a full-featured, non-blocking
290+
MongoDB driver for Python Tornado applications.
291+
292+
- `TxMongo <https://github.com/twisted/txmongo>`__ is an asynchronous Twisted
293+
Python driver for MongoDB.
294+
295+
- `MongoMock <https://github.com/mongomock/mongomock>`__ is a small
296+
library to help test Python code. It uses {+driver-short+} to interact with MongoDB.

source/write/bulk-write.txt

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ The following example creates an instance of ``InsertOne``:
6565
:language: python
6666
:copyable:
6767

68+
You can also create an instance of ``InsertOne`` by passing an instance of a custom class
69+
to the constructor. This provides additional type safety if you're using a type-checking
70+
tool. The instance you pass must inherit from the ``TypedDict`` class.
71+
72+
.. include:: /includes/type-hints/typeddict-availability.rst
73+
74+
The following example constructs an ``InsertOne`` instance by using a custom
75+
class for added type safety:
76+
77+
.. literalinclude:: /includes/write/bulk-write.py
78+
:start-after: start-bulk-insert-one-typed
79+
:end-before: end-bulk-insert-one-typed
80+
:language: python
81+
:copyable:
82+
6883
To insert multiple documents, create an instance of ``InsertOne`` for each document.
6984

7085
.. note::
@@ -122,6 +137,21 @@ The following example creates an instance of ``ReplaceOne``:
122137
:language: python
123138
:copyable:
124139

140+
You can also create an instance of ``ReplaceOne`` by passing an instance of a custom class
141+
to the constructor. This provides additional type safety if you're using a type-checking
142+
tool. The instance you pass must inherit from the ``TypedDict`` class.
143+
144+
.. include:: /includes/type-hints/typeddict-availability.rst
145+
146+
The following example constructs a ``ReplaceOne`` instance by using a custom
147+
class for added type safety:
148+
149+
.. literalinclude:: /includes/write/bulk-write.py
150+
:start-after: start-bulk-replace-one-typed
151+
:end-before: end-bulk-replace-one-typed
152+
:language: python
153+
:copyable:
154+
125155
To replace multiple documents, you must create an instance of ``ReplaceOne`` for each document.
126156

127157
Delete Operations
@@ -286,37 +316,6 @@ The ``bulk_write()`` method returns a ``BulkWriteResult`` object. The
286316
- | A map of the operation's index to the ``_id`` of the upserted documents, if
287317
applicable.
288318

289-
Type Hints
290-
----------
291-
292-
.. include:: /includes/type-hints/intro.rst
293-
294-
When you use the ``bulk_write()`` method to perform an ``InsertOne`` or ``ReplaceOne``
295-
operation,
296-
The following code example shows how to insert
297-
- ``~pymongo.collection.Collection.bulk_write()``
298-
299-
For ``bulk_write()``, both the ``~pymongo.operations.InsertOne()`` and
300-
``~pymongo.operations.ReplaceOne()`` operators are generic.
301-
302-
The following code example shows that the results are the same as the preceding examples
303-
when you call the ``bulk_write()`` method:
304-
305-
.. code-block:: python
306-
307-
from typing import TypedDict
308-
from pymongo import MongoClient
309-
from pymongo.operations import InsertOne
310-
from pymongo.collection import Collection
311-
client: MongoClient = MongoClient()
312-
collection: Collection[Movie] = client.test.test
313-
inserted = collection.bulk_write([InsertOne(Movie(name="Jurassic Park", year=1993))])
314-
result = collection.find_one({"name": "Jurassic Park"})
315-
assert result is not None
316-
assert result["year"] == 1993
317-
318-
.. include:: /includes/type-hints/tip-more-info.rst
319-
320319
Troubleshooting
321320
---------------
322321

0 commit comments

Comments
 (0)