Skip to content

Conversation

@dev-KingMaster
Copy link

@dev-KingMaster dev-KingMaster commented Nov 5, 2025

Summary

  • Removes PLW2901 ignore and fixes all instances where loop or context-manager variables were overwritten, improving readability and avoiding subtle bugs.
  • Why: Aligns with Ruff “no var overwrite” guidance; safer, clearer code; progresses Cleanup ruff ignores #3327.
  • Scope: Touches core modules (e.g., pypdf/_cmap.py, _doc_common.py, _page.py, _writer.py, _text_extraction/_layout_mode/_font.py, filters.py, generic/_files.py) and a few tests where the pattern occurred.
  • Approach: Introduces new local names (*_obj, *_val, operands_for_tm, etc.) instead of reassigning loop/context variables; no behavioral changes.
  • Config: Removes "PLW2901" from [tool.ruff.lint].ignore in pyproject.toml.
  • Validation: ruff check passes cleanly with PLW2901 enforced. No functional logic altered; tests adjusted only for variable naming where needed.

Contribution by Gittensor, learn more at https://gittensor.io/

@dev-KingMaster dev-KingMaster changed the title Ruff cleanup: enforce PLW2901 (no loop/context var overwrite) MAINT: Enforce PLW2901 (avoid loop/context var overwrite) (#3327) Nov 5, 2025
@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

❌ Patch coverage is 98.14815% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.11%. Comparing base (85b53d8) to head (d1e8189).

Files with missing lines Patch % Lines
pypdf/_doc_common.py 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3513   +/-   ##
=======================================
  Coverage   97.11%   97.11%           
=======================================
  Files          57       57           
  Lines        9713     9714    +1     
  Branches     1758     1756    -2     
=======================================
+ Hits         9433     9434    +1     
  Misses        168      168           
  Partials      112      112           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stefan6419846 stefan6419846 changed the title MAINT: Enforce PLW2901 (avoid loop/context var overwrite) (#3327) MAINT: Enforce PLW2901 (avoid loop/context var overwrite) Nov 5, 2025
Copy link
Collaborator

@stefan6419846 stefan6419846 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I have left some remarks for specific changes and some general ones.

@dev-KingMaster
Copy link
Author

Hi @stefan6419846 I addressed all the comments.

pypdf/filters.py Outdated
if isinstance(params, NullObject):
params = {}
for filter_name, params_untyped in zip(filters, decode_parms):
params_typed: Optional[DictionaryObject] = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not params directly to reduce the diff?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you misunderstood me here: I meant to use params_untyped as the loop variable and params as the name for the typed one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. Fixed this now

@dev-KingMaster
Copy link
Author

Resolved all comments. @stefan6419846

# CID fonts have a /W array mapping character codes to widths stashed in /DescendantFonts
if "/DescendantFonts" in self.font_dictionary:
d_font: dict[Any, Any]
d_font: Any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy flagged "dict[Any, Any]" has no attribute "get_object".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you should probably make this a DictionaryObject or PdfObject.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, added PdfObject for that

@stefan6419846 stefan6419846 added the needs-change The PR/issue cannot be handled as issue and needs to be improved label Nov 10, 2025
@dev-KingMaster
Copy link
Author

Addressed comments. @stefan6419846

data = JBIG2Decode.decode(data, params)
elif filter_name == "/Crypt":
if "/Name" in params or "/Type" in params:
if "/Name" in params_untyped or "/Type" in params_untyped:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why untyped? Couldn't we just keep params here, which makes it even more obvious as it is a dictionary object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-change The PR/issue cannot be handled as issue and needs to be improved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants