From 7c2631b4b0c3582c6fd4c3ec0efad6349bdf1d4b Mon Sep 17 00:00:00 2001 From: Vince Reuter Date: Thu, 7 Mar 2024 03:30:39 +0100 Subject: [PATCH 1/3] adopt quotes around type aliases to make Reader contribution copy/paste-ready https://github.com/napari/docs/pull/367#pullrequestreview-1921207030 https://github.com/napari/docs/issues/365 --- _docs/example_plugin/some_module.py | 4 ++-- src/npe2/types.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/_docs/example_plugin/some_module.py b/_docs/example_plugin/some_module.py index ac81ba53..d6d54232 100644 --- a/_docs/example_plugin/some_module.py +++ b/_docs/example_plugin/some_module.py @@ -20,7 +20,7 @@ def write_points(path: str, layer_data: Any, attributes: Dict[str, Any]) -> List return [path] -def get_reader(path: PathOrPaths) -> Optional[ReaderFunction]: +def get_reader(path: PathOrPaths) -> Optional["ReaderFunction"]: # If we recognize the format, we return the actual reader function if isinstance(path, str) and path.endswith(".xyz"): return xyz_file_reader @@ -28,7 +28,7 @@ def get_reader(path: PathOrPaths) -> Optional[ReaderFunction]: return None -def xyz_file_reader(path: PathOrPaths) -> List[LayerData]: +def xyz_file_reader(path: PathOrPaths) -> List["LayerData"]: data = ... # somehow read data from path layer_attributes = {"name": "etc..."} return [(data, layer_attributes)] diff --git a/src/npe2/types.py b/src/npe2/types.py index 3f649005..bdfb535d 100644 --- a/src/npe2/types.py +++ b/src/npe2/types.py @@ -60,7 +60,7 @@ def __array__(self) -> "np.ndarray": WidgetCreator = Callable[..., Widget] # ReaderContribution.command must point to a ReaderGetter -ReaderFunction = Callable[[PathOrPaths], List[LayerData]] +ReaderFunction = Callable[[PathOrPaths], List["LayerData"]] ReaderGetter = Callable[[PathOrPaths], Optional[ReaderFunction]] From c7236b24597b9be0b7c4758a2acf5f5dff797dca Mon Sep 17 00:00:00 2001 From: Vince Reuter Date: Fri, 8 Mar 2024 08:54:41 +0100 Subject: [PATCH 2/3] remove quotes around type alias in core type definition module https://github.com/napari/npe2/pull/340/files#r1516991493 --- src/npe2/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/npe2/types.py b/src/npe2/types.py index bdfb535d..3f649005 100644 --- a/src/npe2/types.py +++ b/src/npe2/types.py @@ -60,7 +60,7 @@ def __array__(self) -> "np.ndarray": WidgetCreator = Callable[..., Widget] # ReaderContribution.command must point to a ReaderGetter -ReaderFunction = Callable[[PathOrPaths], List["LayerData"]] +ReaderFunction = Callable[[PathOrPaths], List[LayerData]] ReaderGetter = Callable[[PathOrPaths], Optional[ReaderFunction]] From 3949b69491fcda69f0d6e46e8cb18a477c160b9d Mon Sep 17 00:00:00 2001 From: Vince Reuter Date: Thu, 21 Mar 2024 20:29:12 +0100 Subject: [PATCH 3/3] take PR suggestion from @tlamber03 to homogenise treatment of type names to preempt reader confusion --- _docs/example_plugin/some_module.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/_docs/example_plugin/some_module.py b/_docs/example_plugin/some_module.py index d6d54232..ebaf4f32 100644 --- a/_docs/example_plugin/some_module.py +++ b/_docs/example_plugin/some_module.py @@ -5,11 +5,10 @@ from magicgui import magic_factory from qtpy.QtWidgets import QWidget -from npe2.types import LayerData, PathOrPaths, ReaderFunction - if TYPE_CHECKING: import napari.types import napari.viewer + from npe2.types import LayerData, PathOrPaths, ReaderFunction def write_points(path: str, layer_data: Any, attributes: Dict[str, Any]) -> List[str]: @@ -20,7 +19,7 @@ def write_points(path: str, layer_data: Any, attributes: Dict[str, Any]) -> List return [path] -def get_reader(path: PathOrPaths) -> Optional["ReaderFunction"]: +def get_reader(path: "PathOrPaths") -> Optional["ReaderFunction"]: # If we recognize the format, we return the actual reader function if isinstance(path, str) and path.endswith(".xyz"): return xyz_file_reader @@ -28,7 +27,7 @@ def get_reader(path: PathOrPaths) -> Optional["ReaderFunction"]: return None -def xyz_file_reader(path: PathOrPaths) -> List["LayerData"]: +def xyz_file_reader(path: "PathOrPaths") -> List["LayerData"]: data = ... # somehow read data from path layer_attributes = {"name": "etc..."} return [(data, layer_attributes)] @@ -64,7 +63,7 @@ def threshold( return (image > threshold).astype(int) -def create_fractal() -> List[LayerData]: +def create_fractal() -> List["LayerData"]: """An example of a Sample Data Function. Note: Sample Data with URIs don't need python code.