diff --git a/salt/loader/lazy.py b/salt/loader/lazy.py index aef3b3580e0f..a868c88136b2 100644 --- a/salt/loader/lazy.py +++ b/salt/loader/lazy.py @@ -247,6 +247,7 @@ def __init__( pack_self=None, # Once we get rid of __utils__, the keyword arguments bellow should be removed _ast_dunder_virtual_inspect=False, + _ast_dunder_virtual_deprecate_only=True, _only_pack_properly_namespaced_functions=True, ): # pylint: disable=W0231 """ @@ -278,6 +279,7 @@ def __init__( self.loaded_base_name = loaded_base_name or LOADED_BASE_NAME self.mod_type_check = mod_type_check or _mod_type self._ast_dunder_virtual_inspect = _ast_dunder_virtual_inspect + self._ast_dunder_virtual_deprecate_only = _ast_dunder_virtual_deprecate_only self._only_pack_properly_namespaced_functions = ( _only_pack_properly_namespaced_functions ) @@ -700,18 +702,19 @@ def _load_module(self, name): and not self._ast_dunder_virtual_defined(name, fpath) ): func_decorator = _deprecated_dunder_utils_usage - # On 3010, un-commend the code below - # log.debug( - # "Not loading %r because it does not define a __virtual__ function", - # name, - # ) - # return False - - # On 3010, remove this code below - log.debug( - "Loading %r into __utils__ but this will stop working in Salt 3010", - name, - ) + if self._ast_dunder_virtual_deprecate_only is True: + # Pre 3010 behavior + log.debug( + "Loading %r into __utils__ but this will stop working in Salt 3010", + name, + ) + else: + # Post 3010 behavior + log.debug( + "Not loading %r because it does not define a __virtual__ function", + name, + ) + return False # if the fpath has `.cpython-3x` in it, but the running Py version # is 3.y, the following will cause us to return immediately and we won't try to import this .pyc. diff --git a/tests/pytests/functional/loader/test_utils.py b/tests/pytests/functional/loader/test_utils.py index 12c74819f631..9ac201138b3f 100644 --- a/tests/pytests/functional/loader/test_utils.py +++ b/tests/pytests/functional/loader/test_utils.py @@ -13,9 +13,19 @@ def loaded_base_name(): return random_string(f"{__name__}.", digits=False, uppercase=False) +def ast_dunder_virtual_deprecate_only_ids(value): + return f"ast_dunder_virtual_deprecate_only={value}" + + +@pytest.fixture(params=(True, False), ids=ast_dunder_virtual_deprecate_only_ids) +def ast_dunder_virtual_deprecate_only(request): + return request.param + + @pytest.fixture -def loader(minion_opts, loaded_base_name): +def loader(request, minion_opts, loaded_base_name, ast_dunder_virtual_deprecate_only): loader = salt.loader.utils(minion_opts, loaded_base_name=loaded_base_name) + loader._ast_dunder_virtual_deprecate_only = ast_dunder_virtual_deprecate_only try: with warnings.catch_warnings(): warnings.simplefilter("ignore") @@ -46,4 +56,10 @@ def test_ast_inspect_loading(loader): "process.", ) for loaded_func in loaded_functions: - assert not loaded_func.startswith(modules_which_do_not_define_dunder_virtual) + if loader._ast_dunder_virtual_deprecate_only: + if loaded_func.startswith(modules_which_do_not_define_dunder_virtual): + assert hasattr(loader[loaded_func], "__wrapped__") + else: + assert not loaded_func.startswith( + modules_which_do_not_define_dunder_virtual + )