Skip to content

Commit 1cebecb

Browse files
committed
Added type hints
1 parent 07df26a commit 1cebecb

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

.ci/requirements-mypy.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ IceSpringPySideStubs-PySide6
44
ipython
55
numpy
66
packaging
7+
pyarrow-stubs
78
pytest
89
sphinx
910
types-atheris

Tests/test_pyarrow.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
from typing import Any # undone
4-
53
import pytest
64

75
from PIL import Image
@@ -12,28 +10,15 @@
1210
hopper,
1311
)
1412

15-
pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed")
13+
TYPE_CHECKING = False
14+
if TYPE_CHECKING:
15+
import pyarrow
16+
else:
17+
pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed")
1618

1719
TEST_IMAGE_SIZE = (10, 10)
1820

1921

20-
def _test_img_equals_pyarray(
21-
img: Image.Image, arr: Any, mask: list[int] | None
22-
) -> None:
23-
assert img.height * img.width == len(arr)
24-
px = img.load()
25-
assert px is not None
26-
for x in range(0, img.size[0], int(img.size[0] / 10)):
27-
for y in range(0, img.size[1], int(img.size[1] / 10)):
28-
if mask:
29-
for ix, elt in enumerate(mask):
30-
pixel = px[x, y]
31-
assert isinstance(pixel, tuple)
32-
assert pixel[ix] == arr[y * img.width + x].as_py()[elt]
33-
else:
34-
assert_deep_equal(px[x, y], arr[y * img.width + x].as_py())
35-
36-
3722
# really hard to get a non-nullable list type
3823
fl_uint8_4_type = pyarrow.field(
3924
"_", pyarrow.list_(pyarrow.field("_", pyarrow.uint8()).with_nullable(False), 4)
@@ -55,16 +40,28 @@ def _test_img_equals_pyarray(
5540
("HSV", fl_uint8_4_type, [0, 1, 2]),
5641
),
5742
)
58-
def test_to_array(mode: str, dtype: Any, mask: list[int] | None) -> None:
43+
def test_to_array(mode: str, dtype: pyarrow.DataType, mask: list[int] | None) -> None:
5944
img = hopper(mode)
6045

6146
# Resize to non-square
6247
img = img.crop((3, 0, 124, 127))
6348
assert img.size == (121, 127)
6449

65-
arr = pyarrow.array(img)
66-
_test_img_equals_pyarray(img, arr, mask)
50+
arr = pyarrow.array(img) # type: ignore[call-overload]
6751
assert arr.type == dtype
52+
assert img.height * img.width == len(arr)
53+
54+
px = img.load()
55+
assert px is not None
56+
for x in range(0, img.size[0], int(img.size[0] / 10)):
57+
for y in range(0, img.size[1], int(img.size[1] / 10)):
58+
if mask:
59+
for ix, elt in enumerate(mask):
60+
pixel = px[x, y]
61+
assert isinstance(pixel, tuple)
62+
assert pixel[ix] == arr[y * img.width + x].as_py()[elt]
63+
else:
64+
assert_deep_equal(px[x, y], arr[y * img.width + x].as_py())
6865

6966
reloaded = Image.fromarrow(arr, mode, img.size)
7067

@@ -79,8 +76,8 @@ def test_lifetime() -> None:
7976

8077
img = hopper("L")
8178

82-
arr_1 = pyarrow.array(img)
83-
arr_2 = pyarrow.array(img)
79+
arr_1 = pyarrow.array(img) # type: ignore[call-overload]
80+
arr_2 = pyarrow.array(img) # type: ignore[call-overload]
8481

8582
del img
8683

@@ -97,8 +94,8 @@ def test_lifetime2() -> None:
9794

9895
img = hopper("L")
9996

100-
arr_1 = pyarrow.array(img)
101-
arr_2 = pyarrow.array(img)
97+
arr_1 = pyarrow.array(img) # type: ignore[call-overload]
98+
arr_2 = pyarrow.array(img) # type: ignore[call-overload]
10299

103100
assert arr_1.sum().as_py() > 0
104101
del arr_1

0 commit comments

Comments
 (0)