Skip to content

Commit 4721c31

Browse files
authored
Merge pull request #8319 from radarhere/type_hint
Added type hints
2 parents cfb093a + 8aa58e3 commit 4721c31

File tree

15 files changed

+133
-87
lines changed

15 files changed

+133
-87
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ build_script:
5151

5252
test_script:
5353
- cd c:\pillow
54-
- '%PYTHON%\%EXECUTABLE% -m pip install pytest pytest-cov pytest-timeout defusedxml numpy olefile pyroma'
54+
- '%PYTHON%\%EXECUTABLE% -m pip install pytest pytest-cov pytest-timeout defusedxml ipython numpy olefile pyroma'
5555
- c:\"Program Files (x86)"\"Windows Kits"\10\Debuggers\x86\gflags.exe /p /enable %PYTHON%\%EXECUTABLE%
5656
- '%PYTHON%\%EXECUTABLE% -c "from PIL import Image"'
5757
- '%PYTHON%\%EXECUTABLE% -m pytest -vx --cov PIL --cov Tests --cov-report term --cov-report xml Tests'

.ci/install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ python3 -m pip install --upgrade pip
3030
python3 -m pip install --upgrade wheel
3131
python3 -m pip install coverage
3232
python3 -m pip install defusedxml
33+
python3 -m pip install ipython
3334
python3 -m pip install olefile
3435
python3 -m pip install -U pytest
3536
python3 -m pip install -U pytest-cov

.github/workflows/macos-install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export PKG_CONFIG_PATH="/usr/local/opt/openblas/lib/pkgconfig"
2323

2424
python3 -m pip install coverage
2525
python3 -m pip install defusedxml
26+
python3 -m pip install ipython
2627
python3 -m pip install olefile
2728
python3 -m pip install -U pytest
2829
python3 -m pip install -U pytest-cov

.github/workflows/test-cygwin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ jobs:
7474
perl
7575
python3${{ matrix.python-minor-version }}-cython
7676
python3${{ matrix.python-minor-version }}-devel
77+
python3${{ matrix.python-minor-version }}-ipython
7778
python3${{ matrix.python-minor-version }}-numpy
7879
python3${{ matrix.python-minor-version }}-sip
7980
python3${{ matrix.python-minor-version }}-tkinter

Tests/test_file_tiff.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,13 @@ def test_planar_configuration_save(self, tmp_path: Path) -> None:
684684
with Image.open(outfile) as reloaded:
685685
assert_image_equal_tofile(reloaded, infile)
686686

687+
def test_invalid_tiled_dimensions(self) -> None:
688+
with open("Tests/images/tiff_tiled_planar_raw.tif", "rb") as fp:
689+
data = fp.read()
690+
b = BytesIO(data[:144] + b"\x02" + data[145:])
691+
with pytest.raises(ValueError):
692+
Image.open(b)
693+
687694
@pytest.mark.parametrize("mode", ("P", "PA"))
688695
def test_palette(self, mode: str, tmp_path: Path) -> None:
689696
outfile = str(tmp_path / "temp.tif")

Tests/test_image.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
except ImportError:
4343
ElementTree = None
4444

45+
PrettyPrinter: type | None
46+
try:
47+
from IPython.lib.pretty import PrettyPrinter
48+
except ImportError:
49+
PrettyPrinter = None
50+
4551

4652
# Deprecation helper
4753
def helper_image_new(mode: str, size: tuple[int, int]) -> Image.Image:
@@ -91,16 +97,15 @@ def test_sanity(self) -> None:
9197
# with pytest.raises(MemoryError):
9298
# Image.new("L", (1000000, 1000000))
9399

100+
@pytest.mark.skipif(PrettyPrinter is None, reason="IPython is not installed")
94101
def test_repr_pretty(self) -> None:
95-
class Pretty:
96-
def text(self, text: str) -> None:
97-
self.pretty_output = text
98-
99102
im = Image.new("L", (100, 100))
100103

101-
p = Pretty()
104+
output = io.StringIO()
105+
assert PrettyPrinter is not None
106+
p = PrettyPrinter(output)
102107
im._repr_pretty_(p, False)
103-
assert p.pretty_output == "<PIL.Image.Image image mode=L size=100x100>"
108+
assert output.getvalue() == "<PIL.Image.Image image mode=L size=100x100>"
104109

105110
def test_open_formats(self) -> None:
106111
PNGFILE = "Tests/images/hopper.png"

Tests/test_imagefile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,8 @@ def test_encode(self) -> None:
412412
with pytest.raises(NotImplementedError):
413413
encoder.encode_to_pyfd()
414414

415-
fh = BytesIO()
416415
with pytest.raises(NotImplementedError):
417-
encoder.encode_to_file(fh, 0)
416+
encoder.encode_to_file(0, 0)
418417

419418
def test_zero_height(self) -> None:
420419
with pytest.raises(UnidentifiedImageError):

Tests/test_psdraw.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from io import BytesIO
66
from pathlib import Path
77

8-
import pytest
9-
108
from PIL import Image, PSDraw
119

1210

@@ -49,15 +47,14 @@ def test_draw_postscript(tmp_path: Path) -> None:
4947
assert os.path.getsize(tempfile) > 0
5048

5149

52-
@pytest.mark.parametrize("buffer", (True, False))
53-
def test_stdout(buffer: bool) -> None:
50+
def test_stdout() -> None:
5451
# Temporarily redirect stdout
5552
old_stdout = sys.stdout
5653

5754
class MyStdOut:
5855
buffer = BytesIO()
5956

60-
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
57+
mystdout = MyStdOut()
6158

6259
sys.stdout = mystdout
6360

@@ -67,6 +64,4 @@ class MyStdOut:
6764
# Reset stdout
6865
sys.stdout = old_stdout
6966

70-
if isinstance(mystdout, MyStdOut):
71-
mystdout = mystdout.buffer
72-
assert mystdout.getvalue() != b""
67+
assert mystdout.buffer.getvalue() != b""

docs/reference/Image.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ Classes
362362
:undoc-members:
363363
:show-inheritance:
364364
.. autoclass:: PIL.Image.ImagePointHandler
365+
.. autoclass:: PIL.Image.ImagePointTransform
365366
.. autoclass:: PIL.Image.ImageTransformHandler
366367

367368
Protocols

src/PIL/IcoImagePlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,11 @@ def _open(self) -> None:
319319
self.load()
320320

321321
@property
322-
def size(self):
322+
def size(self) -> tuple[int, int]:
323323
return self._size
324324

325325
@size.setter
326-
def size(self, value):
326+
def size(self, value: tuple[int, int]) -> None:
327327
if value not in self.info["sizes"]:
328328
msg = "This is not one of the allowed sizes of this image"
329329
raise ValueError(msg)

0 commit comments

Comments
 (0)