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

Deprecate isImageType #8364

Merged
merged 2 commits into from
Sep 10, 2024
Merged
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
4 changes: 4 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,10 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None:
assert len(caplog.records) == 0
assert im.fp is None

def test_deprecation(self) -> None:
with pytest.warns(DeprecationWarning):
assert not Image.isImageType(None)


class TestImageBytes:
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
Expand Down
8 changes: 8 additions & 0 deletions docs/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ vulnerability introduced in FreeType 2.6 (:cve:`2020-15999`).

.. _2.10.4: https://sourceforge.net/projects/freetype/files/freetype2/2.10.4/

Image isImageType()
^^^^^^^^^^^^^^^^^^^

.. deprecated:: 11.0.0

``Image.isImageType(im)`` has been deprecated. Use ``isinstance(im, Image.Image)``
instead.

ImageMath.lambda_eval and ImageMath.unsafe_eval options parameter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
8 changes: 8 additions & 0 deletions docs/releasenotes/11.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ vulnerability introduced in FreeType 2.6 (:cve:`2020-15999`).

.. _2.10.4: https://sourceforge.net/projects/freetype/files/freetype2/2.10.4/

Image isImageType()
^^^^^^^^^^^^^^^^^^^

.. deprecated:: 11.0.0

``Image.isImageType(im)`` has been deprecated. Use ``isinstance(im, Image.Image)``
instead.

ImageMath.lambda_eval and ImageMath.unsafe_eval options parameter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
17 changes: 7 additions & 10 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def isImageType(t: Any) -> TypeGuard[Image]:
:param t: object to check if it's an image
:returns: True if the object is an image
"""
deprecate("Image.isImageType(im)", 12, "isinstance(im, Image.Image)")
return hasattr(t, "im")


Expand Down Expand Up @@ -1823,23 +1824,22 @@ def paste(
:param mask: An optional mask image.
"""

if isImageType(box):
if isinstance(box, Image):
if mask is not None:
msg = "If using second argument as mask, third argument must be None"
raise ValueError(msg)
# abbreviated paste(im, mask) syntax
mask = box
box = None
assert not isinstance(box, Image)

if box is None:
box = (0, 0)

if len(box) == 2:
# upper left corner given; get size from image or mask
if isImageType(im):
if isinstance(im, Image):
size = im.size
elif isImageType(mask):
elif isinstance(mask, Image):
size = mask.size
else:
# FIXME: use self.size here?
Expand All @@ -1852,17 +1852,15 @@ def paste(
from . import ImageColor

source = ImageColor.getcolor(im, self.mode)
elif isImageType(im):
elif isinstance(im, Image):
im.load()
if self.mode != im.mode:
if self.mode != "RGB" or im.mode not in ("LA", "RGBA", "RGBa"):
# should use an adapter for this!
im = im.convert(self.mode)
source = im.im
elif isinstance(im, tuple):
source = im
else:
source = cast(float, im)
source = im

self._ensure_mutable()

Expand Down Expand Up @@ -2023,7 +2021,7 @@ def putalpha(self, alpha: Image | int) -> None:
else:
band = 3

if isImageType(alpha):
if isinstance(alpha, Image):
# alpha layer
if alpha.mode not in ("1", "L"):
msg = "illegal image mode"
Expand All @@ -2033,7 +2031,6 @@ def putalpha(self, alpha: Image | int) -> None:
alpha = alpha.convert("L")
else:
# constant alpha
alpha = cast(int, alpha) # see python/typing#1013
try:
self.im.fillband(band, alpha)
except (AttributeError, ValueError):
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def lambda_eval(
args.update(options)
args.update(kw)
for k, v in args.items():
if hasattr(v, "im"):
if isinstance(v, Image.Image):
args[k] = _Operand(v)

out = expression(args)
Expand Down Expand Up @@ -319,7 +319,7 @@ def unsafe_eval(
args.update(options)
args.update(kw)
for k, v in args.items():
if hasattr(v, "im"):
if isinstance(v, Image.Image):
args[k] = _Operand(v)

compiled_code = compile(expression, "<string>", "eval")
Expand Down
Loading