Skip to content

Commit 4fa4ced

Browse files
authored
1 parent b8258c3 commit 4fa4ced

File tree

10 files changed

+89
-93
lines changed

10 files changed

+89
-93
lines changed

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ ignore = [
134134
"D101", # Missing docstring in public class
135135
"D102", # Missing docstring in public method
136136
"D103", # Missing docstring in public function
137-
"SLF",
138-
"PTH", "PLR", "FBT", "PT", "N", "ARG", "TRY", "S", "EM", "RET", "TID",
137+
"SLF001",
138+
"PLR", "PTH123", "FBT", "PT", "N", "ARG", "TRY", "S", "EM", "RET", "TID",
139139
"C901", "PGH", "DTZ", "TCH", "B", "G", "BLE", "SIM", "E", "INP", "A", "RUF",
140140
"PLW", "PLE"
141141
]
@@ -146,6 +146,7 @@ ignore = [
146146
"json_consistency.py" = ["T201"]
147147
"tests/test_workflows.py" = ["T201"]
148148
"sample-files/*" = ["D100"]
149+
"docs/conf.py" = ["PTH"]
149150

150151
[tool.docformatter]
151152
pre-summary-newline = true

sample-files

tests/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import os
21
import ssl
32
import urllib.request
3+
from pathlib import Path
44
from typing import List
55
from urllib.error import HTTPError
66

@@ -25,17 +25,17 @@ def get_pdf_from_url(url: str, name: str) -> bytes:
2525
if url.startswith("file://"):
2626
with open(url[7:].replace("\\", "/"), "rb") as fp:
2727
return fp.read()
28-
cache_dir = os.path.join(os.path.dirname(__file__), "pdf_cache")
29-
if not os.path.exists(cache_dir):
30-
os.mkdir(cache_dir)
31-
cache_path = os.path.join(cache_dir, name)
32-
if not os.path.exists(cache_path):
28+
cache_dir = Path(__file__).parent / "pdf_cache"
29+
if not cache_dir.exists():
30+
cache_dir.mkdir()
31+
cache_path = cache_dir / name
32+
if not cache_path.exists():
3333
ssl._create_default_https_context = ssl._create_unverified_context
3434
cpt = 3
3535
while cpt > 0:
3636
try:
37-
with urllib.request.urlopen(url) as response, open(
38-
cache_path, "wb"
37+
with urllib.request.urlopen(url) as response, cache_path.open(
38+
"wb"
3939
) as out_file:
4040
out_file.write(response.read())
4141
cpt = 0

tests/test_generic.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Test the pypdf.generic module."""
2-
import os
32
from io import BytesIO
43
from pathlib import Path
54
from unittest.mock import patch
@@ -701,7 +700,7 @@ def test_issue_997(mock_logger_warning):
701700
merger.close()
702701

703702
# cleanup
704-
os.remove(merged_filename)
703+
Path(merged_filename).unlink()
705704

706705

707706
def test_annotation_builder_free_text():
@@ -744,7 +743,7 @@ def test_annotation_builder_free_text():
744743
with open(target, "wb") as fp:
745744
writer.write(fp)
746745

747-
os.remove(target) # comment this out for manual inspection
746+
Path(target).unlink() # comment this out for manual inspection
748747

749748

750749
def test_annotation_builder_polygon():
@@ -772,7 +771,7 @@ def test_annotation_builder_polygon():
772771
with open(target, "wb") as fp:
773772
writer.write(fp)
774773

775-
os.remove(target) # comment this out for manual inspection
774+
Path(target).unlink() # comment this out for manual inspection
776775

777776

778777
def test_annotation_builder_line():
@@ -797,7 +796,7 @@ def test_annotation_builder_line():
797796
with open(target, "wb") as fp:
798797
writer.write(fp)
799798

800-
os.remove(target) # comment this out for manual inspection
799+
Path(target).unlink() # comment this out for manual inspection
801800

802801

803802
def test_annotation_builder_square():
@@ -824,7 +823,7 @@ def test_annotation_builder_square():
824823
with open(target, "wb") as fp:
825824
writer.write(fp)
826825

827-
os.remove(target) # comment this out for manual inspection
826+
Path(target).unlink() # comment this out for manual inspection
828827

829828

830829
def test_annotation_builder_circle():
@@ -852,7 +851,7 @@ def test_annotation_builder_circle():
852851
with open(target, "wb") as fp:
853852
writer.write(fp)
854853

855-
os.remove(target) # comment this out for manual inspection
854+
Path(target).unlink() # comment this out for manual inspection
856855

857856

858857
def test_annotation_builder_link():
@@ -910,7 +909,7 @@ def test_annotation_builder_link():
910909
with open(target, "wb") as fp:
911910
writer.write(fp)
912911

913-
os.remove(target) # comment this out for manual inspection
912+
Path(target).unlink() # comment this out for manual inspection
914913

915914

916915
def test_annotation_builder_text():
@@ -934,7 +933,7 @@ def test_annotation_builder_text():
934933
with open(target, "wb") as fp:
935934
writer.write(fp)
936935

937-
os.remove(target) # comment this out for manual inspection
936+
Path(target).unlink() # comment this out for manual inspection
938937

939938

940939
def test_CheckboxRadioButtonAttributes_opt():

tests/test_merger.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Test the pypdf._merger module."""
2-
import os
32
import sys
43
from io import BytesIO
54
from pathlib import Path
@@ -192,7 +191,7 @@ def test_merger_operations_by_semi_traditional_usage(tmp_path):
192191
merger.write(path) # Act
193192

194193
# Assert
195-
assert os.path.isfile(path)
194+
assert Path(path).is_file()
196195
check_outline(path)
197196

198197

@@ -204,7 +203,7 @@ def test_merger_operations_by_semi_traditional_usage_with_writer(tmp_path):
204203
merger.write(path) # Act
205204

206205
# Assert
207-
assert os.path.isfile(path)
206+
assert Path(path).is_file()
208207
check_outline(path)
209208

210209

@@ -213,7 +212,7 @@ def test_merger_operation_by_new_usage(tmp_path):
213212
with PdfMerger(fileobj=path) as merger:
214213
merger_operate(merger)
215214
# Assert
216-
assert os.path.isfile(path)
215+
assert Path(path).is_file()
217216
check_outline(path)
218217

219218

@@ -223,7 +222,7 @@ def test_merger_operation_by_new_usage_with_writer(tmp_path):
223222
merger_operate(merger)
224223

225224
# Assert
226-
assert os.path.isfile(path)
225+
assert Path(path).is_file()
227226
check_outline(path)
228227

229228

@@ -311,7 +310,7 @@ def test_merge_write_closed_fh_with_writer():
311310
merger.set_page_mode("/UseNone")
312311
merger.add_outline_item("An outline item", 0)
313312

314-
os.unlink("stream1.pdf")
313+
Path("stream1.pdf").unlink()
315314

316315

317316
@pytest.mark.external
@@ -325,7 +324,7 @@ def test_trim_outline_list():
325324
merger.close()
326325

327326
# cleanup
328-
os.remove("tmp-merger-do-not-commit.pdf")
327+
Path("tmp-merger-do-not-commit.pdf").unlink()
329328

330329

331330
@pytest.mark.external
@@ -339,7 +338,7 @@ def test_trim_outline_list_with_writer():
339338
merger.close()
340339

341340
# cleanup
342-
os.remove("tmp-merger-do-not-commit.pdf")
341+
Path("tmp-merger-do-not-commit.pdf").unlink()
343342

344343

345344
@pytest.mark.external
@@ -353,7 +352,7 @@ def test_zoom():
353352
merger.close()
354353

355354
# cleanup
356-
os.remove("tmp-merger-do-not-commit.pdf")
355+
Path("tmp-merger-do-not-commit.pdf").unlink()
357356

358357

359358
@pytest.mark.external
@@ -367,7 +366,7 @@ def test_zoom_with_writer():
367366
merger.close()
368367

369368
# cleanup
370-
os.remove("tmp-merger-do-not-commit.pdf")
369+
Path("tmp-merger-do-not-commit.pdf").unlink()
371370

372371

373372
@pytest.mark.external
@@ -381,7 +380,7 @@ def test_zoom_xyz_no_left():
381380
merger.close()
382381

383382
# cleanup
384-
os.remove("tmp-merger-do-not-commit.pdf")
383+
Path("tmp-merger-do-not-commit.pdf").unlink()
385384

386385

387386
@pytest.mark.external
@@ -395,7 +394,7 @@ def test_zoom_xyz_no_left_with_writer():
395394
merger.close()
396395

397396
# cleanup
398-
os.remove("tmp-merger-do-not-commit.pdf")
397+
Path("tmp-merger-do-not-commit.pdf").unlink()
399398

400399

401400
@pytest.mark.external
@@ -409,7 +408,7 @@ def test_outline_item():
409408
merger.close()
410409

411410
# cleanup
412-
os.remove("tmp-merger-do-not-commit.pdf")
411+
Path("tmp-merger-do-not-commit.pdf").unlink()
413412

414413

415414
@pytest.mark.external
@@ -424,7 +423,7 @@ def test_outline_item_with_writer():
424423
merger.close()
425424

426425
# cleanup
427-
os.remove("tmp-merger-do-not-commit.pdf")
426+
Path("tmp-merger-do-not-commit.pdf").unlink()
428427

429428

430429
@pytest.mark.external
@@ -439,7 +438,7 @@ def test_trim_outline():
439438
merger.close()
440439

441440
# cleanup
442-
os.remove("tmp-merger-do-not-commit.pdf")
441+
Path("tmp-merger-do-not-commit.pdf").unlink()
443442

444443

445444
@pytest.mark.external
@@ -454,7 +453,7 @@ def test_trim_outline_with_writer():
454453
merger.close()
455454

456455
# cleanup
457-
os.remove("tmp-merger-do-not-commit.pdf")
456+
Path("tmp-merger-do-not-commit.pdf").unlink()
458457

459458

460459
@pytest.mark.external
@@ -469,7 +468,7 @@ def test1():
469468
merger.close()
470469

471470
# cleanup
472-
os.remove("tmp-merger-do-not-commit.pdf")
471+
Path("tmp-merger-do-not-commit.pdf").unlink()
473472

474473

475474
@pytest.mark.external
@@ -484,7 +483,7 @@ def test1_with_writer():
484483
merger.close()
485484

486485
# cleanup
487-
os.remove("tmp-merger-do-not-commit.pdf")
486+
Path("tmp-merger-do-not-commit.pdf").unlink()
488487

489488

490489
@pytest.mark.external
@@ -503,7 +502,7 @@ def test_sweep_recursion1():
503502
reader2.pages
504503

505504
# cleanup
506-
os.remove("tmp-merger-do-not-commit.pdf")
505+
Path("tmp-merger-do-not-commit.pdf").unlink()
507506

508507

509508
@pytest.mark.external
@@ -522,7 +521,7 @@ def test_sweep_recursion1_with_writer():
522521
reader2.pages
523522

524523
# cleanup
525-
os.remove("tmp-merger-do-not-commit.pdf")
524+
Path("tmp-merger-do-not-commit.pdf").unlink()
526525

527526

528527
@pytest.mark.external
@@ -552,7 +551,7 @@ def test_sweep_recursion2(url, name):
552551
reader2.pages
553552

554553
# cleanup
555-
os.remove("tmp-merger-do-not-commit.pdf")
554+
Path("tmp-merger-do-not-commit.pdf").unlink()
556555

557556

558557
@pytest.mark.external
@@ -582,7 +581,7 @@ def test_sweep_recursion2_with_writer(url, name):
582581
reader2.pages
583582

584583
# cleanup
585-
os.remove("tmp-merger-do-not-commit.pdf")
584+
Path("tmp-merger-do-not-commit.pdf").unlink()
586585

587586

588587
@pytest.mark.external
@@ -600,7 +599,7 @@ def test_sweep_indirect_list_newobj_is_none(caplog):
600599
reader2.pages
601600

602601
# cleanup
603-
os.remove("tmp-merger-do-not-commit.pdf")
602+
Path("tmp-merger-do-not-commit.pdf").unlink()
604603

605604

606605
@pytest.mark.external
@@ -618,7 +617,7 @@ def test_sweep_indirect_list_newobj_is_none_with_writer(caplog):
618617
reader2.pages
619618

620619
# cleanup
621-
os.remove("tmp-merger-do-not-commit.pdf")
620+
Path("tmp-merger-do-not-commit.pdf").unlink()
622621

623622

624623
@pytest.mark.external

tests/test_page.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Test the pypdf._page module."""
22
import json
3-
import os
43
import random
54
from copy import deepcopy
65
from io import BytesIO
@@ -34,7 +33,7 @@
3433

3534
def get_all_sample_files():
3635
meta_file = SAMPLE_ROOT / "files.json"
37-
if not os.path.isfile(meta_file):
36+
if not Path(meta_file).is_file():
3837
return {"data": []}
3938
with open(meta_file) as fp:
4039
data = fp.read()
@@ -853,7 +852,7 @@ def test_annotation_setter():
853852
writer.write(fp)
854853

855854
# Cleanup
856-
os.remove(target) # remove for testing
855+
Path(target).unlink() # remove for testing
857856

858857

859858
@pytest.mark.external

0 commit comments

Comments
 (0)