Skip to content

Commit

Permalink
🐍 Fix compatibility with Python 3.8 and 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
ariebovenberg committed Aug 15, 2023
1 parent 18329f8 commit 96e663d
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Changelog
all lines now rendered with the same leading.
This is more consistent and allows for faster layouting.

**Fixed**

- 🐍 Fix compatibility with Python 3.8 and 3.9

0.5.0 (2023-05-07)
------------------

Expand Down
6 changes: 4 additions & 2 deletions src/pdfje/atoms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Low-level PDF objects and operations"""
from __future__ import annotations

import abc
import re
from binascii import hexlify
Expand All @@ -7,7 +9,7 @@
from itertools import accumulate, chain, repeat, starmap
from math import isfinite
from secrets import token_bytes
from typing import Collection, Iterable, Iterator, Sequence
from typing import Collection, Iterable, Iterator, Sequence, Tuple
from zlib import compress

from .common import add_slots, setattr_frozen
Expand All @@ -32,7 +34,7 @@ def write(self) -> Iterable[bytes]:
raise NotImplementedError()


Object = tuple[ObjectID, Atom]
Object = Tuple[ObjectID, Atom]


@add_slots
Expand Down
8 changes: 5 additions & 3 deletions src/pdfje/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
Mapping,
Protocol,
Sequence,
Tuple,
TypeVar,
Union,
final,
no_type_check,
overload,
Expand Down Expand Up @@ -267,9 +269,9 @@ def parse(v: SidesLike, /) -> Sides:
raise TypeError(f"Cannot parse {v} as sides")


SidesLike = (
Sides | tuple[Pt, Pt, Pt, Pt] | tuple[Pt, Pt, Pt] | tuple[Pt, Pt] | Pt
)
SidesLike = Union[
Sides, Tuple[Pt, Pt, Pt, Pt], Tuple[Pt, Pt, Pt], Tuple[Pt, Pt], Pt
]


@final
Expand Down
15 changes: 15 additions & 0 deletions src/pdfje/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"Compatibility layer for various Python versions"
from __future__ import annotations

from itertools import tee
from typing import Iterable, Iterator, TypeVar

try:
from itertools import pairwise
except ImportError:
T = TypeVar("T")

def pairwise(i: Iterable[T]) -> Iterator[tuple[T, T]]:
a, b = tee(i)
next(b, None)
return zip(a, b)
11 changes: 6 additions & 5 deletions src/pdfje/fonts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import abc
from dataclasses import dataclass, field
from itertools import chain, count, pairwise
from itertools import chain, count
from pathlib import Path
from typing import TYPE_CHECKING, Iterable, final
from typing import TYPE_CHECKING, Iterable, Tuple, Union, final

from .. import atoms
from ..atoms import ASCII
from ..common import Char, Func, Pos, Pt, add_slots, setattr_frozen
from ..compat import pairwise

FontID = bytes # unique, internal identifier assigned to a font within a PDF
GlyphPt = float # length unit in glyph space
Expand Down Expand Up @@ -158,7 +159,7 @@ def font(self, bold: bool, italic: bool) -> BuiltinFont:
return self.italic if italic else self.regular


Typeface = BuiltinTypeface | TrueType
Typeface = Union[BuiltinTypeface, TrueType]


@final
Expand Down Expand Up @@ -199,8 +200,8 @@ def to_resource(self) -> atoms.Dictionary:
)


KerningTable = Func[tuple[Char, Char], GlyphPt]
Kern = tuple[Pos, GlyphPt]
KerningTable = Func[Tuple[Char, Char], GlyphPt]
Kern = Tuple[Pos, GlyphPt]


def kern(
Expand Down
2 changes: 2 additions & 0 deletions src/pdfje/fonts/registry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dataclasses import dataclass, field
from itertools import chain, count
from typing import Iterable, Iterator
Expand Down
16 changes: 12 additions & 4 deletions src/pdfje/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
Generator,
Iterator,
Sequence,
TypeGuard,
TypeVar,
Union,
final,
)

Expand Down Expand Up @@ -203,7 +203,7 @@ def setdefault(self) -> StyleFull:
EMPTY: ClassVar[Style]


StyleLike = Style | RGB | Typeface | HexColor
StyleLike = Union[Style, RGB, Typeface, HexColor]
Style.EMPTY = Style()

bold = Style(bold=True)
Expand Down Expand Up @@ -280,8 +280,16 @@ def _fallback(a: _T | None, b: _T) -> _T:
return b if a is None else a


def _differs(a: _T | None, b: _T) -> TypeGuard[_T]:
return a is not None and a != b
if TYPE_CHECKING: # pragma: no cover
from typing import TypeGuard

def _differs(a: _T | None, b: _T) -> TypeGuard[_T]:
return a is not None and a != b

else:

def _differs(a: _T | None, b: _T) -> bool:
return a is not None and a != b


class StyledMixin:
Expand Down
10 changes: 6 additions & 4 deletions src/pdfje/typeset/hyphens.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from functools import partial
from itertools import chain, pairwise, starmap
from typing import TYPE_CHECKING, Callable, Iterable
from itertools import chain, starmap
from typing import TYPE_CHECKING, Callable, Iterable, Union

from ..compat import pairwise

Hyphenator = Callable[[str], Iterable[str]]
" hyphenation -> hy phen ation "
Expand Down Expand Up @@ -31,10 +33,10 @@ def positions(self, txt: str) -> Iterable[int]:
from pyphen import Pyphen
except ImportError: # pragma: no cover
HAS_PYPHEN = False
HyphenatorLike = Hyphenator | None
HyphenatorLike = Union[Hyphenator, None]
else:
HAS_PYPHEN = True
HyphenatorLike = Hyphenator | Pyphen | None
HyphenatorLike = Union[Hyphenator, Pyphen, None]


if HAS_PYPHEN:
Expand Down
2 changes: 1 addition & 1 deletion src/pdfje/typeset/tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from __future__ import annotations

from dataclasses import dataclass, field
from itertools import pairwise
from math import sqrt
from operator import attrgetter
from typing import Callable, Iterable, Literal, Sequence

from ..common import add_slots
from ..compat import pairwise

Pos = int # position in the list of boxes
LineNum = int # zero-based line number
Expand Down
3 changes: 2 additions & 1 deletion src/pdfje/typeset/words.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import re
from dataclasses import dataclass
from itertools import chain, pairwise
from itertools import chain
from typing import Generator, Iterable, Iterator, Sequence

from pdfje.atoms import Array, LiteralStr, Real
Expand All @@ -22,6 +22,7 @@
add_slots,
prepend,
)
from ..compat import pairwise
from ..fonts.common import TEXTSPACE_TO_GLYPHSPACE, GlyphPt
from .common import NO_OP, Chain, Command, Slug, State, Stretch

Expand Down
1 change: 1 addition & 0 deletions src/pdfje/vendor/get_kerning_pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# - OTFKernReader takes a TTFont object as input instead of a path
# - added a `for_font` helper method
# - formatted with black and isort
from __future__ import annotations

from typing import Mapping

Expand Down
5 changes: 4 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

from dataclasses import dataclass, field
from functools import partial
from typing import (
TYPE_CHECKING,
ClassVar,
Collection,
Iterable,
List,
Sequence,
TypeVar,
)
Expand All @@ -27,7 +30,7 @@
T = TypeVar("T")


class eq_iter(list[T]):
class eq_iter(List[T]):
"""Test helper for comparing iterables."""

def __eq__(self, other: object) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def outfile(tmpdir, request) -> Path:
return (
base
/ "-".join(
[func.__module__.removeprefix("tests."), func.__qualname__]
[func.__module__.strip("tests."), func.__qualname__]
).replace(".", "-")
).with_suffix(".pdf")

Expand Down
2 changes: 2 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from pdfje.common import RGB, XY, BranchableIterator, Sides
Expand Down
2 changes: 2 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from itertools import cycle, islice
from pathlib import Path

Expand Down
2 changes: 2 additions & 0 deletions tests/typeset/test_common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dataclasses import replace

from pdfje.atoms import LiteralStr, Real
Expand Down
2 changes: 2 additions & 0 deletions tests/typeset/test_lines.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Iterable

from pdfje.common import BranchableIterator, Pt
Expand Down
6 changes: 4 additions & 2 deletions tests/typeset/test_tex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import re
from typing import Callable, Iterator, Sequence, cast
from typing import Callable, Iterator, Sequence, Tuple, cast
from unittest.mock import ANY

import pytest
Expand Down Expand Up @@ -491,6 +493,6 @@ def _into_boxes(


WORDS, BOXES = cast(
tuple[tuple[str, ...], tuple[Box, ...]],
Tuple[Tuple[str, ...], Tuple[Box, ...]],
zip(*_into_boxes(EXAMPLE, times_roman.regular.width)),
)
2 changes: 2 additions & 0 deletions tests/typeset/test_words.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dataclasses import replace
from functools import partial
from typing import Generator, TypeVar
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ commands_pre=
extras=
fonts
hyphens
commands=
pytest

[testenv:py311]
commands =
Expand Down

0 comments on commit 96e663d

Please sign in to comment.