diff --git a/pymongo/collection.py b/pymongo/collection.py index 2b81adf6c..9cf5ea681 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -154,7 +154,11 @@ class Collection(common.BaseObject): - `name`: the name of the collection to get """ if name.startswith('_'): - return super(Collection, self).__getattr__(name) + full_name = '%s.%s' % (self.__name, name) + raise AttributeError( + "Collection has no attribute %r. To access the %s" + " collection, use database['%s']." % ( + name, full_name, full_name)) return self.__getitem__(name) def __getitem__(self, name): diff --git a/pymongo/database.py b/pymongo/database.py index 4de4d8089..c02deab33 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -209,7 +209,9 @@ class Database(common.BaseObject): - `name`: the name of the collection to get """ if name.startswith('_'): - return super(Database, self).__getattr__(name) + raise AttributeError( + "Database has no attribute %r. To access the %s" + " collection, use database[%r]." % (name, name, name)) return self.__getitem__(name) def __getitem__(self, name): diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 9f354f4a3..ce3e52543 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -946,7 +946,9 @@ class MongoClient(common.BaseObject): - `name`: the name of the database to get """ if name.startswith('_'): - return super(MongoClient, self).__getattr__(name) + raise AttributeError( + "MongoClient has no attribute %r. To access the %s" + " database, use client[%r]." % (name, name, name)) return self.__getitem__(name) def __getitem__(self, name): diff --git a/test/test_client.py b/test/test_client.py index 9c3238165..6b0e28853 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -136,9 +136,15 @@ class ClientUnitTest(unittest.TestCase, TestRequestMixin): def test_getattr(self): self.assertTrue(isinstance(self.client['_does_not_exist'], Database)) - with self.assertRaises(AttributeError): + with self.assertRaises(AttributeError) as context: self.client._does_not_exist + # Message should be: + # "AttributeError: MongoClient has no attribute '_does_not_exist'. To + # access the _does_not_exist database, use client['_does_not_exist']". + self.assertIn("has no attribute '_does_not_exist'", + str(context.exception)) + def test_iteration(self): def iterate(): [a for a in self.client] diff --git a/test/test_collection.py b/test/test_collection.py index 4a7e8e70d..fb7c6094a 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -90,9 +90,16 @@ class TestCollectionNoConnect(unittest.TestCase): coll = self.db.test self.assertTrue(isinstance(coll['_does_not_exist'], Collection)) - with self.assertRaises(AttributeError): + with self.assertRaises(AttributeError) as context: coll._does_not_exist + # Message should be: + # "AttributeError: Collection has no attribute '_does_not_exist'. To + # access the test._does_not_exist collection, use + # database['test._does_not_exist']." + self.assertIn("has no attribute '_does_not_exist'", + str(context.exception)) + def test_iteration(self): self.assertRaises(TypeError, next, self.db) diff --git a/test/test_database.py b/test/test_database.py index 913d31736..449729ca4 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -94,9 +94,15 @@ class TestDatabaseNoConnect(unittest.TestCase): db = self.client.pymongo_test self.assertTrue(isinstance(db['_does_not_exist'], Collection)) - with self.assertRaises(AttributeError): + with self.assertRaises(AttributeError) as context: db._does_not_exist + # Message should be: "AttributeError: Database has no attribute + # '_does_not_exist'. To access the _does_not_exist collection, + # use database['_does_not_exist']". + self.assertIn("has no attribute '_does_not_exist'", + str(context.exception)) + def test_iteration(self): self.assertRaises(TypeError, next, self.client.pymongo_test)