Added type hints · python-pillow/Pillow@ddb7df0

11

from __future__ import annotations

223+

from pathlib import Path

4+35

import pytest

4657

from PIL import Image

6879

from .helper import assert_image, assert_image_equal, assert_image_similar, hopper

81091110-

def test_sanity():

11-

def convert(im, mode):

12+

def test_sanity() -> None:

13+

def convert(im: Image.Image, mode: str) -> None:

1214

out = im.convert(mode)

1315

assert out.mode == mode

1416

assert out.size == im.size

@@ -40,13 +42,13 @@ def convert(im, mode):

4042

convert(im, output_mode)

4143424443-

def test_unsupported_conversion():

45+

def test_unsupported_conversion() -> None:

4446

im = hopper()

4547

with pytest.raises(ValueError):

4648

im.convert("INVALID")

4749485049-

def test_default():

51+

def test_default() -> None:

5052

im = hopper("P")

5153

assert im.mode == "P"

5254

converted_im = im.convert()

@@ -62,18 +64,18 @@ def test_default():

6264

# ref https://github.com/python-pillow/Pillow/issues/274

6365646665-

def _test_float_conversion(im):

67+

def _test_float_conversion(im: Image.Image) -> None:

6668

orig = im.getpixel((5, 5))

6769

converted = im.convert("F").getpixel((5, 5))

6870

assert orig == converted

6971707271-

def test_8bit():

73+

def test_8bit() -> None:

7274

with Image.open("Tests/images/hopper.jpg") as im:

7375

_test_float_conversion(im.convert("L"))

7476757776-

def test_16bit():

78+

def test_16bit() -> None:

7779

with Image.open("Tests/images/16bit.cropped.tif") as im:

7880

_test_float_conversion(im)

7981

@@ -83,19 +85,19 @@ def test_16bit():

8385

assert im_i16.getpixel((0, 0)) == 65535

8486858786-

def test_16bit_workaround():

88+

def test_16bit_workaround() -> None:

8789

with Image.open("Tests/images/16bit.cropped.tif") as im:

8890

_test_float_conversion(im.convert("I"))

8991909291-

def test_opaque():

93+

def test_opaque() -> None:

9294

alpha = hopper("P").convert("PA").getchannel("A")

93959496

solid = Image.new("L", (128, 128), 255)

9597

assert_image_equal(alpha, solid)

9698979998-

def test_rgba_p():

100+

def test_rgba_p() -> None:

99101

im = hopper("RGBA")

100102

im.putalpha(hopper("L"))

101103

@@ -105,14 +107,14 @@ def test_rgba_p():

105107

assert_image_similar(im, comparable, 20)

106108107109108-

def test_rgba():

110+

def test_rgba() -> None:

109111

with Image.open("Tests/images/transparent.png") as im:

110112

assert im.mode == "RGBA"

111113112114

assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)

113115114116115-

def test_trns_p(tmp_path):

117+

def test_trns_p(tmp_path: Path) -> None:

116118

im = hopper("P")

117119

im.info["transparency"] = 0

118120

@@ -131,7 +133,7 @@ def test_trns_p(tmp_path):

131133132134133135

@pytest.mark.parametrize("mode", ("LA", "PA", "RGBA"))

134-

def test_trns_p_transparency(mode):

136+

def test_trns_p_transparency(mode: str) -> None:

135137

# Arrange

136138

im = hopper("P")

137139

im.info["transparency"] = 128

@@ -148,7 +150,7 @@ def test_trns_p_transparency(mode):

148150

assert converted_im.palette is None

149151150152151-

def test_trns_l(tmp_path):

153+

def test_trns_l(tmp_path: Path) -> None:

152154

im = hopper("L")

153155

im.info["transparency"] = 128

154156

@@ -171,7 +173,7 @@ def test_trns_l(tmp_path):

171173

im_p.save(f)

172174173175174-

def test_trns_RGB(tmp_path):

176+

def test_trns_RGB(tmp_path: Path) -> None:

175177

im = hopper("RGB")

176178

im.info["transparency"] = im.getpixel((0, 0))

177179

@@ -201,7 +203,7 @@ def test_trns_RGB(tmp_path):

201203202204203205

@pytest.mark.parametrize("convert_mode", ("L", "LA", "I"))

204-

def test_l_macro_rounding(convert_mode):

206+

def test_l_macro_rounding(convert_mode: str) -> None:

205207

for mode in ("P", "PA"):

206208

im = Image.new(mode, (1, 1))

207209

im.palette.getcolor((0, 1, 2))

@@ -214,7 +216,7 @@ def test_l_macro_rounding(convert_mode):

214216

assert converted_color == 1

215217216218217-

def test_gif_with_rgba_palette_to_p():

219+

def test_gif_with_rgba_palette_to_p() -> None:

218220

# See https://github.com/python-pillow/Pillow/issues/2433

219221

with Image.open("Tests/images/hopper.gif") as im:

220222

im.info["transparency"] = 255

@@ -226,7 +228,7 @@ def test_gif_with_rgba_palette_to_p():

226228

im_p.load()

227229228230229-

def test_p_la():

231+

def test_p_la() -> None:

230232

im = hopper("RGBA")

231233

alpha = hopper("L")

232234

im.putalpha(alpha)

@@ -236,7 +238,7 @@ def test_p_la():

236238

assert_image_similar(alpha, comparable, 5)

237239238240239-

def test_p2pa_alpha():

241+

def test_p2pa_alpha() -> None:

240242

with Image.open("Tests/images/tiny.png") as im:

241243

assert im.mode == "P"

242244

@@ -250,13 +252,13 @@ def test_p2pa_alpha():

250252

assert im_a.getpixel((x, y)) == alpha

251253252254253-

def test_p2pa_palette():

255+

def test_p2pa_palette() -> None:

254256

with Image.open("Tests/images/tiny.png") as im:

255257

im_pa = im.convert("PA")

256258

assert im_pa.getpalette() == im.getpalette()

257259258260259-

def test_matrix_illegal_conversion():

261+

def test_matrix_illegal_conversion() -> None:

260262

# Arrange

261263

im = hopper("CMYK")

262264

# fmt: off

@@ -272,7 +274,7 @@ def test_matrix_illegal_conversion():

272274

im.convert(mode="CMYK", matrix=matrix)

273275274276275-

def test_matrix_wrong_mode():

277+

def test_matrix_wrong_mode() -> None:

276278

# Arrange

277279

im = hopper("L")

278280

# fmt: off

@@ -289,7 +291,7 @@ def test_matrix_wrong_mode():

289291290292291293

@pytest.mark.parametrize("mode", ("RGB", "L"))

292-

def test_matrix_xyz(mode):

294+

def test_matrix_xyz(mode: str) -> None:

293295

# Arrange

294296

im = hopper("RGB")

295297

im.info["transparency"] = (255, 0, 0)

@@ -317,7 +319,7 @@ def test_matrix_xyz(mode):

317319

assert converted_im.info["transparency"] == 105

318320319321320-

def test_matrix_identity():

322+

def test_matrix_identity() -> None:

321323

# Arrange

322324

im = hopper("RGB")

323325

# fmt: off