-
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 all 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 @@ | |
: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 @@ | |
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 ``OrderedDict``. | ||
Depending on the strictness of your type-checking rules, you might also need to | ||
Check failure on line 141 in source/connect/mongoclient.txt
|
||
specify types for the key and value, as shown in the following example: | ||
|
||
.. code-block:: python | ||
|
||
client = MongoClient(document_class=OrderedDict[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,46 @@ | |
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 | ||
``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 | ||
|
||
from typing import TypedDict | ||
|
||
class Movie(TypedDict): | ||
name: str | ||
year: int | ||
|
||
client: MongoClient[Movie] = MongoClient() | ||
|
||
Troubleshooting | ||
--------------- | ||
|
||
|
@@ -312,10 +348,14 @@ | |
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>`__ |
Uh oh!
There was an error while loading. Please reload this page.