Skip to content

Commit b18e24a

Browse files
committed
backport topology description helper to 3.12
1 parent 8174713 commit b18e24a

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

doc/api/pymongo/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Sub-modules:
5555
results
5656
son_manipulator
5757
server_api
58+
server_description
59+
topology_description
5860
uri_parser
5961
write_concern
6062
event_loggers

doc/api/pymongo/mongo_client.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Raises :class:`~pymongo.errors.InvalidName` if an invalid database name is used.
1616

1717
.. autoattribute:: event_listeners
18+
.. autoattribute:: topology_description
1819
.. autoattribute:: address
1920
.. autoattribute:: primary
2021
.. autoattribute:: secondaries

doc/api/pymongo/server_description.rst

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,4 @@
66
.. automodule:: pymongo.server_description
77

88
.. autoclass:: pymongo.server_description.ServerDescription()
9-
10-
.. autoattribute:: address
11-
.. autoattribute:: all_hosts
12-
.. autoattribute:: server_type
13-
.. autoattribute:: server_type_name
9+
:members:

doc/api/pymongo/topology_description.rst

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@
66
.. automodule:: pymongo.topology_description
77

88
.. autoclass:: pymongo.topology_description.TopologyDescription()
9+
:members:
910

10-
.. automethod:: has_readable_server(read_preference=ReadPreference.PRIMARY)
11-
.. automethod:: has_writable_server
12-
.. automethod:: server_descriptions
13-
.. autoattribute:: topology_type
14-
.. autoattribute:: topology_type_name

doc/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Changes in Version 3.12.0
99

1010
- PyMongoCrypt 1.1.0 or later is now required for client side field level
1111
encryption support.
12+
- Added :attr:`pymongo.mongo_client.MongoClient.topology_description`.
13+
1214

1315
Notable improvements
1416
....................

pymongo/mongo_client.py

+22
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,28 @@ def event_listeners(self):
990990
"""
991991
return self._event_listeners.event_listeners
992992

993+
@property
994+
def topology_description(self):
995+
"""The description of the connected MongoDB deployment.
996+
997+
>>> client.topology_description
998+
<TopologyDescription id: 605a7b04e76489833a7c6113, topology_type: ReplicaSetWithPrimary, servers: [<ServerDescription ('localhost', 27017) server_type: RSPrimary, rtt: 0.0007973677999995488>, <ServerDescription ('localhost', 27018) server_type: RSSecondary, rtt: 0.0005540556000003249>, <ServerDescription ('localhost', 27019) server_type: RSSecondary, rtt: 0.0010367483999999649>]>
999+
>>> client.topology_description.topology_type_name
1000+
'ReplicaSetWithPrimary'
1001+
1002+
Note that the description is periodically updated in the background
1003+
but the returned object itself is immutable. Access this property again
1004+
to get a more recent
1005+
:class:`~pymongo.topology_description.TopologyDescription`.
1006+
1007+
:Returns:
1008+
An instance of
1009+
:class:`~pymongo.topology_description.TopologyDescription`.
1010+
1011+
.. versionadded:: 3.12
1012+
"""
1013+
return self._topology.description
1014+
9931015
@property
9941016
def address(self):
9951017
"""(host, port) of the current standalone, primary, or mongos, or None.

test/test_client.py

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
writable_server_selector)
6363
from pymongo.server_type import SERVER_TYPE
6464
from pymongo.settings import TOPOLOGY_TYPE
65+
from pymongo.topology import _ErrorContext
66+
from pymongo.topology_description import TopologyDescription
6567
from pymongo.srv_resolver import _HAVE_DNSPYTHON
6668
from pymongo.write_concern import WriteConcern
6769
from test import (client_context,
@@ -602,6 +604,8 @@ def test_init_disconnected(self):
602604
self.assertFalse(c.secondaries)
603605
c = rs_or_single_client(connect=False)
604606
self.assertIsInstance(c.max_write_batch_size, int)
607+
self.assertIsInstance(c.topology_description, TopologyDescription)
608+
self.assertEqual(c.topology_description, c._topology._description)
605609

606610
if client_context.is_rs:
607611
# The primary's host and port are from the replica set config.

0 commit comments

Comments
 (0)