Skip to content

Commit 3a063b8

Browse files
committed
fix documentation, etc
1 parent d4ef6b8 commit 3a063b8

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

doc/examples/type_hints.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ Typed Collection
9494

9595
You can use :py:class:`~typing_extensions.TypedDict` (Python 3.8+) when using a well-defined schema for the data in a :class:`~pymongo.collection.Collection`.
9696
Note that all `schema_validation`_ for inserts and updates is done on the server. This is due to the fact that these methods automatically add
97-
an "_id" field. The "_id" field is decorated by :py:class:`~typing_extensions.NotRequired` decorator to allow it to be accessed when reading
98-
from `result` (albeit without type-checking for that specific field, hence why it should not be used for schema validation).
99-
Another option would be to generate the "_id" field yourself, and make it a required field, which would give the expected behavior.
97+
an "_id" field. In the example below the "_id" field is marked by the :py:class:`~typing_extensions.NotRequired` notation to allow it to be accessed when reading
98+
from `result`. If it is simply not included in the definition, then it will be automatically added, but it will raise a type-checking error if you attempt to access it.
99+
Another option would be to generate the "_id" field yourself, and make it a required field. This would give the expected behavior, but would then also prevent you from
100+
relying on PyMongo to insert the "_id" field.
100101

101102
.. doctest::
102103

@@ -111,12 +112,14 @@ Another option would be to generate the "_id" field yourself, and make it a requ
111112
...
112113
>>> client: MongoClient = MongoClient()
113114
>>> collection: Collection[Movie] = client.test.test
114-
>>> inserted = collection.insert_one({"name": "Jurassic Park", "year": 1993 })
115+
>>> # If NotRequired was not specified above, then you would be required to specify _id
116+
>>> # when you construct the Movie object.
117+
>>> inserted = collection.insert_one(Movie(name="Jurassic Park", year=1993))
115118
>>> result = collection.find_one({"name": "Jurassic Park"})
116119
>>> assert result is not None
117120
>>> assert result["year"] == 1993
118-
>>> # Mypy will not check this because it is NotRequired
119-
>>> assert result["_id"] == tuple()
121+
>>> # This will be type checked, despite being not originally present
122+
>>> assert type(result["_id"]) == ObjectId
120123

121124
Typed Database
122125
--------------

test/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ def has_secondaries(self):
522522
@property
523523
def storage_engine(self):
524524
try:
525-
return self.server_status.get("storageEngine", {}).get(
525+
return self.server_status.get("storageEngine", {}).get( # type:ignore[union-attr]
526526
"name"
527-
) # type:ignore[union-attr]
527+
)
528528
except AttributeError:
529529
# Raised if self.server_status is None.
530530
return None

0 commit comments

Comments
 (0)