-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46812 - Distribute Type Hints #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
1def2b0
0809a78
8f7ba00
4a208ee
c420d12
301d38a
c6bd034
7f48d6f
7f3d560
9a33331
013530d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,7 +119,7 @@ constructor accepts. All parameters are optional. | |
:wikipedia:`round-robin DNS <Round-robin_DNS>` addresses. | ||
|
||
**Data type:** ``Union[str, Sequence[str]]`` | ||
**Default value:** ``"localhost"`` | ||
| **Default value:** ``"localhost"`` | ||
|
||
* - ``port`` | ||
- The port number {+mdb-server+} is running on. | ||
|
@@ -128,17 +128,35 @@ constructor accepts. All parameters are optional. | |
instead of using this parameter. | ||
|
||
**Data type:** ``int`` | ||
**Default value:** ``27017`` | ||
| **Default value:** ``27017`` | ||
|
||
* - ``document_class`` | ||
- The default class that the client uses to decode BSON documents returned by queries. | ||
This parameter supports the ``bson.raw_bson.RawBSONDocument`` type, as well as | ||
subclasses of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``. | ||
This parameter accepts the following types: | ||
|
||
If you specify ``bson.son.SON`` as the document class, you must also specify types | ||
for the key and value. | ||
- ``bson.raw_bson.RawBSONDocument``. To learn more about the ``RawBSONDocument`` class, | ||
see :ref:`pymongo-bson-raw`. | ||
|
||
- A subclass of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``. | ||
If you specify ``bson.son.SON`` as the document class, you may also specify types | ||
for the key and value, as shown in the following example: | ||
|
||
.. code-block:: python | ||
|
||
client = MongoClient(document_class=SON[str, int]) | ||
|
||
- A subclass of the ``TypedDict`` type. To pass a ``TypedDict`` subclass for this | ||
parameter, you must also include the class in a type hint for your ``MongoClient`` | ||
object, as shown in the following example: | ||
|
||
.. code-block:: python | ||
|
||
client: MongoClient[MyTypedDict] = MongoClient() | ||
|
||
.. include:: /includes/type-hints/typeddict-availability.rst | ||
|
||
**Data type:** ``Type[_DocumentType]`` | ||
**Default:** ``dict`` | ||
|
||
* - ``tz_aware`` | ||
- If this parameter is ``True``, the client treats ``datetime`` values as aware. | ||
|
@@ -226,28 +244,40 @@ To use multiprocessing with {+driver-short+}, write code similar to the followin | |
Do not copy an instance of the ``MongoClient`` class from the parent process to a child | ||
process. | ||
|
||
Type Hints | ||
.. _pymongo-type-hints-client: | ||
|
||
Type Hints | ||
---------- | ||
|
||
If you're using Python v3.5 or later, you can add type hints to your Python code. | ||
.. include:: /includes/type-hints/intro.rst | ||
|
||
The following code example shows how to declare a type hint for a ``MongoClient`` | ||
object: | ||
To use type hints in your {+driver-short+} application, you must add a type annotation to your | ||
``MongoClient`` object, as shown in the following example: | ||
|
||
.. code-block:: python | ||
|
||
client: MongoClient = MongoClient() | ||
|
||
In the previous example, the code doesn't specify a type for the documents that the | ||
``MongoClient`` object will work with. To specify a document type, | ||
include the ``Dict[str, Any]`` type when you | ||
create the ``MongoClient`` object, as shown in the following example: | ||
For more accurate type information, you can include the generic document type | ||
mongoKart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``Dict[str, Any]`` in your type annotation. This data type matches all documents in MongoDB. | ||
The following example shows how to include this data type in your type annotation: | ||
|
||
.. code-block:: python | ||
|
||
from typing import Any, Dict | ||
client: MongoClient[Dict[str, Any]] = MongoClient() | ||
|
||
If all the documents that you are working with correspond to a single custom type, you | ||
can specify the custom type as a type hint for your ``MongoClient`` object. This | ||
provides more accurate type information than the generic ``Dict[str, Any]`` type. | ||
|
||
The following example shows how to specify the ``Movie`` type as a type hint for a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add the definition of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. happy to! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still missing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops |
||
``MongoClient`` object: | ||
|
||
.. code-block:: python | ||
|
||
client: MongoClient[Movie] = MongoClient() | ||
|
||
Troubleshooting | ||
--------------- | ||
|
||
|
@@ -312,10 +342,14 @@ process. | |
multithreaded contexts with ``fork()``, see `Issue 6721 <http://bugs.python.org/issue6721>`__ | ||
in the Python Issue Tracker. | ||
|
||
.. include:: /includes/type-hints/troubleshooting-client-type.rst | ||
|
||
.. include:: /includes/type-hints/troubleshooting-incompatible-type.rst | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about creating a ``MongoClient`` object in {+driver-short+}, | ||
see the following API documentation: | ||
|
||
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ | ||
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried this with
SON
without further subtypes. It worked fine. Maybe change "must" to "may".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the old docs need to be updated too, or did I misinterpret?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The subtypes are still required depending on how strict the user has their type checking settings. I don't think we want to update either docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blink1073 Is it most accurate to say 'you may need to specify types' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think that is fair
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we remove all references to
bson.son.SON
here? Its use case for ordered Python dictionaries is no longe relevant now thatOrderedDict
exists. Defaultdict
has also been ordered since Python 3.7.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to
OrderedDict
and removed other references toSON