From 3e2a2df506f955007c9ca6f1650663c9e035cd1c Mon Sep 17 00:00:00 2001 From: Christian Klein <167265+cklein@users.noreply.github.com> Date: Wed, 4 Jan 2023 21:45:26 +0100 Subject: [PATCH 1/2] Respect mock spec when checking for unsafe prefixes --- Lib/test/test_unittest/testmock/testmock.py | 16 ++++++++++++++++ Lib/unittest/mock.py | 2 +- ...023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst diff --git a/Lib/test/test_unittest/testmock/testmock.py b/Lib/test/test_unittest/testmock/testmock.py index 8a92490137f9a7..b224f87fa3e4a1 100644 --- a/Lib/test/test_unittest/testmock/testmock.py +++ b/Lib/test/test_unittest/testmock/testmock.py @@ -1652,6 +1652,22 @@ def test_mock_unsafe(self): m.aseert_foo_call() m.assrt_foo_call() + # gh-100739 + def test_mock_safe_with_spec(self): + class Foo(object): + def assert_bar(self): + pass + + def assertSome(self): + pass + + m = Mock(spec=Foo) + m.assert_bar() + m.assertSome() + + m.assert_bar.assert_called_once() + m.assertSome.assert_called_once() + #Issue21262 def test_assert_not_called(self): m = Mock() diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 994947cad518f9..47928e56485035 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -652,7 +652,7 @@ def __getattr__(self, name): raise AttributeError("Mock object has no attribute %r" % name) elif _is_magic(name): raise AttributeError(name) - if not self._mock_unsafe: + if not self._mock_unsafe and (not self._mock_methods or name not in self._mock_methods): if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')): raise AttributeError( f"{name!r} is not a valid assertion. Use a spec " diff --git a/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst b/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst new file mode 100644 index 00000000000000..d3a0c3dff2ddcc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst @@ -0,0 +1 @@ +Fix Mock not respecting the spec for attribute names prefixed with assert From f43a0f6afefccebb4f2001e1c415bf22cd971b56 Mon Sep 17 00:00:00 2001 From: Christian Klein <167265+cklein@users.noreply.github.com> Date: Wed, 4 Jan 2023 21:45:32 +0100 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst Co-authored-by: Nikita Sobolev --- .../next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst b/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst index d3a0c3dff2ddcc..4753e7b408250c 100644 --- a/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst +++ b/Misc/NEWS.d/next/Library/2023-01-04-09-53-38.gh-issue-100740.-j5UjI.rst @@ -1 +1 @@ -Fix Mock not respecting the spec for attribute names prefixed with assert +Fix ``unittest.mock.Mock`` not respecting the spec for attribute names prefixed with ``assert``.