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 3782a35
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 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
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 3782a35

Please sign in to comment.