Skip to content

TYP,BLD: Fix backend_pdf stub errors and install it - #32586

Merged
iccir merged 2 commits into
matplotlib:mainfrom
maaz7409:typ-fix-install-backend-pdf-stub
Nov 12, 2026
Merged

iccir merged 2 commits into
matplotlib:mainfrom
maaz7409:typ-fix-install-backend-pdf-stub

Conversation

@maaz7409

@maaz7409 maaz7409 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

PR summary

This PR is follow-up to #32481, which installed registry.pyi and left
backend_pdf.pyi out because its errors would reject valid code.
It fixes those errors and adds backend_pdf.pyi to typing_sources.

Fixes
Valid code rejected by the stub Fix
pdf.savefig(fig, dpi=300) **kwargs: dict[str, Any] → **kwargs: Any
fig.savefig(pdf), plt.savefig(pdf) accept PdfPages as fname, as the Figure.savefig docstring says
PdfPages(f, metadata={"CreationDate": None}) (documented) and PdfPages(f, metadata=md) with md: dict[str, str] type metadata as Mapping[str, str | datetime | Name | None]; infodict() still returns a dict
gc.paint(fill_rule=...), Op.paint_path(..., fill_rule=...) add fill_rule (from #32253)
Op.paint_path(...).pdfRepr() return type bytes → Op, as at runtime
Op.fill_evenodd, Op.fill_evenodd_stroke add the members (from #32253)
from matplotlib.backends.backend_pdf import FigureManagerPdf add the alias

I checked with mypy and stubtest (16 → 1 mypy errors on valid usage, 23 → 18 stubtest differences, none from the changed items)

Also tests : ruff, CI's mypy run, test_backend_pdf.py and test_pyplot_up_to_date pass.

AI Disclosure

Claude Code ( Claude Opus 5.5 (high) ) assisted me in writing the fix.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • [N/A] New and changed code is tested (stub-only change)
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • [N/A] Documentation complies with general and docstring guidelines

Comment thread lib/matplotlib/figure.pyi Outdated
def savefig(
self,
fname: str | os.PathLike | IO,
fname: str | os.PathLike | IO | PdfPages,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's more complicated than that. fname is passed on to self.canvas.print_figure, and FigureCanvasBase.print_figure does not accept PdfPages either. Mypy doesn't flag this only because, with figure.pyi present, the implementation of Figure.savefig is never type-checked against the stubs.

The type system can't reasonably express the current situation, where the types savefig accepts depend on the runtime type of another object (figure.canvas). Adding PdfPages to FigureCanvasBase.print_figure would mean adding it to all canvas subclasses as well: too permissive, cumbersome, and ultimately impractical for third-party backends.

T.b.h. I just wouldn't bother with the type imperfection.

The backend_pdf.pyi changes themselves seem ok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, so do i drop both commits : fname changes to this figure.pyi as well as pyplot.py ?

@iccir
iccir merged commit 9ffacb5 into matplotlib:main Nov 12, 2026
42 checks passed
@QuLogic QuLogic added this to the v3.12.0 milestone Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants