Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop python 3.8 #111

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macOS-latest ]
python-version: [ '3.8', '3.9', '3.10', '3.11', '3.12' ]
python-version: [ '3.9', '3.10', '3.11', '3.12' ]

steps:
- uses: actions/checkout@v4
Expand Down
34 changes: 20 additions & 14 deletions audobject/core/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

from collections.abc import Mapping
import io
import os
import typing
import warnings

import oyaml as yaml
Expand All @@ -16,14 +19,14 @@
)


def from_dict( # noqa: D417
d: typing.Dict[str, typing.Any],
root: str = None,
def from_dict(
d: Mapping[str, object],
root: str | None = None,
hagenw marked this conversation as resolved.
Show resolved Hide resolved
*,
auto_install: bool = False,
override_args: typing.Dict[str, typing.Any] = None,
override_args: dict[str, object] | None = None,
ChristianGeng marked this conversation as resolved.
Show resolved Hide resolved
**kwargs,
) -> "Object":
) -> Object:
r"""Create object from dictionary.

Args:
Expand All @@ -32,6 +35,7 @@ def from_dict( # noqa: D417
auto_install: install missing packages needed to create the object
override_args: override arguments in ``d`` or
default values of hidden arguments
kwargs: arguments to add to override
hagenw marked this conversation as resolved.
Show resolved Hide resolved

Returns:
object
Expand Down Expand Up @@ -98,11 +102,11 @@ def from_dict( # noqa: D417
return object


def from_yaml( # noqa: D417
path_or_stream: typing.Union[str, typing.IO],
def from_yaml(
path_or_stream: str | io.IOBase,
*,
auto_install: bool = False,
override_args: typing.Dict[str, typing.Any] = None,
override_args: dict[str, object] | None = None,
**kwargs,
) -> "Object":
r"""Create object from YAML file.
Expand All @@ -112,6 +116,7 @@ def from_yaml( # noqa: D417
auto_install: install missing packages needed to create the object
override_args: override arguments in the YAML file or
default values of hidden arguments
kwargs: arguments to add to override

Returns:
object
Expand Down Expand Up @@ -143,11 +148,11 @@ def from_yaml( # noqa: D417
)


def from_yaml_s( # noqa: D417
def from_yaml_s(
yaml_string: str,
*,
auto_install: bool = False,
override_args: typing.Dict[str, typing.Any] = None,
override_args: dict[str, object] | None = None,
**kwargs,
) -> "Object":
r"""Create object from YAML string.
Expand All @@ -157,6 +162,7 @@ def from_yaml_s( # noqa: D417
auto_install: install missing packages needed to create the object
override_args: override arguments in the YAML string or
default values of hidden arguments
kwargs: arguments to add to override

Returns:
object
Expand All @@ -181,10 +187,10 @@ def from_yaml_s( # noqa: D417


def _decode_value(
value_to_decode: typing.Any,
value_to_decode: object,
auto_install: bool,
override_args: typing.Dict[str, typing.Any],
) -> typing.Any:
override_args: dict[str, object],
) -> object:
r"""Decode value."""
if value_to_decode: # not empty
if isinstance(value_to_decode, list):
Expand Down
10 changes: 6 additions & 4 deletions audobject/core/decorator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import annotations

from collections.abc import Sequence
import functools
import inspect
import typing

from audobject.core import define
from audobject.core import resolver


def init_decorator(
*,
borrow: typing.Dict[str, str] = None,
hide: typing.Sequence[str] = None,
resolvers: typing.Dict[str, typing.Type[resolver.Base]] = None,
borrow: dict[str, str] | None = None,
hide: Sequence[str] | None = None,
resolvers: dict[str, type[resolver.Base]] | None = None,
):
r"""Decorator for ``__init__`` function of :class:`audobject.Object`.

Expand Down
16 changes: 10 additions & 6 deletions audobject/core/dictionary.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import typing
from __future__ import annotations

from collections.abc import ItemsView
from collections.abc import KeysView
from collections.abc import ValuesView

from audobject.core import define
from audobject.core.object import Object
Expand Down Expand Up @@ -44,11 +48,11 @@ def __init__(
for key, value in kwargs.items():
self[key] = value

def keys(self) -> typing.KeysView[str]:
def keys(self) -> KeysView[str]:
r"""Return the keys."""
return self._dict_wo_special_attributes.keys()

def items(self) -> typing.ItemsView[str, typing.Any]:
def items(self) -> ItemsView[str, object]:
r"""Return items view."""
return self._dict_wo_special_attributes.items()

Expand All @@ -65,7 +69,7 @@ def update(
for key, value in other.items():
self[key] = value

def values(self) -> typing.ValuesView[typing.Any]:
def values(self) -> ValuesView[object]:
r"""Return values."""
return self._dict_wo_special_attributes.values()

Expand All @@ -85,15 +89,15 @@ def __contains__(self, key):
r"""Check if key is in dictionary."""
return key in self._dict_wo_special_attributes

def __getitem__(self, name: str) -> typing.Any:
def __getitem__(self, name: str) -> object:
r"""Return value at key from dictionary."""
return self._dict_wo_special_attributes[name]

def __len__(self):
r"""Number of keys in dictionary."""
return len(self._dict_wo_special_attributes)

def __setitem__(self, key: str, value: typing.Any):
def __setitem__(self, key: str, value: object):
r"""Set value at key in dictionary."""
if key not in self.__dict__[define.KEYWORD_ARGUMENTS]:
self.__dict__[define.KEYWORD_ARGUMENTS].append(key)
Expand Down
37 changes: 20 additions & 17 deletions audobject/core/object.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import collections.abc
from __future__ import annotations

from collections.abc import Mapping
from collections.abc import Sequence
import inspect
import io
import os
import typing
import warnings

import oyaml as yaml
Expand Down Expand Up @@ -33,7 +36,7 @@ def __init__(
self.__dict__[define.KEYWORD_ARGUMENTS] = list(kwargs)

@property
def arguments(self) -> typing.Dict[str, typing.Any]:
def arguments(self) -> Mapping[str, object]:
r"""Returns arguments that are serialized.
hagenw marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Expand Down Expand Up @@ -95,7 +98,7 @@ def arguments(self) -> typing.Dict[str, typing.Any]:
if hasattr(self, value):
if hasattr(self.__dict__[value], key):
can_borrow = True
elif isinstance(self.__dict__[value], collections.abc.Mapping):
elif isinstance(self.__dict__[value], Mapping):
can_borrow = True
if not can_borrow:
raise RuntimeError(
Expand All @@ -113,7 +116,7 @@ def arguments(self) -> typing.Dict[str, typing.Any]:
return args

@property
def borrowed_arguments(self) -> typing.Dict[str, str]:
def borrowed_arguments(self) -> Mapping[str, str]:
r"""Returns borrowed arguments.

Returns:
Expand All @@ -127,7 +130,7 @@ def borrowed_arguments(self) -> typing.Dict[str, str]:
return borrowed

@property
def hidden_arguments(self) -> typing.List[str]:
def hidden_arguments(self) -> Sequence[str]:
r"""Returns hidden arguments.

Returns:
Expand Down Expand Up @@ -189,7 +192,7 @@ def is_loaded_from_dict(self) -> bool:
alternative="audobject.from_dict",
)
def from_dict( # noqa: D102
d: typing.Dict[str, typing.Any],
d: Mapping[str, object],
root: str = None,
**kwargs,
) -> "Object": # pragma: no cover
Expand All @@ -203,7 +206,7 @@ def from_dict( # noqa: D102
alternative="audobject.from_yaml",
)
def from_yaml( # noqa: D102
path_or_stream: typing.Union[str, typing.IO],
path_or_stream: str | io.IOBase,
**kwargs,
) -> "Object": # pragma: no cover
from audobject.core.api import from_yaml
Expand All @@ -224,7 +227,7 @@ def from_yaml_s( # noqa: D102
return from_yaml_s(yaml_string, **kwargs)

@property
def resolvers(self) -> typing.Dict[str, resolver.Base]:
def resolvers(self) -> Mapping[str, resolver.Base]:
r"""Return resolvers.

Returns:
Expand Down Expand Up @@ -271,7 +274,7 @@ def to_dict(
include_version: bool = True,
flatten: bool = False,
root: str = None,
) -> typing.Dict[str, resolver.DefaultValueType]:
) -> Mapping[str, resolver.DefaultValueType]:
r"""Converts object to a dictionary.

Includes items from :attr:`audobject.Object.arguments`.
Expand Down Expand Up @@ -313,7 +316,7 @@ def to_dict(

def to_yaml(
self,
path_or_stream: typing.Union[str, typing.IO],
path_or_stream: str | io.IOBase,
*,
include_version: bool = True,
):
Expand Down Expand Up @@ -368,9 +371,9 @@ def to_yaml_s(
def _encode_variable(
self,
name: str,
value: typing.Any,
value: object,
include_version: bool,
root: typing.Optional[str],
root: str | None,
):
r"""Encode value.

Expand All @@ -384,7 +387,7 @@ def _encode_variable(

@staticmethod
def _encode_value(
value: typing.Any,
value: object,
include_version: bool,
):
r"""Default value encoder."""
Expand Down Expand Up @@ -415,7 +418,7 @@ def _encode_value(

@staticmethod
def _flatten(
d: typing.Dict[str, resolver.DefaultValueType],
d: Mapping[str, resolver.DefaultValueType],
):
r"""Flattens a dictionary."""

Expand Down Expand Up @@ -447,8 +450,8 @@ def helper(dict_in: dict, dict_out: dict, prefix: str):
def _resolve_value(
self,
name: str,
value: typing.Any,
root: typing.Optional[str],
value: object,
root: str | None,
) -> resolver.DefaultValueType:
if value is not None and name in self.resolvers:
# let resolver know if we write to a stream
Expand Down
24 changes: 13 additions & 11 deletions audobject/core/parameter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import argparse
from collections.abc import Sequence
import os
import typing

import packaging.specifiers
import packaging.version
Expand Down Expand Up @@ -73,9 +75,9 @@ def __init__(
*,
value_type: type = str,
description: str = "",
value: typing.Any = None,
default_value: typing.Any = None,
choices: typing.Sequence[typing.Any] = None,
value: object = None,
default_value: object = None,
choices: Sequence[object] = None,
version: str = None,
):
self.value_type = value_type
Expand All @@ -99,7 +101,7 @@ def __init__(
else:
self.set_value(default_value)

def __contains__(self, version: typing.Optional[str]) -> bool:
def __contains__(self, version: str | None) -> bool:
r"""Check if parameter is in parameter object."""
if version is None or self.version is None:
return True
Expand All @@ -109,7 +111,7 @@ def __contains__(self, version: typing.Optional[str]) -> bool:

return version in version_range

def set_value(self, value: typing.Any):
def set_value(self, value: object):
r"""Sets a new value.

Applies additional checks, e.g. if value is of the expected type.
Expand All @@ -125,7 +127,7 @@ def set_value(self, value: typing.Any):
self._check_value(value)
self.value = value

def _check_value(self, value: typing.Any):
def _check_value(self, value: object):
r"""Check if value matches expected type."""
if value is not None and not isinstance(value, self.value_type):
raise TypeError(
Expand Down Expand Up @@ -253,8 +255,8 @@ def to_path(
self,
*,
delimiter: str = os.path.sep,
include: typing.Sequence[str] = None,
exclude: typing.Sequence[str] = None,
include: Sequence[str] | None = None,
exclude: Sequence[str] | None = None,
ChristianGeng marked this conversation as resolved.
Show resolved Hide resolved
sort: bool = False,
):
r"""Creates path from parameters.
Expand Down Expand Up @@ -284,13 +286,13 @@ def __call__(self):
r"""Return parameters as dictionary."""
return {name: param.value for name, param in self.items()}

def __getattribute__(self, name) -> typing.Any: # noqa: D105
def __getattribute__(self, name) -> object: # noqa: D105
if not name == "__dict__" and name in self.__dict__:
p = self.__dict__[name]
return p.value
return object.__getattribute__(self, name)

def __setattr__(self, name: str, value: typing.Any): # noqa: D105
def __setattr__(self, name: str, value: object): # noqa: D105
p = self.__dict__[name]
p.set_value(value)

Expand Down
Loading