From 78d0332a7c01a46383be795bcbe2a4898ecf69e0 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 14:35:51 +0100 Subject: [PATCH 01/11] Generate fpdf2 stubs via stubgen --- stubs/fpdf2/METADATA.toml | 1 + stubs/fpdf2/fpdf/__init__.pyi | 11 ++ stubs/fpdf2/fpdf/actions.pyi | 27 ++++ stubs/fpdf2/fpdf/deprecation.pyi | 5 + stubs/fpdf2/fpdf/errors.pyi | 9 ++ stubs/fpdf2/fpdf/fonts.pyi | 4 + stubs/fpdf2/fpdf/fpdf.pyi | 193 ++++++++++++++++++++++++++++ stubs/fpdf2/fpdf/html.pyi | 65 ++++++++++ stubs/fpdf2/fpdf/image_parsing.pyi | 7 + stubs/fpdf2/fpdf/outline.pyi | 31 +++++ stubs/fpdf2/fpdf/recorder.pyi | 14 ++ stubs/fpdf2/fpdf/structure_tree.pyi | 42 ++++++ stubs/fpdf2/fpdf/syntax.pyi | 38 ++++++ stubs/fpdf2/fpdf/template.pyi | 25 ++++ stubs/fpdf2/fpdf/transitions.pyi | 58 +++++++++ stubs/fpdf2/fpdf/ttfonts.pyi | 73 +++++++++++ stubs/fpdf2/fpdf/util.pyi | 9 ++ 17 files changed, 612 insertions(+) create mode 100644 stubs/fpdf2/METADATA.toml create mode 100644 stubs/fpdf2/fpdf/__init__.pyi create mode 100644 stubs/fpdf2/fpdf/actions.pyi create mode 100644 stubs/fpdf2/fpdf/deprecation.pyi create mode 100644 stubs/fpdf2/fpdf/errors.pyi create mode 100644 stubs/fpdf2/fpdf/fonts.pyi create mode 100644 stubs/fpdf2/fpdf/fpdf.pyi create mode 100644 stubs/fpdf2/fpdf/html.pyi create mode 100644 stubs/fpdf2/fpdf/image_parsing.pyi create mode 100644 stubs/fpdf2/fpdf/outline.pyi create mode 100644 stubs/fpdf2/fpdf/recorder.pyi create mode 100644 stubs/fpdf2/fpdf/structure_tree.pyi create mode 100644 stubs/fpdf2/fpdf/syntax.pyi create mode 100644 stubs/fpdf2/fpdf/template.pyi create mode 100644 stubs/fpdf2/fpdf/transitions.pyi create mode 100644 stubs/fpdf2/fpdf/ttfonts.pyi create mode 100644 stubs/fpdf2/fpdf/util.pyi diff --git a/stubs/fpdf2/METADATA.toml b/stubs/fpdf2/METADATA.toml new file mode 100644 index 000000000000..bcb78f17fb41 --- /dev/null +++ b/stubs/fpdf2/METADATA.toml @@ -0,0 +1 @@ +version = "2.4.*" diff --git a/stubs/fpdf2/fpdf/__init__.pyi b/stubs/fpdf2/fpdf/__init__.pyi new file mode 100644 index 000000000000..10827ff84975 --- /dev/null +++ b/stubs/fpdf2/fpdf/__init__.pyi @@ -0,0 +1,11 @@ +from .fpdf import FPDF as FPDF, TitleStyle as TitleStyle +from .html import HTML2FPDF as HTML2FPDF, HTMLMixin as HTMLMixin +from .template import Template as Template +from typing import Any + +FPDF_VERSION: Any +FPDF_FONT_DIR: Any + +# Names in __all__ with no definition: +# __license__ +# __version__ diff --git a/stubs/fpdf2/fpdf/actions.pyi b/stubs/fpdf2/fpdf/actions.pyi new file mode 100644 index 000000000000..b48ae31e6792 --- /dev/null +++ b/stubs/fpdf2/fpdf/actions.pyi @@ -0,0 +1,27 @@ +from .util import enclose_in_parens as enclose_in_parens +from abc import ABC +from typing import Any + +class Action(ABC): + def dict_as_string(self) -> None: ... + +class NamedAction(Action): + action_name: Any + def __init__(self, action_name) -> None: ... + def dict_as_string(self): ... + +class GoToAction(Action): + dest: Any + def __init__(self, dest) -> None: ... + def dict_as_string(self): ... + +class GoToRemoteAction(Action): + file: Any + dest: Any + def __init__(self, file, dest) -> None: ... + def dict_as_string(self): ... + +class LaunchAction(Action): + file: Any + def __init__(self, file) -> None: ... + def dict_as_string(self): ... diff --git a/stubs/fpdf2/fpdf/deprecation.pyi b/stubs/fpdf2/fpdf/deprecation.pyi new file mode 100644 index 000000000000..044aa1cf795d --- /dev/null +++ b/stubs/fpdf2/fpdf/deprecation.pyi @@ -0,0 +1,5 @@ +from types import ModuleType + +class WarnOnDeprecatedModuleAttributes(ModuleType): + def __getattr__(self, name): ... + def __setattr__(self, name, value) -> None: ... diff --git a/stubs/fpdf2/fpdf/errors.pyi b/stubs/fpdf2/fpdf/errors.pyi new file mode 100644 index 000000000000..637a55e25159 --- /dev/null +++ b/stubs/fpdf2/fpdf/errors.pyi @@ -0,0 +1,9 @@ +from typing import Any + +class FPDFException(Exception): ... + +class FPDFPageFormatException(FPDFException): + argument: Any + unknown: Any + one: Any + def __init__(self, argument, unknown: bool = ..., one: bool = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/fonts.pyi b/stubs/fpdf2/fpdf/fonts.pyi new file mode 100644 index 000000000000..5a894fff6278 --- /dev/null +++ b/stubs/fpdf2/fpdf/fonts.pyi @@ -0,0 +1,4 @@ +from typing import Any + +courier: Any +fpdf_charwidths: Any diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi new file mode 100644 index 000000000000..a9e6253d982c --- /dev/null +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -0,0 +1,193 @@ +from .actions import Action +from collections.abc import Generator +from enum import IntEnum +from pathlib import Path +from typing import Any, Callable, List, NamedTuple, Optional, Union + +PAGE_FORMATS: Any + +class DocumentState(IntEnum): + UNINITIALIZED: int + READY: int + GENERATING_PAGE: int + CLOSED: int + +class Annotation(NamedTuple): + type: str + x: int + y: int + width: int + height: int + contents: str + link: Union[str, int] + alt_text: Optional[str] + action: Optional[Action] + +class TitleStyle(NamedTuple): + font_family: Optional[str] + font_style: Optional[str] + font_size_pt: Optional[int] + color: Union[int, tuple] + underline: bool + t_margin: Optional[int] + l_margin: Optional[int] + b_margin: Optional[int] + +class ToCPlaceholder(NamedTuple): + render_function: Callable + start_page: int + y: int + pages: int + +class SubsetMap: + def __init__(self, identities: List[int]) -> None: ... + def pick(self, unicode: int): ... + def dict(self): ... + +def get_page_format(format, k: Any | None = ...): ... +def load_cache(filename: Path): ... + +class FPDF: + MARKDOWN_BOLD_MARKER: str + MARKDOWN_ITALICS_MARKER: str + MARKDOWN_UNDERLINE_MARKER: str + offsets: Any + page: int + n: int + buffer: Any + pages: Any + state: Any + fonts: Any + font_files: Any + diffs: Any + images: Any + annots: Any + links: Any + in_footer: int + lasth: int + current_font: Any + font_family: str + font_style: str + font_size_pt: int + font_stretching: int + str_alias_nb_pages: str + underline: int + draw_color: str + fill_color: str + text_color: str + ws: int + angle: int + font_cache_dir: Any + xmp_metadata: Any + image_filter: str + page_duration: int + page_transition: Any + struct_builder: Any + section_title_styles: Any + core_fonts: Any + core_fonts_encoding: str + font_aliases: Any + k: Any + def_orientation: Any + font_size: Any + c_margin: Any + line_width: Any + compress: bool + pdf_version: str + def __init__(self, orientation: str = ..., unit: str = ..., format: str = ..., font_cache_dir: bool = ...) -> None: ... + @property + def unifontsubset(self): ... + @property + def epw(self): ... + @property + def eph(self): ... + def set_margin(self, margin) -> None: ... + y: Any + t_margin: Any + r_margin: Any + def set_margins(self, left, top, right: int = ...) -> None: ... + x: Any + l_margin: Any + def set_left_margin(self, margin) -> None: ... + def set_top_margin(self, margin) -> None: ... + def set_right_margin(self, margin) -> None: ... + auto_page_break: Any + b_margin: Any + page_break_trigger: Any + def set_auto_page_break(self, auto, margin: int = ...) -> None: ... + zoom_mode: Any + layout_mode: Any + def set_display_mode(self, zoom, layout: str = ...) -> None: ... + def set_compression(self, compress) -> None: ... + title: Any + def set_title(self, title) -> None: ... + lang: Any + def set_lang(self, lang) -> None: ... + subject: Any + def set_subject(self, subject) -> None: ... + author: Any + def set_author(self, author) -> None: ... + keywords: Any + def set_keywords(self, keywords) -> None: ... + creator: Any + def set_creator(self, creator) -> None: ... + producer: Any + def set_producer(self, producer) -> None: ... + creation_date: Any + def set_creation_date(self, date: Any | 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: ... + def alias_nb_pages(self, alias: str = ...) -> None: ... + def open(self) -> None: ... + def close(self) -> None: ... + def add_page(self, orientation: str = ..., format: str = ..., same: bool = ..., duration: int = ..., transition: Any | None = ...) -> None: ... + def header(self) -> None: ... + def footer(self) -> None: ... + def page_no(self): ... + 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_line_width(self, width) -> None: ... + def line(self, x1, y1, x2, y2) -> None: ... + def polyline(self, point_list, fill: bool = ..., polygon: bool = ...) -> None: ... + def polygon(self, point_list, 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 add_font(self, family, style: str = ..., fname: Any | None = ..., uni: bool = ...) -> None: ... + def set_font(self, family: Any | None = ..., style: str = ..., size: int = ...) -> None: ... + def set_font_size(self, size) -> 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 = ...) -> Generator[None, None, None]: ... + @property + def accept_page_break(self): ... + def cell(self, w: Any | None = ..., h: Any | None = ..., txt: str = ..., border: int = ..., ln: int = ..., align: str = ..., fill: bool = ..., link: str = ..., center: bool = ..., markdown: bool = ...): ... + def will_page_break(self, height): ... + def multi_cell(self, w, h: Any | None = ..., txt: str = ..., border: int = ..., align: str = ..., fill: bool = ..., split_only: bool = ..., link: str = ..., ln: int = ..., max_line_height: Any | None = ..., markdown: bool = ...): ... + def write(self, h: Any | None = ..., txt: str = ..., link: str = ...) -> None: ... + def image(self, name, x: Any | None = ..., y: Any | None = ..., w: int = ..., h: int = ..., type: str = ..., link: str = ..., title: Any | None = ..., alt_text: Any | None = ...): ... + def ln(self, h: Any | None = ...) -> None: ... + def get_x(self): ... + def set_x(self, x) -> None: ... + def get_y(self): ... + def set_y(self, y) -> None: ... + def set_xy(self, x, y) -> None: ... + def output(self, name: str = ..., dest: str = ...): ... + 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) -> Generator[None, None, None]: ... + def unbreakable(self) -> Generator[Any, None, None]: ... + 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 = ...) -> None: ... + def start_section(self, name, level: int = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/html.pyi b/stubs/fpdf2/fpdf/html.pyi new file mode 100644 index 000000000000..eb8048f1f58a --- /dev/null +++ b/stubs/fpdf2/fpdf/html.pyi @@ -0,0 +1,65 @@ +from html.parser import HTMLParser +from typing import Any + +LOGGER: Any +BULLET_WIN1252: str +DEFAULT_HEADING_SIZES: Any +COLOR_DICT: Any + +def px2mm(px): ... +def color_as_decimal(color: str = ...): ... + +class HTML2FPDF(HTMLParser): + pdf: Any + image_map: Any + li_tag_indent: Any + table_line_separators: Any + ul_bullet_char: Any + style: Any + href: str + align: str + page_links: Any + font_stack: Any + indent: int + bullet: Any + font_size: Any + font_color: Any + table: Any + table_col_width: Any + table_col_index: Any + td: Any + th: Any + tr: Any + thead: Any + tfoot: Any + tr_index: Any + theader: Any + tfooter: Any + theader_out: bool + table_row_height: int + heading_level: Any + heading_sizes: Any + def __init__(self, pdf, image_map: Any | None = ..., li_tag_indent: int = ..., table_line_separators: bool = ..., ul_bullet_char=..., heading_sizes: Any | None = ..., **_): ... + def width2unit(self, length): ... + def handle_data(self, data) -> None: ... + def box_shadow(self, w, h, bgcolor) -> None: ... + def output_table_header(self) -> None: ... + tfooter_out: bool + def output_table_footer(self) -> None: ... + def output_table_sep(self) -> None: ... + font_face: Any + table_offset: Any + def handle_starttag(self, tag, attrs) -> None: ... + tbody: Any + def handle_endtag(self, tag) -> None: ... + h: Any + def set_font(self, face: Any | None = ..., size: Any | None = ...) -> None: ... + def set_style(self, tag: Any | None = ..., enable: bool = ...) -> None: ... + def set_text_color(self, r: Any | None = ..., g: int = ..., b: int = ...) -> None: ... + def put_link(self, txt) -> None: ... + def render_toc(self, pdf, outline) -> None: ... + def error(self, message) -> None: ... + +class HTMLMixin: + HTML2FPDF_CLASS: Any + def write_html(self, text, *args, **kwargs) -> None: ... diff --git a/stubs/fpdf2/fpdf/image_parsing.pyi b/stubs/fpdf2/fpdf/image_parsing.pyi new file mode 100644 index 000000000000..109ec68e02eb --- /dev/null +++ b/stubs/fpdf2/fpdf/image_parsing.pyi @@ -0,0 +1,7 @@ +from .errors import FPDFException as FPDFException +from typing import Any + +SUPPORTED_IMAGE_FILTERS: Any + +def load_image(filename): ... +def get_img_info(img, image_filter: str = ...): ... diff --git a/stubs/fpdf2/fpdf/outline.pyi b/stubs/fpdf2/fpdf/outline.pyi new file mode 100644 index 000000000000..4cdce3dfb9d7 --- /dev/null +++ b/stubs/fpdf2/fpdf/outline.pyi @@ -0,0 +1,31 @@ +from .structure_tree import StructElem as StructElem +from .syntax import Destination as Destination, PDFObject as PDFObject, PDFString as PDFString +from typing import Any, NamedTuple, Optional + +class OutlineSection(NamedTuple): + name: str + level: str + page_number: int + dest: Destination + struct_elem: Optional[StructElem] + +class OutlineItemDictionary(PDFObject): + title: Any + parent: Any + prev: Any + next: Any + first: Any + last: Any + count: int + dest: Any + struct_elem: Any + def __init__(self, title: str, dest: str = ..., struct_elem: StructElem = ..., **kwargs) -> None: ... + +class OutlineDictionary(PDFObject): + type: str + first: Any + last: Any + count: int + def __init__(self, **kwargs) -> None: ... + +def serialize_outline(sections, first_object_id: int = ..., fpdf: Any | None = ...): ... diff --git a/stubs/fpdf2/fpdf/recorder.pyi b/stubs/fpdf2/fpdf/recorder.pyi new file mode 100644 index 000000000000..056dbda0cd6a --- /dev/null +++ b/stubs/fpdf2/fpdf/recorder.pyi @@ -0,0 +1,14 @@ +from .errors import FPDFException as FPDFException +from typing import Any + +class FPDFRecorder: + pdf: Any + accept_page_break: bool + def __init__(self, pdf, accept_page_break: bool = ...) -> None: ... + def __getattr__(self, name): ... + def rewind(self) -> None: ... + def replay(self) -> None: ... + +class CallRecorder: + def __init__(self, func, calls) -> None: ... + def __call__(self, *args, **kwargs): ... diff --git a/stubs/fpdf2/fpdf/structure_tree.pyi b/stubs/fpdf2/fpdf/structure_tree.pyi new file mode 100644 index 000000000000..db8e1243fda5 --- /dev/null +++ b/stubs/fpdf2/fpdf/structure_tree.pyi @@ -0,0 +1,42 @@ +from .syntax import PDFArray as PDFArray, PDFObject as PDFObject, PDFString as PDFString +from typing import Any, List, NamedTuple, Optional, Union + +class MarkedContent(NamedTuple): + page_object_id: int + struct_parents_id: int + struct_type: str + mcid: Optional[int] + title: Optional[str] + alt_text: Optional[str] + +class NumberTree(PDFObject): + nums: Any + def __init__(self, **kwargs) -> None: ... + def serialize(self, fpdf: Any | None = ..., obj_dict: Any | None = ...): ... + +class StructTreeRoot(PDFObject): + type: str + parent_tree: Any + k: Any + def __init__(self, **kwargs) -> None: ... + +class StructElem(PDFObject): + type: str + s: Any + p: Any + k: Any + pg: Any + t: Any + alt: Any + def __init__(self, struct_type: str, parent: PDFObject, kids: Union[List[int], List[StructElem]], page: PDFObject = ..., title: str = ..., alt: str = ..., **kwargs) -> None: ... + +class StructureTreeBuilder: + struct_tree_root: Any + doc_struct_elem: Any + struct_elem_per_mc: Any + def __init__(self) -> None: ... + def add_marked_content(self, marked_content) -> None: ... + def next_mcid_for_page(self, page_object_id): ... + def empty(self): ... + def serialize(self, first_object_id: int = ..., fpdf: Any | None = ...): ... + def assign_ids(self, n): ... diff --git a/stubs/fpdf2/fpdf/syntax.pyi b/stubs/fpdf2/fpdf/syntax.pyi new file mode 100644 index 000000000000..910fe0942db7 --- /dev/null +++ b/stubs/fpdf2/fpdf/syntax.pyi @@ -0,0 +1,38 @@ +from abc import ABC +from typing import Any + +def clear_empty_fields(d): ... +def create_dictionary_string(dict_, open_dict: str = ..., close_dict: str = ..., field_join: str = ..., key_value_join: str = ..., has_empty_fields: bool = ...): ... +def create_list_string(list_): ... +def iobj_ref(n): ... +def create_stream(stream): ... + +class PDFObject: + def __init__(self, id: Any | None = ...) -> None: ... + @property + def id(self): ... + @id.setter + def id(self, n) -> None: ... + @property + def ref(self): ... + def serialize(self, fpdf: Any | None = ..., obj_dict: Any | None = ...): ... + +def camel_case(property_name): ... + +class PDFString(str): + def serialize(self): ... + +class PDFArray(list): + def serialize(self): ... + +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: ... + def as_str(self, pdf: Any | None = ...): ... diff --git a/stubs/fpdf2/fpdf/template.pyi b/stubs/fpdf2/fpdf/template.pyi new file mode 100644 index 000000000000..36914900021b --- /dev/null +++ b/stubs/fpdf2/fpdf/template.pyi @@ -0,0 +1,25 @@ +from .errors import FPDFException as FPDFException +from .fpdf import FPDF as FPDF +from typing import Any + +class FlexTemplate: + pdf: Any + splitting_pdf: Any + handlers: Any + texts: Any + def __init__(self, pdf, elements: Any | None = ...) -> None: ... + elements: Any + keys: Any + def load_elements(self, elements) -> None: ... + def parse_csv(self, infile, delimiter: str = ..., decimal_sep: str = ..., encoding: Any | None = ...): ... + def __setitem__(self, name, value) -> None: ... + set: Any + def __contains__(self, name): ... + def __getitem__(self, name): ... + def split_multicell(self, text, element_name): ... + def render(self, offsetx: float = ..., offsety: float = ..., rotate: float = ..., scale: float = ...): ... + +class Template(FlexTemplate): + def __init__(self, infile: Any | None = ..., elements: Any | None = ..., format: str = ..., orientation: str = ..., unit: str = ..., title: str = ..., author: str = ..., subject: str = ..., creator: str = ..., keywords: str = ...) -> None: ... + def add_page(self) -> None: ... + def render(self, outfile: Any | None = ..., dest: Any | None = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/transitions.pyi b/stubs/fpdf2/fpdf/transitions.pyi new file mode 100644 index 000000000000..c1d692275e78 --- /dev/null +++ b/stubs/fpdf2/fpdf/transitions.pyi @@ -0,0 +1,58 @@ +from abc import ABC +from typing import Any + +class Transition(ABC): + def dict_as_string(self) -> None: ... + +class SplitTransition(Transition): + dimension: Any + direction: Any + def __init__(self, dimension, direction) -> None: ... + def dict_as_string(self): ... + +class BlindsTransition(Transition): + dimension: Any + def __init__(self, dimension) -> None: ... + def dict_as_string(self): ... + +class BoxTransition(Transition): + direction: Any + def __init__(self, direction) -> None: ... + def dict_as_string(self): ... + +class WipeTransition(Transition): + direction: Any + def __init__(self, direction) -> None: ... + def dict_as_string(self): ... + +class DissolveTransition(Transition): + def dict_as_string(self): ... + +class GlitterTransition(Transition): + direction: Any + def __init__(self, direction) -> None: ... + def dict_as_string(self): ... + +class FlyTransition(Transition): + dimension: Any + direction: Any + def __init__(self, dimension, direction: Any | None = ...) -> None: ... + def dict_as_string(self): ... + +class PushTransition(Transition): + direction: Any + def __init__(self, direction) -> None: ... + def dict_as_string(self): ... + +class CoverTransition(Transition): + direction: Any + def __init__(self, direction) -> None: ... + def dict_as_string(self): ... + +class UncoverTransition(Transition): + direction: Any + def __init__(self, direction) -> None: ... + def dict_as_string(self): ... + +class FadeTransition(Transition): + def dict_as_string(self): ... diff --git a/stubs/fpdf2/fpdf/ttfonts.pyi b/stubs/fpdf2/fpdf/ttfonts.pyi new file mode 100644 index 000000000000..925f117b639c --- /dev/null +++ b/stubs/fpdf2/fpdf/ttfonts.pyi @@ -0,0 +1,73 @@ +from .util import b as b, substr as substr +from typing import Any + +GF_WORDS: Any +GF_SCALE: Any +GF_MORE: Any +GF_XYSCALE: Any +GF_TWOBYTWO: Any + +def sub32(x, y): ... +def calcChecksum(data): ... + +class TTFontFile: + maxStrLenRead: int + def __init__(self) -> None: ... + filename: Any + charWidths: Any + glyphPos: Any + charToGlyph: Any + tables: Any + otables: Any + ascent: int + descent: int + version: Any + def getMetrics(self, file) -> None: ... + numTables: Any + searchRange: Any + entrySelector: Any + rangeShift: Any + def readTableDirectory(self) -> None: ... + def get_table_pos(self, tag): ... + def seek(self, pos) -> None: ... + def skip(self, delta) -> None: ... + def seek_table(self, tag, offset_in_table: int = ...): ... + def read_tag(self): ... + def read_short(self): ... + def read_ushort(self): ... + def read_ulong(self): ... + def get_ushort(self, pos): ... + @staticmethod + def splice(stream, offset, value): ... + def get_chunk(self, pos, length): ... + def get_table(self, tag): ... + def add(self, tag, data) -> None: ... + sFamilyClass: int + sFamilySubClass: int + name: Any + familyName: Any + styleName: Any + fullName: Any + uniqueFontID: Any + unitsPerEm: Any + bbox: Any + capHeight: Any + stemV: Any + italicAngle: Any + underlinePosition: Any + underlineThickness: Any + flags: int + def extractInfo(self) -> None: ... + maxUni: int + codeToGlyph: Any + glyphdata: Any + def makeSubset(self, file, subset): ... + def getGlyphs(self, originalGlyphIdx, nonlocals) -> None: ... + defaultWidth: Any + def getHMTX(self, numberOfHMetrics, numGlyphs, glyphToChar, scale) -> None: ... + def getHMetric(self, numberOfHMetrics, gid): ... + def getLOCA(self, indexToLocFormat, numGlyphs) -> None: ... + maxUniChar: int + def getCMAP4(self, unicode_cmap_offset, glyphToChar, charToGlyph) -> None: ... + def getCMAP12(self, unicode_cmap_offset, glyphToChar, charToGlyph) -> None: ... + def endTTFile(self, stm): ... diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi new file mode 100644 index 000000000000..0a8f0437e6ae --- /dev/null +++ b/stubs/fpdf2/fpdf/util.pyi @@ -0,0 +1,9 @@ +from typing import Iterable, Union + +def substr(s, start, length: int = ...): ... +def enclose_in_parens(s): ... +def escape_parens(s): ... +def b(s): ... +def get_scale_factor(unit: Union[str, float, int]) -> float: ... +def convert_unit(to_convert: Union[float, int, Iterable[Union[float, int, Iterable]]], old_unit: Union[str, float, int], new_unit: Union[str, float, int]) -> Union[float, tuple]: ... +def dochecks() -> None: ... From d7d6f64a8f7b3d5b0e088943e5f3c5c3cff0d934 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 14:41:36 +0100 Subject: [PATCH 02/11] Clean up imports --- stubs/fpdf2/fpdf/__init__.pyi | 11 +++++------ stubs/fpdf2/fpdf/actions.pyi | 1 - stubs/fpdf2/fpdf/fpdf.pyi | 26 +++++++++++++------------- stubs/fpdf2/fpdf/image_parsing.pyi | 1 - stubs/fpdf2/fpdf/outline.pyi | 8 ++++---- stubs/fpdf2/fpdf/recorder.pyi | 1 - stubs/fpdf2/fpdf/structure_tree.pyi | 12 ++++++------ stubs/fpdf2/fpdf/template.pyi | 2 -- stubs/fpdf2/fpdf/ttfonts.pyi | 1 - stubs/fpdf2/fpdf/util.pyi | 6 +++--- 10 files changed, 31 insertions(+), 38 deletions(-) diff --git a/stubs/fpdf2/fpdf/__init__.pyi b/stubs/fpdf2/fpdf/__init__.pyi index 10827ff84975..4ac12aa85f9f 100644 --- a/stubs/fpdf2/fpdf/__init__.pyi +++ b/stubs/fpdf2/fpdf/__init__.pyi @@ -1,11 +1,10 @@ from .fpdf import FPDF as FPDF, TitleStyle as TitleStyle from .html import HTML2FPDF as HTML2FPDF, HTMLMixin as HTMLMixin from .template import Template as Template +from pathlib import Path from typing import Any -FPDF_VERSION: Any -FPDF_FONT_DIR: Any - -# Names in __all__ with no definition: -# __license__ -# __version__ +__license__: str +__version__: str +FPDF_VERSION: str +FPDF_FONT_DIR: Path diff --git a/stubs/fpdf2/fpdf/actions.pyi b/stubs/fpdf2/fpdf/actions.pyi index b48ae31e6792..69feb0a2bf21 100644 --- a/stubs/fpdf2/fpdf/actions.pyi +++ b/stubs/fpdf2/fpdf/actions.pyi @@ -1,4 +1,3 @@ -from .util import enclose_in_parens as enclose_in_parens from abc import ABC from typing import Any diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi index a9e6253d982c..f801566c048d 100644 --- a/stubs/fpdf2/fpdf/fpdf.pyi +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -1,8 +1,8 @@ from .actions import Action -from collections.abc import Generator +from collections.abc import Callable, Generator from enum import IntEnum from pathlib import Path -from typing import Any, Callable, List, NamedTuple, Optional, Union +from typing import Any, NamedTuple PAGE_FORMATS: Any @@ -19,19 +19,19 @@ class Annotation(NamedTuple): width: int height: int contents: str - link: Union[str, int] - alt_text: Optional[str] - action: Optional[Action] + link: str | int + alt_text: str | None + action: Action | None class TitleStyle(NamedTuple): - font_family: Optional[str] - font_style: Optional[str] - font_size_pt: Optional[int] - color: Union[int, tuple] + font_family: str | None + font_style: str | None + font_size_pt: int | None + color: int | tuple underline: bool - t_margin: Optional[int] - l_margin: Optional[int] - b_margin: Optional[int] + t_margin: int | None + l_margin: int | None + b_margin: int | None class ToCPlaceholder(NamedTuple): render_function: Callable @@ -40,7 +40,7 @@ class ToCPlaceholder(NamedTuple): pages: int class SubsetMap: - def __init__(self, identities: List[int]) -> None: ... + def __init__(self, identities: list[int]) -> None: ... def pick(self, unicode: int): ... def dict(self): ... diff --git a/stubs/fpdf2/fpdf/image_parsing.pyi b/stubs/fpdf2/fpdf/image_parsing.pyi index 109ec68e02eb..ba17b8a8df11 100644 --- a/stubs/fpdf2/fpdf/image_parsing.pyi +++ b/stubs/fpdf2/fpdf/image_parsing.pyi @@ -1,4 +1,3 @@ -from .errors import FPDFException as FPDFException from typing import Any SUPPORTED_IMAGE_FILTERS: Any diff --git a/stubs/fpdf2/fpdf/outline.pyi b/stubs/fpdf2/fpdf/outline.pyi index 4cdce3dfb9d7..3e6bbdb27617 100644 --- a/stubs/fpdf2/fpdf/outline.pyi +++ b/stubs/fpdf2/fpdf/outline.pyi @@ -1,13 +1,13 @@ -from .structure_tree import StructElem as StructElem -from .syntax import Destination as Destination, PDFObject as PDFObject, PDFString as PDFString -from typing import Any, NamedTuple, Optional +from .structure_tree import StructElem +from .syntax import Destination, PDFObject +from typing import Any, NamedTuple class OutlineSection(NamedTuple): name: str level: str page_number: int dest: Destination - struct_elem: Optional[StructElem] + struct_elem: StructElem | None class OutlineItemDictionary(PDFObject): title: Any diff --git a/stubs/fpdf2/fpdf/recorder.pyi b/stubs/fpdf2/fpdf/recorder.pyi index 056dbda0cd6a..6401da5362ed 100644 --- a/stubs/fpdf2/fpdf/recorder.pyi +++ b/stubs/fpdf2/fpdf/recorder.pyi @@ -1,4 +1,3 @@ -from .errors import FPDFException as FPDFException from typing import Any class FPDFRecorder: diff --git a/stubs/fpdf2/fpdf/structure_tree.pyi b/stubs/fpdf2/fpdf/structure_tree.pyi index db8e1243fda5..f2093ee9c2be 100644 --- a/stubs/fpdf2/fpdf/structure_tree.pyi +++ b/stubs/fpdf2/fpdf/structure_tree.pyi @@ -1,13 +1,13 @@ -from .syntax import PDFArray as PDFArray, PDFObject as PDFObject, PDFString as PDFString -from typing import Any, List, NamedTuple, Optional, Union +from .syntax import PDFObject +from typing import Any, NamedTuple class MarkedContent(NamedTuple): page_object_id: int struct_parents_id: int struct_type: str - mcid: Optional[int] - title: Optional[str] - alt_text: Optional[str] + mcid: int | None + title: str | None + alt_text: str | None class NumberTree(PDFObject): nums: Any @@ -28,7 +28,7 @@ class StructElem(PDFObject): pg: Any t: Any alt: Any - def __init__(self, struct_type: str, parent: PDFObject, kids: Union[List[int], List[StructElem]], page: PDFObject = ..., title: str = ..., alt: str = ..., **kwargs) -> None: ... + def __init__(self, struct_type: str, parent: PDFObject, kids: list[int] | list[StructElem], page: PDFObject = ..., title: str = ..., alt: str = ..., **kwargs) -> None: ... class StructureTreeBuilder: struct_tree_root: Any diff --git a/stubs/fpdf2/fpdf/template.pyi b/stubs/fpdf2/fpdf/template.pyi index 36914900021b..c6eeb988ecfd 100644 --- a/stubs/fpdf2/fpdf/template.pyi +++ b/stubs/fpdf2/fpdf/template.pyi @@ -1,5 +1,3 @@ -from .errors import FPDFException as FPDFException -from .fpdf import FPDF as FPDF from typing import Any class FlexTemplate: diff --git a/stubs/fpdf2/fpdf/ttfonts.pyi b/stubs/fpdf2/fpdf/ttfonts.pyi index 925f117b639c..84990f260693 100644 --- a/stubs/fpdf2/fpdf/ttfonts.pyi +++ b/stubs/fpdf2/fpdf/ttfonts.pyi @@ -1,4 +1,3 @@ -from .util import b as b, substr as substr from typing import Any GF_WORDS: Any diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi index 0a8f0437e6ae..66d74d978577 100644 --- a/stubs/fpdf2/fpdf/util.pyi +++ b/stubs/fpdf2/fpdf/util.pyi @@ -1,9 +1,9 @@ -from typing import Iterable, Union +from collections.abc import Iterable def substr(s, start, length: int = ...): ... def enclose_in_parens(s): ... def escape_parens(s): ... def b(s): ... -def get_scale_factor(unit: Union[str, float, int]) -> float: ... -def convert_unit(to_convert: Union[float, int, Iterable[Union[float, int, Iterable]]], old_unit: Union[str, float, int], new_unit: Union[str, float, int]) -> Union[float, tuple]: ... +def get_scale_factor(unit: str | float) -> float: ... +def convert_unit(to_convert: float | Iterable[float | Iterable], old_unit: str | float, new_unit: str | float) -> float | tuple: ... def dochecks() -> None: ... From 6a6f744b7fd6a2e5d607f205f5cb69c80eda71d7 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:12:49 +0100 Subject: [PATCH 03/11] Black/isort --- stubs/fpdf2/fpdf/__init__.pyi | 5 ++- stubs/fpdf2/fpdf/fpdf.pyi | 60 ++++++++++++++++++++++++++--- stubs/fpdf2/fpdf/html.pyi | 11 +++++- stubs/fpdf2/fpdf/outline.pyi | 3 +- stubs/fpdf2/fpdf/structure_tree.pyi | 14 ++++++- stubs/fpdf2/fpdf/syntax.pyi | 9 ++++- stubs/fpdf2/fpdf/template.pyi | 14 ++++++- stubs/fpdf2/fpdf/util.pyi | 4 +- 8 files changed, 105 insertions(+), 15 deletions(-) diff --git a/stubs/fpdf2/fpdf/__init__.pyi b/stubs/fpdf2/fpdf/__init__.pyi index 4ac12aa85f9f..ee0938628026 100644 --- a/stubs/fpdf2/fpdf/__init__.pyi +++ b/stubs/fpdf2/fpdf/__init__.pyi @@ -1,8 +1,9 @@ +from pathlib import Path +from typing import Any + from .fpdf import FPDF as FPDF, TitleStyle as TitleStyle from .html import HTML2FPDF as HTML2FPDF, HTMLMixin as HTMLMixin from .template import Template as Template -from pathlib import Path -from typing import Any __license__: str __version__: str diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi index f801566c048d..42d2283b279b 100644 --- a/stubs/fpdf2/fpdf/fpdf.pyi +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -1,9 +1,10 @@ -from .actions import Action from collections.abc import Callable, Generator from enum import IntEnum from pathlib import Path from typing import Any, NamedTuple +from .actions import Action + PAGE_FORMATS: Any class DocumentState(IntEnum): @@ -141,7 +142,9 @@ class FPDF: def alias_nb_pages(self, alias: str = ...) -> None: ... def open(self) -> None: ... def close(self) -> None: ... - def add_page(self, orientation: str = ..., format: str = ..., same: bool = ..., duration: int = ..., transition: Any | None = ...) -> None: ... + def add_page( + self, orientation: str = ..., format: str = ..., same: bool = ..., duration: int = ..., transition: Any | None = ... + ) -> None: ... def header(self) -> None: ... def footer(self) -> None: ... def page_no(self): ... @@ -171,11 +174,47 @@ class FPDF: def rotation(self, angle, x: Any | None = ..., y: Any | None = ...) -> Generator[None, None, None]: ... @property def accept_page_break(self): ... - def cell(self, w: Any | None = ..., h: Any | None = ..., txt: str = ..., border: int = ..., ln: int = ..., align: str = ..., fill: bool = ..., link: str = ..., center: bool = ..., markdown: bool = ...): ... + def cell( + self, + w: Any | None = ..., + h: Any | None = ..., + txt: str = ..., + border: int = ..., + ln: int = ..., + align: str = ..., + fill: bool = ..., + link: str = ..., + center: bool = ..., + markdown: bool = ..., + ): ... def will_page_break(self, height): ... - def multi_cell(self, w, h: Any | None = ..., txt: str = ..., border: int = ..., align: str = ..., fill: bool = ..., split_only: bool = ..., link: str = ..., ln: int = ..., max_line_height: Any | None = ..., markdown: bool = ...): ... + def multi_cell( + self, + w, + h: Any | None = ..., + txt: str = ..., + border: int = ..., + align: str = ..., + fill: bool = ..., + split_only: bool = ..., + link: str = ..., + ln: int = ..., + max_line_height: Any | None = ..., + markdown: bool = ..., + ): ... def write(self, h: Any | None = ..., txt: str = ..., link: str = ...) -> None: ... - def image(self, name, x: Any | None = ..., y: Any | None = ..., w: int = ..., h: int = ..., type: str = ..., link: str = ..., title: Any | None = ..., alt_text: Any | None = ...): ... + def image( + self, + name, + x: Any | None = ..., + y: Any | None = ..., + w: int = ..., + h: int = ..., + type: str = ..., + link: str = ..., + title: Any | None = ..., + alt_text: Any | None = ..., + ): ... def ln(self, h: Any | None = ...) -> None: ... def get_x(self): ... def set_x(self, x) -> None: ... @@ -189,5 +228,14 @@ class FPDF: def rect_clip(self, x, y, w, h) -> Generator[None, None, None]: ... def unbreakable(self) -> Generator[Any, None, None]: ... 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 = ...) -> 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 = ..., + ) -> None: ... def start_section(self, name, level: int = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/html.pyi b/stubs/fpdf2/fpdf/html.pyi index eb8048f1f58a..2b1436240e3c 100644 --- a/stubs/fpdf2/fpdf/html.pyi +++ b/stubs/fpdf2/fpdf/html.pyi @@ -39,7 +39,16 @@ class HTML2FPDF(HTMLParser): table_row_height: int heading_level: Any heading_sizes: Any - def __init__(self, pdf, image_map: Any | None = ..., li_tag_indent: int = ..., table_line_separators: bool = ..., ul_bullet_char=..., heading_sizes: Any | None = ..., **_): ... + def __init__( + self, + pdf, + image_map: Any | None = ..., + li_tag_indent: int = ..., + table_line_separators: bool = ..., + ul_bullet_char=..., + heading_sizes: Any | None = ..., + **_, + ): ... def width2unit(self, length): ... def handle_data(self, data) -> None: ... def box_shadow(self, w, h, bgcolor) -> None: ... diff --git a/stubs/fpdf2/fpdf/outline.pyi b/stubs/fpdf2/fpdf/outline.pyi index 3e6bbdb27617..ece4f357d98a 100644 --- a/stubs/fpdf2/fpdf/outline.pyi +++ b/stubs/fpdf2/fpdf/outline.pyi @@ -1,6 +1,7 @@ +from typing import Any, NamedTuple + from .structure_tree import StructElem from .syntax import Destination, PDFObject -from typing import Any, NamedTuple class OutlineSection(NamedTuple): name: str diff --git a/stubs/fpdf2/fpdf/structure_tree.pyi b/stubs/fpdf2/fpdf/structure_tree.pyi index f2093ee9c2be..67cce7927ad3 100644 --- a/stubs/fpdf2/fpdf/structure_tree.pyi +++ b/stubs/fpdf2/fpdf/structure_tree.pyi @@ -1,6 +1,7 @@ -from .syntax import PDFObject from typing import Any, NamedTuple +from .syntax import PDFObject + class MarkedContent(NamedTuple): page_object_id: int struct_parents_id: int @@ -28,7 +29,16 @@ class StructElem(PDFObject): pg: Any t: Any alt: Any - def __init__(self, struct_type: str, parent: PDFObject, kids: list[int] | list[StructElem], page: PDFObject = ..., title: str = ..., alt: str = ..., **kwargs) -> None: ... + def __init__( + self, + struct_type: str, + parent: PDFObject, + kids: list[int] | list[StructElem], + page: PDFObject = ..., + title: str = ..., + alt: str = ..., + **kwargs, + ) -> None: ... class StructureTreeBuilder: struct_tree_root: Any diff --git a/stubs/fpdf2/fpdf/syntax.pyi b/stubs/fpdf2/fpdf/syntax.pyi index 910fe0942db7..d6917e25849e 100644 --- a/stubs/fpdf2/fpdf/syntax.pyi +++ b/stubs/fpdf2/fpdf/syntax.pyi @@ -2,7 +2,14 @@ from abc import ABC from typing import Any def clear_empty_fields(d): ... -def create_dictionary_string(dict_, open_dict: str = ..., close_dict: str = ..., field_join: str = ..., key_value_join: str = ..., has_empty_fields: bool = ...): ... +def create_dictionary_string( + dict_, + open_dict: str = ..., + close_dict: str = ..., + field_join: str = ..., + key_value_join: str = ..., + has_empty_fields: bool = ..., +): ... def create_list_string(list_): ... def iobj_ref(n): ... def create_stream(stream): ... diff --git a/stubs/fpdf2/fpdf/template.pyi b/stubs/fpdf2/fpdf/template.pyi index c6eeb988ecfd..aacb87fb91e7 100644 --- a/stubs/fpdf2/fpdf/template.pyi +++ b/stubs/fpdf2/fpdf/template.pyi @@ -18,6 +18,18 @@ class FlexTemplate: def render(self, offsetx: float = ..., offsety: float = ..., rotate: float = ..., scale: float = ...): ... class Template(FlexTemplate): - def __init__(self, infile: Any | None = ..., elements: Any | None = ..., format: str = ..., orientation: str = ..., unit: str = ..., title: str = ..., author: str = ..., subject: str = ..., creator: str = ..., keywords: str = ...) -> None: ... + def __init__( + self, + infile: Any | None = ..., + elements: Any | None = ..., + format: str = ..., + orientation: str = ..., + unit: str = ..., + title: str = ..., + author: str = ..., + subject: str = ..., + creator: str = ..., + keywords: str = ..., + ) -> None: ... def add_page(self) -> None: ... def render(self, outfile: Any | None = ..., dest: Any | None = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi index 66d74d978577..57f72ce0569f 100644 --- a/stubs/fpdf2/fpdf/util.pyi +++ b/stubs/fpdf2/fpdf/util.pyi @@ -5,5 +5,7 @@ def enclose_in_parens(s): ... def escape_parens(s): ... def b(s): ... def get_scale_factor(unit: str | float) -> float: ... -def convert_unit(to_convert: float | Iterable[float | Iterable], old_unit: str | float, new_unit: str | float) -> float | tuple: ... +def convert_unit( + to_convert: float | Iterable[float | Iterable], old_unit: str | float, new_unit: str | float +) -> float | tuple: ... def dochecks() -> None: ... From 780a154376eba50dd7026068d66e01ae08811ee0 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:13:24 +0100 Subject: [PATCH 04/11] Add type: ignore --- stubs/fpdf2/fpdf/template.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/fpdf2/fpdf/template.pyi b/stubs/fpdf2/fpdf/template.pyi index aacb87fb91e7..7badad2577dc 100644 --- a/stubs/fpdf2/fpdf/template.pyi +++ b/stubs/fpdf2/fpdf/template.pyi @@ -32,4 +32,4 @@ class Template(FlexTemplate): keywords: str = ..., ) -> None: ... def add_page(self) -> None: ... - def render(self, outfile: Any | None = ..., dest: Any | None = ...) -> None: ... + def render(self, outfile: Any | None = ..., dest: Any | None = ...) -> None: ... # type: ignore From 8bed325d2f4163c90127cb9cfe33a7dcc18ee4c6 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:10:01 +0100 Subject: [PATCH 05/11] Add some annotations --- stubs/fpdf2/fpdf/fpdf.pyi | 113 ++++++++++++++++++++++---------------- stubs/fpdf2/fpdf/util.pyi | 5 +- 2 files changed, 70 insertions(+), 48 deletions(-) diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi index 42d2283b279b..eeaf62bcb82d 100644 --- a/stubs/fpdf2/fpdf/fpdf.pyi +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -1,11 +1,18 @@ +import datetime from collections.abc import Callable, Generator from enum import IntEnum from pathlib import Path from typing import Any, NamedTuple +from typing_extensions import Literal from .actions import Action +from .util import _Unit -PAGE_FORMATS: Any +_Orientation = Literal["portrait", "P", "landscape", "L"] +_Format = Literal["a3", "a4", "a5", "letter", "legal"] +_FontStyle = Literal["", "B", "I"] +_FontStyles = Literal["", "B", "I", "U", "BU", "UB", "BI", "IB", "IU", "UI", "BIU", "BUI", "IBU", "IUB", "UBI", "UIB"] +PAGE_FORMATS: dict[_Format, tuple[float, float]] class DocumentState(IntEnum): UNINITIALIZED: int @@ -45,7 +52,7 @@ class SubsetMap: def pick(self, unicode: int): ... def dict(self): ... -def get_page_format(format, k: Any | None = ...): ... +def get_page_format(format: _Format | tuple[float, float], k: float | None = ...) -> tuple[float, float]: ... def load_cache(filename: Path): ... class FPDF: @@ -88,54 +95,61 @@ class FPDF: core_fonts: Any core_fonts_encoding: str font_aliases: Any - k: Any + k: float def_orientation: Any font_size: Any c_margin: Any - line_width: Any + line_width: float compress: bool pdf_version: str - def __init__(self, orientation: str = ..., unit: str = ..., format: str = ..., font_cache_dir: bool = ...) -> None: ... + + x: float + y: float + t_margin: float + r_margin: float + l_margin: float + def __init__( + self, + orientation: _Orientation = ..., + unit: _Unit | float = ..., + format: _Format | tuple[float, float] = ..., + font_cache_dir: bool = ..., + ) -> None: ... @property def unifontsubset(self): ... @property def epw(self): ... @property def eph(self): ... - def set_margin(self, margin) -> None: ... - y: Any - t_margin: Any - r_margin: Any - def set_margins(self, left, top, right: int = ...) -> None: ... - x: Any - l_margin: Any - def set_left_margin(self, margin) -> None: ... - def set_top_margin(self, margin) -> None: ... - def set_right_margin(self, margin) -> None: ... + def set_margin(self, margin: float) -> None: ... + def set_margins(self, left: float, top: float, right: float = ...) -> None: ... + def set_left_margin(self, margin: float) -> None: ... + def set_top_margin(self, margin: float) -> None: ... + def set_right_margin(self, margin: float) -> None: ... auto_page_break: Any b_margin: Any page_break_trigger: Any - def set_auto_page_break(self, auto, margin: int = ...) -> None: ... + 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 - def set_title(self, title) -> None: ... + def set_title(self, title: str) -> None: ... lang: Any - def set_lang(self, lang) -> None: ... + def set_lang(self, lang: str) -> None: ... subject: Any - def set_subject(self, subject) -> None: ... + def set_subject(self, subject: str) -> None: ... author: Any - def set_author(self, author) -> None: ... + def set_author(self, author: str) -> None: ... keywords: Any - def set_keywords(self, keywords) -> None: ... + def set_keywords(self, keywords: str) -> None: ... creator: Any - def set_creator(self, creator) -> None: ... + def set_creator(self, creator: str) -> None: ... producer: Any - def set_producer(self, producer) -> None: ... + def set_producer(self, producer: str) -> None: ... creation_date: Any - def set_creation_date(self, date: Any | None = ...) -> None: ... + 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: ... @@ -143,7 +157,12 @@ class FPDF: def open(self) -> None: ... def close(self) -> None: ... def add_page( - self, orientation: str = ..., format: str = ..., same: bool = ..., duration: int = ..., transition: Any | None = ... + self, + orientation: _Orientation = ..., + format: _Format | tuple[float, float] = ..., + same: bool = ..., + duration: int = ..., + transition: Any | None = ..., ) -> None: ... def header(self) -> None: ... def footer(self) -> None: ... @@ -152,17 +171,17 @@ class FPDF: 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_line_width(self, width) -> None: ... - def line(self, x1, y1, x2, y2) -> None: ... + 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 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 add_font(self, family, style: str = ..., fname: Any | None = ..., uni: bool = ...) -> None: ... - def set_font(self, family: Any | None = ..., style: str = ..., size: int = ...) -> None: ... - def set_font_size(self, size) -> 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: ... @@ -176,10 +195,10 @@ class FPDF: def accept_page_break(self): ... def cell( self, - w: Any | None = ..., - h: Any | None = ..., + w: float | None = ..., + h: float | None = ..., txt: str = ..., - border: int = ..., + border: bool | Literal[0, 1] | str = ..., ln: int = ..., align: str = ..., fill: bool = ..., @@ -190,10 +209,10 @@ class FPDF: def will_page_break(self, height): ... def multi_cell( self, - w, - h: Any | None = ..., + w: float, + h: float | None = ..., txt: str = ..., - border: int = ..., + border: bool | Literal[0, 1] | str = ..., align: str = ..., fill: bool = ..., split_only: bool = ..., @@ -206,21 +225,21 @@ class FPDF: def image( self, name, - x: Any | None = ..., - y: Any | None = ..., - w: int = ..., - h: int = ..., + x: float | None = ..., + y: float | None = ..., + w: float = ..., + h: float = ..., type: str = ..., link: str = ..., - title: Any | None = ..., - alt_text: Any | None = ..., + title: str | None = ..., + alt_text: str | None = ..., ): ... def ln(self, h: Any | None = ...) -> None: ... - def get_x(self): ... - def set_x(self, x) -> None: ... - def get_y(self): ... - def set_y(self, y) -> None: ... - def set_xy(self, x, y) -> None: ... + def get_x(self) -> float: ... + def set_x(self, x: float) -> None: ... + def get_y(self) -> float: ... + def set_y(self, y: float) -> None: ... + def set_xy(self, x: float, y: float) -> None: ... def output(self, name: str = ..., dest: str = ...): ... def normalize_text(self, txt): ... def interleaved2of5(self, txt, x, y, w: int = ..., h: int = ...) -> None: ... diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi index 57f72ce0569f..abcf8fa0a3dd 100644 --- a/stubs/fpdf2/fpdf/util.pyi +++ b/stubs/fpdf2/fpdf/util.pyi @@ -1,10 +1,13 @@ from collections.abc import Iterable +from typing_extensions import Literal + +_Unit = Literal["pt", "mm", "cm", "in"] def substr(s, start, length: int = ...): ... def enclose_in_parens(s): ... def escape_parens(s): ... def b(s): ... -def get_scale_factor(unit: str | float) -> float: ... +def get_scale_factor(unit: _Unit | float) -> float: ... def convert_unit( to_convert: float | Iterable[float | Iterable], old_unit: str | float, new_unit: str | float ) -> float | tuple: ... From 083f6c15857885e8d26885570e4312a93860de85 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:18:37 +0100 Subject: [PATCH 06/11] Remove unused import --- stubs/fpdf2/fpdf/__init__.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stubs/fpdf2/fpdf/__init__.pyi b/stubs/fpdf2/fpdf/__init__.pyi index ee0938628026..2ee9dd6704d3 100644 --- a/stubs/fpdf2/fpdf/__init__.pyi +++ b/stubs/fpdf2/fpdf/__init__.pyi @@ -1,5 +1,4 @@ from pathlib import Path -from typing import Any from .fpdf import FPDF as FPDF, TitleStyle as TitleStyle from .html import HTML2FPDF as HTML2FPDF, HTMLMixin as HTMLMixin From 54b4c9204f0b2dc9180ea77966d3816b414a9f5a Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:19:34 +0100 Subject: [PATCH 07/11] Exclude fpdf2 from stricter pyright config --- pyrightconfig.stricter.json | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index aa71da99ff9d..90317862373e 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -33,6 +33,7 @@ "stubs/dateparser", "stubs/docutils", "stubs/Flask", + "stubs/fpdf2", "stubs/html5lib", "stubs/httplib2", "stubs/humanfriendly", From af22d90b8e6a86dbad7ecfa46428c07019a761f7 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:33:39 +0100 Subject: [PATCH 08/11] Fix pyright warnings --- stubs/fpdf2/fpdf/fpdf.pyi | 4 ++-- stubs/fpdf2/fpdf/syntax.pyi | 2 +- stubs/fpdf2/fpdf/util.pyi | 7 +++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi index eeaf62bcb82d..a89437fca60d 100644 --- a/stubs/fpdf2/fpdf/fpdf.pyi +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -35,14 +35,14 @@ class TitleStyle(NamedTuple): font_family: str | None font_style: str | None font_size_pt: int | None - color: int | tuple + color: int | tuple[int, int, int] underline: bool t_margin: int | None l_margin: int | None b_margin: int | None class ToCPlaceholder(NamedTuple): - render_function: Callable + render_function: Callable[[FPDF, Any], object] start_page: int y: int pages: int diff --git a/stubs/fpdf2/fpdf/syntax.pyi b/stubs/fpdf2/fpdf/syntax.pyi index d6917e25849e..838880de0794 100644 --- a/stubs/fpdf2/fpdf/syntax.pyi +++ b/stubs/fpdf2/fpdf/syntax.pyi @@ -29,7 +29,7 @@ def camel_case(property_name): ... class PDFString(str): def serialize(self): ... -class PDFArray(list): +class PDFArray(list[Any]): def serialize(self): ... class Destination(ABC): diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi index abcf8fa0a3dd..f9403cb1a6fb 100644 --- a/stubs/fpdf2/fpdf/util.pyi +++ b/stubs/fpdf2/fpdf/util.pyi @@ -9,6 +9,9 @@ def escape_parens(s): ... def b(s): ... def get_scale_factor(unit: _Unit | float) -> float: ... def convert_unit( - to_convert: float | Iterable[float | Iterable], old_unit: str | float, new_unit: str | float -) -> float | tuple: ... + # to_convert has a recursive type + to_convert: float | Iterable[float | Iterable[Any]], + old_unit: str | float, + new_unit: str | float, +) -> float | tuple[float, ...]: ... def dochecks() -> None: ... From 586c83a4d96def24fec1d62f46c27058fefed99c Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:39:07 +0100 Subject: [PATCH 09/11] Add missing import --- stubs/fpdf2/fpdf/util.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi index f9403cb1a6fb..21b00796ab70 100644 --- a/stubs/fpdf2/fpdf/util.pyi +++ b/stubs/fpdf2/fpdf/util.pyi @@ -1,4 +1,5 @@ from collections.abc import Iterable +from typing import Any from typing_extensions import Literal _Unit = Literal["pt", "mm", "cm", "in"] From 1948ac21724ef44a37c24c70009df098d2c507d3 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 15:51:13 +0100 Subject: [PATCH 10/11] Work around mypy bugs --- stubs/fpdf2/fpdf/syntax.pyi | 4 ++-- stubs/fpdf2/fpdf/util.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stubs/fpdf2/fpdf/syntax.pyi b/stubs/fpdf2/fpdf/syntax.pyi index 838880de0794..54788032540a 100644 --- a/stubs/fpdf2/fpdf/syntax.pyi +++ b/stubs/fpdf2/fpdf/syntax.pyi @@ -1,5 +1,5 @@ from abc import ABC -from typing import Any +from typing import Any, List def clear_empty_fields(d): ... def create_dictionary_string( @@ -29,7 +29,7 @@ def camel_case(property_name): ... class PDFString(str): def serialize(self): ... -class PDFArray(list[Any]): +class PDFArray(List[Any]): def serialize(self): ... class Destination(ABC): diff --git a/stubs/fpdf2/fpdf/util.pyi b/stubs/fpdf2/fpdf/util.pyi index 21b00796ab70..0455c3f2a6b8 100644 --- a/stubs/fpdf2/fpdf/util.pyi +++ b/stubs/fpdf2/fpdf/util.pyi @@ -1,5 +1,5 @@ from collections.abc import Iterable -from typing import Any +from typing import Any, Tuple from typing_extensions import Literal _Unit = Literal["pt", "mm", "cm", "in"] @@ -14,5 +14,5 @@ def convert_unit( to_convert: float | Iterable[float | Iterable[Any]], old_unit: str | float, new_unit: str | float, -) -> float | tuple[float, ...]: ... +) -> float | Tuple[float, ...]: ... def dochecks() -> None: ... From b029b874efdb62af1af6f67c52a623d76ac58ed8 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 7 Nov 2021 16:11:56 +0100 Subject: [PATCH 11/11] stubtest fixes --- stubs/fpdf2/fpdf/fpdf.pyi | 30 ++++++++++++++--------------- stubs/fpdf2/fpdf/outline.pyi | 24 +++++++++++------------ stubs/fpdf2/fpdf/structure_tree.pyi | 12 ++++++------ 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/stubs/fpdf2/fpdf/fpdf.pyi b/stubs/fpdf2/fpdf/fpdf.pyi index a89437fca60d..9bcad7986236 100644 --- a/stubs/fpdf2/fpdf/fpdf.pyi +++ b/stubs/fpdf2/fpdf/fpdf.pyi @@ -8,8 +8,8 @@ from typing_extensions import Literal from .actions import Action from .util import _Unit -_Orientation = Literal["portrait", "P", "landscape", "L"] -_Format = Literal["a3", "a4", "a5", "letter", "legal"] +_Orientation = Literal["", "portrait", "P", "landscape", "L"] +_Format = Literal["", "a3", "A3", "a4", "A4", "a5", "A5", "letter", "Letter", "legal", "Legal"] _FontStyle = Literal["", "B", "I"] _FontStyles = Literal["", "B", "I", "U", "BU", "UB", "BI", "IB", "IU", "UI", "BIU", "BUI", "IBU", "IUB", "UBI", "UIB"] PAGE_FORMATS: dict[_Format, tuple[float, float]] @@ -26,26 +26,26 @@ class Annotation(NamedTuple): y: int width: int height: int - contents: str - link: str | int - alt_text: str | None - action: Action | None + contents: str | None = ... + link: str | int | None = ... + alt_text: str | None = ... + action: Action | None = ... class TitleStyle(NamedTuple): - font_family: str | None - font_style: str | None - font_size_pt: int | None - color: int | tuple[int, int, int] - underline: bool - t_margin: int | None - l_margin: int | None - b_margin: int | None + font_family: str | None = ... + font_style: str | None = ... + font_size_pt: int | None = ... + color: int | tuple[int, int, int] | None = ... + underline: bool = ... + t_margin: int | None = ... + l_margin: int | None = ... + b_margin: int | None = ... class ToCPlaceholder(NamedTuple): render_function: Callable[[FPDF, Any], object] start_page: int y: int - pages: int + pages: int = ... class SubsetMap: def __init__(self, identities: list[int]) -> None: ... diff --git a/stubs/fpdf2/fpdf/outline.pyi b/stubs/fpdf2/fpdf/outline.pyi index ece4f357d98a..0df21fb1a58b 100644 --- a/stubs/fpdf2/fpdf/outline.pyi +++ b/stubs/fpdf2/fpdf/outline.pyi @@ -8,24 +8,24 @@ class OutlineSection(NamedTuple): level: str page_number: int dest: Destination - struct_elem: StructElem | None + struct_elem: StructElem | None = ... class OutlineItemDictionary(PDFObject): - title: Any - parent: Any - prev: Any - next: Any - first: Any - last: Any + title: str + parent: Any | None + prev: Any | None + next: Any | None + first: Any | None + last: Any | None count: int - dest: Any - struct_elem: Any - def __init__(self, title: str, dest: str = ..., struct_elem: StructElem = ..., **kwargs) -> None: ... + dest: str | None + struct_elem: StructElem | None + def __init__(self, title: str, dest: str | None = ..., struct_elem: StructElem | None = ..., **kwargs) -> None: ... class OutlineDictionary(PDFObject): type: str - first: Any - last: Any + first: Any | None + last: Any | None count: int def __init__(self, **kwargs) -> None: ... diff --git a/stubs/fpdf2/fpdf/structure_tree.pyi b/stubs/fpdf2/fpdf/structure_tree.pyi index 67cce7927ad3..b8bc849b3d19 100644 --- a/stubs/fpdf2/fpdf/structure_tree.pyi +++ b/stubs/fpdf2/fpdf/structure_tree.pyi @@ -6,9 +6,9 @@ class MarkedContent(NamedTuple): page_object_id: int struct_parents_id: int struct_type: str - mcid: int | None - title: str | None - alt_text: str | None + mcid: int | None = ... + title: str | None = ... + alt_text: str | None = ... class NumberTree(PDFObject): nums: Any @@ -34,9 +34,9 @@ class StructElem(PDFObject): struct_type: str, parent: PDFObject, kids: list[int] | list[StructElem], - page: PDFObject = ..., - title: str = ..., - alt: str = ..., + page: PDFObject | None = ..., + title: str | None = ..., + alt: str | None = ..., **kwargs, ) -> None: ...