diff --git a/glances/cpu_percent.py b/glances/cpu_percent.py index 5a498722..ee7e78eb 100644 --- a/glances/cpu_percent.py +++ b/glances/cpu_percent.py @@ -9,7 +9,7 @@ """CPU percent stats shared between CPU and Quicklook plugins.""" import platform -from typing import Optional, TypedDict +from typing import TypedDict import psutil @@ -21,8 +21,8 @@ __all__ = ["cpu_percent"] class CpuInfo(TypedDict): cpu_name: str - cpu_hz: Optional[float] - cpu_hz_current: Optional[float] + cpu_hz: float | None + cpu_hz_current: float | None class PerCpuPercentInfo(TypedDict): @@ -32,15 +32,15 @@ class PerCpuPercentInfo(TypedDict): user: float system: float idle: float - nice: Optional[float] - iowait: Optional[float] - irq: Optional[float] - softirq: Optional[float] - steal: Optional[float] - guest: Optional[float] - guest_nice: Optional[float] - dpc: Optional[float] - interrupt: Optional[float] + nice: float | None + iowait: float | None + irq: float | None + softirq: float | None + steal: float | None + guest: float | None + guest_nice: float | None + dpc: float | None + interrupt: float | None class CpuPercent: diff --git a/glances/globals.py b/glances/globals.py index 977c50f3..4befb953 100644 --- a/glances/globals.py +++ b/glances/globals.py @@ -364,7 +364,7 @@ def json_dumps(data) -> bytes: return b(res) -def json_loads(data: Union[str, bytes, bytearray]) -> Union[dict, list]: +def json_loads(data: str | bytes | bytearray) -> dict | list: """Load a JSON buffer into memory as a Python object""" return json.loads(data) @@ -401,7 +401,7 @@ def dictlist_json_dumps(data, item): return json_dumps(dl) -def dictlist_first_key_value(data: list[dict], key, value) -> Optional[dict]: +def dictlist_first_key_value(data: list[dict], key, value) -> dict | None: """In a list of dict, return first item where key=value or none if not found.""" try: ret = next(item for item in data if key in item and item[key] == value) diff --git a/glances/outputs/glances_restful_api.py b/glances/outputs/glances_restful_api.py index c16a15ee..03bb7e64 100644 --- a/glances/outputs/glances_restful_api.py +++ b/glances/outputs/glances_restful_api.py @@ -12,7 +12,7 @@ import os import socket import sys import webbrowser -from typing import Annotated, Any, Union +from typing import Annotated, Any from urllib.parse import urljoin from glances import __apiversion__, __version__ @@ -815,7 +815,7 @@ class GlancesRestfulApi: else: return GlancesJSONResponse(ret) - def _api_value(self, plugin: str, item: str, value: Union[str, int, float]): + def _api_value(self, plugin: str, item: str, value: str | int | float): """Glances API RESTful implementation. Return the process stats (dict) for the given item=value diff --git a/glances/plugins/containers/__init__.py b/glances/plugins/containers/__init__.py index 0ef4df40..332b0088 100644 --- a/glances/plugins/containers/__init__.py +++ b/glances/plugins/containers/__init__.py @@ -11,7 +11,7 @@ from copy import deepcopy from functools import partial, reduce from itertools import chain -from typing import Any, Optional +from typing import Any from glances.globals import nativestr from glances.logger import logger @@ -514,7 +514,7 @@ class ContainersPlugin(GlancesPluginModel): return ret - def msg_curse(self, args=None, max_width: Optional[int] = None) -> list[str]: + def msg_curse(self, args=None, max_width: int | None = None) -> list[str]: """Return the dict to display in the curse interface.""" # Init the return message init = [] @@ -594,7 +594,6 @@ def sort_docker_stats(stats: list[dict[str, Any]]) -> tuple[str, list[dict[str, # Return the main sort key and the sorted stats return sort_by, stats - # Return the main sort key and the sorted stats - return sort_by, stats - # Return the main sort key and the sorted stats - return sort_by, stats + + +# End of file diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index 95f95c87..2a867cf0 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -9,7 +9,7 @@ """Docker Extension unit for Glances' Containers plugin.""" import time -from typing import Any, Optional +from typing import Any from glances.globals import nativestr, pretty_date, replace_special_chars from glances.logger import logger @@ -83,7 +83,7 @@ class DockerStatsFetcher: # In case no update, default to 1 return max(1, self._streamer.last_update_time - self._last_stats_computed_time) - def _get_cpu_stats(self) -> Optional[dict[str, float]]: + def _get_cpu_stats(self) -> dict[str, float] | None: """Return the container CPU usage. Output: a dict {'total': 1.49} @@ -117,7 +117,7 @@ class DockerStatsFetcher: # Return the stats return stats - def _get_memory_stats(self) -> Optional[dict[str, float]]: + def _get_memory_stats(self) -> dict[str, float] | None: """Return the container MEMORY. Output: a dict {'usage': ..., 'limit': ..., 'inactive_file': ...} @@ -140,7 +140,7 @@ class DockerStatsFetcher: # Return the stats return stats - def _get_network_stats(self) -> Optional[dict[str, float]]: + def _get_network_stats(self) -> dict[str, float] | None: """Return the container network usage using the Docker API (v1.0 or higher). Output: a dict {'time_since_update': 3000, 'rx': 10, 'tx': 65}. @@ -169,7 +169,7 @@ class DockerStatsFetcher: # Return the stats return stats - def _get_io_stats(self) -> Optional[dict[str, float]]: + def _get_io_stats(self) -> dict[str, float] | None: """Return the container IO usage using the Docker API (v1.0 or higher). Output: a dict {'time_since_update': 3000, 'ior': 10, 'iow': 65}. diff --git a/glances/plugins/containers/engines/podman.py b/glances/plugins/containers/engines/podman.py index c92783e1..d1abecfe 100644 --- a/glances/plugins/containers/engines/podman.py +++ b/glances/plugins/containers/engines/podman.py @@ -9,7 +9,7 @@ import time from datetime import datetime -from typing import Any, Optional +from typing import Any from glances.globals import nativestr, pretty_date, replace_special_chars, string_value_to_float from glances.logger import logger @@ -164,7 +164,7 @@ class PodmanPodStatsFetcher: return result_stats - def _get_cpu_stats(self, stats: dict) -> Optional[dict]: + def _get_cpu_stats(self, stats: dict) -> dict | None: """Return the container CPU usage. Output: a dict {'total': 1.49} @@ -176,7 +176,7 @@ class PodmanPodStatsFetcher: cpu_usage = string_value_to_float(stats["CPU"].rstrip("%")) return {"total": cpu_usage} - def _get_memory_stats(self, stats) -> Optional[dict]: + def _get_memory_stats(self, stats) -> dict | None: """Return the container MEMORY. Output: a dict {'usage': ..., 'limit': ...} @@ -197,7 +197,7 @@ class PodmanPodStatsFetcher: return {'usage': usage, 'limit': limit, 'inactive_file': 0} - def _get_network_stats(self, stats) -> Optional[dict]: + def _get_network_stats(self, stats) -> dict | None: """Return the container network usage using the Docker API (v1.0 or higher). Output: a dict {'time_since_update': 3000, 'rx': 10, 'tx': 65}. @@ -223,7 +223,7 @@ class PodmanPodStatsFetcher: # Hardcode `time_since_update` to 1 as podman docs don't specify the rate calculation procedure return {"rx": rx, "tx": tx, "time_since_update": 1} - def _get_io_stats(self, stats) -> Optional[dict]: + def _get_io_stats(self, stats) -> dict | None: """Return the container IO usage using the Docker API (v1.0 or higher). Output: a dict {'time_since_update': 3000, 'ior': 10, 'iow': 65}. diff --git a/glances/plugins/gpu/cards/amd.py b/glances/plugins/gpu/cards/amd.py index 345cd038..638c29e1 100644 --- a/glances/plugins/gpu/cards/amd.py +++ b/glances/plugins/gpu/cards/amd.py @@ -43,7 +43,6 @@ import functools import glob import os import re -from typing import Optional DRM_ROOT_FOLDER: str = '/sys/class/drm' DEVICE_FOLDER_PATTERN: str = 'card[0-9]/device' @@ -105,7 +104,7 @@ def get_device_list(drm_root_folder: str) -> list[str]: return ret -def read_file(*path_segments: str) -> Optional[str]: +def read_file(*path_segments: str) -> str | None: """Return content of file.""" path = os.path.join(*path_segments) if os.path.isfile(path): @@ -139,7 +138,7 @@ def get_device_name(device_folder: str) -> str: return 'AMD GPU' -def get_mem(device_folder: str) -> Optional[int]: +def get_mem(device_folder: str) -> int | None: """Return the memory consumption in %.""" mem_info_total = read_file(device_folder, GPU_MEM_TOTAL) mem_info_used = read_file(device_folder, GPU_MEM_USED) @@ -160,14 +159,14 @@ def get_mem(device_folder: str) -> Optional[int]: return None -def get_proc(device_folder: str) -> Optional[int]: +def get_proc(device_folder: str) -> int | None: """Return the processor consumption in %.""" if gpu_busy_percent := read_file(device_folder, GPU_PROC_PERCENT): return int(gpu_busy_percent) return None -def get_temperature(device_folder: str) -> Optional[int]: +def get_temperature(device_folder: str) -> int | None: """Return the processor temperature in °C (mean of all HWMON)""" temp_input = [] for temp_file in glob.glob(HWMON_TEMPERATURE_PATTERN, root_dir=device_folder): @@ -180,6 +179,6 @@ def get_temperature(device_folder: str) -> Optional[int]: return None -def get_fan_speed(device_folder: str) -> Optional[int]: +def get_fan_speed(device_folder: str) -> int | None: """Return the fan speed in %.""" return None diff --git a/glances/plugins/vms/__init__.py b/glances/plugins/vms/__init__.py index 87ca1c20..50aae23e 100644 --- a/glances/plugins/vms/__init__.py +++ b/glances/plugins/vms/__init__.py @@ -9,7 +9,7 @@ """Vms plugin.""" from copy import deepcopy -from typing import Any, Optional +from typing import Any from glances.logger import logger from glances.plugins.plugin.model import GlancesPluginModel @@ -201,7 +201,7 @@ class VmsPlugin(GlancesPluginModel): return True - def msg_curse(self, args=None, max_width: Optional[int] = None) -> list[str]: + def msg_curse(self, args=None, max_width: int | None = None) -> list[str]: """Return the dict to display in the curse interface.""" # Init the return message ret = [] @@ -345,3 +345,6 @@ def sort_vm_stats(stats: list[dict[str, Any]]) -> tuple[str, list[dict[str, Any] # Return the main sort key and the sorted stats return sort_by, stats + + +# End of file diff --git a/pyproject.toml b/pyproject.toml index eb05ab16..b0bcdbb3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -134,7 +134,7 @@ include = ["glances*"] [tool.ruff] line-length = 120 -target-version = "py39" +target-version = "py310" [tool.ruff.format] quote-style = "preserve"