diff --git a/src/poppler/document.py b/src/poppler/document.py index 0872e5d..ee484bd 100644 --- a/src/poppler/document.py +++ b/src/poppler/document.py @@ -28,6 +28,7 @@ from collections import namedtuple from functools import singledispatch, wraps from pathlib import Path +from typing import Union, List PDFId = namedtuple("PDFId", ["permanent_id", "update_id"]) @@ -58,24 +59,24 @@ def __init__(self, poppler_document, data=None): raise ValueError("Corrupted or invalid PDF document") self._document = poppler_document - self._data = data + self._data = data # just to prevent data from being GC'ed @ensure_unlocked - def create_font_iterator(self, page=0): + def create_font_iterator(self, page: int = 0) -> FontIterator: return FontIterator(self._document.create_font_iterator(page)) @ensure_unlocked - def create_toc(self): + def create_toc(self) -> Union[Toc, None]: t = self._document.create_toc() return Toc(t) if t else None @ensure_unlocked - def create_page(self, index): + def create_page(self, index: int) -> Page: return Page(self._document.create_page(index)) @property @ensure_unlocked - def author(self): + def author(self) -> str: return self.info_key("Author") @author.setter @@ -167,7 +168,7 @@ def subject(self, subject): @property @ensure_unlocked - def title(self): + def title(self) -> str: return self.info_key("Title") @title.setter @@ -178,7 +179,7 @@ def title(self, title): @property @ensure_unlocked - def pages(self): + def pages(self) -> int: return self._document.pages() @since(0, 74) @@ -190,7 +191,7 @@ def create_destination_map(self): } @ensure_unlocked - def embedded_files(self): + def embedded_files(self) -> List[EmbeddedFile]: return [EmbeddedFile(f) for f in self._document.embedded_files()] @ensure_unlocked @@ -198,10 +199,10 @@ def fonts(self): return [FontInfo(i) for i in self._document.fonts()] @ensure_unlocked - def has_embedded_files(self): + def has_embedded_files(self) -> bool: return self._document.has_embedded_files() - def has_permission(self, which): + def has_permission(self, which) -> bool: return self._document.has_permission(which) @ensure_unlocked @@ -215,7 +216,7 @@ def set_info_date(self, key, val): return self._document.set_info_date(key, to_time_type(val)) @ensure_unlocked - def info_key(self, key): + def info_key(self, key) -> str: info = self._document.info_key(key) return str(info) @@ -239,13 +240,13 @@ def set_info_key(self, key, val): def info_keys(self): return self._document.info_keys() - def is_encrypted(self): + def is_encrypted(self) -> bool: return self._document.is_encrypted() - def is_linearized(self): + def is_linearized(self) -> bool: return self._document.is_linearized() - def is_locked(self): + def is_locked(self) -> bool: return self._document.is_locked() @property @@ -277,18 +278,20 @@ def unlock(self, owner_password, user_password): return self._document.unlock(owner_password, user_password) -def load_from_file(file_name, owner_password=None, user_password=None): +def load_from_file( + file_name: Union[str, Path], owner_password: Union[str, None] = None, user_password: Union[str, None] = None +) -> Document: return Document( - document.load_from_file( - str(file_name), owner_password or "", user_password or "" - ) + document.load_from_file(file_name, owner_password, user_password) ) -def load_from_data(file_data: bytes, owner_password=None, user_password=None): +def load_from_data( + file_data: bytes, owner_password: Union[str, None] = None, user_password: Union[str, None] = "" +) -> Document: return Document( - document.load_from_data(file_data, owner_password or "", user_password or ""), - file_data + document.load_from_data(file_data, owner_password, user_password), + file_data, ) diff --git a/src/poppler/image.py b/src/poppler/image.py index a8c27e2..df715a1 100644 --- a/src/poppler/image.py +++ b/src/poppler/image.py @@ -18,6 +18,8 @@ from poppler.cpp import image from poppler.rectangle import Rectangle +from typing import List + class Image: @@ -40,11 +42,11 @@ def from_object(cls, poppler_object): return cls(poppler_object=poppler_object) @property - def bytes_per_row(self): + def bytes_per_row(self) -> int: return self._image.bytes_per_row() @property - def const_data(self): + def const_data(self) -> bytes: return self._image.data() def copy(self, rect=None): @@ -52,7 +54,7 @@ def copy(self, rect=None): return Image.from_object(image) @property - def data(self): + def data(self) -> bytes: return self._image.data() @data.setter @@ -64,23 +66,23 @@ def format(self): return self._image.format() @property - def height(self): + def height(self) -> int: return self._image.height() @property - def is_valid(self): + def is_valid(self) -> bool: return self._image.is_valid() - def save(self, file_name, out_format, dpi=-1): + def save(self, file_name, out_format, dpi=-1) -> None: return self._image.save(str(file_name), out_format, dpi) @property - def width(self): + def width(self) -> int: return self._image.width() def memoryview(self): return memoryview(self._image) @staticmethod - def supported_image_formats(): + def supported_image_formats() -> List[str]: return image.supported_image_formats()