Skip to content

Commit

Permalink
Merge pull request #8042 from radarhere/type_hint
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored May 9, 2024
2 parents 8246673 + 58ee112 commit 0cad346
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/PIL/BlpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class BlpImageFile(ImageFile.ImageFile):
format = "BLP"
format_description = "Blizzard Mipmap Format"

def _open(self):
def _open(self) -> None:
self.magic = self.fp.read(4)

self.fp.seek(5, os.SEEK_CUR)
Expand Down Expand Up @@ -333,7 +333,7 @@ def _read_bgra(self, palette):


class BLP1Decoder(_BLPBaseDecoder):
def _load(self):
def _load(self) -> None:
if self._blp_compression == Format.JPEG:
self._decode_jpeg_stream()

Expand Down Expand Up @@ -418,7 +418,7 @@ def _load(self):
class BLPEncoder(ImageFile.PyEncoder):
_pushes_fd = True

def _write_palette(self):
def _write_palette(self) -> bytes:
data = b""
palette = self.im.getpalette("RGBA", "RGBA")
for i in range(len(palette) // 4):
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _bitmap(self, header=0, offset=0):
)
]

def _open(self):
def _open(self) -> None:
"""Open file, check magic number and read header"""
# read 14 bytes: magic number, filesize, reserved, header final offset
head_data = self.fp.read(14)
Expand Down Expand Up @@ -376,7 +376,7 @@ class DibImageFile(BmpImageFile):
format = "DIB"
format_description = "Windows Bitmap"

def _open(self):
def _open(self) -> None:
self._bitmap()


Expand Down
4 changes: 2 additions & 2 deletions src/PIL/DcxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def _open(self):
self.is_animated = self.n_frames > 1
self.seek(0)

def seek(self, frame):
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return
self.frame = frame
self.fp = self._fp
self.fp.seek(self._offset[frame])
PcxImageFile._open(self)

def tell(self):
def tell(self) -> int:
return self.frame


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class DdsImageFile(ImageFile.ImageFile):
format = "DDS"
format_description = "DirectDraw Surface"

def _open(self):
def _open(self) -> None:
if not _accept(self.fp.read(4)):
msg = "not a DDS file"
raise SyntaxError(msg)
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def seek(self, offset, whence=io.SEEK_SET):
self.char = None
self.fp.seek(offset, whence)

def readline(self):
def readline(self) -> str:
s = [self.char or b""]
self.char = None

Expand Down Expand Up @@ -212,7 +212,7 @@ class EpsImageFile(ImageFile.ImageFile):

mode_map = {1: "L", 2: "LAB", 3: "RGB", 4: "CMYK"}

def _open(self):
def _open(self) -> None:
(length, offset) = self._find_offset(self.fp)

# go to offset - start of "%!PS"
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/FliImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _palette(self, palette, shift):
palette[i] = (r, g, b)
i += 1

def seek(self, frame):
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return
if frame < self.__frame:
Expand Down Expand Up @@ -162,7 +162,7 @@ def _seek(self, frame):

self.__offset += framesize

def tell(self):
def tell(self) -> int:
return self.__frame


Expand Down
12 changes: 6 additions & 6 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class GifImageFile(ImageFile.ImageFile):

global_palette = None

def data(self):
def data(self) -> bytes | None:
s = self.fp.read(1)
if s and s[0]:
return self.fp.read(s[0])
Expand All @@ -88,7 +88,7 @@ def _is_palette_needed(self, p):
return True
return False

def _open(self):
def _open(self) -> None:
# Screen
s = self.fp.read(13)
if not _accept(s):
Expand Down Expand Up @@ -147,7 +147,7 @@ def is_animated(self):
self.seek(current)
return self._is_animated

def seek(self, frame):
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return
if frame < self.__frame:
Expand Down Expand Up @@ -417,7 +417,7 @@ def _rgb(color):
elif k in self.info:
del self.info[k]

def load_prepare(self):
def load_prepare(self) -> None:
temp_mode = "P" if self._frame_palette else "L"
self._prev_im = None
if self.__frame == 0:
Expand All @@ -437,7 +437,7 @@ def load_prepare(self):

super().load_prepare()

def load_end(self):
def load_end(self) -> None:
if self.__frame == 0:
if self.mode == "P" and LOADING_STRATEGY == LoadingStrategy.RGB_ALWAYS:
if self._frame_transparency is not None:
Expand All @@ -463,7 +463,7 @@ def load_end(self):
else:
self.im.paste(frame_im, self.dispose_extent)

def tell(self):
def tell(self) -> int:
return self.__frame


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Hdf5StubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class HDF5StubImageFile(ImageFile.StubImageFile):
format = "HDF5"
format_description = "HDF5"

def _open(self):
def _open(self) -> None:
offset = self.fp.tell()

if not _accept(self.fp.read(8)):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class IcnsImageFile(ImageFile.ImageFile):
format = "ICNS"
format_description = "Mac OS icns resource"

def _open(self):
def _open(self) -> None:
self.icns = IcnsFile(self.fp)
self._mode = "RGBA"
self.info["sizes"] = self.icns.itersizes()
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class IcoImageFile(ImageFile.ImageFile):
format = "ICO"
format_description = "Windows Icon"

def _open(self):
def _open(self) -> None:
self.ico = IcoFile(self.fp)
self.info["sizes"] = self.ico.sizes()
self.size = self.ico.entry[0]["dim"]
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def n_frames(self):
def is_animated(self):
return self.info[FRAMES] > 1

def seek(self, frame):
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return

Expand All @@ -296,7 +296,7 @@ def seek(self, frame):

self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))]

def tell(self):
def tell(self) -> int:
return self.frame


Expand Down
7 changes: 5 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,10 @@ def _expand(self, xmargin: int, ymargin: int | None = None) -> Image:
self.load()
return self._new(self.im.expand(xmargin, ymargin))

def filter(self, filter):
if TYPE_CHECKING:
from . import ImageFilter

def filter(self, filter: ImageFilter.Filter | type[ImageFilter.Filter]) -> Image:
"""
Filters this image using the given filter. For a list of
available filters, see the :py:mod:`~PIL.ImageFilter` module.
Expand All @@ -1310,7 +1313,7 @@ def filter(self, filter):

self.load()

if isinstance(filter, Callable):
if callable(filter):
filter = filter()
if not hasattr(filter, "filter"):
msg = "filter argument should be ImageFilter.Filter instance or class"
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ def load(self):

return Image.Image.load(self)

def load_prepare(self):
def load_prepare(self) -> None:
# create image memory if necessary
if not self.im or self.im.mode != self.mode or self.im.size != self.size:
self.im = Image.core.new(self.mode, self.size)
# create palette (optional)
if self.mode == "P":
Image.Image.load(self)

def load_end(self):
def load_end(self) -> None:
# may be overridden
pass

Expand Down Expand Up @@ -390,7 +390,7 @@ class Parser:
offset = 0
finished = 0

def reset(self):
def reset(self) -> None:
"""
(Consumer) Reset the parser. Note that you can only call this
method immediately after you've created a parser; parser
Expand Down Expand Up @@ -605,7 +605,7 @@ def _safe_read(fp, size):


class PyCodecState:
def __init__(self):
def __init__(self) -> None:
self.xsize = 0
self.ysize = 0
self.xoff = 0
Expand Down Expand Up @@ -634,7 +634,7 @@ def init(self, args):
"""
self.args = args

def cleanup(self):
def cleanup(self) -> None:
"""
Override to perform codec specific cleanup
Expand Down
5 changes: 4 additions & 1 deletion src/PIL/ImageFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#
from __future__ import annotations

import abc
import functools


class Filter:
pass
@abc.abstractmethod
def filter(self, image):
pass


class MultibandFilter(Filter):
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def ui_handle_clear(self, dc, x0, y0, x1, y1):
def ui_handle_damage(self, x0, y0, x1, y1):
pass

def ui_handle_destroy(self):
def ui_handle_destroy(self) -> None:
pass

def ui_handle_repair(self, dc, x0, y0, x1, y1):
Expand All @@ -213,7 +213,7 @@ def ui_handle_repair(self, dc, x0, y0, x1, y1):
def ui_handle_resize(self, width, height):
pass

def mainloop(self):
def mainloop(self) -> None:
Image.core.eventloop()


Expand Down
4 changes: 2 additions & 2 deletions src/PIL/MicImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
format_description = "Microsoft Image Composer"
_close_exclusive_fp_after_loading = False

def _open(self):
def _open(self) -> None:
# read the OLE directory and see if this is a likely
# to be a Microsoft Image Composer file

Expand Down Expand Up @@ -88,7 +88,7 @@ def seek(self, frame):
def tell(self):
return self.frame

def close(self):
def close(self) -> None:
self.__fp.close()
self.ole.close()
super().close()
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/MpoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _after_jpeg_open(self, mpheader=None):
def load_seek(self, pos):
self._fp.seek(pos)

def seek(self, frame):
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return
self.fp = self._fp
Expand All @@ -149,7 +149,7 @@ def seek(self, frame):
self.tile = [("jpeg", (0, 0) + self.size, self.offset, self.tile[0][-1])]
self.__frame = frame

def tell(self):
def tell(self) -> int:
return self.__frame

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def verify(self) -> None:
self.fp.close()
self.fp = None

def seek(self, frame):
def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return
if frame < self.__frame:
Expand Down Expand Up @@ -909,10 +909,10 @@ def _seek(self, frame, rewind=False):
else:
self.dispose = None

def tell(self):
def tell(self) -> int:
return self.__frame

def load_prepare(self):
def load_prepare(self) -> None:
"""internal: prepare to read PNG file"""

if self.info.get("interlace"):
Expand Down Expand Up @@ -954,7 +954,7 @@ def load_read(self, read_bytes):

return self.fp.read(read_bytes)

def load_end(self):
def load_end(self) -> None:
"""internal: finished reading image data"""
if self.__idat != 0:
self.fp.read(self.__idat)
Expand Down
Loading

0 comments on commit 0cad346

Please sign in to comment.