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

Fix naming of npe1 converted contribution commands and add tests #374

Merged
merged 2 commits into from
Feb 19, 2025
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
6 changes: 3 additions & 3 deletions src/npe2/_inspection/_from_npe1.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ def _parse_writer(self, impl: HookImplementation, layer: str):
)

def add_command(self, impl: HookImplementation, py_name: str = "") -> str:
name = impl.specname.replace("napari_", "")
id = f"{self.package}.{name}"
title = " ".join(name.split("_")).title()
if not py_name:
py_name = _python_name(impl.function)
name = impl.function.__name__
id = f"{self.package}.{name}"
title = " ".join(name.split("_")).title()
c = CommandContribution(id=id, python_name=py_name, title=title)
self.contributions["commands"].append(c)
return id
Expand Down
8 changes: 8 additions & 0 deletions tests/npe1-plugin/npe1_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def gen_data(): ...
def napari_get_reader(path): ...


@napari_hook_implementation(specname="napari_get_reader")
def napari_other_reader(path): ...


@napari_hook_implementation
def napari_write_image(path, data, meta): ...

Expand All @@ -26,6 +30,10 @@ def napari_write_image(path, data, meta): ...
def napari_write_labels(path, data, meta): ...


@napari_hook_implementation(specname="napari_write_labels")
def napari_other_write_labels(path, data, meta): ...


@napari_hook_implementation
def napari_provide_sample_data():
return {
Expand Down
26 changes: 26 additions & 0 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ def test_conversion_from_module(mock_npe1_pm, npe1_plugin_module):
assert isinstance(mf.dict(), dict)


@pytest.mark.filterwarnings("ignore:Failed to convert napari_provide_sample_data")
@pytest.mark.filterwarnings("ignore:Error converting function")
@pytest.mark.filterwarnings("ignore:Error converting dock widget")
def test_conversion_multiple_readers(mock_npe1_pm, npe1_plugin_module):
mf = manifest_from_npe1(module=npe1_plugin_module)
assert isinstance(mf.dict(), dict)
assert (readers := mf.contributions.readers) is not None
reader_commands = {r.command for r in readers}
assert len(reader_commands) == 2
assert "dynamic.napari_get_reader" in reader_commands
assert "dynamic.napari_other_reader" in reader_commands


@pytest.mark.filterwarnings("ignore:Failed to convert napari_provide_sample_data")
@pytest.mark.filterwarnings("ignore:Error converting function")
@pytest.mark.filterwarnings("ignore:Error converting dock widget")
def test_conversion_multiple_writers(mock_npe1_pm, npe1_plugin_module):
mf = manifest_from_npe1(module=npe1_plugin_module)
assert isinstance(mf.dict(), dict)
assert (writers := mf.contributions.writers) is not None
writer_commands = {r.command for r in writers}
assert len(writer_commands) == 3
assert "dynamic.napari_write_labels" in writer_commands
assert "dynamic.napari_other_write_labels" in writer_commands


def test_conversion_from_obj_with_locals(mock_npe1_pm):
from napari_plugin_engine import napari_hook_implementation

Expand Down
Loading