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 231d54b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/reference/internal_design.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Internal Reference Docs
=======================
Internal Reference
==================

.. toctree::
:maxdepth: 2
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/internal_modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ Internal Modules
Provides a convenient way to import type hints that are not available
on some Python versions.

.. py:class:: FileDescriptor
Typing alias.

.. py:class:: StrOrBytesPath
Typing alias.

.. py:class:: SupportsRead
An object that supports the read method.

.. py:data:: TypeGuard
:value: typing.TypeGuard

Expand Down
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[bytes]) -> None:
self.fp = fp
self.bits = 0
self.bitbuffer = 0
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, Union

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 = Union[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 231d54b

Please sign in to comment.