Skip to content

Commit 81cdaff

Browse files
authored
Merge pull request #536 from powerapi-ng/refactor/use-pep5850-annotations
refactor: Use PEP-585 type hints for standard collections
2 parents da2ba34 + 97891a4 commit 81cdaff

29 files changed

+100
-119
lines changed

.ruff.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ select = [
1616
ignore = [
1717
"E501", # line too long
1818

19-
"UP006", # non-pep585-annotation
2019
"UP007", # non-pep604-annotation-union
2120
"UP015", # redundant-open-modes
2221
"UP031", # printf-string-formatting

src/powerapi/cli/config_validator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import logging
3131
import os
3232

33-
from typing import Dict
34-
3533
from powerapi.exception import MissingArgumentException, NotAllowedArgumentValueException, FileDoesNotExistException, \
3634
UnexistingActorException
3735

@@ -42,7 +40,7 @@ class ConfigValidator:
4240
"""
4341

4442
@staticmethod
45-
def validate(config: Dict):
43+
def validate(config: dict):
4644
"""
4745
Validate powerapi config and initialize missing default values
4846
"""
@@ -101,7 +99,7 @@ def validate(config: Dict):
10199
ConfigValidator._validate_input(config)
102100

103101
@staticmethod
104-
def _validate_input(config: Dict):
102+
def _validate_input(config: dict):
105103
"""
106104
Check that csv input type has files that exist
107105
"""
@@ -118,7 +116,7 @@ def _validate_input(config: Dict):
118116
raise FileDoesNotExistException(file_name=file_name)
119117

120118
@staticmethod
121-
def _validate_binding(config: Dict):
119+
def _validate_binding(config: dict):
122120
"""
123121
Check that defined bindings use existing actors defined by the configuration
124122
"""

src/powerapi/cli/generator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import logging
3131
import os
3232
import sys
33-
from typing import Dict, Type, Callable
33+
from typing import Callable
3434

3535
from powerapi.actor import Actor
3636
from powerapi.database import MongoDB, CsvDB, OpenTSDB, SocketDB, PrometheusDB, VirtioFSDB, FileDB
@@ -93,7 +93,7 @@ class Generator:
9393
def __init__(self, component_group_name):
9494
self.component_group_name = component_group_name
9595

96-
def generate(self, main_config: dict) -> Dict[str, Type[Actor]]:
96+
def generate(self, main_config: dict) -> dict[str, type[Actor]]:
9797
"""
9898
Generate an actor class and actor start message from config dict
9999
"""
@@ -115,7 +115,7 @@ def generate(self, main_config: dict) -> Dict[str, Type[Actor]]:
115115

116116
return actors
117117

118-
def _gen_actor(self, component_config: dict, main_config: dict, component_name: str) -> Type[Actor]:
118+
def _gen_actor(self, component_config: dict, main_config: dict, component_name: str) -> type[Actor]:
119119
raise NotImplementedError()
120120

121121

@@ -213,7 +213,7 @@ def remove_db_factory(self, database_name: str):
213213
raise DatabaseNameDoesNotExist(database_name)
214214
del self.db_factory[database_name]
215215

216-
def add_report_class(self, model_name: str, report_class: Type[Report]):
216+
def add_report_class(self, model_name: str, report_class: type[Report]):
217217
"""
218218
add a report class to generator
219219
"""
@@ -287,7 +287,7 @@ class ProcessorGenerator(Generator):
287287
Generator that initialises the processor from config
288288
"""
289289

290-
def __init__(self, component_group_name: str, processor_factory: Dict[str, Callable[[Dict], ProcessorActor]] = None):
290+
def __init__(self, component_group_name: str, processor_factory: dict[str, Callable[[dict], ProcessorActor]] = None):
291291
Generator.__init__(self, component_group_name)
292292

293293
self.processor_factory = processor_factory
@@ -331,7 +331,7 @@ def __init__(self):
331331
super().__init__('pre-processor', self._get_default_processor_factories())
332332

333333
@staticmethod
334-
def _k8s_pre_processor_factory(processor_config: Dict) -> K8sPreProcessorActor:
334+
def _k8s_pre_processor_factory(processor_config: dict) -> K8sPreProcessorActor:
335335
"""
336336
Kubernetes pre-processor actor factory.
337337
:param processor_config: Pre-Processor configuration
@@ -345,7 +345,7 @@ def _k8s_pre_processor_factory(processor_config: Dict) -> K8sPreProcessorActor:
345345
level_logger = logging.DEBUG if processor_config[GENERAL_CONF_VERBOSE_KEY] else logging.INFO
346346
return K8sPreProcessorActor(name, [], target_actors_name, api_mode, api_host, api_key, level_logger)
347347

348-
def _get_default_processor_factories(self) -> Dict[str, Callable[[Dict], ProcessorActor]]:
348+
def _get_default_processor_factories(self) -> dict[str, Callable[[dict], ProcessorActor]]:
349349
"""
350350
Return the default pre-processors factory.
351351
"""
@@ -363,5 +363,5 @@ def __init__(self):
363363
ProcessorGenerator.__init__(self, 'post-processor', self._get_default_processor_factories())
364364

365365
@staticmethod
366-
def _get_default_processor_factories() -> Dict[str, Callable[[Dict], ProcessorActor]]:
366+
def _get_default_processor_factories() -> dict[str, Callable[[dict], ProcessorActor]]:
367367
return {}

src/powerapi/database/csvdb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
2930
import csv
3031
import os
3132

32-
from typing import List, Type
33-
from powerapi.report.report import Report, CSV_HEADER_COMMON
3433
from powerapi.database.base_db import BaseDB, IterDB
3534
from powerapi.exception import PowerAPIException
35+
from powerapi.report.report import Report, CSV_HEADER_COMMON
3636
from powerapi.utils import utils
3737

3838
# Array of field that will not be considered as a group
@@ -192,7 +192,7 @@ class CsvDB(BaseDB):
192192
a CsvDB instance can be define by its current path
193193
"""
194194

195-
def __init__(self, report_type: Type[Report], tags: List[str], current_path="/tmp/csvdbtest", files=[]):
195+
def __init__(self, report_type: type[Report], tags: list[str], current_path="/tmp/csvdbtest", files=[]):
196196
"""
197197
:param current_path: Current path where read/write files
198198
"""
@@ -294,7 +294,7 @@ def save(self, report: Report):
294294
for row in values:
295295
writer.writerow(row)
296296

297-
def save_many(self, reports: List[Report]):
297+
def save_many(self, reports: list[Report]):
298298
"""
299299
Allow to save a batch of report
300300

src/powerapi/database/file_db.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
from datetime import datetime
30+
import json
3131
import logging
32-
from typing import List, Type
3332
import os
34-
import json
33+
from datetime import datetime
34+
3535
from powerapi.database.base_db import BaseDB, DBError, IterDB
3636
from powerapi.report import Report
3737

@@ -93,7 +93,7 @@ class FileDB(BaseDB):
9393
Allow to handle a FileDB database in reading or writing.
9494
"""
9595

96-
def __init__(self, report_type: Type[Report], filename: str):
96+
def __init__(self, report_type: type[Report], filename: str):
9797
"""
9898
:param report_type: Type of the report handled by this database
9999
:param filename: Name of the file containing the report
@@ -142,7 +142,7 @@ def save(self, report: Report):
142142
with open(self.filename, 'w', encoding='utf-8') as file_object:
143143
file_object.write(str(final_dict))
144144

145-
def save_many(self, reports: List[Report]):
145+
def save_many(self, reports: list[Report]):
146146
"""
147147
Allow to save a batch of data
148148

src/powerapi/database/influxdb2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

3030
import logging
31-
from typing import List, Type
3231
from urllib.parse import urlparse
3332
try:
3433
from influxdb_client import InfluxDBClient, WriteOptions
@@ -54,7 +53,7 @@ class InfluxDB2(BaseDB):
5453
Allow to handle a InfluxDB database in reading or writing.
5554
"""
5655

57-
def __init__(self, report_type: Type[Report], url: str, org: str, bucket_name: str, token: str, tags: List[str],
56+
def __init__(self, report_type: type[Report], url: str, org: str, bucket_name: str, token: str, tags: list[str],
5857
port=None):
5958
"""
6059
:param report_type: Type of the report handled by this database
@@ -140,7 +139,7 @@ def save(self, report: Report):
140139
"""
141140
self.save_many([report])
142141

143-
def save_many(self, reports: List[Report]):
142+
def save_many(self, reports: list[Report]):
144143
"""
145144
Save a batch of data
146145

src/powerapi/database/mongodb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
2930
import logging
3031
try:
3132
import pymongo
3233
import pymongo.errors
3334
except ImportError:
3435
logging.getLogger().info("PyMongo is not installed.")
3536

36-
from typing import List, Type
3737
from powerapi.database.base_db import BaseDB, DBError, IterDB
3838
from powerapi.report import Report
3939

@@ -93,7 +93,7 @@ class MongoDB(BaseDB):
9393
Allow to handle a MongoDB database in reading or writing.
9494
"""
9595

96-
def __init__(self, report_type: Type[Report], uri: str, db_name: str, collection_name: str):
96+
def __init__(self, report_type: type[Report], uri: str, db_name: str, collection_name: str):
9797
"""
9898
:param report_type: Type of the report handled by this database
9999
:param uri: URI of the MongoDB server
@@ -164,7 +164,7 @@ def save(self, report: Report):
164164
"""
165165
self.collection.insert_one(self.report_type.to_mongodb(report))
166166

167-
def save_many(self, reports: List[Report]):
167+
def save_many(self, reports: list[Report]):
168168
"""
169169
Allow to save a batch of data
170170

src/powerapi/database/opentsdb.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
2930
import logging
3031
try:
3132
from opentsdb import TSDBClient
3233
except ImportError:
3334
logging.getLogger().info("opentsdb-py is not installed.")
3435

35-
from typing import List, Type
36-
3736
from powerapi.report import PowerReport, Report
3837
from .base_db import BaseDB, DBError
3938

@@ -51,7 +50,7 @@ class OpenTSDB(BaseDB):
5150
Allow to handle an OpenTSDB database to save PowerReport.
5251
"""
5352

54-
def __init__(self, report_type: Type[Report], host: str, port, metric_name: str):
53+
def __init__(self, report_type: type[Report], host: str, port, metric_name: str):
5554
"""
5655
:param host: host of the OpenTSDB server
5756
:param port: port of the OpenTSDB server
@@ -105,7 +104,7 @@ def save(self, report: PowerReport):
105104
self.client.send(self.metric_name, report.power, timestamp=int(report.timestamp.timestamp()),
106105
host=report.target)
107106

108-
def save_many(self, reports: List[Report]):
107+
def save_many(self, reports: list[Report]):
109108
"""
110109
Save a batch of data
111110

src/powerapi/database/prometheus_db.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

3030
import logging
31-
from typing import List, Type
3231

3332
try:
3433
from prometheus_client import start_http_server, Gauge
@@ -55,8 +54,8 @@ class BasePrometheusDB(BaseDB):
5554
Base class to expose data to prometheus instance
5655
"""
5756

58-
def __init__(self, report_type: Type[Report], port: int, metric_name: str,
59-
tags: List[str], metric_description: str = DEFAULT_METRIC_DESCRIPTION, address: str = DEFAULT_ADDRESS):
57+
def __init__(self, report_type: type[Report], port: int, metric_name: str,
58+
tags: list[str], metric_description: str = DEFAULT_METRIC_DESCRIPTION, address: str = DEFAULT_ADDRESS):
6059
BaseDB.__init__(self, report_type)
6160
self.address = address
6261
self.port = port
@@ -85,8 +84,8 @@ class PrometheusDB(BasePrometheusDB):
8584
It can only be used with a pusher actor
8685
"""
8786

88-
def __init__(self, report_type: Type[Report], port: int, address: str, metric_name: str, metric_description: str,
89-
tags: List[str]):
87+
def __init__(self, report_type: type[Report], port: int, address: str, metric_name: str, metric_description: str,
88+
tags: list[str]):
9089
"""
9190
:param address: address that exposes the metric
9291
:param port: port used to expose the metric
@@ -176,7 +175,7 @@ def save(self, report: Report):
176175

177176
self._expose_data(key, measure)
178177

179-
def save_many(self, reports: List[Report]):
178+
def save_many(self, reports: list[Report]):
180179
"""
181180
Save a batch of data
182181

src/powerapi/database/socket_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from queue import SimpleQueue, Empty
3333
from socketserver import ThreadingMixIn, TCPServer, StreamRequestHandler
3434
from threading import Thread
35-
from typing import Type, Iterator
35+
from typing import Iterator
3636

3737
from powerapi.database.base_db import IterDB, BaseDB
3838
from powerapi.report import Report
@@ -135,7 +135,7 @@ class SocketDB(BaseDB):
135135
Database implementation that exposes a TCP socket the clients can connect to.
136136
"""
137137

138-
def __init__(self, report_type: Type[Report], host: str, port: int):
138+
def __init__(self, report_type: type[Report], host: str, port: int):
139139
"""
140140
:param report_type: The type of report to create
141141
:param host: The host address to listen on

0 commit comments

Comments
 (0)