From 74030cf0edf46110dcef23d307c224727d751f84 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Feb 2022 09:53:35 -0500 Subject: Add typings to some standalone functions Signed-off-by: Matthew Peveler --- asciidoc/a2x.py | 17 +++++++++-------- asciidoc/asciidoc.py | 5 +++-- asciidoc/utils.py | 18 +++++++++--------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/asciidoc/a2x.py b/asciidoc/a2x.py index be70367..5bccbc4 100644 --- a/asciidoc/a2x.py +++ b/asciidoc/a2x.py @@ -41,6 +41,7 @@ import shutil import subprocess import sys import traceback +from typing import List, NoReturn, Tuple, Union from urllib.parse import urlparse import zipfile import xml.dom.minidom @@ -97,36 +98,36 @@ XSLTPROC_OPTS = '' OPTIONS = None # These functions read verbose and dry_run command options. -def errmsg(msg): +def errmsg(msg: str) -> None: print('%s: %s\n' % (PROG, msg), file=sys.stderr) -def warning(msg): +def warning(msg: str) -> None: errmsg('WARNING: %s' % msg) -def infomsg(msg): +def infomsg(msg: str) -> None: print('%s: %s' % (PROG, msg)) -def die(msg, exit_code=1): +def die(msg: str, exit_code: int = 1) -> NoReturn: errmsg('ERROR: %s' % msg) sys.exit(exit_code) -def trace(): +def trace() -> None: """Print traceback to stderr.""" errmsg('-'*60) traceback.print_exc(file=sys.stderr) errmsg('-'*60) -def verbose(msg): +def verbose(msg: str) -> None: if OPTIONS.verbose or OPTIONS.dry_run: infomsg(msg) -def flatten(array): +def flatten(array: Union[List, Tuple]) -> List: ret = [] for x in array: if isinstance(x, (list, tuple)): @@ -136,7 +137,7 @@ def flatten(array): return ret -def isexecutable(file_name): +def isexecutable(file_name: str) -> bool: return os.path.isfile(file_name) and os.access(file_name, os.X_OK) diff --git a/asciidoc/asciidoc.py b/asciidoc/asciidoc.py index 61690c4..bfb7ff1 100644 --- a/asciidoc/asciidoc.py +++ b/asciidoc/asciidoc.py @@ -28,6 +28,7 @@ import subprocess import sys import tempfile import time +import typing import traceback import unicodedata import zipfile @@ -5681,7 +5682,7 @@ class Tables_OLD(AbstractBlocks): # --------------------------------------------------------------------------- # filter and theme plugin commands. # --------------------------------------------------------------------------- -def die(msg): +def die(msg: str) -> typing.NoReturn: message.stderr(msg) sys.exit(1) @@ -5909,7 +5910,7 @@ trace = Trace() # Implements trace attribute processing. messages = message.messages -def set_caller(name): +def set_caller(name: str) -> None: global APPLICATION_CALLER APPLICATION_CALLER = name diff --git a/asciidoc/utils.py b/asciidoc/utils.py index fee2c00..82863c3 100644 --- a/asciidoc/utils.py +++ b/asciidoc/utils.py @@ -3,7 +3,7 @@ import math import os import re import time -from typing import Optional +from typing import List, Optional, Tuple, Union import unicodedata @@ -36,7 +36,7 @@ def assign(dst, src): setattr(dst, a, v) -def strip_quotes(s): +def strip_quotes(s: str) -> str: """Trim white space and, if necessary, quote characters from s.""" s = s.strip() # Strip quotation mark characters from quoted strings. @@ -54,7 +54,7 @@ def is_re(s) -> bool: return False -def re_join(relist): +def re_join(relist: List) -> List: """Join list of regular expressions re1,re2,... to single regular expression (re1)|(re2)|...""" if len(relist) == 0: @@ -68,7 +68,7 @@ def re_join(relist): return result -def lstrip_list(s): +def lstrip_list(s: Union[List, Tuple]) -> Union[List, Tuple]: """ Return list with empty items from start of list removed. """ @@ -80,7 +80,7 @@ def lstrip_list(s): return s[i:] -def rstrip_list(s): +def rstrip_list(s: Union[List, Tuple]) -> Union[List, Tuple]: """ Return list with empty items from end of list removed. """ @@ -92,7 +92,7 @@ def rstrip_list(s): return s[:i + 1] -def strip_list(s): +def strip_list(s: Union[List, Tuple]) -> Union[List, Tuple]: """ Return list with empty items from start and end of list removed. """ @@ -133,7 +133,7 @@ def dovetail_tags(stag, content, etag): return dovetail(dovetail(stag, content), etag) -def py2round(n, d=0): +def py2round(n: Union[float, int], d: int = 0) -> int: """Utility function to get python2 rounding in python3. Python3 changed it such that given two equally close multiples, it'll round towards the even choice. For example, round(42.5) == 42 instead of the expected round(42.5) == 43). This function gives us @@ -155,14 +155,14 @@ east_asian_widths = { column widths.""" -def column_width(s): +def column_width(s: str) -> int: width = 0 for c in s: width += east_asian_widths[unicodedata.east_asian_width(c)] return width -def date_time_str(t): +def date_time_str(t: float) -> Tuple[str, str]: """Convert seconds since the Epoch to formatted local date and time strings.""" source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH') if source_date_epoch is not None: -- cgit v1.2.1