Skip to content

Commit

Permalink
Revert typing to Python 3.8 :(
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion committed Jan 2, 2024
1 parent 23d43be commit 51be4e1
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pytest-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: unit-tests

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:

jobs:
Expand All @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.10', '3.11', '3.12']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
Expand Down
9 changes: 6 additions & 3 deletions pylibjpeg/tools/jpegio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
from typing import BinaryIO
from pathlib import Path
from typing import BinaryIO, Union, cast

from .s10918 import parse, JPEG

Expand Down Expand Up @@ -33,16 +34,18 @@ def get_specification(fp: BinaryIO) -> str:
)


def jpgread(path: str | os.PathLike[str] | BinaryIO) -> JPEG:
def jpgread(path: Union[str, os.PathLike[str], BinaryIO]) -> JPEG:
"""Return a represention of the JPEG file at `fpath`."""
LOGGER.debug(f"Reading file: {path}")
if isinstance(path, str | os.PathLike[str]):
if not hasattr(path, "read"):
path = cast(str, path)
with open(path, "rb") as fp:
jpg_format = get_specification(fp)
parser, jpg_class = PARSERS[jpg_format]
meta = parser(fp)
LOGGER.debug("File parsed successfully")
else:
path = cast(BinaryIO, path)
jpg_format = get_specification(path)
parser, jpg_class = PARSERS[jpg_format]
meta = parser(path)
Expand Down
4 changes: 2 additions & 2 deletions pylibjpeg/tools/s10918/_markers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""JPEG 10918 markers"""

from typing import Callable
from typing import Callable, Dict, Tuple, Union

from ._parsers import APP, COM, DAC, DHT, DNL, DQT, DRI, EXP, SOF, SOS


MARKERS: dict[int, tuple[str, str, None | Callable]] = {}
MARKERS: Dict[int, Tuple[str, str, Union[None, Callable]]] = {}
# JPEG reserved markers
for _marker in range(0xFF02, 0xFFBF + 1):
MARKERS[_marker] = ("RES", "Reserved", None)
Expand Down
24 changes: 12 additions & 12 deletions pylibjpeg/tools/s10918/_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
"""

from struct import unpack
from typing import BinaryIO, Any, cast
from typing import BinaryIO, Any, cast, Dict, Union, List, Tuple

from pylibjpeg.tools.utils import split_byte


def APP(fp: BinaryIO) -> dict[str, int | bytes]:
def APP(fp: BinaryIO) -> Dict[str, Union[int, bytes]]:
"""Return a dict containing APP data.
See ISO/IEC 10918-1 Section B.2.4.6.
Expand All @@ -83,7 +83,7 @@ def APP(fp: BinaryIO) -> dict[str, int | bytes]:
return {"Lp": length, "Ap": fp.read(length - 2)}


def COM(fp: BinaryIO) -> dict[str, int | str]:
def COM(fp: BinaryIO) -> Dict[str, Union[int, str]]:
"""Return a dict containing COM data.
See ISO/IEC 10918-1 Section B.2.4.5.
Expand Down Expand Up @@ -111,7 +111,7 @@ def COM(fp: BinaryIO) -> dict[str, int | str]:
return {"Lc": length, "Cm": comment}


def DAC(fp: BinaryIO) -> dict[str, int | list[int]]:
def DAC(fp: BinaryIO) -> Dict[str, Union[int, List[int]]]:
"""Return a dict containing DAC segment data.
See ISO/IEC 10918-1 Section B.2.4.3.
Expand Down Expand Up @@ -150,7 +150,7 @@ def DAC(fp: BinaryIO) -> dict[str, int | list[int]]:
return {"La": length, "Tc": tc, "Tb": tb, "Cs": cs}


def DHT(fp: BinaryIO) -> dict[str, int | list[int] | Any]:
def DHT(fp: BinaryIO) -> Dict[str, Union[int, List[int], Any]]:
"""Return a dict containing DHT segment data.
See ISO/IEC 10918-1 Section B.2.4.2.
Expand Down Expand Up @@ -182,7 +182,7 @@ def DHT(fp: BinaryIO) -> dict[str, int | list[int] | Any]:
bytes_to_read = length - 2

tc, th, li = [], [], []
vij: dict[tuple[int, int], dict[int, tuple[int]]] = {}
vij: Dict[Tuple[int, int], Dict[int, Tuple[int]]] = {}
while bytes_to_read > 0:
_tc, _th = split_byte(fp.read(1))
tc.append(_tc)
Expand All @@ -209,7 +209,7 @@ def DHT(fp: BinaryIO) -> dict[str, int | list[int] | Any]:
return {"Lh": length, "Tc": tc, "Th": th, "Li": li, "Vij": vij}


def DNL(fp: BinaryIO) -> dict[str, int | list[int]]:
def DNL(fp: BinaryIO) -> Dict[str, Union[int, List[int]]]:
"""Return a dict containing DNL segment data.
See ISO/IEC 10918-1 Section B.2.5.
Expand Down Expand Up @@ -237,7 +237,7 @@ def DNL(fp: BinaryIO) -> dict[str, int | list[int]]:
return {"Ld": length, "NL": nr_lines}


def DQT(fp: BinaryIO) -> dict[str, int | list[int] | list[list[int]]]:
def DQT(fp: BinaryIO) -> Dict[str, Union[int, List[int], List[List[int]]]]:
"""Return a dict containing DQT segment data.
See ISO/IEC 10918-1 Section B.2.4.1.
Expand Down Expand Up @@ -290,7 +290,7 @@ def DQT(fp: BinaryIO) -> dict[str, int | list[int] | list[list[int]]]:
return {"Lq": length, "Pq": pq, "Tq": tq, "Qk": qk}


def DRI(fp: BinaryIO) -> dict[str, int]:
def DRI(fp: BinaryIO) -> Dict[str, int]:
"""Return a dict containing DRI segment data.
See ISO/IEC 10918-1 Section B.2.4.4.
Expand All @@ -315,7 +315,7 @@ def DRI(fp: BinaryIO) -> dict[str, int]:
return {"Lr": unpack(">H", fp.read(2))[0], "Ri": unpack(">H", fp.read(2))[0]}


def EXP(fp: BinaryIO) -> dict[str, int]:
def EXP(fp: BinaryIO) -> Dict[str, int]:
"""Return a dict containing EXP segment data.
See ISO/IEC 10918-1 Section B.3.3.
Expand Down Expand Up @@ -344,7 +344,7 @@ def EXP(fp: BinaryIO) -> dict[str, int]:
return {"Le": length, "Eh": eh, "Ev": ev}


def SOF(fp: BinaryIO) -> dict[str, int | dict[int, dict[str, int]]]:
def SOF(fp: BinaryIO) -> Dict[str, Union[int, Dict[int, Dict[str, int]]]]:
"""Return a dict containing SOF header data.
See ISO/IEC 10918-1 Section B.2.2.
Expand Down Expand Up @@ -405,7 +405,7 @@ def SOF(fp: BinaryIO) -> dict[str, int | dict[int, dict[str, int]]]:
}


def SOS(fp: BinaryIO) -> dict[str, int | list[int]]:
def SOS(fp: BinaryIO) -> Dict[str, Union[int, List[int]]]:
"""Return a dict containing SOS header data.
See ISO/IEC 10918-1 Section B.2.3.
Expand Down
30 changes: 15 additions & 15 deletions pylibjpeg/tools/s10918/_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

from struct import unpack
from typing import Any, cast
from typing import Any, cast, Tuple, Dict, Union


ZIGZAG = [
Expand Down Expand Up @@ -99,7 +99,7 @@
}


def _print_app(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_app(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for an APP segment."""
_, _, sub = info

Expand Down Expand Up @@ -160,7 +160,7 @@ def _print_app(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_com(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_com(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a COM segment."""
_m, fill, sub = info

Expand All @@ -182,7 +182,7 @@ def _print_com(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_dac(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_dac(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a DAC segment."""
m_bytes, fill, sub = info

Expand All @@ -193,7 +193,7 @@ def _print_dac(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_dhp(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_dhp(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a DHP segment."""
m_bytes, fill, sub = info

Expand All @@ -219,7 +219,7 @@ def _print_dhp(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_dht(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_dht(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a DHT segment."""
m_bytes, fill, sub = info

Expand Down Expand Up @@ -248,7 +248,7 @@ def _print_dht(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_dqt(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_dqt(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a DQT segment."""
m_bytes, fill, sub = info

Expand Down Expand Up @@ -280,7 +280,7 @@ def _print_dqt(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_dnl(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_dnl(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a DNL segment."""
m_bytes, fill, sub = info

Expand All @@ -291,7 +291,7 @@ def _print_dnl(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_dri(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_dri(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a DRI segment."""
m_bytes, fill, sub = info

Expand All @@ -302,7 +302,7 @@ def _print_dri(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_eoi(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_eoi(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for an EOI segment."""
m_bytes, fill, sub = info
header = f"{marker} marker at offset {offset}"
Expand All @@ -311,7 +311,7 @@ def _print_eoi(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_exp(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_exp(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for an EXP segment."""
m_bytes, fill, sub = info

Expand All @@ -322,7 +322,7 @@ def _print_exp(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_sof(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_sof(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a SOF segment."""
m_bytes, fill, sub = info

Expand Down Expand Up @@ -368,7 +368,7 @@ def _print_sof(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
return "\n".join(ss)


def _print_soi(marker: str, offset: int, info: tuple[int, int, dict[str, Any]]) -> str:
def _print_soi(marker: str, offset: int, info: Tuple[int, int, Dict[str, Any]]) -> str:
"""String output for a SOI segment."""
m_bytes, fill, sub = info

Expand All @@ -381,7 +381,7 @@ def _print_soi(marker: str, offset: int, info: tuple[int, int, dict[str, Any]])
def _print_sos(
marker: str,
offset: int,
info: tuple[int, int, dict[str | tuple[str, int], Any]],
info: Tuple[int, int, Dict[Union[str, Tuple[str, int]], Any]],
) -> str:
"""String output for a SOS segment."""
m_bytes, fill, sub = info
Expand All @@ -408,7 +408,7 @@ def _print_sos(
ss.append(f"\n{' ENC marker at offset {key[1]}':.^63}")
ss.append(f"\n{len(sub[key])} bytes of entropy-coded data")
else:
(name, offset) = cast(tuple[str, int], key)
(name, offset) = cast(Tuple[str, int], key)
ss.append(f"{offset:<7}{name}(FFD{name[-1]})")

return "\n".join(ss)
Expand Down
6 changes: 3 additions & 3 deletions pylibjpeg/tools/s10918/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import logging
from struct import unpack
from typing import BinaryIO, Any, Callable, cast
from typing import BinaryIO, Any, Callable, cast, Dict, Tuple

from ._markers import MARKERS


LOGGER = logging.getLogger(__name__)


def parse(fp: BinaryIO) -> dict[tuple[str, int], Any]:
def parse(fp: BinaryIO) -> Dict[Tuple[str, int], Any]:
"""Return a JPEG but don't decode yet."""
_fill_bytes = 0
while fp.read(1) == b"\xff":
Expand All @@ -23,7 +23,7 @@ def parse(fp: BinaryIO) -> dict[tuple[str, int], Any]:
if fp.read(2) != b"\xFF\xD8":
raise ValueError("SOI marker not found")

info: dict[tuple[str, int], tuple[int, int, Any]] = {
info: Dict[Tuple[str, int], Tuple[int, int, Any]] = {
("SOI", fp.tell() - 2): (unpack(">H", b"\xFF\xD8")[0], _fill_bytes, {})
}

Expand Down
10 changes: 5 additions & 5 deletions pylibjpeg/tools/s10918/rep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, cast
from typing import Any, cast, Dict, Tuple, List

from ._printers import PRINTERS

Expand Down Expand Up @@ -53,7 +53,7 @@ class JPEG:
"""

def __init__(self, meta: dict[tuple[str, int], Any]) -> None:
def __init__(self, meta: Dict[Tuple[str, int], Any]) -> None:
"""Initialise a new JPEG.
Parameters
Expand All @@ -75,7 +75,7 @@ def columns(self) -> int:
"marker was found"
)

def get_keys(self, name: str) -> list[Any]:
def get_keys(self, name: str) -> List[Any]:
"""Return a list of keys with marker containing `name`."""
return [mm for mm in self._keys if name in mm[0]]

Expand Down Expand Up @@ -161,12 +161,12 @@ def is_sequential(self) -> bool:
return not self.is_hierarchical

@property
def _keys(self) -> list[tuple[str, int]]:
def _keys(self) -> List[Tuple[str, int]]:
"""Return a list of the info keys, ordered by offset."""
return sorted(self.info.keys(), key=lambda x: x[1])

@property
def markers(self) -> list[str]:
def markers(self) -> List[str]:
"""Return a list of the found JPEG markers, ordered by offset."""
return [mm[0] for mm in self._keys]

Expand Down
4 changes: 3 additions & 1 deletion pylibjpeg/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Utility functions."""

from typing import Tuple


def get_bit(byte: bytes, index: int) -> int:
"""Return the value of the bit at `index` of `byte`.
Expand All @@ -24,7 +26,7 @@ def get_bit(byte: bytes, index: int) -> int:
return (value >> (7 - index)) & 1


def split_byte(byte: bytes) -> tuple[int, int]:
def split_byte(byte: bytes) -> Tuple[int, int]:
"""Return the 8-bit `byte` as two 4-bit unsigned integers.
Parameters
Expand Down
Loading

0 comments on commit 51be4e1

Please sign in to comment.