From 5b729b569b15ae8d8960e828e177582eca0a79a4 Mon Sep 17 00:00:00 2001 From: Vaclav Brozek Date: Thu, 5 Nov 2020 15:50:25 +0100 Subject: [PATCH 1/2] bpo-41877 Check for asert, aseert, assrt in mocks Currently, a Mock object which is not unsafe will raise an AttributeError if an attribute with the prefix assert or assret is accessed on it. This protects against misspellings of real assert method calls, which lead to tests passing silently even if the tested code does not satisfy the intended assertion. Recently a check was done in a large code base and three more frequent ways of misspelling assert were found causing harm: asert, aseert, assrt. These are now added to the existing check. --- Lib/unittest/mock.py | 4 ++-- Lib/unittest/test/testmock/testmock.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index b495a5f6ccc017..f5f502f257244c 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -631,9 +631,9 @@ def __getattr__(self, name): elif _is_magic(name): raise AttributeError(name) if not self._mock_unsafe: - if name.startswith(('assert', 'assret')): + if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')): raise AttributeError("Attributes cannot start with 'assert' " - "or 'assret'") + "or its misspellings") result = self._mock_children.get(name) if result is _deleted: diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index ce674e713e99cd..194ce3f61bbfdd 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1598,14 +1598,23 @@ def static_method(): pass #Issue21238 def test_mock_unsafe(self): m = Mock() - msg = "Attributes cannot start with 'assert' or 'assret'" + msg = "Attributes cannot start with 'assert' or its misspellings" with self.assertRaisesRegex(AttributeError, msg): m.assert_foo_call() with self.assertRaisesRegex(AttributeError, msg): m.assret_foo_call() + with self.assertRaisesRegex(AttributeError, msg): + m.asert_foo_call() + with self.assertRaisesRegex(AttributeError, msg): + m.aseert_foo_call() + with self.assertRaisesRegex(AttributeError, msg): + m.assrt_foo_call() m = Mock(unsafe=True) m.assert_foo_call() m.assret_foo_call() + m.asert_foo_call() + m.aseert_foo_call() + m.assrt_foo_call() #Issue21262 def test_assert_not_called(self): From dc578eb81bc208deaff739f73b775dbacfd49096 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2020 16:00:05 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst diff --git a/Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst b/Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst new file mode 100644 index 00000000000000..6f6fccb1d4cd18 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst @@ -0,0 +1,2 @@ +Mock objects which are not unsafe will now raise an AttributeError if an attribute with the prefix asert, aseert, +or assrt is accessed, in addition to this already happening for the prefixes assert or assret. \ No newline at end of file