Skip to content

Commit a0d8a5a

Browse files
committed
refactor: update type imports to use collections.abc for Iterable and Callable
1 parent e1ca491 commit a0d8a5a

4 files changed

Lines changed: 26 additions & 22 deletions

File tree

pdfdol/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Base objects for pdfdol"""
22

3-
from typing import Iterable
3+
from collections.abc import Iterable
44
from dol import Files, wrap_kvs, Pipe, KeyCodecs, add_ipython_key_completions
55
from pypdf import PdfReader
66
from io import BytesIO

pdfdol/examples/aggregations.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535
from pathlib import Path
3636
import os
37-
from typing import Iterable, Union, Callable, Optional
37+
from typing import Union, Optional
38+
from collections.abc import Iterable, Callable
3839
from ..util import concat_pdfs
3940
import base64
4041
import imghdr
@@ -63,17 +64,17 @@ def sorted_image_files(
6364

6465

6566
def images_to_pdf(
66-
images: Union[str, Path, Iterable],
67-
egress: Optional[Union[str, Path, Callable]] = None,
67+
images: str | Path | Iterable,
68+
egress: str | Path | Callable | None = None,
6869
*,
6970
sort_dir_files: Callable[[list], list] = sorted_image_files,
7071
_get_pdf_egress=None,
71-
captions: Optional[Iterable[str]] = None,
72+
captions: Iterable[str] | None = None,
7273
# Common layout controls forwarded to mk_image_and_caption_pdf_page
73-
image_fraction: Optional[float] = None,
74-
horizontal_margin: Optional[float] = None,
75-
align: Optional[str] = None,
76-
mk_page_kwargs: Optional[dict] = None,
74+
image_fraction: float | None = None,
75+
horizontal_margin: float | None = None,
76+
align: str | None = None,
77+
mk_page_kwargs: dict | None = None,
7778
):
7879
"""
7980
Aggregate a folder (or iterable) of images into a single PDF (one image per page).

pdfdol/tools.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Pdf Tools."""
22

33
from functools import partial
4-
from typing import Literal, Callable, Union
4+
from typing import Literal, Any
5+
from collections.abc import Callable
56
import os
67
import io
78

@@ -81,7 +82,7 @@ def _resolve_src_kind(src: str) -> SrcKind:
8182
return "text"
8283

8384

84-
def _resolve_bytes_egress(egress: Union[None, str, Callable]) -> Callable[[bytes], any]:
85+
def _resolve_bytes_egress(egress: None | str | Callable) -> Callable[[bytes], any]:
8586
"""
8687
Return a callable that processes PDF bytes based on the given egress.
8788
@@ -167,7 +168,7 @@ def add_css(html_text: str, css=dflt_css) -> str:
167168

168169
def markdown_to_pdf(
169170
md_src: str,
170-
egress: Union[None, str, Callable] = None,
171+
egress: None | str | Callable = None,
171172
*,
172173
markdown_extensions=dflt_markdown_kwargs,
173174
**pdfkit_kwargs,
@@ -176,7 +177,7 @@ def markdown_to_pdf(
176177

177178
if isinstance(md_src, str) and os.path.isfile(md_src):
178179
md_file = md_src
179-
with open(md_file, "r", encoding="utf-8") as f:
180+
with open(md_file, encoding="utf-8") as f:
180181
md_src = f.read()
181182

182183
# Convert Markdown to HTML
@@ -194,7 +195,7 @@ def markdown_to_pdf(
194195

195196
def get_pdf(
196197
src: str,
197-
egress: Union[None, str, Callable] = None,
198+
egress: None | str | Callable = None,
198199
*,
199200
src_kind: SrcKind = None,
200201
# extra options for pdfkit.from_* functions
@@ -206,7 +207,7 @@ def get_pdf(
206207
cover_first=False,
207208
verbose=False,
208209
**kwargs,
209-
) -> Union[bytes, any]:
210+
) -> bytes | Any:
210211
"""
211212
Convert the given source to a PDF (bytes) and process it using the specified egress.
212213

pdfdol/util.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Utils for pdfdol"""
22

33
from pypdf import PdfReader, PdfWriter, PageObject
4-
from typing import Iterable, Mapping, Union, Callable, Iterable
4+
from typing import Union
5+
from collections.abc import Iterable, Mapping, Callable, Iterable
56
import io
67
import os
78
from pathlib import Path
@@ -113,7 +114,8 @@ def remove_empty_pages(
113114

114115
# ---------------------------------------------------------------------------------
115116
# Sourcing pdfs
116-
from typing import Mapping, Union, Iterable
117+
from typing import Union
118+
from collections.abc import Mapping, Iterable
117119
import io
118120
from dol import written_bytes
119121

@@ -128,7 +130,7 @@ def _html_bytes_to_pdf_bytes_writer(html_bytes, buffer):
128130
html_bytes_to_pdf_bytes = written_bytes(_html_bytes_to_pdf_bytes_writer)
129131

130132
def html_to_pdf_w_weasyprint(
131-
htmls: Union[Filepath, Iterable[Filepath]],
133+
htmls: Filepath | Iterable[Filepath],
132134
save_filepath="htmls_to_pdf.pdf",
133135
):
134136
"""Convert one or several HTML files into a single PDF file."""
@@ -163,7 +165,7 @@ def html_to_pdf_w_weasyprint(
163165
}
164166

165167
def html_to_pdf_w_pdfkit(
166-
html_filepaths: Union[Filepath, Iterable[Filepath]],
168+
html_filepaths: Filepath | Iterable[Filepath],
167169
save_filepath="htmls_to_pdf.pdf",
168170
*,
169171
options=DFLT_OPTIONS,
@@ -260,13 +262,13 @@ def concat_pdf_files(pdf_filepaths: Iterable[str], save_filepath=DFLT_SAVE_PDF_N
260262

261263
# TODO: Generalize to allow pdf_source to be a mapping of any keys to pdf bytes (not necessarily filepaths)
262264
def concat_pdfs(
263-
pdf_source: Union[Iterable[bytes], Mapping[str, bytes]],
265+
pdf_source: Iterable[bytes] | Mapping[str, bytes],
264266
save_filepath=False,
265267
*,
266268
filter_extensions=False,
267-
key_order: Union[Callable, Iterable] = None,
269+
key_order: Callable | Iterable = None,
268270
**kwargs,
269-
) -> Union[str, bytes]:
271+
) -> str | bytes:
270272
"""
271273
Concatenate multiple PDFs and/or images given as a mapping of filepaths to bytes.
272274

0 commit comments

Comments
 (0)