From e89780971679285e9510fe862e1e4b9c787648a1 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 15 Jul 2023 17:08:18 +0200 Subject: [PATCH] STY: Fix issues found by ruff (#1969) --- .pre-commit-config.yaml | 8 ++++---- pypdf/_encryption.py | 5 +---- pypdf/_page.py | 4 ++-- pypdf/_reader.py | 6 +----- pypdf/_writer.py | 11 +++-------- pyproject.toml | 2 ++ requirements/ci-3.11.txt | 2 +- tests/test_reader.py | 2 +- tests/test_xmp.py | 4 +--- 9 files changed, 16 insertions(+), 28 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d9f6537f7..022dcf68f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,23 +22,23 @@ repos: # hooks: # - id: mypy - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 23.7.0 hooks: - id: black args: [--target-version, py36] - repo: https://github.com/asottile/blacken-docs - rev: 1.14.0 + rev: 1.15.0 hooks: - id: blacken-docs additional_dependencies: [black==22.1.0] exclude: "docs/user/robustness.md" - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: 'v0.0.275' + rev: 'v0.0.278' hooks: - id: ruff args: ['--fix'] - repo: https://github.com/asottile/pyupgrade - rev: v3.7.0 + rev: v3.9.0 hooks: - id: pyupgrade args: [--py36-plus] diff --git a/pypdf/_encryption.py b/pypdf/_encryption.py index dd4acbc42..b7a65a9ee 100644 --- a/pypdf/_encryption.py +++ b/pypdf/_encryption.py @@ -194,10 +194,7 @@ def encrypt_object(self, obj: PdfObject) -> PdfObject: obj2[key] = self.encrypt_object(value) obj = obj2 elif isinstance(obj, ArrayObject): - obj2 = ArrayObject() # type: ignore - for x in obj: - obj2.append(self.encrypt_object(x)) # type: ignore - obj = obj2 + obj = ArrayObject(self.encrypt_object(x) for x in obj) # type: ignore return obj def decrypt_object(self, obj: PdfObject) -> PdfObject: diff --git a/pypdf/_page.py b/pypdf/_page.py index 4b8721331..081eb8815 100644 --- a/pypdf/_page.py +++ b/pypdf/_page.py @@ -978,7 +978,7 @@ def replace_contents( self[NameObject(PG.CONTENTS)] = content def merge_page( - self, page2: "PageObject", expand: bool = False, over: bool = True + self, page2: "PageObject", expand: bool = False, over: bool = True ) -> None: """ Merge the content streams of two pages into one. @@ -1046,7 +1046,7 @@ def _merge_page( annots = page[PG.ANNOTS] if isinstance(annots, ArrayObject): for ref in annots: - new_annots.append(ref) + new_annots.append(ref) # noqa: PERF402 for res in ( RES.EXT_G_STATE, diff --git a/pypdf/_reader.py b/pypdf/_reader.py index 32105b271..eb570096e 100644 --- a/pypdf/_reader.py +++ b/pypdf/_reader.py @@ -2217,11 +2217,7 @@ def _list_attachments(self) -> List[str]: ) except KeyError: return [] - attachments_names = [] - # Loop through attachments - for f in filenames: - if isinstance(f, str): - attachments_names.append(f) + attachments_names = [f for f in filenames if isinstance(f, str)] return attachments_names def _get_attachment_list(self, name: str) -> List[bytes]: diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 26f9d769e..1abb61566 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -956,9 +956,7 @@ def update_page_form_field_values( or self._get_qualified_field_name(writer_annot) == field ): if isinstance(value, list): - lst = ArrayObject() - for v in value: - lst.append(TextStringObject(v)) + lst = ArrayObject(TextStringObject(v) for v in value) writer_annot[NameObject(FA.V)] = lst else: writer_annot[NameObject(FA.V)] = TextStringObject(value) @@ -2921,7 +2919,7 @@ def merge( pag[NameObject("/Annots")] = lst self.clean_page(pag) - if "/AcroForm" in _ro and _ro['/AcroForm'] is not None: + if "/AcroForm" in _ro and _ro["/AcroForm"] is not None: if "/AcroForm" not in self._root_object: self._root_object[NameObject("/AcroForm")] = self._add_object( cast( @@ -3395,10 +3393,7 @@ def _pdf_objectify(obj: Union[Dict[str, Any], str, int, List[Any]]) -> PdfObject to_add[name_key] = casted_value return to_add elif isinstance(obj, list): - arr = ArrayObject() - for el in obj: - arr.append(_pdf_objectify(el)) - return arr + return ArrayObject(_pdf_objectify(el) for el in obj) elif isinstance(obj, str): if obj.startswith("/"): return NameObject(obj) diff --git a/pyproject.toml b/pyproject.toml index 5da4d9b7f..4066dd061 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -183,6 +183,8 @@ ignore = [ "S101", # Use of `assert` detected "SLF001", # Private member accessed "PD011", # Use `.to_numpy()` instead of `.values` + "FA102", # Missing `from __future__ import annotations`, but uses PEP 604 union + "PERF203", # `try`-`except` within a loop incurs performance overhead ] [tool.ruff.per-file-ignores] diff --git a/requirements/ci-3.11.txt b/requirements/ci-3.11.txt index 7236e1d05..169f93049 100644 --- a/requirements/ci-3.11.txt +++ b/requirements/ci-3.11.txt @@ -61,7 +61,7 @@ pytest-socket==0.6.0 # via -r requirements/ci.in pytest-timeout==2.1.0 # via -r requirements/ci.in -ruff==0.0.275 +ruff==0.0.278 # via -r requirements/ci.in typeguard==3.0.2 # via -r requirements/ci.in diff --git a/tests/test_reader.py b/tests/test_reader.py index 69994f5ca..dc8b44a2f 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -712,7 +712,7 @@ def get_dest_pages(x) -> int: # oi can be destination or a list:preferred to just print them for oi in outline: - out.append(get_dest_pages(oi)) + out.append(get_dest_pages(oi)) # noqa: PERF401 def test_decode_permissions(): diff --git a/tests/test_xmp.py b/tests/test_xmp.py index b2709579c..777a44141 100644 --- a/tests/test_xmp.py +++ b/tests/test_xmp.py @@ -71,9 +71,7 @@ def get_all_tiff(xmp: pypdf.xmp.XmpInformation): about_uri="", namespace="http://ns.adobe.com/tiff/1.0/" ) for tag in tiff_ns: - contents = [] - for content in tag.childNodes: - contents.append(content.data) + contents = [content.data for content in tag.childNodes] data[tag.tagName] = contents return data