From 5c34b6bc4762ce75fff7a03e72b70efbb2c796b1 Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Sun, 26 Apr 2020 17:22:05 +0800 Subject: [PATCH] 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 --- tox.ini | 1 + watcher/hacking/checks.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) 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``")