Skip to content

Commit

Permalink
Replace io.BytesIO in type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 22, 2024
1 parent 1185fb8 commit c422cb5
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/PIL/GdImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class is not registered for use with :py:func:`PIL.Image.open()`. To open a
"""
from __future__ import annotations

from io import BytesIO
from typing import IO

from . import ImageFile, ImagePalette, UnidentifiedImageError
from ._binary import i16be as i16
from ._binary import i32be as i32
from ._typing import FileDescriptor, StrOrBytesPath


class GdImageFile(ImageFile.ImageFile):
Expand Down Expand Up @@ -80,7 +81,9 @@ def _open(self) -> None:
]


def open(fp: BytesIO, mode: str = "r") -> GdImageFile:
def open(
fp: StrOrBytesPath | FileDescriptor | IO[bytes], mode: str = "r"
) -> GdImageFile:
"""
Load texture from a GD image file.
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/MpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
#
from __future__ import annotations

from io import BytesIO

from . import Image, ImageFile
from ._binary import i8
from ._typing import SupportsRead

#
# Bitstream parser


class BitStream:
def __init__(self, fp: BytesIO) -> None:
def __init__(self, fp: SupportsRead) -> None:
self.fp = fp
self.bits = 0
self.bitbuffer = 0
Expand Down
7 changes: 5 additions & 2 deletions src/PIL/PpmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
from __future__ import annotations

import math
from io import BytesIO
from typing import IO

from . import Image, ImageFile
from ._binary import i16be as i16
from ._binary import o8
from ._binary import o32le as o32
from ._typing import FileDescriptor, StrOrBytesPath

#
# --------------------------------------------------------------------
Expand Down Expand Up @@ -324,7 +325,9 @@ def decode(self, buffer: bytes) -> tuple[int, int]:
# --------------------------------------------------------------------


def _save(im: Image.Image, fp: BytesIO, filename: str) -> None:
def _save(
im: Image.Image, fp: StrOrBytesPath | FileDescriptor | IO[bytes], filename: str
) -> None:
if im.mode == "1":
rawmode, head = "1;I", b"P4"
elif im.mode == "L":
Expand Down
7 changes: 5 additions & 2 deletions src/PIL/SgiImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

import os
import struct
from io import BytesIO
from typing import IO

from . import Image, ImageFile
from ._binary import i16be as i16
from ._binary import o8
from ._typing import FileDescriptor, StrOrBytesPath


def _accept(prefix: bytes) -> bool:
Expand Down Expand Up @@ -125,7 +126,9 @@ def _open(self) -> None:
]


def _save(im: Image.Image, fp: BytesIO, filename: str) -> None:
def _save(
im: Image.Image, fp: StrOrBytesPath | FileDescriptor | IO[bytes], filename: str
) -> None:
if im.mode not in {"RGB", "RGBA", "L"}:
msg = "Unsupported SGI image mode"
raise ValueError(msg)
Expand Down
7 changes: 5 additions & 2 deletions src/PIL/TgaImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
from __future__ import annotations

import warnings
from io import BytesIO
from typing import IO

from . import Image, ImageFile, ImagePalette
from ._binary import i16le as i16
from ._binary import o8
from ._binary import o16le as o16
from ._typing import FileDescriptor, StrOrBytesPath

#
# --------------------------------------------------------------------
Expand Down Expand Up @@ -175,7 +176,9 @@ def load_end(self) -> None:
}


def _save(im: Image.Image, fp: BytesIO, filename: str) -> None:
def _save(
im: Image.Image, fp: StrOrBytesPath | FileDescriptor | IO[bytes], filename: str
) -> None:
try:
rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
except KeyError as e:
Expand Down
7 changes: 5 additions & 2 deletions src/PIL/XbmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
from __future__ import annotations

import re
from io import BytesIO
from typing import IO

from . import Image, ImageFile
from ._typing import FileDescriptor, StrOrBytesPath

# XBM header
xbm_head = re.compile(
Expand Down Expand Up @@ -70,7 +71,9 @@ def _open(self) -> None:
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]


def _save(im: Image.Image, fp: BytesIO, filename: str) -> None:
def _save(
im: Image.Image, fp: StrOrBytesPath | FileDescriptor | IO[bytes], filename: str
) -> None:
if im.mode != "1":
msg = f"cannot write mode {im.mode} as XBM"
raise OSError(msg)
Expand Down
16 changes: 15 additions & 1 deletion src/PIL/_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
import sys
from typing import Protocol, TypeVar

if sys.version_info >= (3, 10):
from typing import TypeGuard
Expand All @@ -15,4 +17,16 @@ def __class_getitem__(cls, item: Any) -> type[bool]:
return bool


__all__ = ["TypeGuard"]
_T_co = TypeVar("_T_co", covariant=True)


class SupportsRead(Protocol[_T_co]):
def read(self, __length: int = ...) -> _T_co:
...


FileDescriptor = int
StrOrBytesPath = str | bytes | os.PathLike[str] | os.PathLike[bytes]


__all__ = ["FileDescriptor", "TypeGuard", "StrOrBytesPath", "SupportsRead"]
4 changes: 2 additions & 2 deletions src/PIL/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from pathlib import Path
from typing import Any, NoReturn

from ._typing import TypeGuard
from ._typing import StrOrBytesPath, TypeGuard


def is_path(f: Any) -> TypeGuard[bytes | str | Path]:
def is_path(f: Any) -> TypeGuard[StrOrBytesPath]:
return isinstance(f, (bytes, str, Path))


Expand Down

0 comments on commit c422cb5

Please sign in to comment.