Skip to content

Commit ee9153f

Browse files
authored
DOCSP-46812 - Type Hints and Client Params (#156)
1 parent 0741c1b commit ee9153f

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

source/connect/mongoclient.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,77 @@ instance on port ``27017`` of ``localhost``:
9797
uri = "mongodb://localhost:27017/"
9898
client = MongoClient(uri)
9999

100+
The following table describes the positional parameters that the ``MongoClient()``
101+
constructor accepts. All parameters are optional.
102+
103+
.. list-table::
104+
:widths: 20 80
105+
:header-rows: 1
106+
107+
* - Parameter
108+
- Description
109+
110+
* - ``host``
111+
- The hostname, IP address, or Unix domain socket path of the MongoDB deployment.
112+
If your application connects to a replica set or sharded cluster, you can specify
113+
multiple hostnames or IP addresses in a Python list.
114+
115+
If you pass a literal IPv6 address, you must enclose the address in square brackets
116+
(``[ ]``). For example, pass the value ``[::1]`` to connect to localhost.
117+
118+
{+driver-short+} doesn't support :wikipedia:`multihomed <Multihoming>` and
119+
:wikipedia:`round-robin DNS <Round-robin_DNS>` addresses.
120+
121+
**Data type:** ``Union[str, Sequence[str]]``
122+
**Default value:** ``"localhost"``
123+
124+
* - ``port``
125+
- The port number {+mdb-server+} is running on.
126+
127+
You can include the port number in the ``host`` argument
128+
instead of using this parameter.
129+
130+
**Data type:** ``int``
131+
**Default value:** ``27017``
132+
133+
* - ``document_class``
134+
- The default class that the client uses to decode BSON documents returned by queries.
135+
This parameter supports the ``bson.raw_bson.RawBSONDocument`` type, as well as
136+
subclasses of the ``collections.abc.Mapping`` type, such as ``bson.son.SON``.
137+
138+
If you specify ``bson.son.SON`` as the document class, you must also specify types
139+
for the key and value.
140+
141+
**Data type:** ``Type[_DocumentType]``
142+
143+
* - ``tz_aware``
144+
- If this parameter is ``True``, the client treats ``datetime`` values as aware.
145+
Otherwise, it treats them as naive.
146+
147+
For more information about aware and naive ``datetime`` values, see
148+
`datetime <https://docs.python.org/3/library/datetime.html>`__ in the Python
149+
documentation.
150+
151+
**Data type:** ``bool``
152+
153+
* - ``connect``
154+
- If this parameter is ``True``, the client begins connecting to MongoDB
155+
in the background immediately after you create it. If this parameter is ``False``,
156+
the client connects to MongoDB when it performs the first database operation.
157+
158+
If your application is running in a
159+
:wikipedia:`function-as-a-service (FaaS) <Function_as_a_service>`
160+
environment, the default value is ``False``. Otherwise, the default value is ``True``.
161+
162+
**Data type:** ``bool``
163+
164+
* - ``type_registry``
165+
- An instance of the ``TypeRegistry`` class to enable encoding and decoding of
166+
custom types. For more information about encoding and decoding custom types,
167+
see :ref:`pymongo-custom-types`.
168+
169+
**Data type:** `TypeRegistry <{+api-root+}bson/codec_options.html#bson.codec_options.TypeRegistry>`__
170+
100171
.. tip:: Reusing Your Client
101172

102173
Because each ``MongoClient`` object represents a pool of connections to the
@@ -105,6 +176,28 @@ instance on port ``27017`` of ``localhost``:
105176
a process, the child process *does* need its own ``MongoClient`` object.
106177
To learn more, see the :ref:`FAQ <pymongo-faq>` page.
107178

179+
Type Hints
180+
----------
181+
182+
If you're using Python v3.5 or later, you can add type hints to your Python code.
183+
184+
The following code example shows how to declare a type hint for a ``MongoClient``
185+
object:
186+
187+
.. code-block:: python
188+
189+
client: MongoClient = MongoClient()
190+
191+
In the previous example, the code doesn't specify a type for the documents that the
192+
``MongoClient`` object will work with. To specify a document type,
193+
include the ``Dict[str, Any]`` type when you
194+
create the ``MongoClient`` object, as shown in the following example:
195+
196+
.. code-block:: python
197+
198+
from typing import Any, Dict
199+
client: MongoClient[Dict[str, Any]] = MongoClient()
200+
108201
API Documentation
109202
-----------------
110203

0 commit comments

Comments
 (0)