Typing stubs for ReportLab
types-reportlab provides static type checking annotations for the ReportLab library, enabling IDEs and type checkers (like Mypy or Pyright) to offer better autocompletion, error detection, and code understanding. It is part of the typeshed project, which collects high-quality type stubs for various Python packages. The current version, 4.4.10.20260408, aims to provide accurate annotations for reportlab==4.4.10 and is regularly updated as part of the typeshed release cycle, often reflecting changes in the upstream ReportLab library.
Warnings
- gotcha Installing `types-reportlab` alone does NOT provide runtime functionality. It only provides type annotations. You must install the actual `reportlab` library (and its dependencies like `Pillow` for images) separately for your code to execute.
- gotcha Type checking failures may occur if the version of `types-reportlab` does not align with your installed `reportlab` version. `types-reportlab` versions are specifically tailored to a corresponding `reportlab` version (e.g., `4.4.10.20260408` targets `reportlab==4.4.10`).
- breaking While stub packages themselves don't typically 'break' runtime code, updates to `typeshed` stubs can introduce stricter type checking rules or corrections that might cause existing code to fail type checking, even if the runtime behavior of `reportlab` hasn't changed.
- gotcha ReportLab's documentation can sometimes be challenging to navigate, with some examples considered outdated or incomplete by community members, leading to difficulties in finding specific patterns or features.
Install
-
pip install types-reportlab -
pip install reportlab Pillow
Imports
- Canvas
from reportlab.pdfgen.canvas import Canvas
- SimpleDocTemplate
from reportlab.platypus import SimpleDocTemplate
- Paragraph
from reportlab.platypus import Paragraph
- Table
from reportlab.platypus import Table
- getSampleStyleSheet
from reportlab.lib.styles import getSampleStyleSheet
- letter
from reportlab.lib.pagesizes import letter
- colors
from reportlab.lib import colors
Quickstart
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter
from typing import List
def generate_simple_pdf(filename: str, title_text: str, paragraph_content: str) -> None:
"""Generates a simple PDF document using ReportLab with type hints."""
doc: SimpleDocTemplate = SimpleDocTemplate(filename, pagesize=letter)
story: List[Any] = [] # Use Any for flowables for simplicity here
styles = getSampleStyleSheet()
# Add a title
title_style = styles['h1']
title: Paragraph = Paragraph(title_text, title_style)
story.append(title)
story.append(Spacer(1, 0.2 * inch)) # type: ignore
# Add a paragraph
body_style = styles['Normal']
paragraph: Paragraph = Paragraph(paragraph_content, body_style)
story.append(paragraph)
doc.build(story)
print(f"PDF '{filename}' generated successfully.")
if __name__ == "__main__":
from reportlab.lib.units import inch # type: ignore
generate_simple_pdf(
"hello_typed_report.pdf",
"My Typed ReportLab Document",
"This is a sample paragraph for a PDF generated using ReportLab, "
"with type checking enhanced by types-reportlab stubs."
)