diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 2937c3c31..ab1878602 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -780,7 +780,7 @@ class MongoClient(common.BaseObject): def __ne__(self, other): return not self == other - def __repr__(self): + def _repr_helper(self): def option_repr(option, value): """Fix options whose __repr__ isn't usable in a constructor.""" if option == 'document_class': @@ -804,7 +804,10 @@ class MongoClient(common.BaseObject): option_repr(key, self.__options._options[key]) for key in self.__options._options if key not in set(self._constructor_args)) - return ("MongoClient(%s)" % ', '.join(options)) + return ', '.join(options) + + def __repr__(self): + return ("MongoClient(%s)" % (self._repr_helper(),)) def __getattr__(self, name): """Get a database by name. diff --git a/pymongo/mongo_replica_set_client.py b/pymongo/mongo_replica_set_client.py index 5aec49a1d..c9436c24e 100644 --- a/pymongo/mongo_replica_set_client.py +++ b/pymongo/mongo_replica_set_client.py @@ -45,6 +45,4 @@ class MongoReplicaSetClient(mongo_client.MongoClient): super(MongoReplicaSetClient, self).__init__(*args, **kwargs) def __repr__(self): - sds = self._topology.description.server_descriptions() - return "MongoReplicaSetClient(%r)" % (["%s:%d" % s.address - for s in sds.values()],) + return "MongoReplicaSetClient(%s)" % (self._repr_helper(),) diff --git a/test/test_client.py b/test/test_client.py index 8ee71a944..9b4b361dc 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -258,8 +258,8 @@ class TestClient(IntegrationTest): connect=False, document_class=SON) the_repr = repr(client) + self.assertIn('MongoClient(host=', the_repr) self.assertIn( - "MongoClient(host=['localhost:27017', 'localhost:27018'], " "document_class=bson.son.SON, " "tz_aware=False, " "connect=False, ", @@ -271,10 +271,8 @@ class TestClient(IntegrationTest): @client_context.require_replica_set def test_repr_replica_set(self): - # Like MongoClient(["localhost:27017", "localhost:27018"]). - self.assertIn("MongoClient([", repr(self.client)) - for node in client_context.nodes: - self.assertIn("%s:%d" % node, repr(self.client)) + self.assertIn("MongoClient(host=[", repr(self.client)) + self.assertIn(pair, repr(self.client)) def test_getters(self): self.assertEqual(client_context.client.address, (host, port))