diff --git a/doc/api/pymongo/index.rst b/doc/api/pymongo/index.rst index 1a54a5b42d..5770145936 100644 --- a/doc/api/pymongo/index.rst +++ b/doc/api/pymongo/index.rst @@ -48,6 +48,8 @@ Sub-modules: read_preferences results server_api + server_description + topology_description uri_parser write_concern event_loggers diff --git a/doc/api/pymongo/mongo_client.rst b/doc/api/pymongo/mongo_client.rst index 73a79bb9b2..e48af01ad2 100644 --- a/doc/api/pymongo/mongo_client.rst +++ b/doc/api/pymongo/mongo_client.rst @@ -15,6 +15,7 @@ Raises :class:`~pymongo.errors.InvalidName` if an invalid database name is used. .. autoattribute:: event_listeners + .. autoattribute:: topology_description .. autoattribute:: address .. autoattribute:: primary .. autoattribute:: secondaries diff --git a/doc/api/pymongo/server_description.rst b/doc/api/pymongo/server_description.rst index 2d354fca6f..fc6b55ec74 100644 --- a/doc/api/pymongo/server_description.rst +++ b/doc/api/pymongo/server_description.rst @@ -6,8 +6,4 @@ .. automodule:: pymongo.server_description .. autoclass:: pymongo.server_description.ServerDescription() - - .. autoattribute:: address - .. autoattribute:: all_hosts - .. autoattribute:: server_type - .. autoattribute:: server_type_name + :members: diff --git a/doc/api/pymongo/topology_description.rst b/doc/api/pymongo/topology_description.rst index b14f1bf2c2..8141507df7 100644 --- a/doc/api/pymongo/topology_description.rst +++ b/doc/api/pymongo/topology_description.rst @@ -6,9 +6,5 @@ .. automodule:: pymongo.topology_description .. autoclass:: pymongo.topology_description.TopologyDescription() + :members: - .. automethod:: has_readable_server(read_preference=ReadPreference.PRIMARY) - .. automethod:: has_writable_server - .. automethod:: server_descriptions - .. autoattribute:: topology_type - .. autoattribute:: topology_type_name diff --git a/doc/changelog.rst b/doc/changelog.rst index 62f46f275c..e0cc3a6913 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -11,6 +11,8 @@ changes. For example, all APIs deprecated in PyMongo 3.X have been removed. Be sure to read the changes listed below and the :doc:`migrate-to-pymongo4` before upgrading from PyMongo 3.x. +- Added :attr:`pymongo.mongo_client.MongoClient.topology_description`. + Breaking Changes in 4.0 ....................... diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 21e6d66bb5..5dda8efd81 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -891,6 +891,28 @@ def event_listeners(self): """ return self._event_listeners.event_listeners + @property + def topology_description(self): + """The description of the connected MongoDB deployment. + + >>> client.topology_description + , , ]> + >>> client.topology_description.topology_type_name + 'ReplicaSetWithPrimary' + + Note that the description is periodically updated in the background + but the returned object itself is immutable. Access this property again + to get a more recent + :class:`~pymongo.topology_description.TopologyDescription`. + + :Returns: + An instance of + :class:`~pymongo.topology_description.TopologyDescription`. + + .. versionadded:: 4.0 + """ + return self._topology.description + @property def address(self): """(host, port) of the current standalone, primary, or mongos, or None. diff --git a/test/test_client.py b/test/test_client.py index 31850f701e..dd5e37392a 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -62,6 +62,7 @@ from pymongo.server_type import SERVER_TYPE from pymongo.settings import TOPOLOGY_TYPE from pymongo.topology import _ErrorContext +from pymongo.topology_description import TopologyDescription from pymongo.srv_resolver import _HAVE_DNSPYTHON from pymongo.write_concern import WriteConcern from test import (client_context, @@ -597,6 +598,8 @@ def test_init_disconnected(self): self.assertFalse(c.secondaries) c = rs_or_single_client(connect=False) self.assertIsInstance(c.max_write_batch_size, int) + self.assertIsInstance(c.topology_description, TopologyDescription) + self.assertEqual(c.topology_description, c._topology._description) if client_context.is_rs: # The primary's host and port are from the replica set config.