-
Notifications
You must be signed in to change notification settings - Fork 1.5k
MAINT: Enforce PLW2901 (avoid loop/context var overwrite) #3513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
ff2509a
12353fd
6da4f06
b1bee72
ca1e7ef
e33d82a
e385405
66157a2
412599b
bdbabf1
320d53d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,9 +64,12 @@ def __post_init__(self) -> None: | |
| for d_font_idx, d_font in enumerate( | ||
| self.font_dictionary["/DescendantFonts"] | ||
| ): | ||
| while isinstance(d_font, IndirectObject): | ||
| d_font = d_font.get_object() | ||
| self.font_dictionary["/DescendantFonts"][d_font_idx] = d_font | ||
| if isinstance(d_font, IndirectObject): | ||
|
||
| d_font_object = d_font.get_object() | ||
| else: | ||
| d_font_object = d_font | ||
| assert not isinstance(d_font_object, IndirectObject), d_font_object | ||
| self.font_dictionary["/DescendantFonts"][d_font_idx] = d_font_object | ||
| ord_map = { | ||
| ord(_target): _surrogate | ||
| for _target, _surrogate in self.char_map.items() | ||
|
|
@@ -80,18 +83,18 @@ def __post_init__(self) -> None: | |
| skip_count = 0 | ||
| _w = d_font.get("/W", []) | ||
| for idx, w_entry in enumerate(_w): | ||
| w_entry = w_entry.get_object() | ||
| w_value = w_entry.get_object() | ||
| if skip_count: | ||
| skip_count -= 1 | ||
| continue | ||
| if not isinstance(w_entry, (int, float)): # pragma: no cover | ||
| if not isinstance(w_value, (int, float)): # pragma: no cover | ||
| # We should never get here due to skip_count above. Add a | ||
| # warning and or use reader's "strict" to force an ex??? | ||
| continue | ||
| # check for format (1): `int [int int int int ...]` | ||
| w_next_entry = _w[idx + 1].get_object() | ||
| if isinstance(w_next_entry, Sequence): | ||
| start_idx, width_list = w_entry, w_next_entry | ||
| start_idx, width_list = w_value, w_next_entry | ||
| self.width_map.update( | ||
| { | ||
| ord_map[_cidx]: _width | ||
|
|
@@ -112,7 +115,7 @@ def __post_init__(self) -> None: | |
| _w[idx + 2].get_object(), (int, float) | ||
| ): | ||
| start_idx, stop_idx, const_width = ( | ||
| w_entry, | ||
| w_value, | ||
| w_next_entry, | ||
| _w[idx + 2].get_object(), | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -982,8 +982,8 @@ def update_page_form_field_values( | |||||
| if PG.ANNOTS not in page: | ||||||
| logger_warning("No fields to update on this page", __name__) | ||||||
| return | ||||||
| for annotation in page[PG.ANNOTS]: # type: ignore | ||||||
| annotation = cast(DictionaryObject, annotation.get_object()) | ||||||
| for annotation_ref in page[PG.ANNOTS]: # type: ignore | ||||||
| annotation = cast(DictionaryObject, annotation_ref.get_object()) | ||||||
| if annotation.get("/Subtype", "") != "/Widget": | ||||||
| continue | ||||||
| if "/FT" in annotation and "/T" in annotation: | ||||||
|
|
@@ -1007,7 +1007,6 @@ def update_page_form_field_values( | |||||
| del parent_annotation["/I"] | ||||||
| if flags: | ||||||
| annotation[NameObject(FA.Ff)] = NumberObject(flags) | ||||||
| # Set the field value | ||||||
| if not (value is None and flatten): # Only change values if given by user and not flattening. | ||||||
| if isinstance(value, list): | ||||||
| lst = ArrayObject(TextStringObject(v) for v in value) | ||||||
|
|
@@ -1029,9 +1028,14 @@ def update_page_form_field_values( | |||||
| if v not in normal_ap: | ||||||
| v = NameObject("/Off") | ||||||
| appearance_stream_obj = normal_ap.get(v) | ||||||
| # Other cases will be updated through the for loop | ||||||
| # other cases will be updated through the for loop | ||||||
| annotation[NameObject(AA.AS)] = v | ||||||
| annotation[NameObject(FA.V)] = v | ||||||
| if flatten and appearance_stream_obj is not None: | ||||||
|
||||||
| # We basically copy the entire appearance stream, which should be an XObject that | ||||||
| # is already registered. No need to add font resources. | ||||||
| rct = cast(RectangleObject, annotation[AA.Rect]) | ||||||
| self._add_apstream_object(page, appearance_stream_obj, field, rct[0], rct[1]) | ||||||
| elif ( | ||||||
| parent_annotation.get(FA.FT) == "/Tx" | ||||||
| or parent_annotation.get(FA.FT) == "/Ch" | ||||||
|
|
@@ -1050,7 +1054,7 @@ def update_page_form_field_values( | |||||
| annotation[NameObject(AA.AP)] = DictionaryObject( | ||||||
| {NameObject("/N"): self._add_object(appearance_stream_obj)} | ||||||
| ) | ||||||
| elif "/N" not in (ap:= cast(DictionaryObject, annotation[AA.AP])): | ||||||
| elif "/N" not in (ap := cast(DictionaryObject, annotation[AA.AP])): | ||||||
| cast(DictionaryObject, annotation[NameObject(AA.AP)])[ | ||||||
| NameObject("/N") | ||||||
| ] = self._add_object(appearance_stream_obj) | ||||||
|
|
@@ -1100,9 +1104,9 @@ def reattach_fields( | |||||
| if "/Annots" not in page: | ||||||
| return lst | ||||||
| annotations = cast(ArrayObject, page["/Annots"]) | ||||||
| for idx, annotation in enumerate(annotations): | ||||||
| is_indirect = isinstance(annotation, IndirectObject) | ||||||
| annotation = cast(DictionaryObject, annotation.get_object()) | ||||||
| for idx, annotation_ref in enumerate(annotations): | ||||||
| is_indirect = isinstance(annotation_ref, IndirectObject) | ||||||
| annotation = cast(DictionaryObject, annotation_ref.get_object()) | ||||||
| if annotation.get("/Subtype", "") == "/Widget" and "/FT" in annotation: | ||||||
| if ( | ||||||
| "indirect_reference" in annotation.__dict__ | ||||||
|
|
@@ -1472,9 +1476,10 @@ def _write_pdf_structure(self, stream: StreamType) -> tuple[list[int], list[int] | |||||
| if obj is not None: | ||||||
| object_positions.append(stream.tell()) | ||||||
| stream.write(f"{idnum} 0 obj\n".encode()) | ||||||
| object_to_write = obj | ||||||
| if self._encryption and obj != self._encrypt_entry: | ||||||
| obj = self._encryption.encrypt_object(obj, idnum, 0) | ||||||
| obj.write_to_stream(stream) | ||||||
| object_to_write = self._encryption.encrypt_object(obj, idnum, 0) | ||||||
| object_to_write.write_to_stream(stream) | ||||||
| stream.write(b"\nendobj\n") | ||||||
| else: | ||||||
| object_positions.append(-1) | ||||||
|
|
@@ -1563,9 +1568,8 @@ def add_metadata(self, infos: dict[str, Any]) -> None: | |||||
| if isinstance(infos, PdfObject): | ||||||
| infos = cast(DictionaryObject, infos.get_object()) | ||||||
| for key, value in list(infos.items()): | ||||||
| if isinstance(value, PdfObject): | ||||||
| value = value.get_object() | ||||||
| args[NameObject(key)] = create_string_object(str(value)) | ||||||
| value_obj = value.get_object() if isinstance(value, PdfObject) else value | ||||||
|
||||||
| value_obj = value.get_object() if isinstance(value, PdfObject) else value | |
| value_object = value.get_object() if isinstance(value, PdfObject) else value |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -759,30 +759,31 @@ def decode_stream_data(stream: Any) -> bytes: | |
| # If there is no data to decode, we should not try to decode it. | ||
| if not data: | ||
| return data | ||
| for filter_name, params in zip(filters, decode_parms): | ||
| if isinstance(params, NullObject): | ||
| params = {} | ||
| for filter_name, params_untyped in zip(filters, decode_parms): | ||
| params_typed: Optional[DictionaryObject] = None | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
| if not isinstance(params_untyped, NullObject): | ||
| params_typed = cast(Optional[DictionaryObject], params_untyped) | ||
| if filter_name in (FT.ASCII_HEX_DECODE, FTA.AHx): | ||
| data = ASCIIHexDecode.decode(data) | ||
| elif filter_name in (FT.ASCII_85_DECODE, FTA.A85): | ||
| data = ASCII85Decode.decode(data) | ||
| elif filter_name in (FT.LZW_DECODE, FTA.LZW): | ||
| data = LZWDecode.decode(data, params) | ||
| data = LZWDecode.decode(data, params_typed) | ||
| elif filter_name in (FT.FLATE_DECODE, FTA.FL): | ||
| data = FlateDecode.decode(data, params) | ||
| data = FlateDecode.decode(data, params_typed) | ||
| elif filter_name in (FT.RUN_LENGTH_DECODE, FTA.RL): | ||
| data = RunLengthDecode.decode(data) | ||
| elif filter_name == FT.CCITT_FAX_DECODE: | ||
| height = stream.get(IA.HEIGHT, ()) | ||
| data = CCITTFaxDecode.decode(data, params, height) | ||
| data = CCITTFaxDecode.decode(data, params_typed, height) | ||
| elif filter_name == FT.DCT_DECODE: | ||
| data = DCTDecode.decode(data) | ||
| elif filter_name == FT.JPX_DECODE: | ||
| data = JPXDecode.decode(data) | ||
| elif filter_name == FT.JBIG2_DECODE: | ||
| data = JBIG2Decode.decode(data, params) | ||
| data = JBIG2Decode.decode(data, params_typed) | ||
| elif filter_name == "/Crypt": | ||
| if "/Name" in params or "/Type" in params: | ||
| if "/Name" in params_untyped or "/Type" in params_untyped: | ||
| raise NotImplementedError( | ||
| "/Crypt filter with /Name or /Type not supported yet" | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,8 +41,8 @@ def test_strip_header(data, expected): | |
| def test_get_git_commits_since_tag(): | ||
| make_release = pytest.importorskip("make_release") | ||
|
|
||
| with open(COMMITS__VERSION_4_0_1, mode="rb") as commits, mock.patch( | ||
| "urllib.request.urlopen", side_effect=lambda _: commits | ||
| with open(COMMITS__VERSION_4_0_1, mode="rb") as commits_fh, mock.patch( | ||
| "urllib.request.urlopen", side_effect=lambda _: commits_fh | ||
| ), mock.patch("subprocess.check_output", return_value=GIT_LOG__VERSION_4_0_1): | ||
| commits = make_release.get_git_commits_since_tag("4.0.1") | ||
dev-KingMaster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert commits == [ | ||
|
|
@@ -87,8 +87,8 @@ def test_get_git_commits_since_tag(): | |
| def test_get_formatted_changes(): | ||
| make_release = pytest.importorskip("make_release") | ||
|
|
||
| with open(COMMITS__VERSION_4_0_1, mode="rb") as commits, mock.patch( | ||
| "urllib.request.urlopen", side_effect=lambda _: commits | ||
| with open(COMMITS__VERSION_4_0_1, mode="rb") as commits_fh, mock.patch( | ||
| "urllib.request.urlopen", side_effect=lambda _: commits_fh | ||
| ), mock.patch("subprocess.check_output", return_value=GIT_LOG__VERSION_4_0_1): | ||
| output, output_with_user = make_release.get_formatted_changes("4.0.1") | ||
|
|
||
|
|
@@ -126,8 +126,6 @@ def test_get_formatted_changes(): | |
| - Avoid catching not emitted warnings (#2429) by @stefan6419846 | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is most likely wrong. |
||
| def test_get_formatted_changes__other(): | ||
| make_release = pytest.importorskip("make_release") | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArrayObjectis aPdfObject. If you want to typevalue_for_init, please usevalue_for_init: PdfObject = ...without the cast.