From 4152af1f91d92e61ee75c331ae1e3c0a4069a7cf Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Tue, 3 Sep 2024 12:58:05 -0700 Subject: [PATCH] allow revalidation of already-proxied arrays --- src/numpydantic/interface/hdf5.py | 10 +++++----- src/numpydantic/interface/video.py | 8 ++++++-- tests/test_interface/test_interface.py | 2 ++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/numpydantic/interface/hdf5.py b/src/numpydantic/interface/hdf5.py index 6bb7dda..0bb8720 100644 --- a/src/numpydantic/interface/hdf5.py +++ b/src/numpydantic/interface/hdf5.py @@ -194,10 +194,7 @@ class H5Interface(Interface): passthrough numpy-like interface to the dataset. """ - input_types = ( - H5ArrayPath, - H5Arraylike, - ) + input_types = (H5ArrayPath, H5Arraylike, H5Proxy) return_type = H5Proxy @classmethod @@ -211,7 +208,7 @@ class H5Interface(Interface): Check that the given array is a :class:`.H5ArrayPath` or something that resembles one. """ - if isinstance(array, H5ArrayPath): + if isinstance(array, (H5ArrayPath, H5Proxy)): return True if isinstance(array, (tuple, list)) and len(array) in (2, 3): @@ -242,6 +239,9 @@ class H5Interface(Interface): """Create an :class:`.H5Proxy` to use throughout validation""" if isinstance(array, H5ArrayPath): array = H5Proxy.from_h5array(h5array=array) + elif isinstance(array, H5Proxy): + # nothing to do, already proxied + pass elif isinstance(array, (tuple, list)) and len(array) == 2: # pragma: no cover array = H5Proxy(file=array[0], path=array[1]) elif isinstance(array, (tuple, list)) and len(array) == 3: diff --git a/src/numpydantic/interface/video.py b/src/numpydantic/interface/video.py index 0ac7c66..f64457b 100644 --- a/src/numpydantic/interface/video.py +++ b/src/numpydantic/interface/video.py @@ -190,7 +190,7 @@ class VideoInterface(Interface): OpenCV interface to treat videos as arrays. """ - input_types = (str, Path, VideoCapture) + input_types = (str, Path, VideoCapture, VideoProxy) return_type = VideoProxy @classmethod @@ -204,7 +204,9 @@ class VideoInterface(Interface): Check if array is a string or Path with a supported video extension, or an opened VideoCapture object """ - if VideoCapture is not None and isinstance(array, VideoCapture): + if (VideoCapture is not None and isinstance(array, VideoCapture)) or isinstance( + array, VideoProxy + ): return True if isinstance(array, str): @@ -220,6 +222,8 @@ class VideoInterface(Interface): """Get a :class:`.VideoProxy` object for this video""" if isinstance(array, VideoCapture): proxy = VideoProxy(video=array) + elif isinstance(array, VideoProxy): + proxy = array else: proxy = VideoProxy(path=array) return proxy diff --git a/tests/test_interface/test_interface.py b/tests/test_interface/test_interface.py index 1856578..ba69328 100644 --- a/tests/test_interface/test_interface.py +++ b/tests/test_interface/test_interface.py @@ -156,5 +156,7 @@ def test_interface_recursive(interfaces): def test_interface_revalidate(all_interfaces): """ An interface should revalidate with the output of its initial validation + + See: https://github.com/p2p-ld/numpydantic/pull/14 """ _ = type(all_interfaces)(array=all_interfaces.array)