Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,14 @@ def __dir__(self):
extras = self._mock_methods or []
from_type = dir(type(self))
from_dict = list(self.__dict__)
from_child_mocks = [
m_name for m_name, m_value in self._mock_children.items()
if m_value is not _deleted]

from_type = [e for e in from_type if not e.startswith('_')]
from_dict = [e for e in from_dict if not e.startswith('_') or
_is_magic(e)]
return sorted(set(extras + from_type + from_dict +
list(self._mock_children)))
return sorted(set(extras + from_type + from_dict + from_child_mocks))


def __setattr__(self, name, value):
Expand Down
9 changes: 9 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,15 @@ def test_filter_dir(self):
patcher.stop()


def test_dir_does_not_include_deleted_attributes(self):
mock = Mock()
mock.child.return_value = 1

self.assertIn('child', dir(mock))
del mock.child
self.assertNotIn('child', dir(mock))


def test_configure_mock(self):
mock = Mock(foo='bar')
self.assertEqual(mock.foo, 'bar')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Don't return deleted attributes when calling dir on a
:class:`unittest.mock.Mock`.