Skip to content

Commit f5922dc

Browse files
committed
feat: add type hints
1 parent f96b0b6 commit f5922dc

10 files changed

Lines changed: 51 additions & 27 deletions

File tree

.github/workflows/codestyle.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ jobs:
99
matrix:
1010
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
1111
steps:
12-
- uses: actions/checkout@v2
12+
- uses: actions/checkout@v5
1313
- name: Set up Python ${{ matrix.python-version }}
14-
uses: actions/setup-python@v1
14+
uses: actions/setup-python@v6
1515
with:
1616
python-version: ${{ matrix.python-version }}
17+
allow-prereleases: true
1718
- name: Install dependencies
1819
run: |
1920
python -m pip install --upgrade pip
@@ -23,3 +24,7 @@ jobs:
2324
pip install flake8
2425
# stop the build if there are code styling problems. The GitHub editor is 127 chars wide.
2526
flake8 . --count --max-line-length=127 --show-source --statistics
27+
- name: Check type hints
28+
run: |
29+
pip install mypy lxml-stubs
30+
mypy .

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var/
2222
.installed.cfg
2323
*.egg
2424
tests/output/*
25+
.venv/
26+
.mypy_cache/
2527

2628
# PyInstaller
2729
# Usually these files are written by a python script from a template
@@ -61,4 +63,4 @@ target/
6163
.project
6264

6365
#Pycharm
64-
.idea
66+
.idea

docxtpl/_compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
try:
3+
from html import escape
4+
except ImportError:
5+
# cgi.escape is deprecated in python 3.7
6+
from cgi import escape # type:ignore[attr-defined,no-redef]
7+
8+
9+
__all__ = ("escape",)

docxtpl/inline_image.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,39 @@
44
55
@author: Eric Lapouyade
66
"""
7+
8+
from __future__ import annotations
9+
10+
from typing import IO, TYPE_CHECKING
11+
712
from docx.oxml import OxmlElement, parse_xml
813
from docx.oxml.ns import qn
914

15+
if TYPE_CHECKING:
16+
from docx.shared import Length
17+
from .template import DocxTemplate
18+
1019

1120
class InlineImage(object):
1221
"""Class to generate an inline image
1322
1423
This is much faster than using Subdoc class.
1524
"""
1625

17-
tpl = None
18-
image_descriptor = None
19-
width = None
20-
height = None
26+
tpl: DocxTemplate = None # type:ignore[assignment]
27+
image_descriptor: str | IO[bytes] = None # type:ignore[assignment]
28+
width: int | Length | None = None
29+
height: int | Length | None = None
2130
anchor = None
2231

23-
def __init__(self, tpl, image_descriptor, width=None, height=None, anchor=None):
32+
def __init__(
33+
self,
34+
tpl: DocxTemplate,
35+
image_descriptor: str | IO[bytes],
36+
width: int | Length | None = None,
37+
height: int | Length | None = None,
38+
anchor=None,
39+
) -> None:
2440
self.tpl, self.image_descriptor = tpl, image_descriptor
2541
self.width, self.height = width, height
2642
self.anchor = anchor
@@ -49,8 +65,8 @@ def _add_hyperlink(self, run, url, part):
4965

5066
return run
5167

52-
def _insert_image(self):
53-
pic = self.tpl.current_rendering_part.new_pic_inline(
68+
def _insert_image(self) -> str:
69+
pic = self.tpl.current_rendering_part.new_pic_inline( # type:ignore
5470
self.image_descriptor,
5571
self.width,
5672
self.height,

docxtpl/listing.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
55
@author: Eric Lapouyade
66
"""
7-
try:
8-
from html import escape
9-
except ImportError:
10-
# cgi.escape is deprecated in python 3.7
11-
from cgi import escape
7+
8+
from ._compat import escape
129

1310

1411
class Listing(object):

docxtpl/py.typed

Whitespace-only changes.

docxtpl/richtext.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
55
@author: Eric Lapouyade
66
"""
7-
try:
8-
from html import escape
9-
except ImportError:
10-
# cgi.escape is deprecated in python 3.7
11-
from cgi import escape
7+
8+
from ._compat import escape
129

1310

1411
class RichText(object):

docxtpl/template.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
@author: Eric Lapouyade
66
"""
7+
# mypy: ignore-errors
78

89
from os import PathLike
910
from typing import TYPE_CHECKING, Any, Optional, IO, Union, Dict, Set
@@ -18,11 +19,7 @@
1819
from jinja2 import Environment, Template, meta
1920
from jinja2.exceptions import TemplateError
2021

21-
try:
22-
from html import escape # noqa: F401
23-
except ImportError:
24-
# cgi.escape is deprecated in python 3.7
25-
from cgi import escape # noqa: F401
22+
from ._compat import escape # noqa: F401
2623
import re
2724
import binascii
2825
import os

tests/multi_rendering.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
@author: Eric Lapouyade
66
"""
7+
from typing import cast
78

89
from docxtpl import DocxTemplate
910

@@ -35,6 +36,6 @@
3536

3637
for document_data in documents_data:
3738
dest_file = document_data["dest_file"]
38-
context = document_data["context"]
39+
context = cast(dict, document_data["context"])
3940
tpl.render(context)
4041
tpl.save("output/%s" % dest_file)

tests/replace_picture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
tpl = DocxTemplate("templates/replace_picture_tpl.docx")
1313

14-
context = {}
14+
context: dict = {}
1515

1616
tpl.replace_pic("python_logo.png", "templates/python.png")
1717
tpl.render(context)

0 commit comments

Comments
 (0)