Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic mypy support #829

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install system dependencies ⚙️
if: matrix.platform == 'ubuntu-latest'
run: sudo apt-get update && sudo apt-get install ghostscript libjpeg-dev
run: sudo apt-get update && sudo apt-get install ghostscript libjpeg-dev swig
- name: Install qpdf ⚙️
if: matrix.platform == 'ubuntu-latest' && matrix.python-version != '3.9'
# We run the unit tests WITHOUT qpdf for a single parallel execution / Python version:
Expand Down
30 changes: 15 additions & 15 deletions fpdf/annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hashlib
from datetime import datetime
from typing import NamedTuple, Tuple, Union
from typing import NamedTuple, Tuple, Union, Optional, Dict

from .actions import Action
from .enums import AnnotationFlag, AnnotationName, FileAttachmentAnnotationName
Expand Down Expand Up @@ -31,18 +31,18 @@ def __init__(
width: int,
height: int,
flags: Tuple[AnnotationFlag] = DEFAULT_ANNOT_FLAGS,
contents: str = None,
dest: Destination = None,
action: Action = None,
color: tuple = None,
modification_time: datetime = None,
title: str = None,
quad_points: tuple = None,
contents: Optional[str] = None,
dest: Optional[Destination] = None,
action: Optional[Action] = None,
color: Optional[tuple] = None,
modification_time: Optional[datetime] = None,
title: Optional[str] = None,
quad_points: Optional[tuple] = None,
border_width: int = 0, # PDF readers support: displayed by Acrobat but not Sumatra
name: Union[AnnotationName, FileAttachmentAnnotationName] = None,
ink_list: Tuple[int] = (), # for ink annotations
file_spec: str = None,
field_type: str = None,
name: Optional[Union[AnnotationName, FileAttachmentAnnotationName]] = None,
ink_list: Optional[Tuple[int]] = None, # for ink annotations
file_spec: Optional[str] = None,
field_type: Optional[str] = None,
value=None,
):
self.type = Name("Annot")
Expand Down Expand Up @@ -123,14 +123,14 @@ def __init__(
basename: str,
contents: bytes,
desc: str = "",
creation_date: datetime = None,
modification_date: datetime = None,
creation_date: Optional[datetime] = None,
modification_date: Optional[datetime] = None,
compress: bool = False,
checksum: bool = False,
):
super().__init__(contents=contents, compress=compress)
self.type = Name("EmbeddedFile")
params = {"/Size": len(contents)}
params: Dict[str, Union[int, str]] = {"/Size": len(contents)}
if creation_date:
params["/CreationDate"] = PDFDate(creation_date, with_tz=True).serialize()
if modification_date:
Expand Down
14 changes: 7 additions & 7 deletions fpdf/drawing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy, decimal, math, re
from collections import OrderedDict
from contextlib import contextmanager
from typing import Optional, NamedTuple, Union
from typing import Optional, NamedTuple, Union, Dict

from .enums import (
BlendMode,
Expand All @@ -15,7 +15,7 @@
from .syntax import Name, Raw
from .util import escape_parens

__pdoc__ = {"force_nodocument": False}
__pdoc__: Dict[str, Union[bool, str]] = {"force_nodocument": False}


def force_nodocument(item):
Expand Down Expand Up @@ -195,7 +195,7 @@ class DeviceRGB(
OPERATOR = "rg"
"""The PDF drawing operator used to specify this type of color."""

def __new__(cls, r, g, b, a=None):
def __new__(cls, r: float, g: float, b: float, a: Optional[float] = None):
if a is not None:
_check_range(a)

Expand Down Expand Up @@ -240,7 +240,7 @@ class DeviceGray(
OPERATOR = "g"
"""The PDF drawing operator used to specify this type of color."""

def __new__(cls, g, a=None):
def __new__(cls, g: float, a: Optional[float] = None):
if a is not None:
_check_range(a)

Expand Down Expand Up @@ -605,7 +605,7 @@ def __mul__(self, other):

return NotImplemented

__rmul__ = __mul__
__rmul__ = __mul__ # type: ignore[misc]

@force_document
def __truediv__(self, other):
Expand Down Expand Up @@ -983,7 +983,7 @@ def __mul__(self, other):
return NotImplemented

# scalar multiplication is commutative
__rmul__ = __mul__
__rmul__ = __mul__ # type: ignore[misc]

@force_document
def __matmul__(self, other):
Expand Down Expand Up @@ -3240,7 +3240,7 @@ class PaintedPath:
primitive path elements and `GraphicsContext`.
"""

def __init__(self, x=0, y=0):
def __init__(self, x: float = 0, y: float = 0) -> None:
self._root_graphics_context = GraphicsContext()
self._graphics_context = self._root_graphics_context

Expand Down
2 changes: 1 addition & 1 deletion fpdf/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class FPDFException(Exception):
class FPDFPageFormatException(FPDFException):
"""Error is thrown when a bad page format is given"""

def __init__(self, argument, unknown=False, one=False):
def __init__(self, argument, unknown: bool = False, one: bool = False):
super().__init__()
if unknown and one:
raise TypeError(
Expand Down
Loading