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.
Which must be done by saying::
from unittest import mock
...because if you say::
import mock
...you definitely will not be getting the standard library mock.
That will always import the third party mock library.
This commit adds hacking check N366 to enforce the former.
This check can be removed in the future (and we can start saying
``import mock`` again) if we manage to purge these transitive
dependencies. I'm not holding my breath.
[1]https://review.opendev.org/#/c/717540
Change-Id: I8c8c99024e8de61d9151480d70543f809a100998
This commit is contained in:
1
tox.ini
1
tox.ini
@@ -105,6 +105,7 @@ extension =
|
||||
N340 = checks:check_oslo_i18n_wrapper
|
||||
N341 = checks:check_builtins_gettext
|
||||
N342 = checks:no_redundant_import_alias
|
||||
N366 = checks:import_stock_mock
|
||||
paths = ./watcher/hacking
|
||||
|
||||
|
||||
|
||||
@@ -285,3 +285,31 @@ def no_redundant_import_alias(logical_line):
|
||||
"""
|
||||
if re.match(re_redundant_import_alias, logical_line):
|
||||
yield(0, "N342: No redundant import alias.")
|
||||
|
||||
|
||||
@flake8ext
|
||||
def import_stock_mock(logical_line):
|
||||
"""Use python's mock, not the mock library.
|
||||
|
||||
Since we `dropped support for python 2`__, we no longer need to use the
|
||||
mock library, which existed to backport py3 functionality into py2.
|
||||
Which must be done by saying::
|
||||
|
||||
from unittest import mock
|
||||
|
||||
...because if you say::
|
||||
|
||||
import mock
|
||||
|
||||
...you definitely will not be getting the standard library mock. That will
|
||||
always import the third party mock library. This check can be removed in
|
||||
the future (and we can start saying ``import mock`` again) if we manage to
|
||||
purge these transitive dependencies.
|
||||
|
||||
.. __: https://review.opendev.org/#/c/717540
|
||||
|
||||
N366
|
||||
"""
|
||||
if logical_line == 'import mock':
|
||||
yield (0, "N366: You must explicitly import python's mock: "
|
||||
"``from unittest import mock``")
|
||||
|
||||
Reference in New Issue
Block a user