Skip to content

BUG: Check the page label style and type it as the strings it accepts - #4017

Merged
stefan6419846 merged 3 commits into
py-pdf:mainfrom
RavSinghChandan:validate-page-label-style
Aug 27, 2026
Merged

stefan6419846 merged 3 commits into
py-pdf:mainfrom
RavSinghChandan:validate-page-label-style

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

set_page_label validates its indices, its start and that a style or prefix is given, but not the style itself:

>>> writer.set_page_label(0, 2, "/Nonsense")
>>> PdfReader(out).page_labels
['1', '2', '3']  # the style was dropped

index2label logs "Ignoring unknown page label numbering style" and falls back to the page position, so the caller is left with plain numbers and a log line.

The style was also annotated Optional[PageLabelStyle], which is a plain class holding the five string constants rather than a type any caller can satisfy - every caller passes "/D". Typed as the literal strings it takes, which clears two typeguard failures.

Reverting the change fails the four new tests

@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.00%. Comparing base (0b0721e) to head (dc5edf8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4017   +/-   ##
=======================================
  Coverage   98.00%   98.00%           
=======================================
  Files          57       57           
  Lines       11143    11145    +2     
  Branches     2091     2092    +1     
=======================================
+ Hits        10921    10923    +2     
  Misses        124      124           
  Partials       98       98           

☔ View full report in Codecov by Harness.
📢 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.

@RavSinghChandan
RavSinghChandan force-pushed the validate-page-label-style branch 2 times, most recently from 83638c4 to e92fe87 Compare August 26, 2026 11:54
Comment thread pypdf/_writer.py Resolved
set_page_label validates its indices, its start and that a style or prefix is
given, but not the style itself, so an unknown one was written into /S and
then dropped on the way back out: index2label logs 'Ignoring unknown page
label numbering style' and falls back to the page position, leaving the caller
with plain numbers and a log line.

The style was also annotated Optional[PageLabelStyle], which is a plain class
holding the five string constants rather than a type any caller can satisfy.
Typed as the literal strings it takes, which clears two typeguard failures.
The class held the five style strings but was used as a type, so the
annotation could not be satisfied by the strings every caller passes. Making
it a StrEnum, as FilterTypes already is, lets the annotation name it directly
and the validation check membership, so the separate Literal alias goes away.
@RavSinghChandan
RavSinghChandan force-pushed the validate-page-label-style branch from e92fe87 to e1156e1 Compare August 27, 2026 02:40
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Agreed, that's the better fix. PageLabelStyle is a StrEnum now like FilterTypes, so the annotation can name it directly and the separate alias is gone.

stefan6419846
stefan6419846 previously approved these changes Aug 27, 2026
@stefan6419846
stefan6419846 merged commit 13b5ffc into py-pdf:main Aug 27, 2026
32 of 34 checks passed
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.

2 participants