diff --git a/tox.ini b/tox.ini index b6cb4bc54..3a58cc0cf 100644 --- a/tox.ini +++ b/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 diff --git a/watcher/hacking/checks.py b/watcher/hacking/checks.py index c4bfb85f3..f0e9a51b0 100644 --- a/watcher/hacking/checks.py +++ b/watcher/hacking/checks.py @@ -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``")