Skip to content

Commit

Permalink
Update syntax to Python >=3.9 (#463)
Browse files Browse the repository at this point in the history
* Update syntax to Python >=3.9

* Fix self annotations

* Update test code

* Improve code

* Fix link in docs

* Fix tests

* Fix tests

* Update remaining files

* Update remaining tests

* Remove more typing

* Revert "Remove more typing"

This reverts commit d5c39ca.

* Remove comment
  • Loading branch information
hagenw authored Jan 17, 2025
1 parent 8bbff3e commit 501b699
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 207 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ It is also possible to automatically check if all links are still valid::

python -m sphinx docs/ build/sphinx/html -b linkcheck

.. _Sphinx: https://sphinx-doc.org
.. _Sphinx: https://www.sphinx-doc.org


Running the Tests
Expand Down
3 changes: 1 addition & 2 deletions audformat/core/attachment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import typing

import audeer

Expand Down Expand Up @@ -62,7 +61,7 @@ def __init__(
@property
def files(
self,
) -> typing.List[str]:
) -> list[str]:
r"""List all files that are part of the attachment.
List recursively the relative path
Expand Down
10 changes: 5 additions & 5 deletions audformat/core/column.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations # allow typing without string
from __future__ import annotations

import typing
import warnings
Expand Down Expand Up @@ -103,7 +103,7 @@ def __init__(
self._id = None

@property
def rater(self) -> typing.Optional[Rater]:
def rater(self) -> Rater | None:
r"""Rater object.
Returns:
Expand All @@ -118,7 +118,7 @@ def rater(self) -> typing.Optional[Rater]:
return self.table.db.raters[self.rater_id]

@property
def scheme(self) -> typing.Optional[Scheme]:
def scheme(self) -> Scheme | None:
r"""Scheme object.
Returns:
Expand Down Expand Up @@ -151,7 +151,7 @@ def get(
as_segmented: bool = False,
allow_nat: bool = True,
root: str = None,
num_workers: typing.Optional[int] = 1,
num_workers: int | None = 1,
verbose: bool = False,
) -> pd.Series:
r"""Get labels.
Expand Down Expand Up @@ -377,7 +377,7 @@ def set(

def __eq__(
self,
other: "Column",
other: Column,
) -> bool:
r"""Compare if column equals another column."""
if self.dump() != other.dump():
Expand Down
17 changes: 10 additions & 7 deletions audformat/core/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

from collections import OrderedDict
from collections.abc import Callable
from collections.abc import Sequence
import inspect
import os
import textwrap
import typing

import oyaml as yaml
import pandas as pd
Expand Down Expand Up @@ -72,8 +75,8 @@ def __init__(
*args,
sort_by_key: bool = True,
value_type: type = None,
get_callback: typing.Callable = None,
set_callback: typing.Callable = None,
get_callback: Callable = None,
set_callback: Callable = None,
**kwargs,
):
self.sort_by_key = sort_by_key
Expand Down Expand Up @@ -223,7 +226,7 @@ def to_dict(self) -> dict:
def from_dict(
self,
d: dict,
ignore_keys: typing.Sequence[str] = None,
ignore_keys: Sequence[str] = None,
):
r"""Deserialize object from dictionary.
Expand Down Expand Up @@ -267,7 +270,7 @@ def dump(

def __eq__(
self,
other: "HeaderBase",
other: HeaderBase,
) -> bool:
return self.dump() == other.dump()

Expand Down Expand Up @@ -328,7 +331,7 @@ def series_to_html(self): # pragma: no cover
return df.to_html()


def to_audformat_dtype(dtype: typing.Union[str, typing.Type]) -> str:
def to_audformat_dtype(dtype: str | type) -> str:
r"""Convert pandas to audformat dtype."""
# bool
if pd.api.types.is_bool_dtype(dtype):
Expand Down Expand Up @@ -359,7 +362,7 @@ def to_audformat_dtype(dtype: typing.Union[str, typing.Type]) -> str:
return define.DataType.OBJECT


def to_pandas_dtype(dtype: str) -> typing.Optional[str]:
def to_pandas_dtype(dtype: str) -> str | None:
r"""Convert audformat to pandas dtype.
We use ``"Int64"`` instead of ``"int64"``,
Expand Down
77 changes: 36 additions & 41 deletions audformat/core/database.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

from collections.abc import Callable
from collections.abc import Sequence
import datetime
import itertools
import os
import shutil
import typing

import oyaml as yaml

Expand Down Expand Up @@ -146,11 +149,11 @@ def __init__(
usage: str = define.Usage.UNRESTRICTED,
*,
expires: datetime.date = None,
languages: typing.Union[str, typing.Sequence[str]] = None,
languages: str | Sequence[str] = None,
description: str = None,
author: str = None,
organization: str = None,
license: typing.Union[str, define.License] = None,
license: str | define.License = None,
license_url: str = None,
meta: dict = None,
):
Expand Down Expand Up @@ -264,7 +267,7 @@ def is_portable(
return all(is_relative_path(f) for f in self.files)

@property
def root(self) -> typing.Optional[str]:
def root(self) -> str | None:
r"""Database root directory.
Returns ``None`` if database has not been stored yet.
Expand Down Expand Up @@ -292,12 +295,8 @@ def segments(self) -> pd.MultiIndex:

def drop_files(
self,
files: typing.Union[
str,
typing.Sequence[str],
typing.Callable[[str], bool],
],
num_workers: typing.Optional[int] = 1,
files: (str | Sequence[str] | Callable[[str], bool]),
num_workers: int | None = 1,
verbose: bool = False,
):
r"""Drop files from tables.
Expand All @@ -323,7 +322,7 @@ def drop_files(

def drop_tables(
self,
table_ids: typing.Union[str, typing.Sequence[str]],
table_ids: str | Sequence[str],
):
r"""Drop (miscellaneous) tables by ID.
Expand Down Expand Up @@ -363,7 +362,7 @@ def drop_tables(

def files_duration(
self,
files: typing.Union[str, typing.Sequence[str]],
files: str | Sequence[str],
*,
root: str = None,
) -> pd.Series:
Expand Down Expand Up @@ -438,14 +437,14 @@ def duration(file: str) -> pd.Timedelta:
def get(
self,
scheme: str,
additional_schemes: typing.Union[str, typing.Sequence] = [],
additional_schemes: str | Sequence = [],
*,
tables: typing.Union[str, typing.Sequence] = None,
splits: typing.Union[str, typing.Sequence] = None,
tables: str | Sequence = None,
splits: str | Sequence = None,
strict: bool = False,
map: bool = True,
original_column_names: bool = False,
aggregate_function: typing.Callable[[pd.Series], typing.Any] = None,
aggregate_function: Callable[[pd.Series], object] = None,
aggregate_strategy: str = "mismatch",
) -> pd.DataFrame:
r"""Get labels by scheme.
Expand Down Expand Up @@ -882,8 +881,8 @@ def scheme_in_column(scheme_id, column, column_id):

def map_files(
self,
func: typing.Callable[[str], str],
num_workers: typing.Optional[int] = 1,
func: Callable[[str], str],
num_workers: int | None = 1,
verbose: bool = False,
):
r"""Apply function to file names in all tables.
Expand Down Expand Up @@ -914,12 +913,8 @@ def job(table):

def pick_files(
self,
files: typing.Union[
str,
typing.Sequence[str],
typing.Callable[[str], bool],
],
num_workers: typing.Optional[int] = 1,
files: (str | Sequence[str] | Callable[[str], bool]),
num_workers: int | None = 1,
verbose: bool = False,
):
r"""Pick files from tables.
Expand All @@ -945,7 +940,7 @@ def pick_files(

def pick_tables(
self,
table_ids: typing.Union[str, typing.Sequence[str]],
table_ids: str | Sequence[str],
):
r"""Pick (miscellaneous) tables by ID.
Expand Down Expand Up @@ -977,7 +972,7 @@ def save(
storage_format: str = define.TableStorageFormat.PARQUET,
update_other_formats: bool = True,
header_only: bool = False,
num_workers: typing.Optional[int] = 1,
num_workers: int | None = 1,
verbose: bool = False,
):
r"""Save database to disk.
Expand Down Expand Up @@ -1038,12 +1033,12 @@ def job(obj_id, obj):

def update(
self,
others: typing.Union["Database", typing.Sequence["Database"]],
others: Database | Sequence[Database],
*,
copy_attachments: bool = False,
copy_media: bool = False,
overwrite: bool = False,
) -> "Database":
) -> Database:
r"""Update database with other database(s).
In order to :ref:`update a database <update-a-database>`,
Expand Down Expand Up @@ -1121,7 +1116,7 @@ def assert_equal(

def join_dict(
field: str,
ds: typing.Sequence[dict],
ds: Sequence[dict],
):
r"""Join list of dictionaries.
Expand All @@ -1148,7 +1143,7 @@ def join_dict(
def join_field(
other: Database,
field: str,
op: typing.Callable,
op: Callable,
):
r"""Join two fields of db header."""
value1 = self.__dict__[field]
Expand Down Expand Up @@ -1312,7 +1307,7 @@ def __contains__(
def __getitem__(
self,
table_id: str,
) -> typing.Union[MiscTable, Table]:
) -> MiscTable | Table:
r"""Get (miscellaneous) table from database.
Args:
Expand All @@ -1334,7 +1329,7 @@ def __getitem__(

def __eq__(
self,
other: "Database",
other: Database,
) -> bool:
r"""Comparison if database equals another database."""
if self.dump() != other.dump():
Expand All @@ -1346,15 +1341,15 @@ def __eq__(

def __iter__(
self,
) -> typing.Union[MiscTable, Table]:
) -> MiscTable | Table:
r"""Iterate over (miscellaneous) tables of database."""
yield from sorted(list(self.tables) + list(self.misc_tables))

def __setitem__(
self,
table_id: str,
table: typing.Union[MiscTable, Table],
) -> typing.Union[MiscTable, Table]:
table: MiscTable | Table,
) -> MiscTable | Table:
r"""Add table to database.
Args:
Expand All @@ -1381,9 +1376,9 @@ def load(
*,
name: str = "db",
load_data: bool = False,
num_workers: typing.Optional[int] = 1,
num_workers: int | None = 1,
verbose: bool = False,
) -> "Database":
) -> Database:
r"""Load database from disk.
Expects a header ``<root>/<name>.yaml``
Expand Down Expand Up @@ -1424,7 +1419,7 @@ def load(
if not os.path.exists(path):
raise FileNotFoundError(path)

with open(path, "r") as fp:
with open(path) as fp:
header = yaml.load(fp, Loader=Loader)
db = Database.load_header_from_yaml(header)

Expand Down Expand Up @@ -1467,7 +1462,7 @@ def job(obj, obj_path):
return db

@staticmethod
def load_header_from_yaml(header: dict) -> "Database":
def load_header_from_yaml(header: dict) -> Database:
r"""Load database header from YAML.
Args:
Expand Down Expand Up @@ -1619,8 +1614,8 @@ def _set_scheme(
def _set_table(
self,
table_id: str,
table: typing.Union[MiscTable, Table],
) -> typing.Union[MiscTable, Table]:
table: MiscTable | Table,
) -> MiscTable | Table:
if isinstance(table, MiscTable) and table_id in self.tables:
raise TableExistsError(self[table_id].type, table_id)
elif isinstance(table, Table) and table_id in self.misc_tables:
Expand Down
Loading

0 comments on commit 501b699

Please sign in to comment.