Skip to content

Commit 80d4fc1

Browse files
authored
Merge pull request #7786 from radarhere/type_hints_imageops
Added type hints to ImageOps
2 parents e5415f3 + 5c858d7 commit 80d4fc1

File tree

4 files changed

+139
-74
lines changed

4 files changed

+139
-74
lines changed

src/PIL/Image.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -1430,15 +1430,14 @@ def get_value(element):
14301430
root = ElementTree.fromstring(xmp_tags)
14311431
return {get_name(root.tag): get_value(root)}
14321432

1433-
def getexif(self):
1433+
def getexif(self) -> Exif:
14341434
"""
14351435
Gets EXIF data from the image.
14361436
14371437
:returns: an :py:class:`~PIL.Image.Exif` object.
14381438
"""
14391439
if self._exif is None:
14401440
self._exif = Exif()
1441-
self._exif._loaded = False
14421441
elif self._exif._loaded:
14431442
return self._exif
14441443
self._exif._loaded = True
@@ -1525,7 +1524,7 @@ def getim(self):
15251524
self.load()
15261525
return self.im.ptr
15271526

1528-
def getpalette(self, rawmode="RGB"):
1527+
def getpalette(self, rawmode: str | None = "RGB") -> list[int] | None:
15291528
"""
15301529
Returns the image palette as a list.
15311530
@@ -1615,7 +1614,7 @@ def getprojection(self):
16151614
x, y = self.im.getprojection()
16161615
return list(x), list(y)
16171616

1618-
def histogram(self, mask=None, extrema=None):
1617+
def histogram(self, mask=None, extrema=None) -> list[int]:
16191618
"""
16201619
Returns a histogram for the image. The histogram is returned as a
16211620
list of pixel counts, one for each pixel value in the source
@@ -1804,7 +1803,7 @@ def alpha_composite(self, im, dest=(0, 0), source=(0, 0)):
18041803
result = alpha_composite(background, overlay)
18051804
self.paste(result, box)
18061805

1807-
def point(self, lut, mode=None):
1806+
def point(self, lut, mode: str | None = None) -> Image:
18081807
"""
18091808
Maps this image through a lookup table or function.
18101809
@@ -1928,7 +1927,7 @@ def putdata(self, data, scale=1.0, offset=0.0):
19281927

19291928
self.im.putdata(data, scale, offset)
19301929

1931-
def putpalette(self, data, rawmode="RGB"):
1930+
def putpalette(self, data, rawmode="RGB") -> None:
19321931
"""
19331932
Attaches a palette to this image. The image must be a "P", "PA", "L"
19341933
or "LA" image.
@@ -2108,7 +2107,7 @@ def _get_safe_box(self, size, resample, box):
21082107
min(self.size[1], math.ceil(box[3] + support_y)),
21092108
)
21102109

2111-
def resize(self, size, resample=None, box=None, reducing_gap=None):
2110+
def resize(self, size, resample=None, box=None, reducing_gap=None) -> Image:
21122111
"""
21132112
Returns a resized copy of this image.
21142113
@@ -2200,10 +2199,11 @@ def resize(self, size, resample=None, box=None, reducing_gap=None):
22002199
if factor_x > 1 or factor_y > 1:
22012200
reduce_box = self._get_safe_box(size, resample, box)
22022201
factor = (factor_x, factor_y)
2203-
if callable(self.reduce):
2204-
self = self.reduce(factor, box=reduce_box)
2205-
else:
2206-
self = Image.reduce(self, factor, box=reduce_box)
2202+
self = (
2203+
self.reduce(factor, box=reduce_box)
2204+
if callable(self.reduce)
2205+
else Image.reduce(self, factor, box=reduce_box)
2206+
)
22072207
box = (
22082208
(box[0] - reduce_box[0]) / factor_x,
22092209
(box[1] - reduce_box[1]) / factor_y,
@@ -2818,7 +2818,7 @@ def __transformer(
28182818

28192819
self.im.transform2(box, image.im, method, data, resample, fill)
28202820

2821-
def transpose(self, method):
2821+
def transpose(self, method: Transpose) -> Image:
28222822
"""
28232823
Transpose image (flip or rotate in 90 degree steps)
28242824
@@ -2870,7 +2870,9 @@ class ImagePointHandler:
28702870
(for use with :py:meth:`~PIL.Image.Image.point`)
28712871
"""
28722872

2873-
pass
2873+
@abc.abstractmethod
2874+
def point(self, im: Image) -> Image:
2875+
pass
28742876

28752877

28762878
class ImageTransformHandler:
@@ -3690,6 +3692,7 @@ class Exif(_ExifBase):
36903692

36913693
endian = None
36923694
bigtiff = False
3695+
_loaded = False
36933696

36943697
def __init__(self):
36953698
self._data = {}
@@ -3805,7 +3808,7 @@ def _get_merged_dict(self):
38053808

38063809
return merged_dict
38073810

3808-
def tobytes(self, offset=8):
3811+
def tobytes(self, offset: int = 8) -> bytes:
38093812
from . import TiffImagePlugin
38103813

38113814
head = self._get_head()
@@ -3960,7 +3963,7 @@ def __setitem__(self, tag, value):
39603963
del self._info[tag]
39613964
self._data[tag] = value
39623965

3963-
def __delitem__(self, tag):
3966+
def __delitem__(self, tag: int) -> None:
39643967
if self._info is not None and tag in self._info:
39653968
del self._info[tag]
39663969
else:

src/PIL/ImageColor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def getrgb(color):
124124

125125

126126
@lru_cache
127-
def getcolor(color, mode):
127+
def getcolor(color, mode: str) -> tuple[int, ...]:
128128
"""
129129
Same as :py:func:`~PIL.ImageColor.getrgb` for most modes. However, if
130130
``mode`` is HSV, converts the RGB value to a HSV value, or if ``mode`` is

0 commit comments

Comments
 (0)