Repository navigation
TYP,BLD: Fix backend_pdf stub errors and install it - #32586
Conversation
| def savefig( | ||
| self, | ||
| fname: str | os.PathLike | IO, | ||
| fname: str | os.PathLike | IO | PdfPages, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ok, so do i drop both commits : fname changes to this figure.pyi as well as pyplot.py ?
b19f8ec to
0fa049d
Compare
PR summary
This PR is follow-up to #32481, which installed
registry.pyiand leftbackend_pdf.pyiout because its errors would reject valid code.It fixes those errors and adds
backend_pdf.pyitotyping_sources.Fixes
pdf.savefig(fig, dpi=300)**kwargs: dict[str, Any]→**kwargs: Anyfig.savefig(pdf),plt.savefig(pdf)PdfPagesasfname, as theFigure.savefigdocstring saysPdfPages(f, metadata={"CreationDate": None})(documented) andPdfPages(f, metadata=md)withmd: dict[str, str]metadataasMapping[str, str | datetime | Name | None];infodict()still returns adictgc.paint(fill_rule=...),Op.paint_path(..., fill_rule=...)fill_rule(from #32253)Op.paint_path(...).pdfRepr()bytes→Op, as at runtimeOp.fill_evenodd,Op.fill_evenodd_strokefrom matplotlib.backends.backend_pdf import FigureManagerPdfI 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