Clean up mypy type checker errors.
authorScott <[email protected]>
Tue, 1 Feb 2022 05:03:41 +0000 (21:03 -0800)
committerScott <[email protected]>
Tue, 1 Feb 2022 05:03:41 +0000 (21:03 -0800)
datetime_utils.py
list_utils.py
string_utils.py

index 60b859afd0a0ccfec361034f66b2bff615e5bc43..6f504f6c304b850e830cffab08d0ed8be67fed39 100644 (file)
@@ -780,7 +780,7 @@ def describe_timedelta(delta: datetime.timedelta) -> str:
     '1 day, and 10 minutes'
 
     """
     '1 day, and 10 minutes'
 
     """
-    return describe_duration(delta.total_seconds())
+    return describe_duration(int(delta.total_seconds()))  # Note: drops milliseconds
 
 
 def describe_duration_briefly(seconds: int, *, include_seconds=False) -> str:
 
 
 def describe_duration_briefly(seconds: int, *, include_seconds=False) -> str:
@@ -825,7 +825,9 @@ def describe_timedelta_briefly(delta: datetime.timedelta) -> str:
     '1d 10m'
 
     """
     '1d 10m'
 
     """
-    return describe_duration_briefly(delta.total_seconds())
+    return describe_duration_briefly(
+        int(delta.total_seconds())
+    )  # Note: drops milliseconds
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':
index b65ab0d69fab36196a94b4ef3327fae63b88f48d..71630dc9a6fdb4d968f890b2ac4d3b8cb78360d2 100644 (file)
@@ -65,7 +65,7 @@ def remove_list_if_one_element(lst: List[Any]) -> Any:
         return lst
 
 
         return lst
 
 
-def population_counts(lst: List[Any]) -> Mapping[Any, int]:
+def population_counts(lst: List[Any]) -> Counter:
     """
     Return a population count mapping for the list (i.e. the keys are
     list items and the values are the number of occurrances of that
     """
     Return a population count mapping for the list (i.e. the keys are
     list items and the values are the number of occurrances of that
@@ -200,9 +200,9 @@ def ngrams(lst: Sequence[Any], n):
         yield lst[i : i + n]
 
 
         yield lst[i : i + n]
 
 
-def permute(seq: Sequence[Any]):
+def permute(seq: str):
     """
     """
-    Returns all permutations of a sequence; takes O(N^2) time.
+    Returns all permutations of a sequence; takes O(N!) time.
 
     >>> for x in permute('cat'):
     ...     print(x)
 
     >>> for x in permute('cat'):
     ...     print(x)
@@ -217,11 +217,12 @@ def permute(seq: Sequence[Any]):
     yield from _permute(seq, "")
 
 
     yield from _permute(seq, "")
 
 
-def _permute(seq: Sequence[Any], path):
-    if len(seq) == 0:
+def _permute(seq: str, path: str):
+    seq_len = len(seq)
+    if seq_len == 0:
         yield path
 
         yield path
 
-    for i in range(len(seq)):
+    for i in range(seq_len):
         car = seq[i]
         left = seq[0:i]
         right = seq[i + 1 :]
         car = seq[i]
         left = seq[0:i]
         right = seq[i + 1 :]
index 9f67207cdf11af0772a6f3872a17fa5a1be67306..991793d96ad1bf27e503ca2ae673b1b93211e25d 100644 (file)
@@ -27,7 +27,7 @@ This class is based on: https://github.com/daveoncode/python-string-utils.
 """
 
 import base64
 """
 
 import base64
-import contextlib
+import contextlib  # type: ignore
 import datetime
 import io
 from itertools import zip_longest
 import datetime
 import io
 from itertools import zip_longest
@@ -1241,7 +1241,7 @@ class SprintfStdout(object):
 
     def __init__(self) -> None:
         self.destination = io.StringIO()
 
     def __init__(self) -> None:
         self.destination = io.StringIO()
-        self.recorder = None
+        self.recorder: contextlib.redirect_stdout
 
     def __enter__(self) -> Callable[[], str]:
         self.recorder = contextlib.redirect_stdout(self.destination)
 
     def __enter__(self) -> Callable[[], str]:
         self.recorder = contextlib.redirect_stdout(self.destination)
@@ -1338,7 +1338,7 @@ def trigrams(txt: str):
 
 
 def shuffle_columns_into_list(
 
 
 def shuffle_columns_into_list(
-    input_lines: Iterable[str], column_specs: Iterable[Iterable[int]], delim=''
+    input_lines: Sequence[str], column_specs: Iterable[Iterable[int]], delim=''
 ) -> Iterable[str]:
     """Helper to shuffle / parse columnar data and return the results as a
     list.  The column_specs argument is an iterable collection of
 ) -> Iterable[str]:
     """Helper to shuffle / parse columnar data and return the results as a
     list.  The column_specs argument is an iterable collection of
@@ -1368,7 +1368,7 @@ def shuffle_columns_into_list(
 
 
 def shuffle_columns_into_dict(
 
 
 def shuffle_columns_into_dict(
-    input_lines: Iterable[str],
+    input_lines: Sequence[str],
     column_specs: Iterable[Tuple[str, Iterable[int]]],
     delim='',
 ) -> Dict[str, str]:
     column_specs: Iterable[Tuple[str, Iterable[int]]],
     delim='',
 ) -> Dict[str, str]:
@@ -1425,7 +1425,7 @@ def to_ascii(x: str):
     raise Exception('to_ascii works with strings and bytes')
 
 
     raise Exception('to_ascii works with strings and bytes')
 
 
-def to_base64(txt: str, *, encoding='utf-8', errors='surrogatepass') -> str:
+def to_base64(txt: str, *, encoding='utf-8', errors='surrogatepass') -> bytes:
     """Encode txt and then encode the bytes with a 64-character
     alphabet.  This is compatible with uudecode.
 
     """Encode txt and then encode the bytes with a 64-character
     alphabet.  This is compatible with uudecode.
 
@@ -1458,7 +1458,7 @@ def is_base64(txt: str) -> bool:
     return True
 
 
     return True
 
 
-def from_base64(b64: str, encoding='utf-8', errors='surrogatepass') -> str:
+def from_base64(b64: bytes, encoding='utf-8', errors='surrogatepass') -> str:
     """Convert base64 encoded string back to normal strings.
 
     >>> from_base64(b'aGVsbG8/\\n')
     """Convert base64 encoded string back to normal strings.
 
     >>> from_base64(b'aGVsbG8/\\n')
@@ -1529,7 +1529,7 @@ def from_bitstring(bits: str, encoding='utf-8', errors='surrogatepass') -> str:
     return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or '\0'
 
 
     return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or '\0'
 
 
-def ip_v4_sort_key(txt: str) -> Tuple[int]:
+def ip_v4_sort_key(txt: str) -> Optional[Tuple[int, ...]]:
     """Turn an IPv4 address into a tuple for sorting purposes.
 
     >>> ip_v4_sort_key('10.0.0.18')
     """Turn an IPv4 address into a tuple for sorting purposes.
 
     >>> ip_v4_sort_key('10.0.0.18')
@@ -1546,7 +1546,7 @@ def ip_v4_sort_key(txt: str) -> Tuple[int]:
     return tuple([int(x) for x in txt.split('.')])
 
 
     return tuple([int(x) for x in txt.split('.')])
 
 
-def path_ancestors_before_descendants_sort_key(volume: str) -> Tuple[str]:
+def path_ancestors_before_descendants_sort_key(volume: str) -> Tuple[str, ...]:
     """Chunk up a file path so that parent/ancestor paths sort before
     children/descendant paths.
 
     """Chunk up a file path so that parent/ancestor paths sort before
     children/descendant paths.