Skip to content

Commit e897809

Browse files
authored
STY: Fix issues found by ruff (#1969)
1 parent 052f8ac commit e897809

File tree

9 files changed

+16
-28
lines changed

9 files changed

+16
-28
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ repos:
2222
# hooks:
2323
# - id: mypy
2424
- repo: https://github.com/psf/black
25-
rev: 23.3.0
25+
rev: 23.7.0
2626
hooks:
2727
- id: black
2828
args: [--target-version, py36]
2929
- repo: https://github.com/asottile/blacken-docs
30-
rev: 1.14.0
30+
rev: 1.15.0
3131
hooks:
3232
- id: blacken-docs
3333
additional_dependencies: [black==22.1.0]
3434
exclude: "docs/user/robustness.md"
3535
- repo: https://github.com/charliermarsh/ruff-pre-commit
36-
rev: 'v0.0.275'
36+
rev: 'v0.0.278'
3737
hooks:
3838
- id: ruff
3939
args: ['--fix']
4040
- repo: https://github.com/asottile/pyupgrade
41-
rev: v3.7.0
41+
rev: v3.9.0
4242
hooks:
4343
- id: pyupgrade
4444
args: [--py36-plus]

pypdf/_encryption.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,7 @@ def encrypt_object(self, obj: PdfObject) -> PdfObject:
194194
obj2[key] = self.encrypt_object(value)
195195
obj = obj2
196196
elif isinstance(obj, ArrayObject):
197-
obj2 = ArrayObject() # type: ignore
198-
for x in obj:
199-
obj2.append(self.encrypt_object(x)) # type: ignore
200-
obj = obj2
197+
obj = ArrayObject(self.encrypt_object(x) for x in obj) # type: ignore
201198
return obj
202199

203200
def decrypt_object(self, obj: PdfObject) -> PdfObject:

pypdf/_page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ def replace_contents(
978978
self[NameObject(PG.CONTENTS)] = content
979979

980980
def merge_page(
981-
self, page2: "PageObject", expand: bool = False, over: bool = True
981+
self, page2: "PageObject", expand: bool = False, over: bool = True
982982
) -> None:
983983
"""
984984
Merge the content streams of two pages into one.
@@ -1046,7 +1046,7 @@ def _merge_page(
10461046
annots = page[PG.ANNOTS]
10471047
if isinstance(annots, ArrayObject):
10481048
for ref in annots:
1049-
new_annots.append(ref)
1049+
new_annots.append(ref) # noqa: PERF402
10501050

10511051
for res in (
10521052
RES.EXT_G_STATE,

pypdf/_reader.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,11 +2217,7 @@ def _list_attachments(self) -> List[str]:
22172217
)
22182218
except KeyError:
22192219
return []
2220-
attachments_names = []
2221-
# Loop through attachments
2222-
for f in filenames:
2223-
if isinstance(f, str):
2224-
attachments_names.append(f)
2220+
attachments_names = [f for f in filenames if isinstance(f, str)]
22252221
return attachments_names
22262222

22272223
def _get_attachment_list(self, name: str) -> List[bytes]:

pypdf/_writer.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,7 @@ def update_page_form_field_values(
956956
or self._get_qualified_field_name(writer_annot) == field
957957
):
958958
if isinstance(value, list):
959-
lst = ArrayObject()
960-
for v in value:
961-
lst.append(TextStringObject(v))
959+
lst = ArrayObject(TextStringObject(v) for v in value)
962960
writer_annot[NameObject(FA.V)] = lst
963961
else:
964962
writer_annot[NameObject(FA.V)] = TextStringObject(value)
@@ -2921,7 +2919,7 @@ def merge(
29212919
pag[NameObject("/Annots")] = lst
29222920
self.clean_page(pag)
29232921

2924-
if "/AcroForm" in _ro and _ro['/AcroForm'] is not None:
2922+
if "/AcroForm" in _ro and _ro["/AcroForm"] is not None:
29252923
if "/AcroForm" not in self._root_object:
29262924
self._root_object[NameObject("/AcroForm")] = self._add_object(
29272925
cast(
@@ -3395,10 +3393,7 @@ def _pdf_objectify(obj: Union[Dict[str, Any], str, int, List[Any]]) -> PdfObject
33953393
to_add[name_key] = casted_value
33963394
return to_add
33973395
elif isinstance(obj, list):
3398-
arr = ArrayObject()
3399-
for el in obj:
3400-
arr.append(_pdf_objectify(el))
3401-
return arr
3396+
return ArrayObject(_pdf_objectify(el) for el in obj)
34023397
elif isinstance(obj, str):
34033398
if obj.startswith("/"):
34043399
return NameObject(obj)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ ignore = [
183183
"S101", # Use of `assert` detected
184184
"SLF001", # Private member accessed
185185
"PD011", # Use `.to_numpy()` instead of `.values`
186+
"FA102", # Missing `from __future__ import annotations`, but uses PEP 604 union
187+
"PERF203", # `try`-`except` within a loop incurs performance overhead
186188
]
187189

188190
[tool.ruff.per-file-ignores]

requirements/ci-3.11.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pytest-socket==0.6.0
6161
# via -r requirements/ci.in
6262
pytest-timeout==2.1.0
6363
# via -r requirements/ci.in
64-
ruff==0.0.275
64+
ruff==0.0.278
6565
# via -r requirements/ci.in
6666
typeguard==3.0.2
6767
# via -r requirements/ci.in

tests/test_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def get_dest_pages(x) -> int:
712712

713713
# oi can be destination or a list:preferred to just print them
714714
for oi in outline:
715-
out.append(get_dest_pages(oi))
715+
out.append(get_dest_pages(oi)) # noqa: PERF401
716716

717717

718718
def test_decode_permissions():

tests/test_xmp.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ def get_all_tiff(xmp: pypdf.xmp.XmpInformation):
7171
about_uri="", namespace="http://ns.adobe.com/tiff/1.0/"
7272
)
7373
for tag in tiff_ns:
74-
contents = []
75-
for content in tag.childNodes:
76-
contents.append(content.data)
74+
contents = [content.data for content in tag.childNodes]
7775
data[tag.tagName] = contents
7876
return data
7977

0 commit comments

Comments
 (0)