Add type annotations from monkeytype + jw-devops/test

Add type annotations as generated by monkeytype and jw-devops/test, plus some
hand editing to satisfy both monkeytype and mypy.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2020-04-10 17:55:36 +02:00
commit 77d43aebad
7 changed files with 51 additions and 42 deletions

View file

@ -1,4 +1,6 @@
from __future__ import annotations
from collections import OrderedDict
from typing import Optional, Union
from jwutils.log import *
def quote(s):
@ -10,7 +12,7 @@ def quote(s):
return "'" + s + "'"
return '"' + s + '"'
def is_quoted(s):
def is_quoted(s: str) -> bool:
if isinstance(s, StringTree):
return False
s = s.strip()
@ -21,7 +23,7 @@ def is_quoted(s):
return True
return False
def cleanup_string(s):
def cleanup_string(s: str) -> str:
if isinstance(s, StringTree):
return s
s = s.strip()
@ -31,9 +33,9 @@ def cleanup_string(s):
class StringTree: # export
def __init__(self, path, content):
def __init__(self, path: str, content: str) -> None:
slog(DEBUG, "ctor, path =", path, "content =", content)
self.children = OrderedDict()
self.children: OrderedDict[str, StringTree] = OrderedDict()
self.content = None
self.__set(path, content)
assert(hasattr(self, "content"))
@ -106,10 +108,10 @@ class StringTree: # export
self.children[nibble].children[gc.content] = gc
return self.children[nibble]
def __str__(self):
def __str__(self) -> str:
return 'st:"{}"'.format(self.content)
def __getitem__(self, path):
def __getitem__(self, path: str) -> str:
r = self.get(path)
if r is None:
raise KeyError(path)
@ -139,11 +141,11 @@ class StringTree: # export
raise Exception("Tried to set empty content")
self.content = content
def add(self, path, content = None, split = True):
def add(self, path: str, content: Optional[Union[str, StringTree]] = None, split: bool = True) -> StringTree:
slog(DEBUG, "adding >{}< at >{}< to >{}<".format(content, path, self.content))
return self.__set(path, content, split)
def get(self, path_):
def get(self, path_: str) -> Optional[StringTree]:
slog(DEBUG, 'looking for "{}" in "{}"'.format(path_, self.content))
assert not isinstance(path_, int)
path = cleanup_string(path_)
@ -168,10 +170,10 @@ class StringTree: # export
relpath = '.'.join(components[1:])
return self.children[name].get(relpath)
def value(self):
def value(self) -> str:
if len(self.children) == 0:
return None
return self.children[next(reversed(self.children))].content
raise Exception('tried to get value from leave "{}"'.format(self.content))
return self.children[next(reversed(self.children))].content # type: ignore
def child_list(self, depth_first=True):
if depth_first == False:
@ -181,7 +183,7 @@ class StringTree: # export
r.append(c)
r.extend(c.to_list())
def dump(self, prio, *args, **kwargs):
def dump(self, prio: int, *args, **kwargs) -> None:
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
msg = ''
if args is not None: