Skip to content

Commit a27731b

Browse files
authored
Improve type hints (#8883)
2 parents 3d261a2 + 04c984f commit a27731b

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

Tests/test_file_avif.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ def test_load_transparent_rgb(self) -> None:
254254
assert_image(im, "RGBA", (64, 64))
255255

256256
# image has 876 transparent pixels
257-
assert im.getchannel("A").getcolors()[0] == (876, 0)
257+
colors = im.getchannel("A").getcolors()
258+
assert colors is not None
259+
assert colors[0] == (876, 0)
258260

259261
def test_save_transparent(self, tmp_path: Path) -> None:
260262
im = Image.new("RGBA", (10, 10), (0, 0, 0, 0))

Tests/test_file_gif.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ def test_dispose_background_transparency() -> None:
540540
img.seek(2)
541541
px = img.load()
542542
assert px is not None
543-
assert px[35, 30][3] == 0
543+
value = px[35, 30]
544+
assert isinstance(value, tuple)
545+
assert value[3] == 0
544546

545547

546548
@pytest.mark.parametrize(
@@ -1422,7 +1424,9 @@ def test_getdata(monkeypatch: pytest.MonkeyPatch) -> None:
14221424
def test_lzw_bits() -> None:
14231425
# see https://github.com/python-pillow/Pillow/issues/2811
14241426
with Image.open("Tests/images/issue_2811.gif") as im:
1425-
assert im.tile[0][3][0] == 11 # LZW bits
1427+
args = im.tile[0][3]
1428+
assert isinstance(args, tuple)
1429+
assert args[0] == 11 # LZW bits
14261430
# codec error prepatch
14271431
im.load()
14281432

@@ -1477,7 +1481,11 @@ def test_saving_rgba(tmp_path: Path) -> None:
14771481

14781482
with Image.open(out) as reloaded:
14791483
reloaded_rgba = reloaded.convert("RGBA")
1480-
assert reloaded_rgba.load()[0, 0][3] == 0
1484+
px = reloaded_rgba.load()
1485+
assert px is not None
1486+
value = px[0, 0]
1487+
assert isinstance(value, tuple)
1488+
assert value[3] == 0
14811489

14821490

14831491
@pytest.mark.parametrize("params", ({}, {"disposal": 2, "optimize": False}))

Tests/test_file_jpeg.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,27 @@ def test_comment_write(self) -> None:
130130
def test_cmyk(self) -> None:
131131
# Test CMYK handling. Thanks to Tim and Charlie for test data,
132132
# Michael for getting me to look one more time.
133-
f = "Tests/images/pil_sample_cmyk.jpg"
134-
with Image.open(f) as im:
135-
# the source image has red pixels in the upper left corner.
136-
c, m, y, k = (x / 255.0 for x in im.getpixel((0, 0)))
137-
assert c == 0.0
138-
assert m > 0.8
139-
assert y > 0.8
140-
assert k == 0.0
141-
# the opposite corner is black
142-
c, m, y, k = (
143-
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
144-
)
145-
assert k > 0.9
146-
# roundtrip, and check again
147-
im = self.roundtrip(im)
133+
def check(im: ImageFile.ImageFile) -> None:
148134
cmyk = im.getpixel((0, 0))
149135
assert isinstance(cmyk, tuple)
150136
c, m, y, k = (x / 255.0 for x in cmyk)
151137
assert c == 0.0
152138
assert m > 0.8
153139
assert y > 0.8
154140
assert k == 0.0
141+
# the opposite corner is black
155142
cmyk = im.getpixel((im.size[0] - 1, im.size[1] - 1))
156143
assert isinstance(cmyk, tuple)
157144
k = cmyk[3] / 255.0
158145
assert k > 0.9
159146

147+
with Image.open("Tests/images/pil_sample_cmyk.jpg") as im:
148+
# the source image has red pixels in the upper left corner.
149+
check(im)
150+
151+
# roundtrip, and check again
152+
check(self.roundtrip(im))
153+
160154
def test_rgb(self) -> None:
161155
def getchannels(im: JpegImagePlugin.JpegImageFile) -> tuple[int, ...]:
162156
return tuple(v[0] for v in im.layer)

Tests/test_file_mpo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def test_reload_exif_after_seek() -> None:
156156
def test_mp(test_file: str) -> None:
157157
with Image.open(test_file) as im:
158158
mpinfo = im._getmp()
159+
assert mpinfo is not None
159160
assert mpinfo[45056] == b"0100"
160161
assert mpinfo[45057] == 2
161162

@@ -165,6 +166,7 @@ def test_mp_offset() -> None:
165166
# in APP2 data, in contrast to normal 8
166167
with Image.open("Tests/images/sugarshack_ifd_offset.mpo") as im:
167168
mpinfo = im._getmp()
169+
assert mpinfo is not None
168170
assert mpinfo[45056] == b"0100"
169171
assert mpinfo[45057] == 2
170172

@@ -181,6 +183,7 @@ def test_mp_no_data() -> None:
181183
def test_mp_attribute(test_file: str) -> None:
182184
with Image.open(test_file) as im:
183185
mpinfo = im._getmp()
186+
assert mpinfo is not None
184187
for frame_number, mpentry in enumerate(mpinfo[0xB002]):
185188
mpattr = mpentry["Attribute"]
186189
if frame_number:

Tests/test_file_tga.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,16 @@ def test_horizontal_orientations() -> None:
220220
with Image.open("Tests/images/rgb32rle_top_right.tga") as im:
221221
px = im.load()
222222
assert px is not None
223-
assert px[90, 90][:3] == (0, 0, 0)
223+
value = px[90, 90]
224+
assert isinstance(value, tuple)
225+
assert value[:3] == (0, 0, 0)
224226

225227
with Image.open("Tests/images/rgb32rle_bottom_right.tga") as im:
226228
px = im.load()
227229
assert px is not None
228-
assert px[90, 90][:3] == (0, 255, 0)
230+
value = px[90, 90]
231+
assert isinstance(value, tuple)
232+
assert value[:3] == (0, 255, 0)
229233

230234

231235
def test_save_rle(tmp_path: Path) -> None:

Tests/test_file_webp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def test_background_from_gif(self, tmp_path: Path) -> None:
219219
# Save P mode GIF with background
220220
with Image.open("Tests/images/chi.gif") as im:
221221
original_value = im.convert("RGB").getpixel((1, 1))
222+
assert isinstance(original_value, tuple)
222223

223224
# Save as WEBP
224225
im.save(out_webp, save_all=True)
@@ -230,6 +231,7 @@ def test_background_from_gif(self, tmp_path: Path) -> None:
230231

231232
with Image.open(out_gif) as reread:
232233
reread_value = reread.convert("RGB").getpixel((1, 1))
234+
assert isinstance(reread_value, tuple)
233235
difference = sum(abs(original_value[i] - reread_value[i]) for i in range(3))
234236
assert difference < 5
235237

0 commit comments

Comments
 (0)