From 3d77723a0c9d245f61e124c58a11e3a1779b3c0d Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 17 Apr 2025 21:42:42 +0100 Subject: [PATCH 01/10] Added arrow support for a flat array of 4*uint8 for image32 modes --- Tests/test_pyarrow.py | 66 +++++++++++++++++++++++++++++++++++++--- src/libImaging/Storage.c | 14 +++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index ece9f8f26..e7f2bc5f9 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -18,18 +18,25 @@ TEST_IMAGE_SIZE = (10, 10) def _test_img_equals_pyarray( - img: Image.Image, arr: Any, mask: list[int] | None + img: Image.Image, arr: Any, mask: list[int] | None, elts_per_pixel: int = 1 ) -> None: - assert img.height * img.width == len(arr) + assert img.height * img.width * elts_per_pixel == len(arr) px = img.load() assert px is not None + if elts_per_pixel > 1 and mask is None: + # have to do element wise comparison when we're comparing + # flattened r,g,b,a to a pixel. + mask = list(range(elts_per_pixel)) for x in range(0, img.size[0], int(img.size[0] / 10)): for y in range(0, img.size[1], int(img.size[1] / 10)): if mask: + pixel = px[x, y] + assert isinstance(pixel, tuple) for ix, elt in enumerate(mask): - pixel = px[x, y] - assert isinstance(pixel, tuple) - assert pixel[ix] == arr[y * img.width + x].as_py()[elt] + if elts_per_pixel == 1: + assert pixel[ix] == arr[y * img.width + x].as_py()[elt] + else: + assert pixel[ix] == arr[(y * img.width + x) * elts_per_pixel + elt].as_py() else: assert_deep_equal(px[x, y], arr[y * img.width + x].as_py()) @@ -110,3 +117,52 @@ def test_lifetime2() -> None: px = img2.load() assert px # make mypy happy assert isinstance(px[0, 0], int) + + +UINT_ARR = ( + fl_uint8_4_type, + [1,2,3,4], + 1 +) +UINT = ( + pyarrow.uint8(), + 3, + 4 +) + + + +@pytest.mark.parametrize( + "mode, data_tp, mask", + ( + ("L", (pyarrow.uint8(), 3, 1), None), + ("I", (pyarrow.int32(), 1<<24, 1), None), + ("F", (pyarrow.float32(), 3.14159, 1), None), + ("LA", UINT_ARR, [0, 3]), + ("LA", UINT, [0, 3]), + ("RGB", UINT_ARR, [0, 1, 2]), + ("RGBA", UINT_ARR, None), + ("RGBA", UINT_ARR, None), + ("CMYK", UINT_ARR, None), + ("YCbCr", UINT_ARR, [0, 1, 2]), + ("HSV", UINT_ARR, [0, 1, 2]), + ("RGB", UINT, [0, 1, 2]), + ("RGBA", UINT, None), + ("RGBA", UINT, None), + ("CMYK", UINT, None), + ("YCbCr", UINT, [0, 1, 2]), + ("HSV", UINT, [0, 1, 2]), + ), +) +def test_fromarray(mode: str, + data_tp: tuple, + mask:list[int] | None) -> None: + (dtype, + elt, + elts_per_pixel) = data_tp + + ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] + arr = pyarrow.array([elt]*(ct_pixels*elts_per_pixel), type=dtype) + img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) + + _test_img_equals_pyarray(img, arr, mask, elts_per_pixel) diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 4fa4ecd1c..7f8d9c4a0 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -723,6 +723,8 @@ ImagingNewArrow( int64_t pixels = (int64_t)xsize * (int64_t)ysize; // fmt:off // don't reformat this + // stored as a single array, one element per pixel, either single band + // or multiband, where each pixel is an I32. if (((strcmp(schema->format, "I") == 0 // int32 && im->pixelsize == 4 // 4xchar* storage && im->bands >= 2) // INT32 into any INT32 Storage mode @@ -735,6 +737,7 @@ ImagingNewArrow( return im; } } + // Stored as [[r,g,b,a],....] if (strcmp(schema->format, "+w:4") == 0 // 4 up array && im->pixelsize == 4 // storage as 32 bpc && schema->n_children > 0 // make sure schema is well formed. @@ -750,6 +753,17 @@ ImagingNewArrow( return im; } } + // Stored as [r,g,b,a,r,g,b,a....] + if (strcmp(schema->format, "C") == 0 // uint8 + && im->pixelsize == 4 // storage as 32 bpc + && schema->n_children == 0 // make sure schema is well formed. + && strcmp(im->arrow_band_format, "C") == 0 // Expected Format + && 4* pixels == external_array->length) { // expected length + // single flat array, interleaved storage. + if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) { + return im; + } + } // fmt: on ImagingDelete(im); return NULL; From c729d4e2085b96662b89ad09f99327f4516ce4ed Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 17 Apr 2025 22:16:27 +0100 Subject: [PATCH 02/10] Test uint32 array creation -> image32 images --- Tests/test_pyarrow.py | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index e7f2bc5f9..92bc4c807 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -9,6 +9,7 @@ from PIL import Image from .helper import ( assert_deep_equal, assert_image_equal, + is_big_endian, hopper, ) @@ -41,6 +42,34 @@ def _test_img_equals_pyarray( assert_deep_equal(px[x, y], arr[y * img.width + x].as_py()) +def _test_img_equals_int32_pyarray( + img: Image.Image, arr: Any, mask: list[int] | None, elts_per_pixel: int = 1 +) -> None: + assert img.height * img.width * elts_per_pixel == len(arr) + px = img.load() + assert px is not None + if mask is None: + # have to do element wise comparison when we're comparing + # flattened rgba in an uint32 to a pixel. + mask = list(range(elts_per_pixel)) + for x in range(0, img.size[0], int(img.size[0] / 10)): + for y in range(0, img.size[1], int(img.size[1] / 10)): + pixel = px[x, y] + assert isinstance(pixel, tuple) + arr_pixel_int = arr[y * img.width + x].as_py() + arr_pixel_tuple = ( + arr_pixel_int % 256, + (arr_pixel_int // 256) % 256, + (arr_pixel_int // 256**2) % 256, + (arr_pixel_int // 256**3), + ) + if is_big_endian(): + arr_pixel_tuple = arr_pixel_tuple[::-1] + + for ix, elt in enumerate(mask): + assert pixel[ix] == arr_pixel_tuple[elt] + + # really hard to get a non-nullable list type fl_uint8_4_type = pyarrow.field( "_", pyarrow.list_(pyarrow.field("_", pyarrow.uint8()).with_nullable(False), 4) @@ -129,7 +158,11 @@ UINT = ( 3, 4 ) - +INT32 = ( + pyarrow.uint32(), + 0xabcdef45, + 1 +) @pytest.mark.parametrize( @@ -166,3 +199,29 @@ def test_fromarray(mode: str, img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) _test_img_equals_pyarray(img, arr, mask, elts_per_pixel) + + +@pytest.mark.parametrize( + "mode, data_tp, mask", + ( + ("LA", INT32, [0, 3]), + ("RGB", INT32, [0, 1, 2]), + ("RGBA", INT32, None), + ("RGBA", INT32, None), + ("CMYK", INT32, None), + ("YCbCr", INT32, [0, 1, 2]), + ("HSV", INT32, [0, 1, 2]), + ), +) +def test_from_int32array(mode: str, + data_tp: tuple, + mask:list[int] | None) -> None: + (dtype, + elt, + elts_per_pixel) = data_tp + + ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] + arr = pyarrow.array([elt]*(ct_pixels*elts_per_pixel), type=dtype) + img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) + + _test_img_equals_int32_pyarray(img, arr, mask, elts_per_pixel) From ac500460dfc6ddaa7c0660de3f0233d05e207852 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 17 Apr 2025 22:22:31 +0100 Subject: [PATCH 03/10] lint --- Tests/test_pyarrow.py | 44 ++++++++++++++++------------------------ src/libImaging/Storage.c | 10 ++++----- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index 92bc4c807..bcdd7ddc9 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -9,8 +9,8 @@ from PIL import Image from .helper import ( assert_deep_equal, assert_image_equal, - is_big_endian, hopper, + is_big_endian, ) pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed") @@ -37,7 +37,10 @@ def _test_img_equals_pyarray( if elts_per_pixel == 1: assert pixel[ix] == arr[y * img.width + x].as_py()[elt] else: - assert pixel[ix] == arr[(y * img.width + x) * elts_per_pixel + elt].as_py() + assert ( + pixel[ix] + == arr[(y * img.width + x) * elts_per_pixel + elt].as_py() + ) else: assert_deep_equal(px[x, y], arr[y * img.width + x].as_py()) @@ -169,33 +172,27 @@ INT32 = ( "mode, data_tp, mask", ( ("L", (pyarrow.uint8(), 3, 1), None), - ("I", (pyarrow.int32(), 1<<24, 1), None), + ("I", (pyarrow.int32(), 1 << 24, 1), None), ("F", (pyarrow.float32(), 3.14159, 1), None), ("LA", UINT_ARR, [0, 3]), ("LA", UINT, [0, 3]), ("RGB", UINT_ARR, [0, 1, 2]), ("RGBA", UINT_ARR, None), - ("RGBA", UINT_ARR, None), ("CMYK", UINT_ARR, None), - ("YCbCr", UINT_ARR, [0, 1, 2]), - ("HSV", UINT_ARR, [0, 1, 2]), + ("YCbCr", UINT_ARR, [0, 1, 2]), + ("HSV", UINT_ARR, [0, 1, 2]), ("RGB", UINT, [0, 1, 2]), ("RGBA", UINT, None), - ("RGBA", UINT, None), ("CMYK", UINT, None), - ("YCbCr", UINT, [0, 1, 2]), - ("HSV", UINT, [0, 1, 2]), + ("YCbCr", UINT, [0, 1, 2]), + ("HSV", UINT, [0, 1, 2]), ), ) -def test_fromarray(mode: str, - data_tp: tuple, - mask:list[int] | None) -> None: - (dtype, - elt, - elts_per_pixel) = data_tp +def test_fromarray(mode: str, data_tp: tuple, mask: list[int] | None) -> None: + (dtype, elt, elts_per_pixel) = data_tp ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] - arr = pyarrow.array([elt]*(ct_pixels*elts_per_pixel), type=dtype) + arr = pyarrow.array([elt] * (ct_pixels * elts_per_pixel), type=dtype) img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) _test_img_equals_pyarray(img, arr, mask, elts_per_pixel) @@ -207,21 +204,16 @@ def test_fromarray(mode: str, ("LA", INT32, [0, 3]), ("RGB", INT32, [0, 1, 2]), ("RGBA", INT32, None), - ("RGBA", INT32, None), ("CMYK", INT32, None), - ("YCbCr", INT32, [0, 1, 2]), - ("HSV", INT32, [0, 1, 2]), + ("YCbCr", INT32, [0, 1, 2]), + ("HSV", INT32, [0, 1, 2]), ), ) -def test_from_int32array(mode: str, - data_tp: tuple, - mask:list[int] | None) -> None: - (dtype, - elt, - elts_per_pixel) = data_tp +def test_from_int32array(mode: str, data_tp: tuple, mask: list[int] | None) -> None: + (dtype, elt, elts_per_pixel) = data_tp ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] - arr = pyarrow.array([elt]*(ct_pixels*elts_per_pixel), type=dtype) + arr = pyarrow.array([elt] * (ct_pixels * elts_per_pixel), type=dtype) img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) _test_img_equals_int32_pyarray(img, arr, mask, elts_per_pixel) diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 7f8d9c4a0..2c57165c1 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -754,11 +754,11 @@ ImagingNewArrow( } } // Stored as [r,g,b,a,r,g,b,a....] - if (strcmp(schema->format, "C") == 0 // uint8 - && im->pixelsize == 4 // storage as 32 bpc - && schema->n_children == 0 // make sure schema is well formed. - && strcmp(im->arrow_band_format, "C") == 0 // Expected Format - && 4* pixels == external_array->length) { // expected length + if (strcmp(schema->format, "C") == 0 // uint8 + && im->pixelsize == 4 // storage as 32 bpc + && schema->n_children == 0 // make sure schema is well formed. + && strcmp(im->arrow_band_format, "C") == 0 // Expected Format + && 4 * pixels == external_array->length) { // expected length // single flat array, interleaved storage. if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) { return im; From 6bf791a3e7b2490bcb34ae9eb44419ee65c3caee Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 21 Apr 2025 10:27:49 +0100 Subject: [PATCH 04/10] Use a named tuple for the packed parameters --- Tests/test_pyarrow.py | 56 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index bcdd7ddc9..822cd18ac 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any # undone +from typing import Any, NamedTuple import pytest @@ -151,29 +151,37 @@ def test_lifetime2() -> None: assert isinstance(px[0, 0], int) -UINT_ARR = ( - fl_uint8_4_type, - [1,2,3,4], - 1 +class DataShape(NamedTuple): + dtype: Any + elt: Any + elts_per_pixel: int + + +UINT_ARR = DataShape( + dtype=fl_uint8_4_type, + elt=[1, 2, 3, 4], # array of 4 uint 8 per pixel + elts_per_pixel=1, # only one array per pixel ) -UINT = ( - pyarrow.uint8(), - 3, - 4 + +UINT = DataShape( + dtype=pyarrow.uint8(), + elt=3, # one uint8, + elts_per_pixel=4, # but repeated 4x per pixel ) -INT32 = ( - pyarrow.uint32(), - 0xabcdef45, - 1 + +UINT32 = DataShape( + dtype=pyarrow.uint32(), + elt=0xABCDEF45, # one packed int, doesn't fit in a int32 > 0x80000000 + elts_per_pixel=1, # one per pixel ) @pytest.mark.parametrize( "mode, data_tp, mask", ( - ("L", (pyarrow.uint8(), 3, 1), None), - ("I", (pyarrow.int32(), 1 << 24, 1), None), - ("F", (pyarrow.float32(), 3.14159, 1), None), + ("L", DataShape(pyarrow.uint8(), 3, 1), None), + ("I", DataShape(pyarrow.int32(), 1 << 24, 1), None), + ("F", DataShape(pyarrow.float32(), 3.14159, 1), None), ("LA", UINT_ARR, [0, 3]), ("LA", UINT, [0, 3]), ("RGB", UINT_ARR, [0, 1, 2]), @@ -188,7 +196,7 @@ INT32 = ( ("HSV", UINT, [0, 1, 2]), ), ) -def test_fromarray(mode: str, data_tp: tuple, mask: list[int] | None) -> None: +def test_fromarray(mode: str, data_tp: DataShape, mask: list[int] | None) -> None: (dtype, elt, elts_per_pixel) = data_tp ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] @@ -201,15 +209,15 @@ def test_fromarray(mode: str, data_tp: tuple, mask: list[int] | None) -> None: @pytest.mark.parametrize( "mode, data_tp, mask", ( - ("LA", INT32, [0, 3]), - ("RGB", INT32, [0, 1, 2]), - ("RGBA", INT32, None), - ("CMYK", INT32, None), - ("YCbCr", INT32, [0, 1, 2]), - ("HSV", INT32, [0, 1, 2]), + ("LA", UINT32, [0, 3]), + ("RGB", UINT32, [0, 1, 2]), + ("RGBA", UINT32, None), + ("CMYK", UINT32, None), + ("YCbCr", UINT32, [0, 1, 2]), + ("HSV", UINT32, [0, 1, 2]), ), ) -def test_from_int32array(mode: str, data_tp: tuple, mask: list[int] | None) -> None: +def test_from_int32array(mode: str, data_tp: DataShape, mask: list[int] | None) -> None: (dtype, elt, elts_per_pixel) = data_tp ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] From ce204f47f45f2ecdc831faac9c1b7ad8192a9fc7 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 21 Apr 2025 10:37:32 +0100 Subject: [PATCH 05/10] lint --- src/libImaging/Storage.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 2c57165c1..1a9171a0c 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -737,7 +737,7 @@ ImagingNewArrow( return im; } } - // Stored as [[r,g,b,a],....] + // Stored as [[r,g,b,a],...] if (strcmp(schema->format, "+w:4") == 0 // 4 up array && im->pixelsize == 4 // storage as 32 bpc && schema->n_children > 0 // make sure schema is well formed. @@ -753,7 +753,7 @@ ImagingNewArrow( return im; } } - // Stored as [r,g,b,a,r,g,b,a....] + // Stored as [r,g,b,a,r,g,b,a,...] if (strcmp(schema->format, "C") == 0 // uint8 && im->pixelsize == 4 // storage as 32 bpc && schema->n_children == 0 // make sure schema is well formed. From bc4b664b7094a311eb516e7eab1b88acf7496b67 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 21 Apr 2025 10:46:45 +0100 Subject: [PATCH 06/10] Add integer range tests --- Tests/test_pyarrow.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index 822cd18ac..6eedcafe7 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -153,7 +153,10 @@ def test_lifetime2() -> None: class DataShape(NamedTuple): dtype: Any - elt: Any + elt: Any # Strictly speaking, this should be a pixel or pixel component, + # so list[uint8][4], float, int, uint32, uint8, etc. + # But more correctly, it should be exactly the dtype from the + # line above. elts_per_pixel: int @@ -175,6 +178,12 @@ UINT32 = DataShape( elts_per_pixel=1, # one per pixel ) +INT32 = DataShape( + dtype=pyarrow.uint32(), + elt=0x12CDEF45, # one packed int + elts_per_pixel=1, # one per pixel +) + @pytest.mark.parametrize( "mode, data_tp, mask", @@ -215,6 +224,12 @@ def test_fromarray(mode: str, data_tp: DataShape, mask: list[int] | None) -> Non ("CMYK", UINT32, None), ("YCbCr", UINT32, [0, 1, 2]), ("HSV", UINT32, [0, 1, 2]), + ("LA", INT32, [0, 3]), + ("RGB", INT32, [0, 1, 2]), + ("RGBA", INT32, None), + ("CMYK", INT32, None), + ("YCbCr", INT32, [0, 1, 2]), + ("HSV", INT32, [0, 1, 2]), ), ) def test_from_int32array(mode: str, data_tp: DataShape, mask: list[int] | None) -> None: From 45e24e429f7d443000a6955d228fa00055104414 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 21 Apr 2025 10:54:00 +0100 Subject: [PATCH 07/10] Rearrance so black doesn't screw up the formatting --- Tests/test_pyarrow.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index 6eedcafe7..e7fce1e33 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -153,10 +153,10 @@ def test_lifetime2() -> None: class DataShape(NamedTuple): dtype: Any - elt: Any # Strictly speaking, this should be a pixel or pixel component, - # so list[uint8][4], float, int, uint32, uint8, etc. - # But more correctly, it should be exactly the dtype from the - # line above. + # Strictly speaking, elt should be a pixel or pixel component, so + # list[uint8][4], float, int, uint32, uint8, etc. But more + # correctly, it should be exactly the dtype from the line above. + elt: Any elts_per_pixel: int From 78887f6114e68d4208a6d5e8f3d5134a6da6831a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 9 May 2025 23:52:18 +1000 Subject: [PATCH 08/10] Corrected comment --- Tests/test_pyarrow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index e7fce1e33..c5872231b 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -162,7 +162,7 @@ class DataShape(NamedTuple): UINT_ARR = DataShape( dtype=fl_uint8_4_type, - elt=[1, 2, 3, 4], # array of 4 uint 8 per pixel + elt=[1, 2, 3, 4], # array of 4 uint8 per pixel elts_per_pixel=1, # only one array per pixel ) From 9526d949b07bbddfc7e515810dc23738b778bee4 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Fri, 23 May 2025 10:58:28 +0100 Subject: [PATCH 09/10] Update Tests/test_pyarrow.py Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_pyarrow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index e7fce1e33..c5872231b 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -162,7 +162,7 @@ class DataShape(NamedTuple): UINT_ARR = DataShape( dtype=fl_uint8_4_type, - elt=[1, 2, 3, 4], # array of 4 uint 8 per pixel + elt=[1, 2, 3, 4], # array of 4 uint8 per pixel elts_per_pixel=1, # only one array per pixel ) From 6807bd3d70cc5873b3cad29d598e08a34fdc1fa0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 10 May 2025 00:03:08 +1000 Subject: [PATCH 10/10] Added type hints --- .ci/requirements-mypy.txt | 1 + Tests/test_pyarrow.py | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.ci/requirements-mypy.txt b/.ci/requirements-mypy.txt index 2e3610478..86ac2e0b2 100644 --- a/.ci/requirements-mypy.txt +++ b/.ci/requirements-mypy.txt @@ -4,6 +4,7 @@ IceSpringPySideStubs-PySide6 ipython numpy packaging +pyarrow-stubs pytest sphinx types-atheris diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index c5872231b..2029f96f5 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -13,7 +13,11 @@ from .helper import ( is_big_endian, ) -pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed") +TYPE_CHECKING = False +if TYPE_CHECKING: + import pyarrow +else: + pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed") TEST_IMAGE_SIZE = (10, 10) @@ -94,14 +98,14 @@ fl_uint8_4_type = pyarrow.field( ("HSV", fl_uint8_4_type, [0, 1, 2]), ), ) -def test_to_array(mode: str, dtype: Any, mask: list[int] | None) -> None: +def test_to_array(mode: str, dtype: pyarrow.DataType, mask: list[int] | None) -> None: img = hopper(mode) # Resize to non-square img = img.crop((3, 0, 124, 127)) assert img.size == (121, 127) - arr = pyarrow.array(img) + arr = pyarrow.array(img) # type: ignore[call-overload] _test_img_equals_pyarray(img, arr, mask) assert arr.type == dtype @@ -118,8 +122,8 @@ def test_lifetime() -> None: img = hopper("L") - arr_1 = pyarrow.array(img) - arr_2 = pyarrow.array(img) + arr_1 = pyarrow.array(img) # type: ignore[call-overload] + arr_2 = pyarrow.array(img) # type: ignore[call-overload] del img @@ -136,8 +140,8 @@ def test_lifetime2() -> None: img = hopper("L") - arr_1 = pyarrow.array(img) - arr_2 = pyarrow.array(img) + arr_1 = pyarrow.array(img) # type: ignore[call-overload] + arr_2 = pyarrow.array(img) # type: ignore[call-overload] assert arr_1.sum().as_py() > 0 del arr_1 @@ -152,7 +156,7 @@ def test_lifetime2() -> None: class DataShape(NamedTuple): - dtype: Any + dtype: pyarrow.DataType # Strictly speaking, elt should be a pixel or pixel component, so # list[uint8][4], float, int, uint32, uint8, etc. But more # correctly, it should be exactly the dtype from the line above.