Merge pull request #7795 from radarhere/type_hints

Use IO[bytes] in type hints
This commit is contained in:
Andrew Murray 2024-02-15 20:23:43 +11:00 committed by GitHub
commit 912a33f5e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 12 deletions

View File

@ -32,7 +32,7 @@ import io
import itertools import itertools
import struct import struct
import sys import sys
from typing import Any, NamedTuple from typing import IO, Any, NamedTuple
from . import Image from . import Image
from ._deprecate import deprecate from ._deprecate import deprecate
@ -616,7 +616,7 @@ class PyCodecState:
class PyCodec: class PyCodec:
fd: io.BytesIO | None fd: IO[bytes] | None
def __init__(self, mode, *args): def __init__(self, mode, *args):
self.im = None self.im = None

View File

@ -26,6 +26,7 @@ from __future__ import annotations
import io import io
import struct import struct
from typing import IO
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i16le as i16 from ._binary import i16le as i16
@ -163,7 +164,7 @@ Image.register_decoder("MSP", MspDecoder)
# write MSP files (uncompressed only) # write MSP files (uncompressed only)
def _save(im: Image.Image, fp: io.BytesIO, filename: str) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
if im.mode != "1": if im.mode != "1":
msg = f"cannot write mode {im.mode} as MSP" msg = f"cannot write mode {im.mode} as MSP"
raise OSError(msg) raise OSError(msg)

View File

@ -28,6 +28,7 @@ from __future__ import annotations
import io import io
import logging import logging
from typing import IO
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
from ._binary import i16le as i16 from ._binary import i16le as i16
@ -143,7 +144,7 @@ SAVE = {
} }
def _save(im: Image.Image, fp: io.BytesIO, filename: str) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
try: try:
version, bits, planes, rawmode = SAVE[im.mode] version, bits, planes, rawmode = SAVE[im.mode]
except KeyError as e: except KeyError as e:

View File

@ -16,7 +16,7 @@
from __future__ import annotations from __future__ import annotations
import math import math
from io import BytesIO from typing import IO
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i16be as i16 from ._binary import i16be as i16
@ -324,7 +324,7 @@ class PpmDecoder(ImageFile.PyDecoder):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
def _save(im: Image.Image, fp: BytesIO, filename: str) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
if im.mode == "1": if im.mode == "1":
rawmode, head = "1;I", b"P4" rawmode, head = "1;I", b"P4"
elif im.mode == "L": elif im.mode == "L":

View File

@ -24,7 +24,7 @@ from __future__ import annotations
import os import os
import struct import struct
from io import BytesIO from typing import IO
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i16be as i16 from ._binary import i16be as i16
@ -125,7 +125,7 @@ class SgiImageFile(ImageFile.ImageFile):
] ]
def _save(im: Image.Image, fp: BytesIO, filename: str) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
if im.mode not in {"RGB", "RGBA", "L"}: if im.mode not in {"RGB", "RGBA", "L"}:
msg = "Unsupported SGI image mode" msg = "Unsupported SGI image mode"
raise ValueError(msg) raise ValueError(msg)

View File

@ -18,7 +18,7 @@
from __future__ import annotations from __future__ import annotations
import warnings import warnings
from io import BytesIO from typing import IO
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
from ._binary import i16le as i16 from ._binary import i16le as i16
@ -175,7 +175,7 @@ SAVE = {
} }
def _save(im: Image.Image, fp: BytesIO, filename: str) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
try: try:
rawmode, bits, colormaptype, imagetype = SAVE[im.mode] rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
except KeyError as e: except KeyError as e:

View File

@ -21,7 +21,7 @@
from __future__ import annotations from __future__ import annotations
import re import re
from io import BytesIO from typing import IO
from . import Image, ImageFile from . import Image, ImageFile
@ -70,7 +70,7 @@ class XbmImageFile(ImageFile.ImageFile):
self.tile = [("xbm", (0, 0) + self.size, m.end(), 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: IO[bytes], filename: str) -> None:
if im.mode != "1": if im.mode != "1":
msg = f"cannot write mode {im.mode} as XBM" msg = f"cannot write mode {im.mode} as XBM"
raise OSError(msg) raise OSError(msg)