Skip to content

Commit 812a5dc

Browse files
authored
Update syntax to Python >=3.9 (#194)
* Update syntax to Python >=3.9 * Be explicit on optional types * Try to fix Python 3.9 * Be more specific on dict
1 parent ded6357 commit 812a5dc

File tree

7 files changed

+212
-202
lines changed

7 files changed

+212
-202
lines changed

audinterface/core/feature.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Callable
4+
from collections.abc import Sequence
15
import errno
26
import inspect
37
import os
4-
import typing
58

69
import numpy as np
710
import pandas as pd
@@ -246,25 +249,25 @@ class Feature:
246249

247250
def __init__(
248251
self,
249-
feature_names: typing.Union[str, typing.Sequence[str]],
252+
feature_names: str | Sequence[str],
250253
*,
251-
name: str = None,
252-
params: typing.Dict = None,
253-
process_func: typing.Callable[..., typing.Any] = None,
254-
process_func_args: typing.Dict[str, typing.Any] = None,
254+
name: str | None = None,
255+
params: dict | None = None,
256+
process_func: Callable[..., object] | None = None,
257+
process_func_args: dict[str, object] | None = None,
255258
process_func_is_mono: bool = False,
256259
process_func_applies_sliding_window: bool = False,
257-
sampling_rate: int = None,
260+
sampling_rate: int | None = None,
258261
resample: bool = False,
259-
channels: typing.Union[int, typing.Sequence[int]] = 0,
262+
channels: int | Sequence[int] = 0,
260263
mixdown: bool = False,
261-
win_dur: Timestamp = None,
262-
hop_dur: Timestamp = None,
263-
min_signal_dur: Timestamp = None,
264-
max_signal_dur: Timestamp = None,
265-
segment: Segment = None,
264+
win_dur: Timestamp | None = None,
265+
hop_dur: Timestamp | None = None,
266+
min_signal_dur: Timestamp | None = None,
267+
max_signal_dur: Timestamp | None = None,
268+
segment: Segment | None = None,
266269
keep_nat: bool = False,
267-
num_workers: typing.Optional[int] = 1,
270+
num_workers: int | None = 1,
268271
multiprocessing: bool = False,
269272
verbose: bool = False,
270273
):
@@ -359,10 +362,10 @@ def process_file(
359362
self,
360363
file: str,
361364
*,
362-
start: Timestamp = None,
363-
end: Timestamp = None,
364-
root: str = None,
365-
process_func_args: typing.Dict[str, typing.Any] = None,
365+
start: Timestamp | None = None,
366+
end: Timestamp | None = None,
367+
root: str | None = None,
368+
process_func_args: dict[str, object] | None = None,
366369
) -> pd.DataFrame:
367370
r"""Extract features from an audio file.
368371
@@ -399,12 +402,12 @@ def process_file(
399402

400403
def process_files(
401404
self,
402-
files: typing.Sequence[str],
405+
files: Sequence[str],
403406
*,
404-
starts: Timestamps = None,
405-
ends: Timestamps = None,
406-
root: str = None,
407-
process_func_args: typing.Dict[str, typing.Any] = None,
407+
starts: Timestamps | None = None,
408+
ends: Timestamps | None = None,
409+
root: str | None = None,
410+
process_func_args: dict[str, object] | None = None,
408411
) -> pd.DataFrame:
409412
r"""Extract features for a list of files.
410413
@@ -449,7 +452,7 @@ def process_folder(
449452
*,
450453
filetype: str = "wav",
451454
include_root: bool = True,
452-
process_func_args: typing.Dict[str, typing.Any] = None,
455+
process_func_args: dict[str, object] | None = None,
453456
) -> pd.DataFrame:
454457
r"""Extract features from files in a folder.
455458
@@ -500,9 +503,9 @@ def process_index(
500503
index: pd.Index,
501504
*,
502505
preserve_index: bool = False,
503-
root: str = None,
504-
cache_root: str = None,
505-
process_func_args: typing.Dict[str, typing.Any] = None,
506+
root: str | None = None,
507+
cache_root: str | None = None,
508+
process_func_args: dict[str, object] | None = None,
506509
) -> pd.DataFrame:
507510
r"""Extract features from an index conform to audformat_.
508511
@@ -572,10 +575,10 @@ def process_signal(
572575
signal: np.ndarray,
573576
sampling_rate: int,
574577
*,
575-
file: str = None,
576-
start: Timestamp = None,
577-
end: Timestamp = None,
578-
process_func_args: typing.Dict[str, typing.Any] = None,
578+
file: str | None = None,
579+
start: Timestamp | None = None,
580+
end: Timestamp | None = None,
581+
process_func_args: dict[str, object] | None = None,
579582
) -> pd.DataFrame:
580583
r"""Extract features for an audio signal.
581584
@@ -627,7 +630,7 @@ def process_signal_from_index(
627630
signal: np.ndarray,
628631
sampling_rate: int,
629632
index: pd.MultiIndex,
630-
process_func_args: typing.Dict[str, typing.Any] = None,
633+
process_func_args: dict[str, object] | None = None,
631634
) -> pd.DataFrame:
632635
r"""Split a signal into segments and extract features for each segment.
633636
@@ -676,7 +679,7 @@ def to_numpy(
676679
"""
677680
return frame.values.T.reshape(self.num_channels, self.num_features, -1)
678681

679-
def _reshape_3d(self, features: typing.Union[np.ndarray, pd.Series]):
682+
def _reshape_3d(self, features: np.ndarray | pd.Series):
680683
r"""Reshape to [n_channels, n_features, n_frames]."""
681684
features = np.array(features)
682685
features = np.atleast_1d(features)

audinterface/core/process.py

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Callable
4+
from collections.abc import Sequence
15
import errno
26
import inspect
37
import itertools
48
import os
5-
import typing
69

710
import numpy as np
811
import pandas as pd
@@ -155,20 +158,20 @@ class Process:
155158
def __init__(
156159
self,
157160
*,
158-
process_func: typing.Callable[..., typing.Any] = None,
159-
process_func_args: typing.Dict[str, typing.Any] = None,
161+
process_func: Callable[..., object] | None = None,
162+
process_func_args: dict[str, object] | None = None,
160163
process_func_is_mono: bool = False,
161-
sampling_rate: int = None,
164+
sampling_rate: int | None = None,
162165
resample: bool = False,
163-
channels: typing.Union[int, typing.Sequence[int]] = None,
166+
channels: int | Sequence[int] | None = None,
164167
mixdown: bool = False,
165-
win_dur: Timestamp = None,
166-
hop_dur: Timestamp = None,
167-
min_signal_dur: Timestamp = None,
168-
max_signal_dur: Timestamp = None,
169-
segment: Segment = None,
168+
win_dur: Timestamp | None = None,
169+
hop_dur: Timestamp | None = None,
170+
min_signal_dur: Timestamp | None = None,
171+
max_signal_dur: Timestamp | None = None,
172+
segment: Segment | None = None,
170173
keep_nat: bool = False,
171-
num_workers: typing.Optional[int] = 1,
174+
num_workers: int | None = 1,
172175
multiprocessing: bool = False,
173176
verbose: bool = False,
174177
):
@@ -241,15 +244,15 @@ def _process_file(
241244
file: str,
242245
*,
243246
idx: int = 0,
244-
root: str = None,
245-
start: pd.Timedelta = None,
246-
end: pd.Timedelta = None,
247-
process_func_args: typing.Dict[str, typing.Any] = None,
248-
) -> typing.Tuple[
249-
typing.List[typing.Any],
250-
typing.List[str],
251-
typing.List[pd.Timedelta],
252-
typing.List[pd.Timedelta],
247+
root: str | None = None,
248+
start: pd.Timedelta | None = None,
249+
end: pd.Timedelta | None = None,
250+
process_func_args: dict[str, object] | None = None,
251+
) -> tuple[
252+
list[object],
253+
list[str],
254+
list[pd.Timedelta],
255+
list[pd.Timedelta],
253256
]:
254257
if start is not None:
255258
start = utils.to_timedelta(start, self.sampling_rate)
@@ -300,10 +303,10 @@ def process_file(
300303
self,
301304
file: str,
302305
*,
303-
start: Timestamp = None,
304-
end: Timestamp = None,
305-
root: str = None,
306-
process_func_args: typing.Dict[str, typing.Any] = None,
306+
start: Timestamp | None = None,
307+
end: Timestamp | None = None,
308+
root: str | None = None,
309+
process_func_args: dict[str, object] | None = None,
307310
) -> pd.Series:
308311
r"""Process the content of an audio file.
309312
@@ -358,12 +361,12 @@ def process_file(
358361

359362
def process_files(
360363
self,
361-
files: typing.Sequence[str],
364+
files: Sequence[str],
362365
*,
363-
starts: Timestamps = None,
364-
ends: Timestamps = None,
365-
root: str = None,
366-
process_func_args: typing.Dict[str, typing.Any] = None,
366+
starts: Timestamps | None = None,
367+
ends: Timestamps | None = None,
368+
root: str | None = None,
369+
process_func_args: dict[str, object] | None = None,
367370
) -> pd.Series:
368371
r"""Process a list of files.
369372
@@ -460,7 +463,7 @@ def process_folder(
460463
*,
461464
filetype: str = "wav",
462465
include_root: bool = True,
463-
process_func_args: typing.Dict[str, typing.Any] = None,
466+
process_func_args: dict[str, object] | None = None,
464467
) -> pd.Series:
465468
r"""Process files in a folder.
466469
@@ -512,8 +515,8 @@ def process_folder(
512515
def _process_index_wo_segment(
513516
self,
514517
index: pd.Index,
515-
root: typing.Optional[str],
516-
process_func_args: typing.Dict[str, typing.Any] = None,
518+
root: str | None,
519+
process_func_args: dict[str, object] | None = None,
517520
) -> pd.Series:
518521
r"""Like process_index, but does not apply segmentation."""
519522
if index.empty:
@@ -558,9 +561,9 @@ def process_index(
558561
index: pd.Index,
559562
*,
560563
preserve_index: bool = False,
561-
root: str = None,
562-
cache_root: str = None,
563-
process_func_args: typing.Dict[str, typing.Any] = None,
564+
root: str | None = None,
565+
cache_root: str | None = None,
566+
process_func_args: dict[str, object] | None = None,
564567
) -> pd.Series:
565568
r"""Process from an index conform to audformat_.
566569
@@ -638,16 +641,16 @@ def _process_signal(
638641
sampling_rate: int,
639642
*,
640643
idx: int = 0,
641-
root: str = None,
642-
file: str = None,
643-
start: pd.Timedelta = None,
644-
end: pd.Timedelta = None,
645-
process_func_args: typing.Dict[str, typing.Any] = None,
646-
) -> typing.Tuple[
647-
typing.List[typing.Any],
648-
typing.List[str],
649-
typing.List[pd.Timedelta],
650-
typing.List[pd.Timedelta],
644+
root: str | None = None,
645+
file: str | None = None,
646+
start: pd.Timedelta | None = None,
647+
end: pd.Timedelta | None = None,
648+
process_func_args: dict[str, object] | None = None,
649+
) -> tuple[
650+
list[object],
651+
list[str],
652+
list[pd.Timedelta],
653+
list[pd.Timedelta],
651654
]:
652655
signal = np.atleast_2d(signal)
653656

@@ -718,10 +721,10 @@ def process_signal(
718721
signal: np.ndarray,
719722
sampling_rate: int,
720723
*,
721-
file: str = None,
722-
start: Timestamp = None,
723-
end: Timestamp = None,
724-
process_func_args: typing.Dict[str, typing.Any] = None,
724+
file: str | None = None,
725+
start: Timestamp | None = None,
726+
end: Timestamp | None = None,
727+
process_func_args: dict[str, object] | None = None,
725728
) -> pd.Series:
726729
r"""Process audio signal and return result.
727730
@@ -799,7 +802,7 @@ def _process_signal_from_index_wo_segment(
799802
signal: np.ndarray,
800803
sampling_rate: int,
801804
index: pd.Index,
802-
process_func_args: typing.Dict[str, typing.Any] = None,
805+
process_func_args: dict[str, object] | None = None,
803806
) -> pd.Series:
804807
r"""Like process_signal_from_index, but does not apply segmentation."""
805808
if index.empty:
@@ -865,7 +868,7 @@ def process_signal_from_index(
865868
signal: np.ndarray,
866869
sampling_rate: int,
867870
index: pd.Index,
868-
process_func_args: typing.Dict[str, typing.Any] = None,
871+
process_func_args: dict[str, object] | None = None,
869872
) -> pd.Series:
870873
r"""Split a signal into segments and process each segment.
871874
@@ -919,10 +922,10 @@ def _call(
919922
sampling_rate: int,
920923
*,
921924
idx: int = 0,
922-
root: str = None,
923-
file: str = None,
924-
process_func_args: typing.Dict[str, typing.Any] = None,
925-
) -> typing.Any:
925+
root: str | None = None,
926+
file: str | None = None,
927+
process_func_args: dict[str, object] | None = None,
928+
) -> object:
926929
r"""Call processing function, possibly pass special args."""
927930
signal, sampling_rate = utils.preprocess_signal(
928931
signal,
@@ -980,7 +983,7 @@ def __call__(
980983
self,
981984
signal: np.ndarray,
982985
sampling_rate: int,
983-
) -> typing.Any:
986+
) -> object:
984987
r"""Apply processing to signal.
985988
986989
This function processes the signal **without** transforming the output

0 commit comments

Comments
 (0)