Skip to content

Commit 53e82e4

Browse files
authored
Merge pull request #8117 from radarhere/type_hint
2 parents 0a45381 + 56c79b6 commit 53e82e4

File tree

8 files changed

+53
-31
lines changed

8 files changed

+53
-31
lines changed

src/PIL/GifImagePlugin.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,11 @@ def _normalize_palette(im, palette, info):
558558
return im
559559

560560

561-
def _write_single_frame(im, fp, palette):
561+
def _write_single_frame(
562+
im: Image.Image,
563+
fp: IO[bytes],
564+
palette: bytes | bytearray | list[int] | ImagePalette.ImagePalette,
565+
) -> None:
562566
im_out = _normalize_mode(im)
563567
for k, v in im_out.info.items():
564568
im.encoderinfo.setdefault(k, v)
@@ -579,7 +583,9 @@ def _write_single_frame(im, fp, palette):
579583
fp.write(b"\0") # end of image data
580584

581585

582-
def _getbbox(base_im, im_frame):
586+
def _getbbox(
587+
base_im: Image.Image, im_frame: Image.Image
588+
) -> tuple[Image.Image, tuple[int, int, int, int]]:
583589
if _get_palette_bytes(im_frame) != _get_palette_bytes(base_im):
584590
im_frame = im_frame.convert("RGBA")
585591
base_im = base_im.convert("RGBA")
@@ -790,7 +796,7 @@ def _write_local_header(fp, im, offset, flags):
790796
fp.write(o8(8)) # bits
791797

792798

793-
def _save_netpbm(im, fp, filename):
799+
def _save_netpbm(im: Image.Image, fp: IO[bytes], filename: str) -> None:
794800
# Unused by default.
795801
# To use, uncomment the register_save call at the end of the file.
796802
#
@@ -821,6 +827,7 @@ def _save_netpbm(im, fp, filename):
821827
)
822828

823829
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
830+
assert quant_proc.stdout is not None
824831
quant_proc.stdout.close()
825832

826833
retcode = quant_proc.wait()
@@ -1080,7 +1087,7 @@ def getdata(im, offset=(0, 0), **params):
10801087
class Collector:
10811088
data = []
10821089

1083-
def write(self, data):
1090+
def write(self, data: bytes) -> None:
10841091
self.data.append(data)
10851092

10861093
im.load() # make sure raster data is available

src/PIL/GimpGradientFile.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
from __future__ import annotations
2222

2323
from math import log, pi, sin, sqrt
24+
from typing import IO, Callable
2425

2526
from ._binary import o8
2627

2728
EPSILON = 1e-10
2829
"""""" # Enable auto-doc for data member
2930

3031

31-
def linear(middle, pos):
32+
def linear(middle: float, pos: float) -> float:
3233
if pos <= middle:
3334
if middle < EPSILON:
3435
return 0.0
@@ -43,19 +44,19 @@ def linear(middle, pos):
4344
return 0.5 + 0.5 * pos / middle
4445

4546

46-
def curved(middle, pos):
47+
def curved(middle: float, pos: float) -> float:
4748
return pos ** (log(0.5) / log(max(middle, EPSILON)))
4849

4950

50-
def sine(middle, pos):
51+
def sine(middle: float, pos: float) -> float:
5152
return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
5253

5354

54-
def sphere_increasing(middle, pos):
55+
def sphere_increasing(middle: float, pos: float) -> float:
5556
return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
5657

5758

58-
def sphere_decreasing(middle, pos):
59+
def sphere_decreasing(middle: float, pos: float) -> float:
5960
return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
6061

6162

@@ -64,9 +65,22 @@ def sphere_decreasing(middle, pos):
6465

6566

6667
class GradientFile:
67-
gradient = None
68-
69-
def getpalette(self, entries=256):
68+
gradient: (
69+
list[
70+
tuple[
71+
float,
72+
float,
73+
float,
74+
list[float],
75+
list[float],
76+
Callable[[float, float], float],
77+
]
78+
]
79+
| None
80+
) = None
81+
82+
def getpalette(self, entries: int = 256) -> tuple[bytes, str]:
83+
assert self.gradient is not None
7084
palette = []
7185

7286
ix = 0
@@ -101,7 +115,7 @@ def getpalette(self, entries=256):
101115
class GimpGradientFile(GradientFile):
102116
"""File handler for GIMP's gradient format."""
103117

104-
def __init__(self, fp):
118+
def __init__(self, fp: IO[bytes]) -> None:
105119
if fp.readline()[:13] != b"GIMP Gradient":
106120
msg = "not a GIMP gradient file"
107121
raise SyntaxError(msg)
@@ -114,7 +128,7 @@ def __init__(self, fp):
114128

115129
count = int(line)
116130

117-
gradient = []
131+
self.gradient = []
118132

119133
for i in range(count):
120134
s = fp.readline().split()
@@ -132,6 +146,4 @@ def __init__(self, fp):
132146
msg = "cannot handle HSV colour space"
133147
raise OSError(msg)
134148

135-
gradient.append((x0, x1, xm, rgb0, rgb1, segment))
136-
137-
self.gradient = gradient
149+
self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))

src/PIL/Image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,7 @@ def save(
24702470

24712471
save_all = params.pop("save_all", False)
24722472
self.encoderinfo = params
2473-
self.encoderconfig = ()
2473+
self.encoderconfig: tuple[Any, ...] = ()
24742474

24752475
preinit()
24762476

src/PIL/ImageDraw2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
class Pen:
3131
"""Stores an outline color and width."""
3232

33-
def __init__(self, color, width=1, opacity=255):
33+
def __init__(self, color: str, width: int = 1, opacity: int = 255) -> None:
3434
self.color = ImageColor.getrgb(color)
3535
self.width = width
3636

3737

3838
class Brush:
3939
"""Stores a fill color"""
4040

41-
def __init__(self, color, opacity=255):
41+
def __init__(self, color: str, opacity: int = 255) -> None:
4242
self.color = ImageColor.getrgb(color)
4343

4444

@@ -63,7 +63,7 @@ def __init__(self, image, size=None, color=None):
6363
self.image = image
6464
self.transform = None
6565

66-
def flush(self):
66+
def flush(self) -> Image.Image:
6767
return self.image
6868

6969
def render(self, op, xy, pen, brush=None):

src/PIL/ImageTk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
_pilbitmap_ok = None
3838

3939

40-
def _pilbitmap_check():
40+
def _pilbitmap_check() -> int:
4141
global _pilbitmap_ok
4242
if _pilbitmap_ok is None:
4343
try:
@@ -162,7 +162,7 @@ def height(self) -> int:
162162
"""
163163
return self.__size[1]
164164

165-
def paste(self, im):
165+
def paste(self, im: Image.Image) -> None:
166166
"""
167167
Paste a PIL image into the photo image. Note that this can
168168
be very slow if the photo image is displayed.
@@ -254,7 +254,7 @@ def __str__(self) -> str:
254254
return str(self.__photo)
255255

256256

257-
def getimage(photo):
257+
def getimage(photo: PhotoImage) -> Image.Image:
258258
"""Copies the contents of a PhotoImage to a PIL image memory."""
259259
im = Image.new("RGBA", (photo.width(), photo.height()))
260260
block = im.im

src/PIL/Jpeg2KImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io
1919
import os
2020
import struct
21+
from typing import IO
2122

2223
from . import Image, ImageFile, ImagePalette, _binary
2324

@@ -328,7 +329,7 @@ def _accept(prefix: bytes) -> bool:
328329
# Save support
329330

330331

331-
def _save(im, fp, filename):
332+
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
332333
# Get the keyword arguments
333334
info = im.encoderinfo
334335

src/PIL/PaletteFile.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#
1515
from __future__ import annotations
1616

17+
from typing import IO
18+
1719
from ._binary import o8
1820

1921

@@ -22,8 +24,8 @@ class PaletteFile:
2224

2325
rawmode = "RGB"
2426

25-
def __init__(self, fp):
26-
self.palette = [(i, i, i) for i in range(256)]
27+
def __init__(self, fp: IO[bytes]) -> None:
28+
palette = [o8(i) * 3 for i in range(256)]
2729

2830
while True:
2931
s = fp.readline()
@@ -44,9 +46,9 @@ def __init__(self, fp):
4446
g = b = r
4547

4648
if 0 <= i <= 255:
47-
self.palette[i] = o8(r) + o8(g) + o8(b)
49+
palette[i] = o8(r) + o8(g) + o8(b)
4850

49-
self.palette = b"".join(self.palette)
51+
self.palette = b"".join(palette)
5052

5153
def getpalette(self) -> tuple[bytes, str]:
5254
return self.palette, self.rawmode

src/PIL/TiffImagePlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from collections.abc import MutableMapping
5151
from fractions import Fraction
5252
from numbers import Number, Rational
53-
from typing import TYPE_CHECKING, Any, Callable
53+
from typing import IO, TYPE_CHECKING, Any, Callable
5454

5555
from . import ExifTags, Image, ImageFile, ImageOps, ImagePalette, TiffTags
5656
from ._binary import i16be as i16
@@ -2149,7 +2149,7 @@ def fixOffsets(self, count, isShort=False, isLong=False):
21492149
self.rewriteLastLong(offset)
21502150

21512151

2152-
def _save_all(im, fp, filename):
2152+
def _save_all(im: Image.Image, fp: IO[bytes], filename: str) -> None:
21532153
encoderinfo = im.encoderinfo.copy()
21542154
encoderconfig = im.encoderconfig
21552155
append_images = list(encoderinfo.get("append_images", []))

0 commit comments

Comments
 (0)