Fix MongoClient.__getattr__ implementation.

Before, MongoClient._does_not_exist had raised AttributeError, 'super' object has no
attribute '__getattr__'. Now it raises, AttributeError: MongoClient has no attribute
'_does_not_exist'. To access the _does_not_exist database, use client['_does_not_exist'].
This commit is contained in:
A. Jesse Jiryu Davis 2014-10-01 22:19:21 -04:00
parent c554297a0f
commit de215fe88b
6 changed files with 33 additions and 6 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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]

View File

@ -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)

View File

@ -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)