1+ from __future__ import annotations
2+
13import argparse
24import json
35import os
1113QUIET_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
0 commit comments