Skip to content

Commit

Permalink
add typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Yay295 committed May 23, 2024
1 parent ed71e2f commit 5c75b0b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ def _open(self) -> None:
self.best_size[1] * self.best_size[2],
)

@Image.Image.size.setter
def size(self, value):
info_size = value
@Image.Image._size.setter
def size(self, value: tuple[int, int]) -> None:
info_size: tuple[int, ...] = value
if info_size not in self.info["sizes"] and len(info_size) == 2:
info_size = (info_size[0], info_size[1], 1)
if (
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ def _open(self) -> None:
self.size = self.ico.entry[0]["dim"]
self.load()

@Image.Image.size.setter
def size(self, value):
@Image.Image._size.setter
def size(self, value: tuple[int, int]) -> None:
if value not in self.info["sizes"]:
msg = "This is not one of the allowed sizes of this image"
raise ValueError(msg)
Expand Down
26 changes: 15 additions & 11 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def __init__(self):
self.pyaccess = None
self._exif = None

def _use_im_values(self):
def _use_im_values(self) -> bool:
"""
Whether or not to try using values from self.im
in addition to the values in this class.
Expand All @@ -548,32 +548,36 @@ def height(self) -> int:
return self.size[1]

@property
def size(self) -> tuple[int, int]:
def size(self) -> tuple[int, int] | None:
if self._use_im_values():
return self.im.size
return self.__size

def _set_size(self, value):
def _set_size(self, value: tuple[int, int] | None) -> None:
# set im.size first in case it raises an exception
if self._use_im_values():
self.im.size = value
self.__size = value

_size = property(fset=_set_size)
# MyPy doesn't support "x = property(...)"
# https://github.com/python/mypy/issues/8083
_size: tuple[int, int] = cast(tuple[int, int], property(fset=_set_size))

@property
def mode(self):
def mode(self) -> str | None:
if self._use_im_values():
return self.im.mode
return self.__mode

def _set_mode(self, value):
def _set_mode(self, value: str | None) -> None:
# set im.mode first in case it raises an exception
if self._use_im_values():
self.im.mode = value
self.__mode = value

_mode = property(fset=_set_mode)
# MyPy doesn't support "x = property(...)"
# https://github.com/python/mypy/issues/8083
_mode: str = cast(str, property(fset=_set_mode))

def _new(self, im) -> Image:
new = Image()
Expand Down Expand Up @@ -774,7 +778,7 @@ def __setstate__(self, state) -> None:
self.putpalette(palette)
self.frombytes(data)

def tobytes(self, encoder_name: str = "raw", *args) -> bytes:
def tobytes(self, encoder_name: str = "raw", *args: Any) -> bytes:
"""
Return image as a bytes object.
Expand All @@ -801,7 +805,7 @@ def tobytes(self, encoder_name: str = "raw", *args) -> bytes:
args = args[0]

if encoder_name == "raw" and args == ():
args = self.mode
args = (self.mode,)

self.load()

Expand Down Expand Up @@ -852,7 +856,7 @@ def tobitmap(self, name: str = "image") -> bytes:
]
)

def frombytes(self, data: bytes, decoder_name: str = "raw", *args) -> None:
def frombytes(self, data: bytes, decoder_name: str = "raw", *args: Any) -> None:
"""
Loads this image with pixel data from a bytes object.
Expand All @@ -869,7 +873,7 @@ def frombytes(self, data: bytes, decoder_name: str = "raw", *args) -> None:

# default format
if decoder_name == "raw" and args == ():
args = self.mode
args = (self.mode,)

# unpack data
d = _getdecoder(self.mode, decoder_name, args)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get_format_mimetype(self):
if self.format is not None:
return Image.MIME.get(self.format.upper())

def _use_im_values(self):
def _use_im_values(self) -> bool:
return self.tile is None and self.im is not None

def __setstate__(self, state):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/QoiImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _open(self) -> None:
msg = "not a QOI file"
raise SyntaxError(msg)

self._size = tuple(i32(self.fp.read(4)) for i in range(2))
self._size = (i32(self.fp.read(4)), i32(self.fp.read(4)))

channels = self.fp.read(1)[0]
self._mode = "RGB" if channels == 3 else "RGBA"
Expand Down

0 comments on commit 5c75b0b

Please sign in to comment.