type hinting fixes

This commit is contained in:
scito
2022-12-30 01:22:05 +01:00
parent 58fc1b85ac
commit fb43c6793c
3 changed files with 9 additions and 8 deletions

View File

@@ -6,3 +6,4 @@ pytest-mock
pytest-cov pytest-cov
mypy mypy
types-protobuf types-protobuf
setuptools

View File

@@ -1,4 +1,3 @@
from __future__ import annotations # for compatibility with Python < 3.11
import pathlib import pathlib
from setuptools import setup from setuptools import setup

View File

@@ -22,7 +22,7 @@ import re
import shutil import shutil
import sys import sys
import pathlib import pathlib
from typing import BinaryIO, Any, Union from typing import BinaryIO, Any, Union, List
# Types # Types
@@ -31,7 +31,8 @@ PathLike = Union[str, pathlib.Path]
# Ref. https://stackoverflow.com/a/16571630 # Ref. https://stackoverflow.com/a/16571630
class Capturing(list[Any]): # PYTHON 3.11: class Capturing(list[Any]):
class Capturing(List[Any]):
'''Capture stdout and stderr '''Capture stdout and stderr
Usage: Usage:
with Capturing() as output: with Capturing() as output:
@@ -72,19 +73,19 @@ def remove_dir_with_files(dir: PathLike) -> None:
if os.path.exists(dir): shutil.rmtree(dir) if os.path.exists(dir): shutil.rmtree(dir)
def read_csv(filename: str) -> list[list[str]]: def read_csv(filename: str) -> List[List[str]]:
"""Returns a list of lines.""" """Returns a list of lines."""
with open(filename, "r", encoding="utf-8", newline='') as infile: with open(filename, "r", encoding="utf-8", newline='') as infile:
lines: list[list[str]] = [] lines: List[List[str]] = []
reader = csv.reader(infile) reader = csv.reader(infile)
for line in reader: for line in reader:
lines.append(line) lines.append(line)
return lines return lines
def read_csv_str(data_str: str) -> list[list[str]]: def read_csv_str(data_str: str) -> List[List[str]]:
"""Returns a list of lines.""" """Returns a list of lines."""
lines: list[list[str]] = [] lines: List[List[str]] = []
reader = csv.reader(data_str.splitlines()) reader = csv.reader(data_str.splitlines())
for line in reader: for line in reader:
lines.append(line) lines.append(line)
@@ -102,7 +103,7 @@ def read_json_str(data_str: str) -> Any:
return json.loads(data_str) return json.loads(data_str)
def read_file_to_list(filename: str) -> list[str]: def read_file_to_list(filename: str) -> List[str]:
"""Returns a list of lines.""" """Returns a list of lines."""
with open(filename, "r", encoding="utf-8") as infile: with open(filename, "r", encoding="utf-8") as infile:
return infile.readlines() return infile.readlines()