Skip to content

Commit ae134ab

Browse files
committed
refactor: type hints for __main__.py
1 parent f5922dc commit ae134ab

5 files changed

Lines changed: 138 additions & 114 deletions

File tree

.github/workflows/codestyle.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ jobs:
2828
run: |
2929
pip install mypy lxml-stubs
3030
mypy .
31+
- name: Ensure library work without docxcompose
32+
run: |
33+
pip uninstall docxcompose
34+
python -c "from docxtpl import *"

.gitignore

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

2826
# PyInstaller
2927
# Usually these files are written by a python script from a template
@@ -64,3 +62,6 @@ target/
6462

6563
#Pycharm
6664
.idea
65+
66+
# In Project Virtual Environment
67+
.venv/

docxtpl/__main__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import argparse
24
import json
35
import os
@@ -11,7 +13,7 @@
1113
QUIET_ARG = "quiet"
1214

1315

14-
def make_arg_parser():
16+
def make_arg_parser() -> argparse.ArgumentParser:
1517
parser = argparse.ArgumentParser(
1618
usage="python -m docxtpl [-h] [-o] [-q] {} {} {}".format(
1719
TEMPLATE_ARG, JSON_ARG, OUTPUT_ARG
@@ -42,7 +44,7 @@ def make_arg_parser():
4244
return parser
4345

4446

45-
def get_args(parser):
47+
def get_args(parser) -> dict:
4648
try:
4749
parsed_args = vars(parser.parse_args())
4850
return parsed_args
@@ -57,7 +59,7 @@ def get_args(parser):
5759
)
5860

5961

60-
def is_argument_valid(arg_name, arg_value, overwrite):
62+
def is_argument_valid(arg_name: str, arg_value: str, overwrite: bool) -> bool | None:
6163
# Basic checks for the arguments
6264
if arg_name == TEMPLATE_ARG:
6365
return os.path.isfile(arg_value) and arg_value.endswith(".docx")
@@ -69,9 +71,10 @@ def is_argument_valid(arg_name, arg_value, overwrite):
6971
)
7072
elif arg_name in [OVERWRITE_ARG, QUIET_ARG]:
7173
return arg_value in [True, False]
74+
return None
7275

7376

74-
def check_exists_ask_overwrite(arg_value, overwrite):
77+
def check_exists_ask_overwrite(arg_value: str, overwrite: bool) -> bool:
7578
# If output file does not exist or command was run with overwrite option,
7679
# returns True, else asks for overwrite confirmation. If overwrite is
7780
# confirmed returns True, else raises OSError.
@@ -93,7 +96,7 @@ def check_exists_ask_overwrite(arg_value, overwrite):
9396
return True
9497

9598

96-
def validate_all_args(parsed_args):
99+
def validate_all_args(parsed_args: dict) -> None:
97100
overwrite = parsed_args[OVERWRITE_ARG]
98101
# Raises AssertionError if any of the arguments is not validated
99102
try:
@@ -108,7 +111,7 @@ def validate_all_args(parsed_args):
108111
)
109112

110113

111-
def get_json_data(json_path):
114+
def get_json_data(json_path) -> dict:
112115
with open(json_path) as file:
113116
try:
114117
json_data = json.load(file)
@@ -121,22 +124,22 @@ def get_json_data(json_path):
121124
raise RuntimeError("Failed to get json data.")
122125

123126

124-
def make_docxtemplate(template_path):
127+
def make_docxtemplate(template_path: str) -> DocxTemplate:
125128
try:
126129
return DocxTemplate(template_path)
127130
except TemplateError:
128131
raise RuntimeError("Could not create docx template.")
129132

130133

131-
def render_docx(doc, json_data):
134+
def render_docx(doc: DocxTemplate, json_data: dict) -> DocxTemplate:
132135
try:
133136
doc.render(json_data)
134137
return doc
135138
except TemplateError:
136139
raise RuntimeError("An error ocurred while trying to render the docx")
137140

138141

139-
def save_file(doc, parsed_args):
142+
def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
140143
try:
141144
output_path = parsed_args[OUTPUT_ARG]
142145
doc.save(output_path)
@@ -151,7 +154,7 @@ def save_file(doc, parsed_args):
151154
raise RuntimeError("Failed to save file.")
152155

153156

154-
def main():
157+
def main() -> None:
155158
parser = make_arg_parser()
156159
# Everything is in a try-except block that catches a RuntimeError that is
157160
# raised if any of the individual functions called cause an error

docxtpl/inline_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _add_hyperlink(self, run, url, part):
6666
return run
6767

6868
def _insert_image(self) -> str:
69-
pic = self.tpl.current_rendering_part.new_pic_inline( # type:ignore
69+
pic = self.tpl.current_rendering_part.new_pic_inline(
7070
self.image_descriptor,
7171
self.width,
7272
self.height,

0 commit comments

Comments
 (0)