From 6e2889878dfe6bfceac503ac10f7ffb73e68dfb9 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Mon, 5 Dec 2022 23:50:08 -0500 Subject: [PATCH] patch importlib.import_module last --- tests/unit/test_version.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_version.py b/tests/unit/test_version.py index 6545891fc54..217988ba5e2 100644 --- a/tests/unit/test_version.py +++ b/tests/unit/test_version.py @@ -673,10 +673,16 @@ def mock_import(*args, **kwargs): def mock_versions(mocker, installed="1.0.0", latest=None, plugins={}): mocker.patch("dbt.version.__version__", installed) - mock_plugins(mocker, plugins) mock_latest_versions(mocker, latest, plugins) + # mock_plugins must be called last to avoid erronously raising an ImportError. + mock_plugins(mocker, plugins) +# NOTE: mock_plugins patches importlib.import_module, and should always be the last +# patch to be mocked in order to avoid erronously raising an ImportError. +# Explanation: As of Python 3.11, mock.patch indirectly uses importlib.import_module +# and thus uses the mocked object (in this case, mock_import) instead of the real +# implementation in subsequent mock.patch calls. Issue: https://github.com/python/cpython/issues/98771 def mock_plugins(mocker, plugins): mock_find_spec = mocker.patch("importlib.util.find_spec") path = "/tmp/dbt/adapters"