Skip to content

Commit 615ded4

Browse files
committed
Updated type hints
1 parent 4b258be commit 615ded4

25 files changed

+93
-73
lines changed

Tests/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import pytest
1919
from packaging.version import parse as parse_version
2020

21-
from PIL import Image, ImageMath, features
21+
from PIL import Image, ImageFile, ImageMath, features
2222

2323
logger = logging.getLogger(__name__)
2424

@@ -240,7 +240,7 @@ def _test_leak(self, core: Callable[[], None]) -> None:
240240
# helpers
241241

242242

243-
def fromstring(data: bytes) -> Image.Image:
243+
def fromstring(data: bytes) -> ImageFile.ImageFile:
244244
return Image.open(BytesIO(data))
245245

246246

Tests/test_file_jpeg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,10 @@ def test_separate_tables(self) -> None:
10331033

10341034
def test_repr_jpeg(self) -> None:
10351035
im = hopper()
1036+
b = im._repr_jpeg_()
1037+
assert b is not None
10361038

1037-
with Image.open(BytesIO(im._repr_jpeg_())) as repr_jpeg:
1039+
with Image.open(BytesIO(b)) as repr_jpeg:
10381040
assert repr_jpeg.format == "JPEG"
10391041
assert_image_similar(im, repr_jpeg, 17)
10401042

Tests/test_file_png.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,10 @@ def test_roundtrip_no_icc_profile(self) -> None:
535535

536536
def test_repr_png(self) -> None:
537537
im = hopper()
538+
b = im._repr_png_()
539+
assert b is not None
538540

539-
with Image.open(BytesIO(im._repr_png_())) as repr_png:
541+
with Image.open(BytesIO(b)) as repr_png:
540542
assert repr_png.format == "PNG"
541543
assert_image_equal(im, repr_png)
542544

@@ -768,14 +770,10 @@ def test_seek(self) -> None:
768770
def test_save_stdout(self, buffer: bool) -> None:
769771
old_stdout = sys.stdout
770772

771-
if buffer:
773+
class MyStdOut:
774+
buffer = BytesIO()
772775

773-
class MyStdOut:
774-
buffer = BytesIO()
775-
776-
mystdout = MyStdOut()
777-
else:
778-
mystdout = BytesIO()
776+
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
779777

780778
sys.stdout = mystdout
781779

@@ -785,7 +783,7 @@ class MyStdOut:
785783
# Reset stdout
786784
sys.stdout = old_stdout
787785

788-
if buffer:
786+
if isinstance(mystdout, MyStdOut):
789787
mystdout = mystdout.buffer
790788
with Image.open(mystdout) as reloaded:
791789
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)

Tests/test_file_ppm.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,10 @@ def test_mimetypes(tmp_path: Path) -> None:
368368
def test_save_stdout(buffer: bool) -> None:
369369
old_stdout = sys.stdout
370370

371-
if buffer:
371+
class MyStdOut:
372+
buffer = BytesIO()
372373

373-
class MyStdOut:
374-
buffer = BytesIO()
375-
376-
mystdout = MyStdOut()
377-
else:
378-
mystdout = BytesIO()
374+
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
379375

380376
sys.stdout = mystdout
381377

@@ -385,7 +381,7 @@ class MyStdOut:
385381
# Reset stdout
386382
sys.stdout = old_stdout
387383

388-
if buffer:
384+
if isinstance(mystdout, MyStdOut):
389385
mystdout = mystdout.buffer
390386
with Image.open(mystdout) as reloaded:
391387
assert_image_equal_tofile(reloaded, TEST_FILE)

Tests/test_file_tiff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_seek_too_large(self) -> None:
120120
def test_set_legacy_api(self) -> None:
121121
ifd = TiffImagePlugin.ImageFileDirectory_v2()
122122
with pytest.raises(Exception) as e:
123-
ifd.legacy_api = None
123+
ifd.legacy_api = False
124124
assert str(e.value) == "Not allowing setting of legacy api"
125125

126126
def test_xyres_tiff(self) -> None:

Tests/test_font_leaks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TestDefaultFontLeak(TestTTypeFontLeak):
3434

3535
def test_leak(self) -> None:
3636
if features.check_module("freetype2"):
37-
ImageFont.core = _util.DeferredError(ImportError)
37+
ImageFont.core = _util.DeferredError(ImportError("Disabled for testing"))
3838
try:
3939
default_font = ImageFont.load_default()
4040
finally:

Tests/test_image.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,13 @@ def test_alpha_inplace(self) -> None:
393393

394394
# errors
395395
with pytest.raises(ValueError):
396-
source.alpha_composite(over, "invalid source")
396+
source.alpha_composite(over, "invalid destination") # type: ignore[arg-type]
397397
with pytest.raises(ValueError):
398-
source.alpha_composite(over, (0, 0), "invalid destination")
398+
source.alpha_composite(over, (0, 0), "invalid source") # type: ignore[arg-type]
399399
with pytest.raises(ValueError):
400-
source.alpha_composite(over, 0)
400+
source.alpha_composite(over, 0) # type: ignore[arg-type]
401401
with pytest.raises(ValueError):
402-
source.alpha_composite(over, (0, 0), 0)
402+
source.alpha_composite(over, (0, 0), 0) # type: ignore[arg-type]
403403
with pytest.raises(ValueError):
404404
source.alpha_composite(over, (0, 0), (0, -1))
405405

Tests/test_image_draft.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def draft_roundtrip(
1616
im = Image.new(in_mode, in_size)
1717
data = tostring(im, "JPEG")
1818
im = fromstring(data)
19-
mode, box = im.draft(req_mode, req_size)
19+
result = im.draft(req_mode, req_size)
20+
assert result is not None
21+
box = result[1]
2022
scale, _ = im.decoderconfig
2123
assert box[:2] == (0, 0)
2224
assert (im.width - scale) < box[2] <= im.width

Tests/test_image_paste.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,8 @@ def test_different_sizes(self) -> None:
338338

339339
im.copy().paste(im2)
340340
im.copy().paste(im2, (0, 0))
341+
342+
def test_incorrect_abbreviated_form(self) -> None:
343+
im = Image.new("L", (1, 1))
344+
with pytest.raises(ValueError):
345+
im.paste(im, im, im)

Tests/test_image_point.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ def test_f_lut() -> None:
6161
def test_f_mode() -> None:
6262
im = hopper("F")
6363
with pytest.raises(ValueError):
64-
im.point(None)
64+
im.point([])

0 commit comments

Comments
 (0)