Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: raise if several interfaces are found #113

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions substratools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,17 @@ def load_interface_from_module(module_name, interface_class, interface_signature
)

# find interface class
found_interfaces = []
for _, obj in inspect.getmembers(module, inspect.isclass):
if issubclass(obj, interface_class):
return obj() # return interface instance
if issubclass(obj, interface_class) and obj != interface_class:
found_interfaces.append(obj)

if len(found_interfaces) == 1:
return found_interfaces[0]() # return interface instance
elif len(found_interfaces) > 1:
raise exceptions.InvalidInterfaceError(
f"Multiple interfaces found in module '{module_name}': {found_interfaces}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the error message no one knows what interface mean

Copy link

@jeandut jeandut Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Multiple opener sub-classes defined in the opener.py file, this is not allowed."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The opener and the different subclasses will appear in module_name and found_interfaces

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree you don't have the info of interface_class which is in this case opener

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add at least interface_class to the debug print statement

)

# backward compatibility; accept methods at module level directly
if interface_signature is None:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ def test_load_opener_from_path(tmp_cwd, valid_opener_code):
assert o.get_data()[0] == "X"


def test_load_opener_from_path_error_with_inheritance(tmp_cwd):
wrong_opener_code = """
import json
from substratools import Opener

class FakeOpener(Opener):
def get_data(self, folder):
return 'X', list(range(0, 3))

def fake_data(self, n_samples):
return ['Xfake'] * n_samples, [0] * n_samples

class FinalOpener(FakeOpener):
def __init__(self):
super().__init__()
"""
dirpath = tmp_cwd / "myopener"
dirpath.mkdir()
path = dirpath / "my_opener.py"
path.write_text(wrong_opener_code)

with pytest.raises(exceptions.InvalidInterfaceError):
load_interface_from_module(
"opener",
interface_class=Opener,
interface_signature=None, # XXX does not support interface for debugging
path=path,
)


def test_opener_check_folders(tmp_cwd):
script = """
from substratools import Opener
Expand Down
Loading