Skip to content

Commit

Permalink
feat: allow for python hook names to be implied by class name
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Jan 18, 2024
1 parent 29a35b4 commit d2ebf17
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 44 deletions.
5 changes: 5 additions & 0 deletions tackle/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand Down
2 changes: 1 addition & 1 deletion tests/imports/no_hook_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class MyHook(BaseHook):
foo: str = 'bar'
hook_name: list = ['bar']

def exec(self):
return self.foo
44 changes: 1 addition & 43 deletions tests/imports/test_imports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import shutil
import subprocess
import sys

import pytest

Expand Down Expand Up @@ -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(
Expand All @@ -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."""
Expand Down

0 comments on commit d2ebf17

Please sign in to comment.