Moved Buffer into _typing

This commit is contained in:
Andrew Murray 2024-09-08 08:30:30 +10:00
parent 1105256f2b
commit 8b3ef3784b
4 changed files with 17 additions and 26 deletions

View File

@ -33,6 +33,10 @@ Internal Modules
Provides a convenient way to import type hints that are not available Provides a convenient way to import type hints that are not available
on some Python versions. on some Python versions.
.. py:class:: Buffer
Typing alias.
.. py:class:: IntegralLike .. py:class:: IntegralLike
Typing alias. Typing alias.

View File

@ -29,7 +29,6 @@ import itertools
import math import math
import os import os
import subprocess import subprocess
import sys
from enum import IntEnum from enum import IntEnum
from functools import cached_property from functools import cached_property
from typing import IO, TYPE_CHECKING, Any, Literal, NamedTuple, Union from typing import IO, TYPE_CHECKING, Any, Literal, NamedTuple, Union
@ -49,6 +48,7 @@ from ._binary import o16le as o16
if TYPE_CHECKING: if TYPE_CHECKING:
from . import _imaging from . import _imaging
from ._typing import Buffer
class LoadingStrategy(IntEnum): class LoadingStrategy(IntEnum):
@ -1157,18 +1157,9 @@ def getdata(
class Collector(BytesIO): class Collector(BytesIO):
data = [] data = []
if sys.version_info >= (3, 12): def write(self, data: Buffer) -> int:
from collections.abc import Buffer self.data.append(data)
return len(data)
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)
im.load() # make sure raster data is available im.load() # make sure raster data is available

View File

@ -46,7 +46,6 @@ import logging
import math import math
import os import os
import struct import struct
import sys
import warnings import warnings
from collections.abc import Iterator, MutableMapping from collections.abc import Iterator, MutableMapping
from fractions import Fraction from fractions import Fraction
@ -63,7 +62,7 @@ from ._util import is_path
from .TiffTags import TYPES from .TiffTags import TYPES
if TYPE_CHECKING: if TYPE_CHECKING:
from ._typing import IntegralLike from ._typing import Buffer, IntegralLike
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -2108,16 +2107,8 @@ class AppendingTiffWriter(io.BytesIO):
num_tags = self.readShort() num_tags = self.readShort()
self.f.seek(num_tags * 12, os.SEEK_CUR) self.f.seek(num_tags * 12, os.SEEK_CUR)
if sys.version_info >= (3, 12): def write(self, data: Buffer, /) -> int:
from collections.abc import Buffer return self.f.write(data)
def write(self, data: Buffer, /) -> int:
return self.f.write(data)
else:
def write(self, data: Any, /) -> int:
return self.f.write(data)
def readShort(self) -> int: def readShort(self) -> int:
(value,) = struct.unpack(self.shortFmt, self.f.read(2)) (value,) = struct.unpack(self.shortFmt, self.f.read(2))

View File

@ -15,6 +15,11 @@ if TYPE_CHECKING:
except (ImportError, AttributeError): except (ImportError, AttributeError):
pass pass
if sys.version_info >= (3, 12):
from collections.abc import Buffer
else:
Buffer = Any
if sys.version_info >= (3, 10): if sys.version_info >= (3, 10):
from typing import TypeGuard from typing import TypeGuard
else: else:
@ -40,4 +45,4 @@ class SupportsRead(Protocol[_T_co]):
StrOrBytesPath = Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"] StrOrBytesPath = Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"]
__all__ = ["IntegralLike", "StrOrBytesPath", "SupportsRead", "TypeGuard"] __all__ = ["Buffer", "IntegralLike", "StrOrBytesPath", "SupportsRead", "TypeGuard"]