Skip to content

Commit 1d7ff59

Browse files
authored
Merge pull request #7724 from radarhere/type_hints_sgi
2 parents 4c5e2e4 + 5a58719 commit 1d7ff59

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/PIL/Image.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ def __setstate__(self, state):
710710
self.putpalette(palette)
711711
self.frombytes(data)
712712

713-
def tobytes(self, encoder_name="raw", *args):
713+
def tobytes(self, encoder_name: str = "raw", *args) -> bytes:
714714
"""
715715
Return image as a bytes object.
716716
@@ -788,7 +788,7 @@ def tobitmap(self, name="image"):
788788
]
789789
)
790790

791-
def frombytes(self, data, decoder_name="raw", *args):
791+
def frombytes(self, data: bytes, decoder_name: str = "raw", *args) -> None:
792792
"""
793793
Loads this image with pixel data from a bytes object.
794794
@@ -1297,7 +1297,7 @@ def filter(self, filter):
12971297
]
12981298
return merge(self.mode, ims)
12991299

1300-
def getbands(self):
1300+
def getbands(self) -> tuple[str, ...]:
13011301
"""
13021302
Returns a tuple containing the name of each band in this image.
13031303
For example, ``getbands`` on an RGB image returns ("R", "G", "B").
@@ -2495,7 +2495,7 @@ def show(self, title=None):
24952495

24962496
_show(self, title=title)
24972497

2498-
def split(self):
2498+
def split(self) -> tuple[Image, ...]:
24992499
"""
25002500
Split this image into individual bands. This method returns a
25012501
tuple of individual image bands from an image. For example,

src/PIL/SgiImagePlugin.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424

2525
import os
2626
import struct
27+
from io import BytesIO
2728

2829
from . import Image, ImageFile
2930
from ._binary import i16be as i16
3031
from ._binary import o8
3132

3233

33-
def _accept(prefix):
34+
def _accept(prefix: bytes) -> bool:
3435
return len(prefix) >= 2 and i16(prefix) == 474
3536

3637

@@ -52,8 +53,10 @@ class SgiImageFile(ImageFile.ImageFile):
5253
format = "SGI"
5354
format_description = "SGI Image File Format"
5455

55-
def _open(self):
56+
def _open(self) -> None:
5657
# HEAD
58+
assert self.fp is not None
59+
5760
headlen = 512
5861
s = self.fp.read(headlen)
5962

@@ -122,7 +125,7 @@ def _open(self):
122125
]
123126

124127

125-
def _save(im, fp, filename):
128+
def _save(im: Image.Image, fp: BytesIO, filename: str) -> None:
126129
if im.mode not in {"RGB", "RGBA", "L"}:
127130
msg = "Unsupported SGI image mode"
128131
raise ValueError(msg)
@@ -168,8 +171,8 @@ def _save(im, fp, filename):
168171
# Maximum Byte value (255 = 8bits per pixel)
169172
pinmax = 255
170173
# Image name (79 characters max, truncated below in write)
171-
img_name = os.path.splitext(os.path.basename(filename))[0]
172-
img_name = img_name.encode("ascii", "ignore")
174+
filename = os.path.basename(filename)
175+
img_name = os.path.splitext(filename)[0].encode("ascii", "ignore")
173176
# Standard representation of pixel in the file
174177
colormap = 0
175178
fp.write(struct.pack(">h", magic_number))
@@ -201,7 +204,10 @@ def _save(im, fp, filename):
201204
class SGI16Decoder(ImageFile.PyDecoder):
202205
_pulls_fd = True
203206

204-
def decode(self, buffer):
207+
def decode(self, buffer: bytes) -> tuple[int, int]:
208+
assert self.fd is not None
209+
assert self.im is not None
210+
205211
rawmode, stride, orientation = self.args
206212
pagesize = self.state.xsize * self.state.ysize
207213
zsize = len(self.mode)

0 commit comments

Comments
 (0)