Skip to content

Commit eda9e50

Browse files
committed
Extract methods to make code cleaner
1 parent 1cb2b12 commit eda9e50

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

exasol/toolbox/config.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
from collections.abc import Callable
23
from typing import (
34
Annotated,
45
Any,
@@ -19,34 +20,50 @@
1920
from exasol.toolbox.util.version import Version
2021

2122

23+
def get_methods_with_hook_implementation(
24+
plugin_class: type[Any],
25+
) -> tuple[tuple[str, Callable], ...]:
26+
"""
27+
Get all methods from a plugin_class which were specified with a @hookimpl.
28+
"""
29+
return tuple(
30+
(name, method)
31+
for name, method in inspect.getmembers(plugin_class, inspect.isroutine)
32+
if hasattr(method, PLUGIN_ATTR_NAME)
33+
)
34+
35+
36+
def filter_not_specified_methods(
37+
methods: tuple[tuple[str, Callable], ...],
38+
) -> tuple[str, ...]:
39+
"""
40+
Filter methods which were specified with a @hookimpl but where not specified
41+
in `exasol.toolbox.nox.plugins.NoxTasks`.
42+
"""
43+
return tuple(name for name, _ in methods if name not in METHODS_SPECIFIED_FOR_HOOKS)
44+
45+
2246
def validate_plugin_hook(plugin_class: type[Any]):
2347
"""
24-
Checks methods in a class for at least one specific pluggy @hookimpl marker
25-
and verifies that this method is also specified in
26-
`exasol.toolbox.nox.plugins.NoxTasks`.
48+
Validate methods in a class for at least one pluggy @hookimpl marker and verifies
49+
that this method is also specified in `exasol.toolbox.nox.plugins.NoxTasks`.
2750
"""
28-
has_hook_implementation = False
29-
not_specified_decorated_methods = []
30-
for name, method in inspect.getmembers(plugin_class, inspect.isroutine):
31-
if hasattr(method, PLUGIN_ATTR_NAME):
32-
has_hook_implementation = True
33-
if name not in METHODS_SPECIFIED_FOR_HOOKS:
34-
not_specified_decorated_methods.append(name)
35-
36-
if not has_hook_implementation:
51+
methods_with_hook = get_methods_with_hook_implementation(plugin_class=plugin_class)
52+
53+
if len(methods_with_hook) == 0:
3754
raise ValueError(
3855
f"No methods in `{plugin_class.__name__}` were found to be decorated"
3956
"with `@hookimpl`. The `@hookimpl` decorator indicates that this"
4057
"will be used with pluggy and used in specific nox sessions."
4158
"Without it, this class does not modify any nox sessions."
4259
)
4360

44-
if not_specified_decorated_methods:
61+
if not_specified_methods := filter_not_specified_methods(methods_with_hook):
4562
raise ValueError(
46-
f"{len(not_specified_decorated_methods)} method(s) were "
63+
f"{len(not_specified_methods)} method(s) were "
4764
"decorated with `@hookimpl`, but these methods were not "
4865
"specified in `exasol.toolbox.nox.plugins.NoxTasks`: "
49-
f"{not_specified_decorated_methods}. The `@hookimpl` decorator indicates "
66+
f"{not_specified_methods}. The `@hookimpl` decorator indicates "
5067
"that these methods will be used by pluggy to modify specific nox sessions."
5168
"If the method was not previously specified, then no nox sessions will"
5269
"be modified. The `@hookimpl` is only used by nox sessions provided by the"

test/unit/config_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_raises_exception_method_with_hook_not_specified():
121121
with pytest.raises(ValidationError) as ex:
122122
BaseConfig(plugins_for_nox_sessions=(WithNotSpecifiedHook,))
123123
assert "1 method(s) were decorated with `@hookimpl`, but" in str(ex.value)
124-
assert "['not_specified_anywhere']" in str(ex.value)
124+
assert "('not_specified_anywhere',)" in str(ex.value)
125125

126126
@staticmethod
127127
def test_raises_exception_without_hook():

0 commit comments

Comments
 (0)