diff --git a/tackle/imports.py b/tackle/imports.py index dace350d6..3a1ee9629 100644 --- a/tackle/imports.py +++ b/tackle/imports.py @@ -8,6 +8,7 @@ from pydantic import PydanticUserError, ValidationError from pydantic._internal._model_construction import ModelMetaclass # noqa +from pydantic_core import PydanticUndefined from tackle import exceptions from tackle.context import Context, Data @@ -72,6 +73,10 @@ def import_python_hooks_from_file( if not is_base_hook_subclass(key=k, value=v): continue # Skip all non-hooks hook_name = v.model_fields['hook_name'].default + + if hook_name == PydanticUndefined: + # The `hook_name` field was not specified so just using the class name + hook_name = k if not isinstance(hook_name, str): raise exceptions.MalformedHookDefinitionException( f"The python hook defined in class=`{k}` does not " diff --git a/tests/imports/no_hook_name.py b/tests/imports/no_hook_name.py index f6411a2a9..db75e8a68 100644 --- a/tests/imports/no_hook_name.py +++ b/tests/imports/no_hook_name.py @@ -2,7 +2,7 @@ class MyHook(BaseHook): - foo: str = 'bar' + hook_name: list = ['bar'] def exec(self): return self.foo diff --git a/tests/imports/test_imports.py b/tests/imports/test_imports.py index 9d0bdb1d8..afcca3547 100644 --- a/tests/imports/test_imports.py +++ b/tests/imports/test_imports.py @@ -1,7 +1,5 @@ import os import shutil -import subprocess -import sys import pytest @@ -86,7 +84,7 @@ def test_imports_exceptions_reserved_field_no_annotation(hooks): ) -def test_imports_exceptions_no_hook_name(hooks): +def test_imports_exceptions_bad_hook_name_type(hooks): """Check we catch errors importing python hooks without hook_name defined.""" with pytest.raises(exceptions.MalformedHookDefinitionException): imports.import_hooks_from_file( @@ -96,46 +94,6 @@ def test_imports_exceptions_no_hook_name(hooks): ) -@pytest.fixture() -def temporary_uninstall(): - """Fixture to uninstall a package and install it after the test.""" - - def f(package): - subprocess.check_call( - [ - sys.executable, - "-m", - "pip", - "uninstall", - "--quiet", - "--disable-pip-version-check", - "-y", - package, - ] - ) - - return f - - -@pytest.mark.slow -def test_parser_provider_import_installs_requirements(temporary_uninstall): - """Validate that if a package is missing, that it will be installed and usable.""" - temporary_uninstall('requests') - try: - import requests - - # Fails in CI - I believe requests is available from system python as locally - # this assert works. - # assert False - except ImportError: - assert True - - tackle('test-install-dep.yaml') - import requests # noqa - - assert requests - - @pytest.fixture() def remove_provider(): """Fixture to remove a provider."""