Skip to content

Commit 588d063

Browse files
committed
Modernise typing
1 parent f190dda commit 588d063

19 files changed

Lines changed: 134 additions & 196 deletions

File tree

‎examples/Geant4/geant4_simvue.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@click.option("--momentum", type=float, default=10)
2828
@click.option("--events", type=int, default=100)
2929
def geant4_simvue_example(
30-
g4_binary: str, config: typing.Optional[str], ci: bool, momentum: float, events: int
30+
g4_binary: str, config: str | None, ci: bool, momentum: float, events: int
3131
) -> None:
3232
@mp_file_parse.file_parser
3333
def root_file_parser(

‎simvue/api/objects/folder.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Folder(SimvueObject):
3030
3131
"""
3232

33-
def __init__(self, identifier: typing.Optional[str] = None, **kwargs) -> None:
33+
def __init__(self, identifier: str | None = None, **kwargs) -> None:
3434
"""Initialise a Folder
3535
3636
If an identifier is provided a connection will be made to the
@@ -82,7 +82,7 @@ def path(self) -> pathlib.Path:
8282

8383
@property
8484
@staging_check
85-
def description(self) -> typing.Optional[str]:
85+
def description(self) -> str | None:
8686
"""Return the folder description"""
8787
return self._get().get("description")
8888

@@ -95,7 +95,7 @@ def description(self, description: str) -> None:
9595

9696
@property
9797
@staging_check
98-
def name(self) -> typing.Optional[str]:
98+
def name(self) -> str | None:
9999
"""Return the folder name"""
100100
return self._get().get("name")
101101

‎simvue/api/objects/run.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
class Run(SimvueObject):
28-
def __init__(self, identifier: typing.Optional[str] = None, **kwargs) -> None:
28+
def __init__(self, identifier: str | None = None, **kwargs) -> None:
2929
"""Initialise a Run
3030
3131
If an identifier is provided a connection will be made to the

‎simvue/api/objects/storage/base.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class StorageBase(SimvueObject):
1111
def __init__(
1212
self,
13-
identifier: typing.Optional[str] = None,
13+
identifier: str | None = None,
1414
_read_only: bool = False,
1515
**kwargs,
1616
) -> None:

‎simvue/api/objects/storage/s3.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
class S3Storage(StorageBase):
16-
def __init__(self, identifier: typing.Optional[str] = None, **kwargs) -> None:
16+
def __init__(self, identifier: str | None = None, **kwargs) -> None:
1717
self.config = Config(self)
1818
super().__init__(identifier, **kwargs)
1919

‎simvue/api/request.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def post(
8888
8989
"""
9090
if is_json:
91-
data_sent: typing.Union[str, dict[str, typing.Any]] = json_module.dumps(data)
91+
data_sent: str | dict[str, typing.Any] = json_module.dumps(data)
9292
headers = set_json_header(headers)
9393
else:
9494
data_sent = data
@@ -144,7 +144,7 @@ def put(
144144
response from executing PUT
145145
"""
146146
if is_json and data:
147-
data_sent: typing.Union[str, dict[str, typing.Any]] = json_module.dumps(data)
147+
data_sent: str | dict[str, typing.Any] = json_module.dumps(data)
148148
headers = set_json_header(headers)
149149
else:
150150
data_sent = data
@@ -231,7 +231,7 @@ def get_json_from_response(
231231
response: requests.Response,
232232
allow_parse_failure: bool = False,
233233
expected_type: typing.Type[dict | list] = dict,
234-
) -> typing.Union[dict, list]:
234+
) -> dict | list:
235235
try:
236236
json_response = response.json()
237237
json_response = json_response or ({} if expected_type is dict else [])

‎simvue/client.py‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class Client:
6464
def __init__(
6565
self,
6666
*,
67-
server_token: typing.Optional[pydantic.SecretStr] = None,
68-
server_url: typing.Optional[str] = None,
67+
server_token: pydantic.SecretStr | None = None,
68+
server_url: str | None = None,
6969
) -> None:
7070
"""Initialise an instance of the Simvue client
7171
@@ -136,7 +136,7 @@ def get_run_id_from_name(
136136

137137
@prettify_pydantic
138138
@pydantic.validate_call
139-
def get_run(self, run_id: str) -> typing.Optional[Run]:
139+
def get_run(self, run_id: str) -> Run | None:
140140
"""Retrieve a single run
141141
142142
Parameters
@@ -177,7 +177,7 @@ def get_run_name_from_id(self, run_id: str) -> str:
177177
@pydantic.validate_call
178178
def get_runs(
179179
self,
180-
filters: typing.Optional[list[str]],
180+
filters: list[str] | None,
181181
*,
182182
system: bool = False,
183183
metrics: bool = False,
@@ -187,7 +187,7 @@ def get_runs(
187187
count_limit: pydantic.PositiveInt | None = 100,
188188
start_index: pydantic.NonNegativeInt = 0,
189189
show_shared: bool = False,
190-
) -> typing.Union[DataFrame, typing.Generator[tuple[str, Run], None, None], None]:
190+
) -> DataFrame | typing.Generator[tuple[str, Run], None, None] | None:
191191
"""Retrieve all runs matching filters.
192192
193193
Parameters
@@ -216,7 +216,7 @@ def get_runs(
216216
217217
Returns
218218
-------
219-
dict | pandas.DataFrame
219+
pandas.DataFrame | Generator[tuple[str, Run], None, None]
220220
either the JSON response from the runs request or the results in the
221221
form of a Pandas DataFrame
222222
@@ -280,7 +280,7 @@ def get_runs(
280280

281281
@prettify_pydantic
282282
@pydantic.validate_call
283-
def delete_run(self, run_id: str) -> typing.Optional[dict]:
283+
def delete_run(self, run_id: str) -> dict | None:
284284
"""Delete run by identifier
285285
286286
Parameters
@@ -300,7 +300,7 @@ def delete_run(self, run_id: str) -> typing.Optional[dict]:
300300
"""
301301
return Run(identifier=run_id).delete() or None
302302

303-
def _get_folder_from_path(self, path: str) -> typing.Optional[Folder]:
303+
def _get_folder_from_path(self, path: str) -> Folder | None:
304304
"""Retrieve folder for the specified path if found
305305
306306
Parameters
@@ -321,7 +321,7 @@ def _get_folder_from_path(self, path: str) -> typing.Optional[Folder]:
321321
except StopIteration:
322322
return None
323323

324-
def _get_folder_id_from_path(self, path: str) -> typing.Optional[str]:
324+
def _get_folder_id_from_path(self, path: str) -> str | None:
325325
"""Retrieve folder identifier for the specified path if found
326326
327327
Parameters
@@ -342,7 +342,7 @@ def _get_folder_id_from_path(self, path: str) -> typing.Optional[str]:
342342
@pydantic.validate_call
343343
def delete_runs(
344344
self, folder_path: typing.Annotated[str, pydantic.Field(pattern=FOLDER_REGEX)]
345-
) -> typing.Optional[list]:
345+
) -> list | None:
346346
"""Delete runs in a named folder
347347
348348
Parameters
@@ -374,7 +374,7 @@ def delete_folder(
374374
recursive: bool = False,
375375
remove_runs: bool = False,
376376
allow_missing: bool = False,
377-
) -> typing.Optional[list]:
377+
) -> list | None:
378378
"""Delete a folder by name
379379
380380
Parameters
@@ -460,7 +460,7 @@ def _retrieve_artifacts_from_server(
460460

461461
@prettify_pydantic
462462
@pydantic.validate_call
463-
def abort_run(self, run_id: str, reason: str) -> typing.Union[dict, list]:
463+
def abort_run(self, run_id: str, reason: str) -> dict | list:
464464
"""Abort a currently active run on the server
465465
466466
Parameters
@@ -707,7 +707,7 @@ def _get_run_metrics_from_server(
707707
aggregate: bool,
708708
max_points: int | None = None,
709709
) -> dict[str, typing.Any]:
710-
params: dict[str, typing.Union[str, int, None]] = {
710+
params: dict[str, str | int | None] = {
711711
"runs": json.dumps(run_ids),
712712
"aggregate": aggregate,
713713
"metrics": json.dumps(metric_names),
@@ -740,7 +740,7 @@ def get_metric_values(
740740
use_run_names: bool = False,
741741
aggregate: bool = False,
742742
max_points: pydantic.PositiveInt | None = None,
743-
) -> typing.Union[dict, DataFrame, None]:
743+
) -> dict | DataFrame | None:
744744
"""Retrieve the values for a given metric across multiple runs
745745
746746
Uses filters to specify which runs should be retrieved.
@@ -828,7 +828,7 @@ def plot_metrics(
828828
metric_names: list[str],
829829
xaxis: typing.Literal["step", "time"],
830830
*,
831-
max_points: typing.Optional[int] = None,
831+
max_points: int | None = None,
832832
) -> typing.Any:
833833
"""Plt the time series values for multiple metrics/runs
834834
@@ -910,9 +910,9 @@ def get_events(
910910
self,
911911
run_id: str,
912912
*,
913-
message_contains: typing.Optional[str] = None,
914-
start_index: typing.Optional[pydantic.NonNegativeInt] = None,
915-
count_limit: typing.Optional[pydantic.PositiveInt] = None,
913+
message_contains: str | None = None,
914+
start_index: pydantic.NonNegativeInt | None = None,
915+
count_limit: pydantic.PositiveInt | None = None,
916916
) -> list[dict[str, str]]:
917917
"""Return events for a specified run
918918
@@ -944,7 +944,7 @@ def get_events(
944944
else ""
945945
)
946946

947-
params: dict[str, typing.Union[str, int]] = {
947+
params: dict[str, str | int] = {
948948
"run": run_id,
949949
"filters": msg_filter,
950950
"start": start_index or 0,
@@ -970,11 +970,11 @@ def get_events(
970970
def get_alerts(
971971
self,
972972
*,
973-
run_id: typing.Optional[str] = None,
973+
run_id: str | None = None,
974974
critical_only: bool = True,
975975
names_only: bool = True,
976-
start_index: typing.Optional[pydantic.NonNegativeInt] = None,
977-
count_limit: typing.Optional[pydantic.PositiveInt] = None,
976+
start_index: pydantic.NonNegativeInt | None = None,
977+
count_limit: pydantic.PositiveInt | None = None,
978978
) -> list[AlertBase] | list[str | None]:
979979
"""Retrieve alerts for a given run
980980
@@ -1018,8 +1018,8 @@ def get_alerts(
10181018
def get_tags(
10191019
self,
10201020
*,
1021-
start_index: typing.Optional[pydantic.NonNegativeInt] = None,
1022-
count_limit: typing.Optional[pydantic.PositiveInt] = None,
1021+
start_index: pydantic.NonNegativeInt | None = None,
1022+
count_limit: pydantic.PositiveInt | None = None,
10231023
) -> typing.Generator[Tag, None, None]:
10241024
"""Retrieve tags
10251025

‎simvue/config/parameters.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def check_token(cls, v: typing.Any) -> str:
4343

4444

4545
class OfflineSpecifications(pydantic.BaseModel):
46-
cache: typing.Optional[pathlib.Path] = None
46+
cache: pathlib.Path | None = None
4747

4848

4949
class MetricsSpecifications(pydantic.BaseModel):
@@ -53,11 +53,11 @@ class MetricsSpecifications(pydantic.BaseModel):
5353

5454

5555
class DefaultRunSpecifications(pydantic.BaseModel):
56-
name: typing.Optional[str] = None
57-
description: typing.Optional[str] = None
58-
tags: typing.Optional[list[str]] = None
56+
name: str | None = None
57+
description: str | None = None
58+
tags: list[str] | None = None
5959
folder: str = pydantic.Field("/", pattern=sv_models.FOLDER_REGEX)
60-
metadata: typing.Optional[dict[str, typing.Union[str, int, float, bool]]] = None
60+
metadata: dict[str, str | int | float | bool] | None = None
6161
mode: typing.Literal["offline", "disabled", "online"] = "online"
6262

6363

‎simvue/config/user.py‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@
3636

3737
logger = logging.getLogger(__name__)
3838

39-
SIMVUE_SERVER_UPPER_CONSTRAINT: typing.Optional[semver.Version] = semver.Version.parse(
40-
"2.0.0"
41-
)
42-
SIMVUE_SERVER_LOWER_CONSTRAINT: typing.Optional[semver.Version] = semver.Version.parse(
43-
"1.0.0"
44-
)
39+
SIMVUE_SERVER_UPPER_CONSTRAINT: semver.Version | None = semver.Version.parse("2.0.0")
40+
SIMVUE_SERVER_LOWER_CONSTRAINT: semver.Version | None = semver.Version.parse("1.0.0")
4541

4642

4743
class SimvueConfiguration(pydantic.BaseModel):
@@ -56,7 +52,7 @@ class SimvueConfiguration(pydantic.BaseModel):
5652
metrics: MetricsSpecifications = MetricsSpecifications()
5753

5854
@classmethod
59-
def _load_pyproject_configs(cls) -> typing.Optional[dict]:
55+
def _load_pyproject_configs(cls) -> dict | None:
6056
"""Recover any Simvue non-authentication configurations from pyproject.toml"""
6157
_pyproject_toml = sv_util.find_first_instance_of_file(
6258
file_names=["pyproject.toml"], check_user_space=False
@@ -227,10 +223,8 @@ def fetch(
227223
@functools.lru_cache
228224
def config_file(cls) -> pathlib.Path:
229225
"""Returns the path of top level configuration file used for the session"""
230-
_config_file: typing.Optional[pathlib.Path] = (
231-
sv_util.find_first_instance_of_file(
232-
CONFIG_FILE_NAMES, check_user_space=True
233-
)
226+
_config_file: pathlib.Path | None = sv_util.find_first_instance_of_file(
227+
CONFIG_FILE_NAMES, check_user_space=True
234228
)
235229

236230
# NOTE: Legacy INI support has been removed

‎simvue/converters.py‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ def aggregated_metrics_to_dataframe(
1919
request_response_data: dict[str, list[dict[str, float]]],
2020
xaxis: str,
2121
parse_to: typing.Literal["dict", "dataframe"] = "dict",
22-
) -> typing.Union[
23-
"DataFrame", dict[str, dict[tuple[float, str], typing.Optional[float]]]
24-
]:
22+
) -> typing.Union["DataFrame", dict[str, dict[tuple[float, str], float]] | None]:
2523
"""Create data frame for an aggregate of metrics
2624
2725
Returns a dataframe with columns being metrics and sub-columns being the
@@ -58,7 +56,7 @@ def aggregated_metrics_to_dataframe(
5856
_value_types = list(_value_types)
5957
_value_types.remove(xaxis)
6058

61-
result_dict: dict[str, dict[tuple[float, str], typing.Optional[float]]] = {
59+
result_dict: dict[str, dict[tuple[float, str], float]] | None = {
6260
metric_name: {} for metric_name in request_response_data
6361
}
6462

@@ -91,9 +89,7 @@ def parse_run_set_metrics(
9189
xaxis: str,
9290
run_labels: list[str],
9391
parse_to: typing.Literal["dict", "dataframe"] = "dict",
94-
) -> typing.Union[
95-
dict[str, dict[tuple[float, str], typing.Optional[float]]], "DataFrame"
96-
]:
92+
) -> typing.Union[dict[str, dict[tuple[float, str], float]] | None, "DataFrame"]:
9793
"""Parse JSON response metric data from the server into the specified form
9894
9995
Creates either a dictionary or a pandas dataframe of the data collected
@@ -112,7 +108,7 @@ def parse_run_set_metrics(
112108
113109
Returns
114110
-------
115-
dict[str, dict[tuple[float, str], typing.Optional[float]]] | DataFrame
111+
dict[str, dict[tuple[float, str], float]] | None | DataFrame
116112
either a dictionary or Pandas DataFrame containing the results
117113
118114
Raises
@@ -144,7 +140,7 @@ def parse_run_set_metrics(
144140
_value_types = list(_value_types)
145141
_value_types.remove(xaxis)
146142

147-
result_dict: dict[str, dict[tuple[float, str], typing.Optional[float]]] = {
143+
result_dict: dict[str, dict[tuple[float, str], float]] | None = {
148144
metric_name: {} for metric_name in _all_metrics
149145
}
150146

@@ -224,7 +220,7 @@ def to_dataframe(data) -> pandas.DataFrame:
224220
def metric_time_series_to_dataframe(
225221
data: list[dict[str, float]],
226222
xaxis: typing.Literal["step", "time", "timestamp"],
227-
name: typing.Optional[str] = None,
223+
name: str | None = None,
228224
) -> "DataFrame":
229225
"""Convert a single metric value set from a run into a dataframe
230226

0 commit comments

Comments
 (0)