Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly type Image __init__ attributes #8275

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def _bitmap(self, header: int = 0, offset: int = 0) -> None:

# ------------------ Special case : header is reported 40, which
# ---------------------- is shorter than real size for bpp >= 16
assert isinstance(file_info["width"], int)
assert isinstance(file_info["height"], int)
self._size = file_info["width"], file_info["height"]

# ------- If color count was not found in the header, compute from bits
Expand Down
20 changes: 12 additions & 8 deletions src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _open(self) -> None:
self.fp.seek(offset)

self._mode = "RGB"
self._size = None
image_size: tuple[int, int] | None = None

byte_arr = bytearray(255)
bytes_mv = memoryview(byte_arr)
Expand All @@ -213,7 +213,7 @@ def check_required_header_comments() -> None:
raise SyntaxError(msg)

def _read_comment(s: str) -> bool:
nonlocal reading_trailer_comments
nonlocal image_size, reading_trailer_comments
try:
m = split.match(s)
except re.error as e:
Expand All @@ -228,16 +228,18 @@ def _read_comment(s: str) -> bool:
if k == "BoundingBox":
if v == "(atend)":
reading_trailer_comments = True
elif not self._size or (trailer_reached and reading_trailer_comments):
elif image_size is None or (
trailer_reached and reading_trailer_comments
):
try:
# Note: The DSC spec says that BoundingBox
# fields should be integers, but some drivers
# put floating point values there anyway.
box = [int(float(i)) for i in v.split()]
self._size = box[2] - box[0], box[3] - box[1]
image_size = box[2] - box[0], box[3] - box[1]
self.tile = [
ImageFile._Tile(
"eps", (0, 0) + self.size, offset, (length, box)
"eps", (0, 0) + image_size, offset, (length, box)
)
]
except Exception:
Expand Down Expand Up @@ -334,8 +336,8 @@ def _read_comment(s: str) -> bool:
else:
break

self._size = columns, rows
return
image_size = columns, rows
break
elif bytes_mv[:5] == b"%%EOF":
break
elif trailer_reached and reading_trailer_comments:
Expand All @@ -346,7 +348,9 @@ def _read_comment(s: str) -> bool:
trailer_reached = True
bytes_read = 0

if not self._size:
if image_size:
self._size = image_size
else:
msg = "cannot determine EPS bounding box"
raise OSError(msg)

Expand Down
3 changes: 2 additions & 1 deletion src/PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def _open_index(self, index: int = 1) -> None:
)

# size (highest resolution)

assert isinstance(prop[0x1000002], int)
assert isinstance(prop[0x1000003], int)
self._size = prop[0x1000002], prop[0x1000003]

size = max(self.size)
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ def __init__(self):
# FIXME: take "new" parameters / other image?
# FIXME: turn mode and size into delegating properties?
self.im = None
self._mode = ""
self._size = (0, 0)
self._mode: str = ""
self._size: tuple[int, int] = (0, 0)
self.palette = None
self.info = {}
self.readonly = 0
self._exif = None
self.info: dict[Any, Any] = {}
self.readonly: int = 0
self._exif: Exif | None = None

@property
def width(self) -> int:
Expand Down
5 changes: 3 additions & 2 deletions src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ def _seek(self, frame: int, rewind: bool = False) -> None:
assert self.png is not None

self.dispose: _imaging.ImagingCore | None
self.dispose_extent: tuple[int, int, int, int]
if frame == 0:
if rewind:
self._fp.seek(self.__rewind)
Expand All @@ -877,7 +878,7 @@ def _seek(self, frame: int, rewind: bool = False) -> None:
self.default_image = self.info.get("default_image", False)
self.dispose_op = self.info.get("disposal")
self.blend_op = self.info.get("blend")
self.dispose_extent = self.info.get("bbox")
self.dispose_extent = cast(tuple[int, int, int, int], self.info.get("bbox"))
self.__frame = 0
else:
if frame != self.__frame + 1:
Expand Down Expand Up @@ -935,7 +936,7 @@ def _seek(self, frame: int, rewind: bool = False) -> None:
self.tile = self.png.im_tile
self.dispose_op = self.info.get("disposal")
self.blend_op = self.info.get("blend")
self.dispose_extent = self.info.get("bbox")
self.dispose_extent = cast(tuple[int, int, int, int], self.info.get("bbox"))

if not self.tile:
msg = "image not found in APNG frame"
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
Loading