8000 gh-100739: Respect mock spec when checking for unsafe prefixes (GH-10… · python/cpython@f49cc3c · GitHub
[go: up one dir, main page]

Skip to content

Commit f49cc3c

Browse files
miss-islingtonckleinsobolevn
authored
gh-100739: Respect mock spec when checking for unsafe prefixes (GH-100740)
(cherry picked from commit 7f1eefc) Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com> Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent fee4059 commit f49cc3c

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def __getattr__(self, name):
643643
raise AttributeError("Mock object has no attribute %r" % name)
644644
elif _is_magic(name):
645645
raise AttributeError(name)
646-
if not self._mock_unsafe:
646+
if not self._mock_unsafe and (not self._mock_methods or name not in self._mock_methods):
647647
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
648648
raise AttributeError(
649649
f"{name!r} is not a valid assertion. Use a spec "

Lib/unittest/test/testmock/testmock.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,22 @@ def test_mock_unsafe(self):
16441644
m.aseert_foo_call()
16451645
m.assrt_foo_call()
16461646

1647+
# gh-100739
1648+
def test_mock_safe_with_spec(self):
1649+
class Foo(object):
1650+
def assert_bar(self):
1651+
pass
1652+
1653+
def assertSome(self):
1654+
pass
1655+
1656+
m = Mock(spec=Foo)
1657+
m.assert_bar()
1658+
m.assertSome()
1659+
1660+
m.assert_bar.assert_called_once()
1661+
m.assertSome.assert_called_once()
1662+
16471663
#Issue21262
16481664
def test_assert_not_called(self):
16491665
m = Mock()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``unittest.mock.Mock`` not respecting the spec for attribute names prefixed with ``assert``.

0 commit comments

Comments
 (0)
0