From cdcf264d07f826bb1c87ca170145fcf03136d613 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 26 Jun 2022 20:53:29 +0530 Subject: [PATCH] fpdf2: improve types (#8176) Co-authored-by: hauntsaninja <> --- stubs/fpdf2/fpdf/fpdf.pyi | 166 ++++++++++++++++++------------------ stubs/fpdf2/fpdf/syntax.pyi | 15 ++-- 2 files changed, 92 insertions(+), 89 deletions(-) diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi index 220fa07b2..f7b80a2cb 100644 --- a/stubs/fpdf2/fpdf/fpdf.pyi +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -1,12 +1,11 @@ import datetime -from _typeshed import StrPath +from _typeshed import Incomplete, StrPath from collections import defaultdict from collections.abc import Callable from contextlib import _GeneratorContextManager from enum import IntEnum from io import BytesIO -from pathlib import Path -from typing import Any, NamedTuple, overload +from typing import Any, ClassVar, NamedTuple, overload from typing_extensions import Literal, TypeAlias from PIL import Image @@ -57,11 +56,10 @@ class ToCPlaceholder(NamedTuple): class SubsetMap: def __init__(self, identities: list[int]) -> None: ... - def pick(self, unicode: int): ... - def dict(self): ... + def pick(self, unicode: int) -> int: ... + def dict(self) -> dict[int, int]: ... def get_page_format(format: _Format | tuple[float, float], k: float | None = ...) -> tuple[float, float]: ... -def load_cache(filename: Path): ... # TODO: TypedDicts _Page: TypeAlias = dict[str, Any] @@ -70,9 +68,9 @@ _FontFile: TypeAlias = dict[str, Any] _Image: TypeAlias = dict[str, Any] class FPDF: - MARKDOWN_BOLD_MARKER: str - MARKDOWN_ITALICS_MARKER: str - MARKDOWN_UNDERLINE_MARKER: str + MARKDOWN_BOLD_MARKER: ClassVar[str] + MARKDOWN_ITALICS_MARKER: ClassVar[str] + MARKDOWN_UNDERLINE_MARKER: ClassVar[str] offsets: dict[int, int] page: int n: int @@ -91,7 +89,6 @@ class FPDF: font_family: str font_style: str font_size_pt: int - font_stretching: int str_alias_nb_pages: str underline: int draw_color: str @@ -99,31 +96,26 @@ class FPDF: text_color: str ws: int angle: int - font_cache_dir: Any - xmp_metadata: Any + xmp_metadata: str | None image_filter: str page_duration: int - page_transition: Any - struct_builder: Any - section_title_styles: Any - core_fonts: Any + page_transition: Incomplete | None + struct_builder: Incomplete + section_title_styles: Incomplete + core_fonts: Incomplete core_fonts_encoding: str - font_aliases: Any + font_aliases: Incomplete k: float - def_orientation: Any - font_size: Any - c_margin: Any + def_orientation: Incomplete + font_size: float + c_margin: float line_width: float dw_pt: float dh_pt: float - compress: bool pdf_version: str x: float y: float - t_margin: float - r_margin: float - l_margin: float # Set during call to _set_orientation(), called from __init__(). cur_orientation: Literal["P", "L"] @@ -139,43 +131,50 @@ class FPDF: font_cache_dir: bool = ..., ) -> None: ... @property - def unifontsubset(self): ... + def unifontsubset(self) -> bool: ... @property - def epw(self): ... + def epw(self) -> float: ... @property - def eph(self): ... + def eph(self) -> float: ... def set_margin(self, margin: float) -> None: ... def set_margins(self, left: float, top: float, right: float = ...) -> None: ... + l_margin: float def set_left_margin(self, margin: float) -> None: ... + t_margin: float def set_top_margin(self, margin: float) -> None: ... + r_margin: float def set_right_margin(self, margin: float) -> None: ... - auto_page_break: Any - b_margin: Any - page_break_trigger: Any + auto_page_break: bool + b_margin: float + page_break_trigger: float def set_auto_page_break(self, auto: bool, margin: float = ...) -> None: ... - zoom_mode: Any - layout_mode: Any - def set_display_mode(self, zoom, layout: str = ...) -> None: ... - def set_compression(self, compress) -> None: ... - title: Any + zoom_mode: Literal["fullpage", "fullwidth", "real", "default"] | float + def set_display_mode( + self, + zoom: Literal["fullpage", "fullwidth", "real", "default"] | float, + layout: Literal["single", "continuous", "two", "default"] = ..., + ) -> None: ... + compress: bool + def set_compression(self, compress: bool) -> None: ... + title: str def set_title(self, title: str) -> None: ... - lang: Any + lang: str def set_lang(self, lang: str) -> None: ... - subject: Any + subject: str def set_subject(self, subject: str) -> None: ... - author: Any + author: str def set_author(self, author: str) -> None: ... - keywords: Any + keywords: str def set_keywords(self, keywords: str) -> None: ... - creator: Any + creator: str def set_creator(self, creator: str) -> None: ... - producer: Any + producer: str def set_producer(self, producer: str) -> None: ... - creation_date: Any - def set_creation_date(self, date: datetime.datetime | None = ...) -> None: ... - def set_xmp_metadata(self, xmp_metadata) -> None: ... - def set_doc_option(self, opt, value) -> None: ... - def set_image_filter(self, image_filter) -> None: ... + creation_date: datetime.datetime | bool | None + def set_creation_date(self, date: datetime.datetime | bool | None = ...) -> None: ... + def set_xmp_metadata(self, xmp_metadata: str) -> None: ... + def set_doc_option(self, opt: str, value: str) -> None: ... + def set_image_filter(self, image_filter: str) -> None: ... def alias_nb_pages(self, alias: str = ...) -> None: ... def open(self) -> None: ... def close(self) -> None: ... @@ -185,37 +184,38 @@ class FPDF: format: _Format | tuple[float, float] = ..., same: bool = ..., duration: int = ..., - transition: Any | None = ..., + transition: Incomplete | None = ..., ) -> None: ... def header(self) -> None: ... def footer(self) -> None: ... def page_no(self) -> int: ... - def set_draw_color(self, r, g: int = ..., b: int = ...) -> None: ... - def set_fill_color(self, r, g: int = ..., b: int = ...) -> None: ... - def set_text_color(self, r, g: int = ..., b: int = ...) -> None: ... - def get_string_width(self, s, normalized: bool = ..., markdown: bool = ...): ... + def set_draw_color(self, r: int, g: int = ..., b: int = ...) -> None: ... + def set_fill_color(self, r: int, g: int = ..., b: int = ...) -> None: ... + def set_text_color(self, r: int, g: int = ..., b: int = ...) -> None: ... + def get_string_width(self, s: str, normalized: bool = ..., markdown: bool = ...) -> float: ... def set_line_width(self, width: float) -> None: ... def line(self, x1: float, y1: float, x2: float, y2: float) -> None: ... - def polyline(self, point_list, fill: bool = ..., polygon: bool = ...) -> None: ... - def polygon(self, point_list, fill: bool = ...) -> None: ... + def polyline(self, point_list: list[tuple[float, float]], fill: bool = ..., polygon: bool = ...) -> None: ... + def polygon(self, point_list: list[tuple[float, float]], fill: bool = ...) -> None: ... def dashed_line(self, x1, y1, x2, y2, dash_length: int = ..., space_length: int = ...) -> None: ... - def rect(self, x, y, w, h, style: Any | None = ...) -> None: ... - def ellipse(self, x, y, w, h, style: Any | None = ...) -> None: ... - def circle(self, x, y, r, style: Any | None = ...) -> None: ... + def rect(self, x: float, y: float, w: float, h: float, style: Incomplete | None = ...) -> None: ... + def ellipse(self, x: float, y: float, w: float, h: float, style: Incomplete | None = ...) -> None: ... + def circle(self, x: float, y: float, r, style: Incomplete | None = ...) -> None: ... def add_font(self, family: str, style: _FontStyle = ..., fname: str | None = ..., uni: bool = ...) -> None: ... def set_font(self, family: str | None = ..., style: _FontStyles = ..., size: int = ...) -> None: ... def set_font_size(self, size: int) -> None: ... - def set_stretching(self, stretching) -> None: ... - def add_link(self): ... - def set_link(self, link, y: int = ..., x: int = ..., page: int = ..., zoom: str = ...) -> None: ... - def link(self, x, y, w, h, link, alt_text: Any | None = ...) -> None: ... - def text_annotation(self, x, y, text) -> None: ... - def add_action(self, action, x, y, w, h) -> None: ... - def text(self, x, y, txt: str = ...) -> None: ... - def rotate(self, angle, x: Any | None = ..., y: Any | None = ...) -> None: ... - def rotation(self, angle, x: Any | None = ..., y: Any | None = ...) -> _GeneratorContextManager[None]: ... + font_stretching: float + def set_stretching(self, stretching: float) -> None: ... + def add_link(self) -> int: ... + def set_link(self, link, y: int = ..., x: int = ..., page: int = ..., zoom: float | Literal["null"] = ...) -> None: ... + def link(self, x: float, y: float, w: float, h: float, link: str | int, alt_text: str | None = ...) -> Annotation: ... + def text_annotation(self, x: float, y: float, text: str) -> None: ... + def add_action(self, action, x: float, y: float, w: float, h: float) -> None: ... + def text(self, x: float, y: float, txt: str = ...) -> None: ... + def rotate(self, angle: float, x: float | None = ..., y: float | None = ...) -> None: ... + def rotation(self, angle: float, x: float | None = ..., y: float | None = ...) -> _GeneratorContextManager[None]: ... @property - def accept_page_break(self): ... + def accept_page_break(self) -> bool: ... def cell( self, w: float | None = ..., @@ -228,8 +228,8 @@ class FPDF: link: str = ..., center: bool = ..., markdown: bool = ..., - ): ... - def will_page_break(self, height): ... + ) -> bool: ... + def will_page_break(self, height: float) -> bool: ... def multi_cell( self, w: float, @@ -241,10 +241,10 @@ class FPDF: split_only: bool = ..., link: str = ..., ln: int = ..., - max_line_height: Any | None = ..., + max_line_height: float | None = ..., markdown: bool = ..., ): ... - def write(self, h: Any | None = ..., txt: str = ..., link: str = ...) -> None: ... + def write(self, h: float | None = ..., txt: str = ..., link: str = ...) -> None: ... def image( self, name: str | Image.Image | BytesIO | StrPath, @@ -257,7 +257,7 @@ class FPDF: title: str | None = ..., alt_text: str | None = ..., ) -> _Image: ... - def ln(self, h: Any | None = ...) -> None: ... + def ln(self, h: float | None = ...) -> None: ... def get_x(self) -> float: ... def set_x(self, x: float) -> None: ... def get_y(self) -> float: ... @@ -267,20 +267,20 @@ class FPDF: def output(self, name: Literal[""] = ...) -> bytearray: ... # type: ignore[misc] @overload def output(self, name: str) -> None: ... - def normalize_text(self, txt): ... - def interleaved2of5(self, txt, x, y, w: int = ..., h: int = ...) -> None: ... - def code39(self, txt, x, y, w: float = ..., h: int = ...) -> None: ... - def rect_clip(self, x, y, w, h) -> _GeneratorContextManager[None]: ... + def normalize_text(self, txt: str) -> str: ... + def interleaved2of5(self, txt, x: float, y: float, w: float = ..., h: float = ...) -> None: ... + def code39(self, txt, x: float, y: float, w: float = ..., h: float = ...) -> None: ... + def rect_clip(self, x: float, y: float, w: float, h: float) -> _GeneratorContextManager[None]: ... def unbreakable(self) -> _GeneratorContextManager[FPDFRecorder]: ... def insert_toc_placeholder(self, render_toc_function, pages: int = ...) -> None: ... def set_section_title_styles( self, - level0, - level1: Any | None = ..., - level2: Any | None = ..., - level3: Any | None = ..., - level4: Any | None = ..., - level5: Any | None = ..., - level6: Any | None = ..., + level0: TitleStyle, + level1: TitleStyle | None = ..., + level2: TitleStyle | None = ..., + level3: TitleStyle | None = ..., + level4: TitleStyle | None = ..., + level5: TitleStyle | None = ..., + level6: TitleStyle | None = ..., ) -> None: ... - def start_section(self, name, level: int = ...) -> None: ... + def start_section(self, name: str, level: int = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/syntax.pyi b/stubs/fpdf2/fpdf/syntax.pyi index 838880de0..eccb1b845 100644 --- a/stubs/fpdf2/fpdf/syntax.pyi +++ b/stubs/fpdf2/fpdf/syntax.pyi @@ -1,5 +1,6 @@ from abc import ABC from typing import Any +from typing_extensions import Literal def clear_empty_fields(d): ... def create_dictionary_string( @@ -36,10 +37,12 @@ class Destination(ABC): def as_str(self, pdf: Any | None = ...) -> None: ... class DestinationXYZ(Destination): - page: Any - x: Any - y: Any - zoom: Any - page_as_obj_id: Any - def __init__(self, page, x: int = ..., y: int = ..., zoom: str = ..., page_as_obj_id: bool = ...) -> None: ... + page: int + x: float + y: float + zoom: float | Literal["null"] + page_as_obj_id: bool + def __init__( + self, page: int, x: float = ..., y: float = ..., zoom: float | Literal["null"] = ..., page_as_obj_id: bool = ... + ) -> None: ... def as_str(self, pdf: Any | None = ...): ...