Skip to content

Commit 4f9dfef

Browse files
ckleinChristian Klein
authored and
Christian Klein
committed
Respect mock spec when checking for unsafe prefixes
1 parent a286caa commit 4f9dfef

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_unittest/testmock/testmock.py

+9
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,15 @@ def test_mock_unsafe(self):
16521652
m.aseert_foo_call()
16531653
m.assrt_foo_call()
16541654

1655+
# gh-100739
1656+
def test_mock_safe_with_spec(self):
1657+
class Foo(object):
1658+
def assert_bar(self):
1659+
pass
1660+
1661+
m = Mock(spec=Foo)
1662+
m.assert_bar()
1663+
16551664
#Issue21262
16561665
def test_assert_not_called(self):
16571666
m = Mock()

Lib/unittest/mock.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def __getattr__(self, name):
652652
raise AttributeError("Mock object has no attribute %r" % name)
653653
elif _is_magic(name):
654654
raise AttributeError(name)
655-
if not self._mock_unsafe:
655+
if not self._mock_unsafe and (not self._mock_methods or name not in self._mock_methods):
656656
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
657657
raise AttributeError(
658658
f"{name!r} is not a valid assertion. Use a spec "
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix Mock not respecting the spec for attribute names prefixed with assert

0 commit comments

Comments
 (0)