Merge pull request #7656 from nulano/types-containerio

Add type hints to PIL.ContainerIO
This commit is contained in:
Andrew Murray 2024-01-01 00:25:20 +11:00 committed by GitHub
commit 42c574afdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View File

@ -16,15 +16,16 @@
from __future__ import annotations from __future__ import annotations
import io import io
from typing import IO, AnyStr, Generic, Literal
class ContainerIO: class ContainerIO(Generic[AnyStr]):
""" """
A file object that provides read access to a part of an existing A file object that provides read access to a part of an existing
file (for example a TAR file). file (for example a TAR file).
""" """
def __init__(self, file, offset, length) -> None: def __init__(self, file: IO[AnyStr], offset: int, length: int) -> None:
""" """
Create file object. Create file object.
@ -32,7 +33,7 @@ class ContainerIO:
:param offset: Start of region, in bytes. :param offset: Start of region, in bytes.
:param length: Size of region, in bytes. :param length: Size of region, in bytes.
""" """
self.fh = file self.fh: IO[AnyStr] = file
self.pos = 0 self.pos = 0
self.offset = offset self.offset = offset
self.length = length self.length = length
@ -41,10 +42,10 @@ class ContainerIO:
## ##
# Always false. # Always false.
def isatty(self): def isatty(self) -> bool:
return False return False
def seek(self, offset, mode=io.SEEK_SET): def seek(self, offset: int, mode: Literal[0, 1, 2] = io.SEEK_SET) -> None:
""" """
Move file pointer. Move file pointer.
@ -63,7 +64,7 @@ class ContainerIO:
self.pos = max(0, min(self.pos, self.length)) self.pos = max(0, min(self.pos, self.length))
self.fh.seek(self.offset + self.pos) self.fh.seek(self.offset + self.pos)
def tell(self): def tell(self) -> int:
""" """
Get current file pointer. Get current file pointer.
@ -71,7 +72,7 @@ class ContainerIO:
""" """
return self.pos return self.pos
def read(self, n=0): def read(self, n: int = 0) -> AnyStr:
""" """
Read data. Read data.
@ -84,17 +85,17 @@ class ContainerIO:
else: else:
n = self.length - self.pos n = self.length - self.pos
if not n: # EOF if not n: # EOF
return b"" if "b" in self.fh.mode else "" return b"" if "b" in self.fh.mode else "" # type: ignore[return-value]
self.pos = self.pos + n self.pos = self.pos + n
return self.fh.read(n) return self.fh.read(n)
def readline(self): def readline(self) -> AnyStr:
""" """
Read a line of text. Read a line of text.
:returns: An 8-bit string. :returns: An 8-bit string.
""" """
s = b"" if "b" in self.fh.mode else "" s: AnyStr = b"" if "b" in self.fh.mode else "" # type: ignore[assignment]
newline_character = b"\n" if "b" in self.fh.mode else "\n" newline_character = b"\n" if "b" in self.fh.mode else "\n"
while True: while True:
c = self.read(1) c = self.read(1)
@ -105,7 +106,7 @@ class ContainerIO:
break break
return s return s
def readlines(self): def readlines(self) -> list[AnyStr]:
""" """
Read multiple lines of text. Read multiple lines of text.

View File

@ -21,7 +21,7 @@ from types import TracebackType
from . import ContainerIO from . import ContainerIO
class TarIO(ContainerIO.ContainerIO): class TarIO(ContainerIO.ContainerIO[bytes]):
"""A file object that provides read access to a given member of a TAR file.""" """A file object that provides read access to a given member of a TAR file."""
def __init__(self, tarfile: str, file: str) -> None: def __init__(self, tarfile: str, file: str) -> None: