Skip to content

Commit a889296

Browse files
Replace obsolete types (#644)
The tool `pyupgrade` was used to modernize the code base given that frequenz-SDK is already using python 3.11 as minimum version. The tool was mainly used to replace obsolete types. Just as a reference the tool was run with the following shell command: ```sh find ./src ./benchmarks ./docs ./examples ./tests -name '*.py' -exec pyupgrade --py311-plus {} \; ``` Also `black` and `isort` were both run to format the code base after running `pyupgrade`. Fixes #433
2 parents 01b8d9d + 0b02c56 commit a889296

File tree

82 files changed

+461
-528
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+461
-528
lines changed

benchmarks/power_distribution/power_distributor.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import csv
88
import random
99
import timeit
10+
from collections.abc import Coroutine
1011
from datetime import timedelta
11-
from typing import Any, Coroutine, Dict, List, Set # pylint: disable=unused-import
12+
from typing import Any
1213

1314
from frequenz.channels import Broadcast
1415

@@ -32,7 +33,7 @@
3233
PORT = 61060
3334

3435

35-
async def send_requests(batteries: Set[int], request_num: int) -> List[Result]:
36+
async def send_requests(batteries: set[int], request_num: int) -> list[Result]:
3637
"""Send requests to the PowerDistributingActor and wait for the response.
3738
3839
Args:
@@ -47,7 +48,7 @@ async def send_requests(batteries: Set[int], request_num: int) -> List[Result]:
4748
"""
4849
battery_pool = microgrid.battery_pool(batteries)
4950
results_rx = battery_pool.power_distribution_results()
50-
result: List[Result] = []
51+
result: list[Result] = []
5152
for _ in range(request_num):
5253
await battery_pool.set_power(Power(float(random.randrange(100000, 1000000))))
5354
try:
@@ -61,7 +62,7 @@ async def send_requests(batteries: Set[int], request_num: int) -> List[Result]:
6162
return result
6263

6364

64-
def parse_result(result: List[List[Result]]) -> Dict[str, float]:
65+
def parse_result(result: list[list[Result]]) -> dict[str, float]:
6566
"""Parse result.
6667
6768
Args:
@@ -91,8 +92,8 @@ def parse_result(result: List[List[Result]]) -> Dict[str, float]:
9192

9293
async def run_test( # pylint: disable=too-many-locals
9394
num_requests: int,
94-
batteries: Set[int],
95-
) -> Dict[str, Any]:
95+
batteries: set[int],
96+
) -> dict[str, Any]:
9697
"""Run test.
9798
9899
Args:
@@ -112,7 +113,7 @@ async def run_test( # pylint: disable=too-many-locals
112113
requests_receiver=power_request_channel.new_receiver(),
113114
battery_status_sender=battery_status_channel.new_sender(),
114115
):
115-
tasks: List[Coroutine[Any, Any, List[Result]]] = []
116+
tasks: list[Coroutine[Any, Any, list[Result]]] = []
116117
tasks.append(send_requests(batteries, num_requests))
117118

118119
result = await asyncio.gather(*tasks)
@@ -133,7 +134,7 @@ async def run() -> None:
133134
HOST, PORT, ResamplerConfig(resampling_period=timedelta(seconds=1.0))
134135
)
135136

136-
all_batteries: Set[Component] = connection_manager.get().component_graph.components(
137+
all_batteries: set[Component] = connection_manager.get().component_graph.components(
137138
component_category={ComponentCategory.BATTERY}
138139
)
139140
batteries_ids = {c.component_id for c in all_batteries}

benchmarks/timeseries/benchmark_datasourcing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import sys
1515
import tracemalloc
1616
from time import perf_counter
17-
from typing import Any, Tuple
17+
from typing import Any
1818

1919
from frequenz.channels import Broadcast, Receiver, ReceiverStoppedError
2020

@@ -131,7 +131,7 @@ async def consume(channel: Receiver[Any]) -> None:
131131
)
132132

133133

134-
def parse_args() -> Tuple[int, int, bool]:
134+
def parse_args() -> tuple[int, int, bool]:
135135
"""Parse the command line arguments.
136136
137137
Returns:

benchmarks/timeseries/benchmark_ringbuffer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import random
77
import timeit
88
from datetime import datetime, timedelta, timezone
9-
from typing import Any, Dict, TypeVar
9+
from typing import Any, TypeVar
1010

1111
import numpy as np
1212

@@ -66,7 +66,7 @@ def test_slices(days: int, buffer: OrderedRingBuffer[Any], median: bool) -> None
6666
total += float(np.average(minutes))
6767

6868

69-
def test_29_days_list(num_runs: int) -> Dict[str, float]:
69+
def test_29_days_list(num_runs: int) -> dict[str, float]:
7070
"""Run the 29 day test on the list backend."""
7171
days = 29
7272
buffer = OrderedRingBuffer([0.0] * MINUTES_IN_29_DAYS, timedelta(minutes=1))
@@ -76,7 +76,7 @@ def test_29_days_list(num_runs: int) -> Dict[str, float]:
7676
return {"fill": fill_time, "test": test_time}
7777

7878

79-
def test_29_days_array(num_runs: int) -> Dict[str, float]:
79+
def test_29_days_array(num_runs: int) -> dict[str, float]:
8080
"""Run the 29 day test on the array backend."""
8181
days = 29
8282
buffer = OrderedRingBuffer(
@@ -91,7 +91,7 @@ def test_29_days_array(num_runs: int) -> Dict[str, float]:
9191
return {"fill": fill_time, "test": test_time}
9292

9393

94-
def test_29_days_slicing_list(num_runs: int) -> Dict[str, float]:
94+
def test_29_days_slicing_list(num_runs: int) -> dict[str, float]:
9595
"""Run slicing tests on list backend."""
9696
days = 29
9797
buffer = OrderedRingBuffer([0.0] * MINUTES_IN_29_DAYS, timedelta(minutes=1))
@@ -107,7 +107,7 @@ def test_29_days_slicing_list(num_runs: int) -> Dict[str, float]:
107107
return {"fill": fill_time, "median": median_test_time, "avg": avg_test_time}
108108

109109

110-
def test_29_days_slicing_array(num_runs: int) -> Dict[str, float]:
110+
def test_29_days_slicing_array(num_runs: int) -> dict[str, float]:
111111
"""Run slicing tests on array backend."""
112112
days = 29
113113
buffer = OrderedRingBuffer(

benchmarks/timeseries/periodic_feature_extractor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
implementation.
1010
"""
1111

12-
from __future__ import annotations
1312

1413
import asyncio
1514
import collections.abc
@@ -18,7 +17,6 @@
1817
from datetime import datetime, timedelta, timezone
1918
from functools import partial
2019
from timeit import timeit
21-
from typing import List
2220

2321
import numpy as np
2422
from frequenz.channels import Broadcast
@@ -79,7 +77,7 @@ def _calculate_avg_window_py(
7977
feature_extractor: PeriodicFeatureExtractor,
8078
window: NDArray[np.float_],
8179
window_size: int,
82-
weights: List[float] | None = None,
80+
weights: list[float] | None = None,
8381
) -> NDArray[np.float_]:
8482
"""
8583
Plain python version of the average calculator.

benchmarks/timeseries/resampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
"""Benchmark resampling."""
55

6+
from collections.abc import Sequence
67
from datetime import datetime, timedelta, timezone
78
from timeit import timeit
8-
from typing import Sequence
99

1010
from frequenz.sdk.timeseries import Sample
1111
from frequenz.sdk.timeseries._quantities import Quantity

benchmarks/timeseries/ringbuffer_memusage.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Memory allocation benchmark for the ringbuffer."""
55

6-
from __future__ import annotations
76

87
import argparse
98
import tracemalloc

benchmarks/timeseries/ringbuffer_serialization.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Benchmarks the serialization of the `OrderedRingBuffer` class."""
55

6-
from __future__ import annotations
76

87
import fnmatch
98
import os

examples/battery_pool.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
"""Script with an example how to use BatteryPool."""
55

6-
from __future__ import annotations
76

87
import asyncio
98
import logging
109
from datetime import timedelta
11-
from typing import Any, Dict
10+
from typing import Any
1211

1312
from frequenz.channels import Receiver
1413
from frequenz.channels.util import MergeNamed
@@ -32,7 +31,7 @@ async def main() -> None:
3231
)
3332

3433
battery_pool = microgrid.battery_pool()
35-
receivers: Dict[str, Receiver[Any]] = {
34+
receivers: dict[str, Receiver[Any]] = {
3635
"soc": battery_pool.soc.new_receiver(maxsize=1),
3736
"capacity": battery_pool.capacity.new_receiver(maxsize=1),
3837
"power_bounds": battery_pool.power_bounds.new_receiver(maxsize=1),

src/frequenz/sdk/_internal/_asyncio.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""General purpose async tools."""
55

6-
from __future__ import annotations
76

87
import asyncio
98
from abc import ABC

src/frequenz/sdk/_internal/_singleton_meta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"""Definition of Singleton metaclass."""
55

66
from threading import Lock
7-
from typing import Any, Dict
7+
from typing import Any
88

99

1010
class SingletonMeta(type):
1111
"""This is a thread-safe implementation of Singleton."""
1212

13-
_instances: Dict[Any, type] = {}
13+
_instances: dict[Any, type] = {}
1414
"""The dictionary of instances of the singleton classes."""
1515

1616
_lock: Lock = Lock()

0 commit comments

Comments
 (0)