Skip to content
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

BUG: Fixed reference of value with str.__new__ in TextStringObject #2952

Merged
merged 4 commits into from
Nov 29, 2024

Conversation

thomas-forte
Copy link
Contributor

@thomas-forte thomas-forte commented Nov 18, 2024

based on comments from #2949

  • Fixing reference of value with str.new output
  • updated type annotation on update_page_form_field_values

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.36%. Comparing base (5587414) to head (00f075f).
Report is 124 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2952   +/-   ##
=======================================
  Coverage   96.36%   96.36%           
=======================================
  Files          52       52           
  Lines        8745     8747    +2     
  Branches     1589     1591    +2     
=======================================
+ Hits         8427     8429    +2     
  Misses        190      190           
  Partials      128      128           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thomas-forte thomas-forte changed the title Fixing reference of value with str.__new__ output, updating type annotation on update_page_form_field_values BUG: Fixed reference of value with str.__new__ in TextStringObject Nov 18, 2024
@stefan6419846
Copy link
Collaborator

Could you please add a test which shows why this change is necessary?

@stefan6419846 stefan6419846 added the needs-test A test should be added before this PR is merged. label Nov 23, 2024
@PJBrs
Copy link

PJBrs commented Nov 29, 2024

I was running into the same issue and this PR fixes it. The code I'm working on sometimes writes an int value into a form, which up until recently worked without problem. I'd help out with writing a test, but my distro is pretty ancient in certain regards...

@stefan6419846
Copy link
Collaborator

As long as your distro supports running the latest pypdf version, id est at least Python 3.8, this should be no real issue in theory.

@PJBrs
Copy link

PJBrs commented Nov 29, 2024

... or maybe existing tests can be altered a bit, like this:

diff --git a/tests/test_writer.py b/tests/test_writer.py
index c1078226..b328b7d5 100644
--- a/tests/test_writer.py
+++ b/tests/test_writer.py
@@ -1458,7 +1458,7 @@ def test_update_form_fields(tmp_path):
             "RdoS1": "/",
             "Combo1": "!!monCombo!!",
             "Liste1": "Liste2",
-            "Liste2": ["Lst1", "Lst3"],
+            "Liste2": [int(1), float(2.8), "Lst3"],
             "DropList1": "DropListe3",
         },
         auto_regenerate=False,
@@ -1487,7 +1487,7 @@ def test_update_form_fields(tmp_path):
     assert flds["RadioGroup1"]["/V"] == "/2"
     assert flds["RadioGroup1"]["/Kids"][0].get_object()["/AS"] == "/Off"
     assert flds["RadioGroup1"]["/Kids"][1].get_object()["/AS"] == "/2"
-    assert all(x in flds["Liste2"]["/V"] for x in ["Lst1", "Lst3"])
+    assert all(x in flds["Liste2"]["/V"] for x in ["1", "2.8", "Lst3"])
 
     assert all(x in flds["CheckBox1"]["/_States_"] for x in ["/Off", "/Yes"])
     assert all(x in flds["RadioGroup1"]["/_States_"] for x in ["/1", "/2", "/3"])

@stefan6419846 Does this suffices? If so, @thomas-forte , please feel free to just add this to your PR.

@stefan6419846 I'm on Slackware64-15.0 with python 3.9. Tests failed first with:
pytest3: error: unrecognized arguments: --disable-socket

and if I disable that option, I get:

E pytest.PytestUnknownMarkWarning: Unknown pytest.mark.timeout

Anyway, with more commenting out I was able to cobble up and test the above :-)

@stefan6419846
Copy link
Collaborator

I have just added some even simpler tests to the TextStringObject directly as mypy validation is not running on the tests anyway.

Regarding your test issues: Please make sure to install all required dependencies using pip install -r requirements/ci.txt beforehand. We depend on some pytest plugins which are reported as missing in your case.

@stefan6419846 stefan6419846 merged commit a1d5de1 into py-pdf:main Nov 29, 2024
16 checks passed
@stefan6419846 stefan6419846 removed the needs-test A test should be added before this PR is merged. label Nov 29, 2024
@thomas-forte
Copy link
Contributor Author

I'm sorry I wasn't on top of this, was dealing with family and holidays, but thank you for adding that simple test!

For me the type annotations and docstring's were not showing for this libraries context in my IDE. So while I was supplying an int or float sometimes I had no idea that was incorrect. It still worked on the previous releases, my app was using 3.9.1, before updating. Specifically it was introduced on the UTF16_BOM pull request when the new method was added.

stefan6419846 added a commit that referenced this pull request Jan 26, 2025
## What's new

### Deprecations (DEP)
- Deprecate with replacement CCITParameters (#3019) by @j-t-1
- Correct deprecation of interiour_color (#2947) by @j-t-1

### New Features (ENH)
- Support alternative (U)F names for embedded file retrieval (#3072) by @stefan6419846
- Adding support for reading .metadata.keywords (#2939) by @Lucas-C

### Bug Fixes (BUG)
- Handle further Tf operators in text extraction layout mode (#3073) by @blushingpenguin
- Ensure `add_metadata` can deal with `_info = None` (#3040) by @xmo-odoo
- Handle IndirectObject in CCITTFaxDecode filter (#2965) by @stefan6419846
- Handle chained colorspace for inline images when no filter is set (#3008) by @stefan6419846
- Avoid extracting inline images twice and dropping other operators (#3002) by @stefan6419846
- Fixed reference of value with `str.__new__` in TextStringObject (#2952) by @thomas-forte
- Handle indirect objects in font width calculations (#2967) by @nsw42
- Title sometimes is bytes and not str (#2930) by @reformy
- Fix undefined variable for text extraction (regression) (#2934) by @stefan6419846
- Don't close stream passed to PdfWriter.write() (#2909) by @alexaryn

### Robustness (ROB)
- Handle zero height fonts when extracting text (#3075) by @blushingpenguin
- Deal with content streams not containing streams (#3005) by @stefan6419846
- Gracefully handle some text operators when the operands are missing (#3006) by @stefan6419846
- Fall back to non-Adobe Ascii85 format for missing end markers (#3007) by @stefan6419846
- Ignore odd-length strings when processing cmap lines (#3009) by @stefan6419846
- Skip annotation destination being NullObject in PdfWriter (#2964) by @stefan6419846
- Skip destination page being None in PdfWriter (#2963) by @dxsooo
- Fix infinite loop case when reading null objects within an Array by @jakep-allenai
- Fixing infinite loop in ArrayObject read_from_stream (#2928) by @jakep-allenai

### Documentation (DOC)
- Add note about default line colors (#3014) by @stefan6419846

### Developer Experience (DEV)
- Remove ignoring Ruff rule PGH004 (#3071) by @j-t-1
- Tidy ignore array in tool.ruff.lint (#3069) by @j-t-1
- Move Windows CI to Python 3.13 (#3003) by @stefan6419846
- Move to Ubuntu 22.04 (#3004) by @stefan6419846

### Maintenance (MAINT)
- Fix formatting of warning message and include exception message (#3076) by @stefan6419846
- Narrow return type for `ContentStream.operations` (#2941) by @kmurphy4

### Testing (TST)
- Fix image similarity for upcoming Ubuntu 24.04 (#3039) by @stefan6419846
- Replace broken Apache Tika Corpora urls (#3041) by @stefan6419846

### Code Style (STY)
- Add form feed to WHITESPACES (#3054) by @j-t-1
- Lots of small internal changes by @j-t-1

[Full Changelog](5.1.0...5.2.0)

Signed-off-by: stefan6419846 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants