Skip to content

Commit c36782a

Browse files
Eric Friedgibizer
authored andcommitted
hacking: force explicit import of python's mock
Since we dropped support for python 2 [1], we no longer need to use the mock library, which existed to backport py3 functionality into py2. Change Ib44b5bff657c8e76c4f701e14d51a4efda3f6d32 cut over to importing the stock mock, which must be done by saying:: from unittest import mock ...because if you say:: import mock ...you will be using the third party mock library instead, which may or may not be installed. This commit adds hacking check N371 to enforce the former. [1] https://review.opendev.org/#/c/687954/ Change-Id: I71439580e80d33cff62aba807df2b35164a47cbe
1 parent f8cf050 commit c36782a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

nova/hacking/checks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,3 +1046,35 @@ def check_six(logical_line):
10461046
match = re.match(six_re, logical_line)
10471047
if match:
10481048
yield (0, "N370: Don't use or import six")
1049+
1050+
1051+
@core.flake8ext
1052+
def import_stock_mock(logical_line):
1053+
"""Use python's mock, not the mock library.
1054+
1055+
Since we `dropped support for python 2`__, we no longer need to use the
1056+
mock library, which existed to backport py3 functionality into py2. Change
1057+
Ib44b5bff657c8e76c4f701e14d51a4efda3f6d32 cut over to importing the stock
1058+
mock, which must be done by saying::
1059+
1060+
from unittest import mock
1061+
1062+
...because if you say::
1063+
1064+
import mock
1065+
1066+
...you may be getting the stock mock; or, due to transitive dependencies in
1067+
the environment, the library mock. This check can be removed in the future
1068+
(and we can start saying ``import mock`` again) if we manage to purge these
1069+
transitive dependencies.
1070+
1071+
.. __: https://review.opendev.org/#/c/687954/
1072+
1073+
N371
1074+
"""
1075+
if logical_line == 'import mock' or logical_line.startswith('from mock'):
1076+
yield (
1077+
0,
1078+
"N371: You must explicitly import python's mock: "
1079+
"``from unittest import mock``"
1080+
)

nova/tests/unit/test_hacking.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,16 @@ def test_check_six(self):
10301030
"""
10311031
errors = [(x + 1, 0, 'N370') for x in range(4)]
10321032
self._assert_has_errors(code, checks.check_six, expected_errors=errors)
1033+
1034+
def test_import_stock_mock(self):
1035+
self._assert_has_errors(
1036+
"import mock",
1037+
checks.import_stock_mock, expected_errors=[(1, 0, 'N371')])
1038+
self._assert_has_errors(
1039+
"from mock import patch",
1040+
checks.import_stock_mock, expected_errors=[(1, 0, 'N371')])
1041+
code = """
1042+
from unittest import mock
1043+
import unittest.mock
1044+
"""
1045+
self._assert_has_no_errors(code, checks.import_stock_mock)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ extension =
345345
N368 = checks:do_not_use_mock_class_as_new_mock_value
346346
N369 = checks:check_lockutils_rwlocks
347347
N370 = checks:check_six
348+
N371 = checks:import_stock_mock
348349
paths =
349350
./nova/hacking
350351

0 commit comments

Comments
 (0)