Skip to content

Commit 98511ac

Browse files
authored
STY: Apply ruff suggestions (#1570)
1 parent 962ce55 commit 98511ac

24 files changed

+451
-282
lines changed

make_changelog.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def main(changelog_path: str):
2727
today = datetime.now()
2828
header = f"Version {new_version}, {today:%Y-%m-%d}\n"
2929
header = header + "-" * (len(header) - 1) + "\n"
30-
trailer = f"\n[Full Changelog](https://github.com/py-pdf/pypdf/compare/{git_tag}...{new_version})\n\n"
30+
url = f"https://github.com/py-pdf/pypdf/compare/{git_tag}...{new_version}"
31+
trailer = f"\n[Full Changelog]({url})\n\n"
3132
new_entry = header + changes + trailer
3233
print(new_entry)
3334

pypdf/_cmap.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ def build_char_map(
2727
encoding, space_code = parse_encoding(ft, space_code)
2828
map_dict, space_code, int_entry = parse_to_unicode(ft, space_code)
2929

30-
# encoding can be either a string for decode (on 1,2 or a variable number of bytes) of a char table (for 1 byte only for me)
31-
# if empty string, it means it is than encoding field is not present and we have to select the good encoding from cmap input data
30+
# encoding can be either a string for decode
31+
# (on 1,2 or a variable number of bytes) of a char table (for 1 byte only for me)
32+
# if empty string, it means it is than encoding field is not present and
33+
# we have to select the good encoding from cmap input data
3234
if encoding == "":
3335
if -1 not in map_dict or map_dict[-1] == 1:
3436
# I have not been able to find any rule for no /Encoding nor /ToUnicode
3537
# One example shows /Symbol,bold I consider 8 bits encoding default
3638
encoding = "charmap"
3739
else:
3840
encoding = "utf-16-be"
39-
# apply rule from PDF ref 1.7 §5.9.1, 1st bullet : if cmap not empty encoding should be discarded (here transformed into identity for those characters)
41+
# apply rule from PDF ref 1.7 §5.9.1, 1st bullet :
42+
# if cmap not empty encoding should be discarded
43+
# (here transformed into identity for those characters)
4044
# if encoding is an str it is expected to be a identity translation
4145
elif isinstance(encoding, dict):
4246
for x in int_entry:
@@ -131,7 +135,9 @@ def parse_encoding(
131135
enc: Union(str, DictionaryObject) = ft["/Encoding"].get_object() # type: ignore
132136
if isinstance(enc, str):
133137
try:
134-
# allready done : enc = NameObject.unnumber(enc.encode()).decode() # for #xx decoding
138+
# allready done :
139+
# enc = NameObject.unnumber(enc.encode()).decode()
140+
# for #xx decoding
135141
if enc in charset_encoding:
136142
encoding = charset_encoding[enc].copy()
137143
elif enc in _predefined_cmap:
@@ -214,10 +220,12 @@ def prepare_cm(ft: DictionaryObject) -> bytes:
214220
if isinstance(tu, StreamObject):
215221
cm = cast(DecodedStreamObject, ft["/ToUnicode"]).get_data()
216222
elif isinstance(tu, str) and tu.startswith("/Identity"):
217-
cm = b"beginbfrange\n<0000> <0001> <0000>\nendbfrange" # the full range 0000-FFFF will be processed
223+
# the full range 0000-FFFF will be processed
224+
cm = b"beginbfrange\n<0000> <0001> <0000>\nendbfrange"
218225
if isinstance(cm, str):
219226
cm = cm.encode()
220-
# we need to prepare cm before due to missing return line in pdf printed to pdf from word
227+
# we need to prepare cm before due to missing return line in pdf printed
228+
# to pdf from word
221229
cm = (
222230
cm.strip()
223231
.replace(b"beginbfchar", b"\nbeginbfchar\n")

pypdf/_encryption.py

+163-111
Large diffs are not rendered by default.

pypdf/_merger.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def merge(
178178
)
179179
else:
180180
raise ValueError(
181-
"The argument position of merge is deprecated. Use page_number only."
181+
"The argument position of merge is deprecated. "
182+
"Use page_number only."
182183
)
183184

184185
if page_number is None: # deprecated
@@ -335,7 +336,8 @@ def write(self, fileobj: Union[Path, StrByteType]) -> None:
335336
page.out_pagedata = self.output.get_reference(
336337
pages_obj[PA.KIDS][-1].get_object()
337338
)
338-
# idnum = self.output._objects.index(self.output._pages.get_object()[PA.KIDS][-1].get_object()) + 1
339+
# key_temp = self.output._pages.get_object()[PA.KIDS][-1].get_object()
340+
# idnum = self.output._objects.index(key_temp) + 1
339341
# page.out_pagedata = IndirectObject(idnum, 0, self.output)
340342

341343
# Once all pages are added, create outline items to point at those pages
@@ -703,7 +705,8 @@ def add_outline_item(
703705
"""
704706
if page_number is not None and pagenum is not None:
705707
raise ValueError(
706-
"The argument pagenum of add_outline_item is deprecated. Use page_number only."
708+
"The argument pagenum of add_outline_item is deprecated. "
709+
"Use page_number only."
707710
)
708711
if pagenum is not None:
709712
old_term = "pagenum"
@@ -809,7 +812,8 @@ def add_named_destination(
809812
"""
810813
if page_number is not None and pagenum is not None:
811814
raise ValueError(
812-
"The argument pagenum of add_named_destination is deprecated. Use page_number only."
815+
"The argument pagenum of add_named_destination is deprecated. "
816+
"Use page_number only."
813817
)
814818
if pagenum is not None:
815819
old_term = "pagenum"

pypdf/_page.py

+59-34
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,20 @@ def set_custom_rtl(
9494
If set to `None`, the value will not be changed.
9595
If set to an integer or string, it will be converted to its ASCII code.
9696
The default value is -1, which sets no additional range to be converted.
97-
_max: The new maximum value for the range of custom characters that will be written right to left.
97+
_max: The new maximum value for the range of custom characters that will
98+
be written right to left.
9899
If set to `None`, the value will not be changed.
99100
If set to an integer or string, it will be converted to its ASCII code.
100101
The default value is -1, which sets no additional range to be converted.
101-
specials: The new list of special characters to be inserted in the current insertion order.
102+
specials: The new list of special characters to be inserted in the
103+
current insertion order.
102104
If set to `None`, the current value will not be changed.
103105
If set to a string, it will be converted to a list of ASCII codes.
104106
The default value is an empty list.
105107
106108
Returns:
107-
A tuple containing the new values for `CUSTOM_RTL_MIN`, `CUSTOM_RTL_MAX`, and `CUSTOM_RTL_SPECIAL_CHARS`.
109+
A tuple containing the new values for `CUSTOM_RTL_MIN`,
110+
`CUSTOM_RTL_MAX`, and `CUSTOM_RTL_SPECIAL_CHARS`.
108111
"""
109112
global CUSTOM_RTL_MIN, CUSTOM_RTL_MAX, CUSTOM_RTL_SPECIAL_CHARS
110113
if isinstance(_min, int):
@@ -919,7 +922,8 @@ def mergeScaledPage(
919922
"""
920923
deprecation_with_replacement(
921924
"page.mergeScaledPage(page2, scale, expand)",
922-
"page2.add_transformation(Transformation().scale(scale)); page.merge_page(page2, expand)",
925+
"page2.add_transformation(Transformation().scale(scale)); "
926+
"page.merge_page(page2, expand)",
923927
"3.0.0",
924928
)
925929
op = Transformation().scale(scale, scale)
@@ -944,7 +948,8 @@ def mergeRotatedPage(
944948
"""
945949
deprecation_with_replacement(
946950
"page.mergeRotatedPage(page2, rotation, expand)",
947-
"page2.add_transformation(Transformation().rotate(rotation)); page.merge_page(page2, expand)",
951+
"page2.add_transformation(Transformation().rotate(rotation)); "
952+
"page.merge_page(page2, expand)",
948953
"3.0.0",
949954
)
950955
op = Transformation().rotate(rotation)
@@ -970,7 +975,8 @@ def mergeTranslatedPage(
970975
"""
971976
deprecation_with_replacement(
972977
"page.mergeTranslatedPage(page2, tx, ty, expand)",
973-
"page2.add_transformation(Transformation().translate(tx, ty)); page.merge_page(page2, expand)",
978+
"page2.add_transformation(Transformation().translate(tx, ty)); "
979+
"page.merge_page(page2, expand)",
974980
"3.0.0",
975981
)
976982
op = Transformation().translate(tx, ty)
@@ -1002,7 +1008,8 @@ def mergeRotatedTranslatedPage(
10021008
"""
10031009
deprecation_with_replacement(
10041010
"page.mergeRotatedTranslatedPage(page2, rotation, tx, ty, expand)",
1005-
"page2.add_transformation(Transformation().rotate(rotation).translate(tx, ty)); page.merge_page(page2, expand)",
1011+
"page2.add_transformation(Transformation().rotate(rotation).translate(tx, ty)); "
1012+
"page.merge_page(page2, expand)",
10061013
"3.0.0",
10071014
)
10081015
op = Transformation().translate(-tx, -ty).rotate(rotation).translate(tx, ty)
@@ -1028,7 +1035,8 @@ def mergeRotatedScaledPage(
10281035
"""
10291036
deprecation_with_replacement(
10301037
"page.mergeRotatedScaledPage(page2, rotation, scale, expand)",
1031-
"page2.add_transformation(Transformation().rotate(rotation).scale(scale)); page.merge_page(page2, expand)",
1038+
"page2.add_transformation(Transformation().rotate(rotation).scale(scale)); "
1039+
"page.merge_page(page2, expand)",
10321040
"3.0.0",
10331041
)
10341042
op = Transformation().rotate(rotation).scale(scale, scale)
@@ -1060,7 +1068,8 @@ def mergeScaledTranslatedPage(
10601068
"""
10611069
deprecation_with_replacement(
10621070
"page.mergeScaledTranslatedPage(page2, scale, tx, ty, expand)",
1063-
"page2.add_transformation(Transformation().scale(scale).translate(tx, ty)); page.merge_page(page2, expand)",
1071+
"page2.add_transformation(Transformation().scale(scale).translate(tx, ty)); "
1072+
"page.merge_page(page2, expand)",
10641073
"3.0.0",
10651074
)
10661075
op = Transformation().scale(scale, scale).translate(tx, ty)
@@ -1095,7 +1104,8 @@ def mergeRotatedScaledTranslatedPage(
10951104
"""
10961105
deprecation_with_replacement(
10971106
"page.mergeRotatedScaledTranslatedPage(page2, rotation, tx, ty, expand)",
1098-
"page2.add_transformation(Transformation().rotate(rotation).scale(scale)); page.merge_page(page2, expand)",
1107+
"page2.add_transformation(Transformation().rotate(rotation).scale(scale)); "
1108+
"page.merge_page(page2, expand)",
10991109
"3.0.0",
11001110
)
11011111
op = Transformation().rotate(rotation).scale(scale, scale).translate(tx, ty)
@@ -1359,10 +1369,13 @@ def _extract_text(
13591369
while NameObject(PG.RESOURCES) not in objr:
13601370
# /Resources can be inherited sometimes so we look to parents
13611371
objr = objr["/Parent"].get_object()
1362-
# if no parents we will have no /Resources will be available => an exception wil be raised
1372+
# if no parents we will have no /Resources will be available
1373+
# => an exception wil be raised
13631374
resources_dict = cast(DictionaryObject, objr[PG.RESOURCES])
13641375
except Exception:
1365-
return "" # no resources means no text is possible (no font) we consider the file as not damaged, no need to check for TJ or Tj
1376+
# no resources means no text is possible (no font) we consider the
1377+
# file as not damaged, no need to check for TJ or Tj
1378+
return ""
13661379
if "/Font" in resources_dict:
13671380
for f in cast(DictionaryObject, resources_dict["/Font"]):
13681381
cmaps[f] = build_char_map(f, space_width, obj)
@@ -1428,7 +1441,9 @@ def current_spacewidth() -> float:
14281441
return _space_width / 1000.0
14291442

14301443
def process_operation(operator: bytes, operands: List) -> None:
1431-
nonlocal cm_matrix, cm_stack, tm_matrix, tm_prev, output, text, char_scale, space_scale, _space_width, TL, font_size, cmap, orientations, rtl_dir, visitor_text
1444+
nonlocal cm_matrix, cm_stack, tm_matrix, tm_prev, output, text
1445+
nonlocal char_scale, space_scale, _space_width, TL, font_size, cmap
1446+
nonlocal orientations, rtl_dir, visitor_text
14321447
global CUSTOM_RTL_MIN, CUSTOM_RTL_MAX, CUSTOM_RTL_SPECIAL_CHARS
14331448

14341449
check_crlf_space: bool = False
@@ -1509,10 +1524,12 @@ def process_operation(operator: bytes, operands: List) -> None:
15091524
text = ""
15101525
# rtl_dir = False
15111526
try:
1512-
# charMapTuple: font_type, float(sp_width / 2), encoding, map_dict, font-dictionary
1527+
# charMapTuple: font_type, float(sp_width / 2), encoding,
1528+
# map_dict, font-dictionary
15131529
charMapTuple = cmaps[operands[0]]
15141530
_space_width = charMapTuple[1]
1515-
# current cmap: encoding, map_dict, font resource name (internal name, not the real font-name),
1531+
# current cmap: encoding, map_dict, font resource name
1532+
# (internal name, not the real font-name),
15161533
# font-dictionary. The font-dictionary describes the font.
15171534
cmap = (
15181535
charMapTuple[2],
@@ -1575,7 +1592,10 @@ def process_operation(operator: bytes, operands: List) -> None:
15751592
t = tt.decode(
15761593
cmap[0], "surrogatepass"
15771594
) # apply str encoding
1578-
except Exception: # the data does not match the expectation, we use the alternative ; text extraction may not be good
1595+
except Exception:
1596+
# the data does not match the expectation,
1597+
# we use the alternative ;
1598+
# text extraction may not be good
15791599
t = tt.decode(
15801600
"utf-16-be" if cmap[0] == "charmap" else "charmap",
15811601
"surrogatepass",
@@ -1593,7 +1613,9 @@ def process_operation(operator: bytes, operands: List) -> None:
15931613
):
15941614
xx = ord(x)
15951615
# fmt: off
1596-
if ( # cases where the current inserting order is kept (punctuation,...)
1616+
if (
1617+
# cases where the current inserting order is
1618+
# kept (punctuation,...)
15971619
(xx <= 0x2F) # punctuations but...
15981620
or (0x3A <= xx and xx <= 0x40) # numbers (x30-39)
15991621
or (0x2000 <= xx and xx <= 0x206F) # upper punctuations..
@@ -1809,9 +1831,11 @@ def extract_text(
18091831
will change if this function is made more sophisticated.
18101832
18111833
Arabic, Hebrew,... are extracted in the good order.
1812-
If required an custom RTL range of characters can be defined; see function set_custom_rtl
1834+
If required an custom RTL range of characters can be defined;
1835+
see function set_custom_rtl
18131836
1814-
Additionally you can provide visitor-methods to get informed on all operands and all text-objects.
1837+
Additionally you can provide visitor-methods to get informed on all
1838+
operands and all text-objects.
18151839
For example in some PDF files this can be useful to parse tables.
18161840
18171841
Args:
@@ -1938,9 +1962,9 @@ def _get_fonts(self) -> Tuple[Set[str], Set[str]]:
19381962

19391963
mediabox = _create_rectangle_accessor(PG.MEDIABOX, ())
19401964
"""
1941-
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in default user space units,
1942-
defining the boundaries of the physical medium on which the page is
1943-
intended to be displayed or printed.
1965+
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in
1966+
default user space units, defining the boundaries of the physical medium on
1967+
which the page is intended to be displayed or printed.
19441968
"""
19451969

19461970
@property
@@ -1965,10 +1989,10 @@ def mediaBox(self, value: RectangleObject) -> None: # deprecated
19651989

19661990
cropbox = _create_rectangle_accessor("/CropBox", (PG.MEDIABOX,))
19671991
"""
1968-
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in default user space units,
1969-
defining the visible region of default user space. When the page is
1970-
displayed or printed, its contents are to be clipped (cropped) to this
1971-
rectangle and then imposed on the output medium in some
1992+
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in
1993+
default user space units, defining the visible region of default user space.
1994+
When the page is displayed or printed, its contents are to be clipped
1995+
(cropped) to this rectangle and then imposed on the output medium in some
19721996
implementation-defined manner. Default value: same as :attr:`mediabox<mediabox>`.
19731997
"""
19741998

@@ -1989,9 +2013,9 @@ def cropBox(self, value: RectangleObject) -> None: # deprecated
19892013

19902014
bleedbox = _create_rectangle_accessor("/BleedBox", ("/CropBox", PG.MEDIABOX))
19912015
"""
1992-
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in default user space units,
1993-
defining the region to which the contents of the page should be clipped
1994-
when output in a production environment.
2016+
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in
2017+
default user space units, defining the region to which the contents of the
2018+
page should be clipped when output in a production environment.
19952019
"""
19962020

19972021
@property
@@ -2011,8 +2035,9 @@ def bleedBox(self, value: RectangleObject) -> None: # deprecated
20112035

20122036
trimbox = _create_rectangle_accessor("/TrimBox", ("/CropBox", PG.MEDIABOX))
20132037
"""
2014-
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in default user space units,
2015-
defining the intended dimensions of the finished page after trimming.
2038+
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in
2039+
default user space units, defining the intended dimensions of the finished
2040+
page after trimming.
20162041
"""
20172042

20182043
@property
@@ -2032,9 +2057,9 @@ def trimBox(self, value: RectangleObject) -> None: # deprecated
20322057

20332058
artbox = _create_rectangle_accessor("/ArtBox", ("/CropBox", PG.MEDIABOX))
20342059
"""
2035-
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in default user space units,
2036-
defining the extent of the page's meaningful content as intended by the
2037-
page's creator.
2060+
A :class:`RectangleObject<pypdf.generic.RectangleObject>`, expressed in
2061+
default user space units, defining the extent of the page's meaningful
2062+
content as intended by the page's creator.
20382063
"""
20392064

20402065
@property

pypdf/_reader.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def convertToInt(d: bytes, size: int) -> Union[int, Tuple[Any, ...]]: # depreca
118118
class DocumentInformation(DictionaryObject):
119119
"""
120120
A class representing the basic document metadata provided in a PDF File.
121-
This class is accessible through :py:class:`PdfReader.metadata<pypdf.PdfReader.metadata>`.
121+
This class is accessible through
122+
:py:class:`PdfReader.metadata<pypdf.PdfReader.metadata>`.
122123
123124
All text properties of the document metadata have
124125
*two* properties, eg. author and author_raw. The non-raw property will

pypdf/_utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,9 @@ def rename_kwargs( # type: ignore
470470
)
471471
if new_term in kwargs:
472472
raise TypeError(
473-
f"{func_name} received both {old_term} and {new_term} as an argument. "
474-
f"{old_term} is deprecated. Use {new_term} instead."
473+
f"{func_name} received both {old_term} and {new_term} as "
474+
f"an argument. {old_term} is deprecated. "
475+
f"Use {new_term} instead."
475476
)
476477
kwargs[new_term] = kwargs.pop(old_term)
477478
warnings.warn(

0 commit comments

Comments
 (0)