Skip to content

Commit b4c76a5

Browse files
committed
fix mypy errors
1 parent a235833 commit b4c76a5

23 files changed

Lines changed: 112 additions & 71 deletions

Tests/test_file_jpeg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
UnidentifiedImageError,
2020
features,
2121
)
22+
from PIL._typing import Buffer
2223

2324
from .helper import (
2425
assert_image,
@@ -40,6 +41,7 @@
4041
except ImportError:
4142
ElementTree = None
4243

44+
4345
TEST_FILE = "Tests/images/hopper.jpg"
4446

4547

@@ -1054,7 +1056,7 @@ def test_eof(self, monkeypatch: pytest.MonkeyPatch) -> None:
10541056
# the image should still end when there is no new data
10551057
class InfiniteMockPyDecoder(ImageFile.PyDecoder):
10561058
def decode(
1057-
self, buffer: bytes | Image.SupportsArrayInterface
1059+
self, buffer: Buffer | Image.SupportsArrayInterface
10581060
) -> tuple[int, int]:
10591061
return 0, 0
10601062

Tests/test_file_jpeg2k.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_reduce() -> None:
181181
assert isinstance(im, Jpeg2KImagePlugin.Jpeg2KImageFile)
182182

183183
im.reduce = 2
184-
assert im.reduce == 2
184+
assert im.reduce == 2 # type: ignore[comparison-overlap]
185185

186186
im.load()
187187
assert im.size == (160, 120)

Tests/test_image_frombytes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def test_sanity(data_type: str) -> None:
1212
im1 = hopper()
1313

14-
data = im1.tobytes()
14+
data: bytes | memoryview[int] = im1.tobytes()
1515
if data_type == "memoryview":
1616
data = memoryview(data)
1717
im2 = Image.frombytes(im1.mode, im1.size, data)

Tests/test_imagefile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
_binary,
1515
features,
1616
)
17+
from PIL._typing import Buffer
1718

1819
from .helper import (
1920
assert_image,
@@ -260,7 +261,7 @@ def __init__(self, mode: str, *args: Any) -> None:
260261

261262
super().__init__(mode, *args)
262263

263-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
264+
def decode(self, buffer: Buffer | Image.SupportsArrayInterface) -> tuple[int, int]:
264265
# eof
265266
return -1, 0
266267

Tests/test_imagewin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_dib_frombytes_tobytes_roundtrip(self) -> None:
116116

117117
# Act
118118
# Make one the same as the using tobytes()/frombytes()
119-
test_buffer = dib1.tobytes()
119+
test_buffer: bytes | memoryview[int] = dib1.tobytes()
120120
for datatype in ("bytes", "memoryview"):
121121
if datatype == "memoryview":
122122
test_buffer = memoryview(test_buffer)

Tests/test_qt_image_toqimage.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
if TYPE_CHECKING:
1111
from pathlib import Path
1212

13+
QImage = type
14+
else:
15+
if ImageQt.qt_is_installed:
16+
from PIL.ImageQt import QImage
17+
1318
pytestmark = pytest.mark.skipif(
1419
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
1520
)
1621

17-
if ImageQt.qt_is_installed:
18-
from PIL.ImageQt import QImage
19-
2022

2123
@pytest.mark.parametrize("mode", ("RGB", "RGBA", "L", "P", "1"))
2224
def test_sanity(mode: str, tmp_path: Path) -> None:

docs/example/DdsImagePlugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414

1515
import struct
1616
from io import BytesIO
17-
from typing import IO
1817

1918
from PIL import Image, ImageFile
2019

20+
TYPE_CHECKING = False
21+
if TYPE_CHECKING:
22+
from typing import IO
23+
24+
from typing_extensions import Buffer
25+
26+
2127
# Magic ("DDS ")
2228
DDS_MAGIC = 0x20534444
2329

@@ -258,7 +264,7 @@ def load_seek(self, pos: int) -> None:
258264
class DXT1Decoder(ImageFile.PyDecoder):
259265
_pulls_fd = True
260266

261-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
267+
def decode(self, buffer: Buffer | Image.SupportsArrayInterface) -> tuple[int, int]:
262268
assert self.fd is not None
263269
try:
264270
self.set_as_raw(_dxt1(self.fd, self.state.xsize, self.state.ysize))
@@ -271,7 +277,7 @@ def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int
271277
class DXT5Decoder(ImageFile.PyDecoder):
272278
_pulls_fd = True
273279

274-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
280+
def decode(self, buffer: Buffer | Image.SupportsArrayInterface) -> tuple[int, int]:
275281
assert self.fd is not None
276282
try:
277283
self.set_as_raw(_dxt5(self.fd, self.state.xsize, self.state.ysize))

src/PIL/BlpImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from typing import IO
4040

4141
from . import Image, ImageFile
42+
from ._typing import Buffer
4243

4344

4445
class Format(IntEnum):
@@ -295,7 +296,7 @@ def _open(self) -> None:
295296
class _BLPBaseDecoder(abc.ABC, ImageFile.PyDecoder):
296297
_pulls_fd = True
297298

298-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
299+
def decode(self, buffer: Buffer | Image.SupportsArrayInterface) -> tuple[int, int]:
299300
try:
300301
self._read_header()
301302
self._load()

src/PIL/BmpImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from ._binary import o8
3434
from ._binary import o16le as o16
3535
from ._binary import o32le as o32
36+
from ._typing import Buffer
3637

3738
#
3839
# --------------------------------------------------------------------
@@ -324,7 +325,7 @@ def _open(self) -> None:
324325
class BmpRleDecoder(ImageFile.PyDecoder):
325326
_pulls_fd = True
326327

327-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
328+
def decode(self, buffer: Buffer | Image.SupportsArrayInterface) -> tuple[int, int]:
328329
assert self.fd is not None
329330
rle4 = self.args[1]
330331
data = bytearray()

src/PIL/DdsImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ._binary import i32le as i32
2222
from ._binary import o8
2323
from ._binary import o32le as o32
24+
from ._typing import Buffer
2425

2526
# Magic ("DDS ")
2627
DDS_MAGIC = 0x20534444
@@ -488,7 +489,7 @@ def load_seek(self, pos: int) -> None:
488489
class DdsRgbDecoder(ImageFile.PyDecoder):
489490
_pulls_fd = True
490491

491-
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
492+
def decode(self, buffer: Buffer | Image.SupportsArrayInterface) -> tuple[int, int]:
492493
assert self.fd is not None
493494
bitcount, masks = self.args
494495

0 commit comments

Comments
 (0)