mirror of
https://github.com/python/cpython.git
synced 2025-07-29 14:15:07 +00:00
gh-100739: Respect mock spec when checking for unsafe prefixes (GH-100740)
(cherry picked from commit 7f1eefc6f4
)
Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
eba6b00953
commit
541e7b8029
3 changed files with 18 additions and 1 deletions
|
@ -647,7 +647,7 @@ class NonCallableMock(Base):
|
||||||
raise AttributeError("Mock object has no attribute %r" % name)
|
raise AttributeError("Mock object has no attribute %r" % name)
|
||||||
elif _is_magic(name):
|
elif _is_magic(name):
|
||||||
raise AttributeError(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')):
|
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
|
||||||
raise AttributeError(
|
raise AttributeError(
|
||||||
f"{name!r} is not a valid assertion. Use a spec "
|
f"{name!r} is not a valid assertion. Use a spec "
|
||||||
|
|
|
@ -1652,6 +1652,22 @@ class MockTest(unittest.TestCase):
|
||||||
m.aseert_foo_call()
|
m.aseert_foo_call()
|
||||||
m.assrt_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
|
#Issue21262
|
||||||
def test_assert_not_called(self):
|
def test_assert_not_called(self):
|
||||||
m = Mock()
|
m = Mock()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix ``unittest.mock.Mock`` not respecting the spec for attribute names prefixed with ``assert``.
|
Loading…
Add table
Add a link
Reference in a new issue