diff --git a/docs/reference/internal_modules.rst b/docs/reference/internal_modules.rst index 31d60cd832e..93fd82cf9df 100644 --- a/docs/reference/internal_modules.rst +++ b/docs/reference/internal_modules.rst @@ -33,6 +33,10 @@ Internal Modules Provides a convenient way to import type hints that are not available on some Python versions. +.. py:class:: Buffer + + Typing alias. + .. py:class:: IntegralLike Typing alias. diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index a27f7aae9dc..f206fbb9cef 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -29,7 +29,6 @@ import math import os import subprocess -import sys from enum import IntEnum from functools import cached_property from typing import IO, TYPE_CHECKING, Any, Literal, NamedTuple, Union @@ -49,6 +48,7 @@ if TYPE_CHECKING: from . import _imaging + from ._typing import Buffer class LoadingStrategy(IntEnum): @@ -1157,18 +1157,9 @@ def getdata( class Collector(BytesIO): data = [] - if sys.version_info >= (3, 12): - from collections.abc import Buffer - - def write(self, data: Buffer) -> int: - self.data.append(data) - return len(data) - - else: - - def write(self, data: Any) -> int: - self.data.append(data) - return len(data) + def write(self, data: Buffer) -> int: + self.data.append(data) + return len(data) im.load() # make sure raster data is available diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 53f6ae621c7..a6b03e171e5 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -46,7 +46,6 @@ import math import os import struct -import sys import warnings from collections.abc import Iterator, MutableMapping from fractions import Fraction @@ -63,7 +62,7 @@ from .TiffTags import TYPES if TYPE_CHECKING: - from ._typing import IntegralLike + from ._typing import Buffer, IntegralLike logger = logging.getLogger(__name__) @@ -2108,16 +2107,8 @@ def skipIFDs(self) -> None: num_tags = self.readShort() self.f.seek(num_tags * 12, os.SEEK_CUR) - if sys.version_info >= (3, 12): - from collections.abc import Buffer - - def write(self, data: Buffer, /) -> int: - return self.f.write(data) - - else: - - def write(self, data: Any, /) -> int: - return self.f.write(data) + def write(self, data: Buffer, /) -> int: + return self.f.write(data) def readShort(self) -> int: (value,) = struct.unpack(self.shortFmt, self.f.read(2)) diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py index 093778464da..da49f7c5d5a 100644 --- a/src/PIL/_typing.py +++ b/src/PIL/_typing.py @@ -15,6 +15,11 @@ except (ImportError, AttributeError): pass +if sys.version_info >= (3, 12): + from collections.abc import Buffer +else: + Buffer = Any + if sys.version_info >= (3, 10): from typing import TypeGuard else: @@ -40,4 +45,4 @@ def read(self, __length: int = ...) -> _T_co: ... StrOrBytesPath = Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"] -__all__ = ["IntegralLike", "StrOrBytesPath", "SupportsRead", "TypeGuard"] +__all__ = ["Buffer", "IntegralLike", "StrOrBytesPath", "SupportsRead", "TypeGuard"]