aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/utils
diff options
context:
space:
mode:
authorDmitrii Agibov <dmitrii.agibov@arm.com>2022-09-08 14:24:39 +0100
committerDmitrii Agibov <dmitrii.agibov@arm.com>2022-09-09 17:21:48 +0100
commitf5b293d0927506c2a979a091bf0d07ecc78fa181 (patch)
tree4de585b7cb6ed34da8237063752270189a730a41 /src/mlia/utils
parentcde0c6ee140bd108849bff40467d8f18ffc332ef (diff)
downloadmlia-f5b293d0927506c2a979a091bf0d07ecc78fa181.tar.gz
MLIA-386 Simplify typing in the source code
- Enable deferred annotations evaluation - Use builtin types for type hints whenever possible - Use | syntax for union types - Rename mlia.core._typing into mlia.core.typing Change-Id: I3f6ffc02fa069c589bdd9e8bddbccd504285427a
Diffstat (limited to 'src/mlia/utils')
-rw-r--r--src/mlia/utils/console.py8
-rw-r--r--src/mlia/utils/download.py10
-rw-r--r--src/mlia/utils/filesystem.py20
-rw-r--r--src/mlia/utils/logging.py22
-rw-r--r--src/mlia/utils/types.py7
5 files changed, 33 insertions, 34 deletions
diff --git a/src/mlia/utils/console.py b/src/mlia/utils/console.py
index 7cb3d83..1f428a7 100644
--- a/src/mlia/utils/console.py
+++ b/src/mlia/utils/console.py
@@ -1,9 +1,9 @@
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Console output utility functions."""
+from __future__ import annotations
+
from typing import Iterable
-from typing import List
-from typing import Optional
from rich.console import Console
from rich.console import RenderableType
@@ -13,7 +13,7 @@ from rich.text import Text
def create_section_header(
- section_name: Optional[str] = None, length: int = 80, sep: str = "-"
+ section_name: str | None = None, length: int = 80, sep: str = "-"
) -> str:
"""Return section header."""
if not section_name:
@@ -41,7 +41,7 @@ def style_improvement(result: bool) -> str:
def produce_table(
rows: Iterable,
- headers: Optional[List[str]] = None,
+ headers: list[str] | None = None,
table_style: str = "default",
) -> str:
"""Represent data in tabular form."""
diff --git a/src/mlia/utils/download.py b/src/mlia/utils/download.py
index 4658738..9ef2d9e 100644
--- a/src/mlia/utils/download.py
+++ b/src/mlia/utils/download.py
@@ -1,11 +1,11 @@
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Utils for files downloading."""
+from __future__ import annotations
+
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
-from typing import List
-from typing import Optional
import requests
from rich.progress import BarColumn
@@ -20,10 +20,10 @@ from mlia.utils.types import parse_int
def download_progress(
- content_chunks: Iterable[bytes], content_length: Optional[int], label: Optional[str]
+ content_chunks: Iterable[bytes], content_length: int | None, label: str | None
) -> Iterable[bytes]:
"""Show progress info while reading content."""
- columns: List[ProgressColumn] = [TextColumn("{task.description}")]
+ columns: list[ProgressColumn] = [TextColumn("{task.description}")]
if content_length is None:
total = float("inf")
@@ -44,7 +44,7 @@ def download(
url: str,
dest: Path,
show_progress: bool = False,
- label: Optional[str] = None,
+ label: str | None = None,
chunk_size: int = 8192,
) -> None:
"""Download the file."""
diff --git a/src/mlia/utils/filesystem.py b/src/mlia/utils/filesystem.py
index 0c28d35..25619c5 100644
--- a/src/mlia/utils/filesystem.py
+++ b/src/mlia/utils/filesystem.py
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Utils related to file management."""
+from __future__ import annotations
+
import hashlib
import importlib.resources as pkg_resources
import json
@@ -12,12 +14,8 @@ from tempfile import mkstemp
from tempfile import TemporaryDirectory
from typing import Any
from typing import cast
-from typing import Dict
from typing import Generator
from typing import Iterable
-from typing import List
-from typing import Optional
-from typing import Union
def get_mlia_resources() -> Path:
@@ -37,7 +35,7 @@ def get_profiles_file() -> Path:
return get_mlia_resources() / "profiles.json"
-def get_profiles_data() -> Dict[str, Dict[str, Any]]:
+def get_profiles_data() -> dict[str, dict[str, Any]]:
"""Get the profile values as a dictionary."""
with open(get_profiles_file(), encoding="utf-8") as json_file:
profiles = json.load(json_file)
@@ -48,7 +46,7 @@ def get_profiles_data() -> Dict[str, Dict[str, Any]]:
return profiles
-def get_profile(target_profile: str) -> Dict[str, Any]:
+def get_profile(target_profile: str) -> dict[str, Any]:
"""Get settings for the provided target profile."""
if not target_profile:
raise Exception("Target profile is not provided")
@@ -61,7 +59,7 @@ def get_profile(target_profile: str) -> Dict[str, Any]:
raise Exception(f"Unable to find target profile {target_profile}") from err
-def get_supported_profile_names() -> List[str]:
+def get_supported_profile_names() -> list[str]:
"""Get the supported Ethos-U profile names."""
return list(get_profiles_data().keys())
@@ -73,7 +71,7 @@ def get_target(target_profile: str) -> str:
@contextmanager
-def temp_file(suffix: Optional[str] = None) -> Generator[Path, None, None]:
+def temp_file(suffix: str | None = None) -> Generator[Path, None, None]:
"""Create temp file and remove it after."""
_, tmp_file = mkstemp(suffix=suffix)
@@ -84,14 +82,14 @@ def temp_file(suffix: Optional[str] = None) -> Generator[Path, None, None]:
@contextmanager
-def temp_directory(suffix: Optional[str] = None) -> Generator[Path, None, None]:
+def temp_directory(suffix: str | None = None) -> Generator[Path, None, None]:
"""Create temp directory and remove it after."""
with TemporaryDirectory(suffix=suffix) as tmpdir:
yield Path(tmpdir)
def file_chunks(
- filepath: Union[Path, str], chunk_size: int = 4096
+ filepath: str | Path, chunk_size: int = 4096
) -> Generator[bytes, None, None]:
"""Return sequence of the file chunks."""
with open(filepath, "rb") as file:
@@ -99,7 +97,7 @@ def file_chunks(
yield data
-def hexdigest(filepath: Union[Path, str], hash_obj: "hashlib._Hash") -> str:
+def hexdigest(filepath: str | Path, hash_obj: "hashlib._Hash") -> str:
"""Return hex digest of the file."""
for chunk in file_chunks(filepath):
hash_obj.update(chunk)
diff --git a/src/mlia/utils/logging.py b/src/mlia/utils/logging.py
index 86d7567..793500a 100644
--- a/src/mlia/utils/logging.py
+++ b/src/mlia/utils/logging.py
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Logging utility functions."""
+from __future__ import annotations
+
import logging
from contextlib import contextmanager
from contextlib import ExitStack
@@ -10,8 +12,6 @@ from pathlib import Path
from typing import Any
from typing import Callable
from typing import Generator
-from typing import List
-from typing import Optional
class LoggerWriter:
@@ -61,7 +61,7 @@ class LogFilter(logging.Filter):
return self.log_record_filter(record)
@classmethod
- def equals(cls, log_level: int) -> "LogFilter":
+ def equals(cls, log_level: int) -> LogFilter:
"""Return log filter that filters messages by log level."""
def filter_by_level(log_record: logging.LogRecord) -> bool:
@@ -70,7 +70,7 @@ class LogFilter(logging.Filter):
return cls(filter_by_level)
@classmethod
- def skip(cls, log_level: int) -> "LogFilter":
+ def skip(cls, log_level: int) -> LogFilter:
"""Return log filter that skips messages with particular level."""
def skip_by_level(log_record: logging.LogRecord) -> bool:
@@ -81,15 +81,15 @@ class LogFilter(logging.Filter):
def create_log_handler(
*,
- file_path: Optional[Path] = None,
- stream: Optional[Any] = None,
- log_level: Optional[int] = None,
- log_format: Optional[str] = None,
- log_filter: Optional[logging.Filter] = None,
+ file_path: Path | None = None,
+ stream: Any | None = None,
+ log_level: int | None = None,
+ log_format: str | None = None,
+ log_filter: logging.Filter | None = None,
delay: bool = True,
) -> logging.Handler:
"""Create logger handler."""
- handler: Optional[logging.Handler] = None
+ handler: logging.Handler | None = None
if file_path is not None:
handler = logging.FileHandler(file_path, delay=delay)
@@ -112,7 +112,7 @@ def create_log_handler(
def attach_handlers(
- handlers: List[logging.Handler], loggers: List[logging.Logger]
+ handlers: list[logging.Handler], loggers: list[logging.Logger]
) -> None:
"""Attach handlers to the loggers."""
for handler in handlers:
diff --git a/src/mlia/utils/types.py b/src/mlia/utils/types.py
index 9b63928..ea067b8 100644
--- a/src/mlia/utils/types.py
+++ b/src/mlia/utils/types.py
@@ -1,11 +1,12 @@
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Types related utility functions."""
+from __future__ import annotations
+
from typing import Any
-from typing import Optional
-def is_list_of(data: Any, cls: type, elem_num: Optional[int] = None) -> bool:
+def is_list_of(data: Any, cls: type, elem_num: int | None = None) -> bool:
"""Check if data is a list of object of the same class."""
return (
isinstance(data, (tuple, list))
@@ -24,7 +25,7 @@ def is_number(value: str) -> bool:
return True
-def parse_int(value: Any, default: Optional[int] = None) -> Optional[int]:
+def parse_int(value: Any, default: int | None = None) -> int | None:
"""Parse integer value."""
try:
return int(value)