Skip to content

Commit

Permalink
Replace flake8 with ruff (#20)
Browse files Browse the repository at this point in the history
* Remplace flake8 by ruff
* Update python version to 3.9 and fix code
  • Loading branch information
kmagusiak authored Jun 30, 2024
1 parent 3685091 commit 436d2ff
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 86 deletions.
23 changes: 0 additions & 23 deletions .flake8

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ __pycache__
dist
.ipynb_checkpoints
*.egg-info
.mypy_cache
.pytest_cache
.*_cache
5 changes: 3 additions & 2 deletions alphaconf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import re
import warnings
from typing import Callable, MutableSequence, Optional, Sequence, TypeVar, Union
from collections.abc import MutableSequence, Sequence
from typing import Callable, Optional, TypeVar, Union

from .frozendict import frozendict # noqa: F401 (expose)
from .frozendict import frozendict
from .internal.application import Application
from .internal.configuration import Configuration

Expand Down
3 changes: 2 additions & 1 deletion alphaconf/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from typing import Callable, Optional, Sequence, TypeVar, Union
from collections.abc import Sequence
from typing import Callable, Optional, TypeVar, Union

from omegaconf import MissingMandatoryValue, OmegaConf

Expand Down
8 changes: 4 additions & 4 deletions alphaconf/inject.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import functools
import inspect
from typing import Any, Callable, Dict, Optional, Union
from typing import Any, Callable, Optional, Union

import alphaconf

Expand All @@ -12,7 +12,7 @@
class ParamDefaultsFunction:
"""Function wrapper that injects default parameters"""

_arg_factory: Dict[str, Callable[[], Any]]
_arg_factory: dict[str, Callable[[], Any]]

def __init__(self, func: Callable):
self.func = func
Expand Down Expand Up @@ -52,8 +52,8 @@ def getter(
ktype = next(type_from_annotation(ptype), None)
if param is not None and param.default is not param.empty:
xparam = param
return (
lambda: xparam.default
return lambda: (
xparam.default
if (value := alphaconf.get(key, ktype, default=None)) is None
and xparam.default is not xparam.empty
else value
Expand Down
3 changes: 1 addition & 2 deletions alphaconf/interactive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from typing import List

from . import set_application
from .internal.application import Application
Expand All @@ -11,7 +10,7 @@
application = Application(name="interactive")


def mount(configuration_paths: List[str] = [], setup_logging: bool = True):
def mount(configuration_paths: list[str] = [], setup_logging: bool = True):
"""Mount the interactive application and setup configuration"""
application.setup_configuration(configuration_paths=configuration_paths)
set_application(application)
Expand Down
9 changes: 5 additions & 4 deletions alphaconf/internal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import os
import sys
import uuid
from typing import Callable, Iterable, List, MutableMapping, Optional, Tuple, Union, cast
from collections.abc import Iterable, MutableMapping
from typing import Callable, Optional, Union, cast

from omegaconf import DictConfig, OmegaConf

Expand Down Expand Up @@ -133,7 +134,7 @@ def _get_configurations(
self.log.debug('Load configuration from %s', path)
yield load_file.read_configuration_file(path)
# Environment
prefixes: Optional[Tuple[str, ...]]
prefixes: Optional[tuple[str, ...]]
if env_prefixes is True:
self.log.debug('Detecting accepted env prefixes')
default_keys = {str(k) for k in default_configuration}
Expand All @@ -155,7 +156,7 @@ def _get_configurations(
def setup_configuration(
self,
*,
arguments: List[str] = [],
arguments: list[str] = [],
configuration_paths: Iterable[str] = [],
load_dotenv: Optional[bool] = None,
env_prefixes: Union[bool, Iterable[str]] = True,
Expand Down Expand Up @@ -198,7 +199,7 @@ def masked_configuration(
*,
mask_base: bool = True,
mask_secrets: bool = True,
mask_keys: List[str] = ['application.uuid'],
mask_keys: list[str] = ['application.uuid'],
) -> dict:
"""Get the configuration as dict with masked items
Expand Down
23 changes: 12 additions & 11 deletions alphaconf/internal/arg_parser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import itertools
from typing import Dict, Iterable, List, Mapping, Optional, Tuple, Type, Union, cast
from collections.abc import Iterable, Mapping
from typing import Optional, Union, cast

from omegaconf import DictConfig, OmegaConf


def _split(value: str, char: str = "=") -> Tuple[str, Optional[str]]:
def _split(value: str, char: str = "=") -> tuple[str, Optional[str]]:
vs = value.split(char, 1)
if len(vs) < 2:
return vs[0], None
Expand Down Expand Up @@ -91,7 +92,7 @@ class ConfigurationAction(Action):

def check_argument(self, value):
if self.metavar and '=' in self.metavar and '=' not in value:
return 'Argument should be in format %s' % self.metavar
return f'Argument should be in format {self.metavar}'
return super().check_argument(value)

def handle(self, result, value):
Expand Down Expand Up @@ -119,24 +120,24 @@ def check_argument(self, value):
def handle(self, result, value):
key, value = _split(value)
value = value or 'default'
arg = "{key}=${{oc.select:base.{key}.{value}}}".format(key=key, value=value)
arg = f"{key}=${{oc.select:base.{key}.{value}}}"
return super().handle(result, arg)


class ParseResult:
"""The result of argument parsing"""

result: Optional[Action]
rest: List[str]
_config: List[Union[str, DictConfig]]
rest: list[str]
_config: list[Union[str, DictConfig]]

def __init__(self) -> None:
"""Initialize the result"""
self.result = None
self.rest = []
self._config = []

def _add_config(self, value: Union[List[str], DictConfig, Dict, str]):
def _add_config(self, value: Union[list[str], DictConfig, dict, str]):
"""Add a configuration item"""
if isinstance(value, list):
self._config.extend(value)
Expand Down Expand Up @@ -169,16 +170,16 @@ def __repr__(self) -> str:
class ArgumentParser:
"""Parses arguments for alphaconf"""

_opt_actions: Dict[str, Action]
_pos_actions: List[Action]
_opt_actions: dict[str, Action]
_pos_actions: list[Action]
help_messages: Mapping[str, str]

def __init__(self, help_messages: Mapping[str, str] = {}) -> None:
self._opt_actions = {}
self._pos_actions = []
self.help_messages = help_messages or {}

def parse_args(self, arguments: List[str]) -> ParseResult:
def parse_args(self, arguments: list[str]) -> ParseResult:
"""Parse the argument"""
result = ParseResult()
arguments = list(arguments)
Expand Down Expand Up @@ -229,7 +230,7 @@ def parse_args(self, arguments: List[str]) -> ParseResult:
result.rest += arguments
return result

def add_argument(self, action_class: Type[Action], *names: str, **kw):
def add_argument(self, action_class: type[Action], *names: str, **kw):
"""Add an argument handler
:param action_class: Action(kw) will be added as a handler
Expand Down
35 changes: 14 additions & 21 deletions alphaconf/internal/configuration.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import copy
import os
import warnings
from collections.abc import Iterable, MutableMapping
from enum import Enum
from typing import (
Any,
Dict,
Iterable,
List,
MutableMapping,
Optional,
Type,
TypeVar,
Union,
cast,
Expand All @@ -33,9 +29,9 @@ class RaiseOnMissingType(Enum):

class Configuration:
c: DictConfig
__type_path: MutableMapping[Type, Optional[str]]
__type_value: MutableMapping[Type, Any]
helpers: Dict[str, str]
__type_path: MutableMapping[type, Optional[str]]
__type_value: MutableMapping[type, Any]
helpers: dict[str, str]

def __init__(self, *, parent: Optional["Configuration"] = None) -> None:
if parent:
Expand All @@ -52,11 +48,10 @@ def __init__(self, *, parent: Optional["Configuration"] = None) -> None:
def get(
self,
key: str,
type: Type[T],
type: type[T],
*,
default: Union[T, RaiseOnMissingType] = raise_on_missing,
) -> T:
...
) -> T: ...

@overload
def get(
Expand All @@ -65,20 +60,18 @@ def get(
type: Union[str, None] = None,
*,
default: Any = raise_on_missing,
) -> Any:
...
) -> Any: ...

@overload
def get(
self,
key: Type[T],
key: type[T],
type: None = None,
*,
default: Union[T, RaiseOnMissingType] = raise_on_missing,
) -> T:
...
) -> T: ...

def get(self, key: Union[str, Type], type=None, *, default=raise_on_missing):
def get(self, key: Union[str, type], type=None, *, default=raise_on_missing):
"""Get a configuation value and cast to the correct type"""
if isinstance(key, _cla_type):
return self.__get_type(key, default=default)
Expand All @@ -102,7 +95,7 @@ def get(self, key: Union[str, Type], type=None, *, default=raise_on_missing):
value = convert_to_type(value, type)
return value

def __get_type(self, key: Type, *, default=raise_on_missing):
def __get_type(self, key: type, *, default=raise_on_missing):
value = self.__type_value.get(key)
if value is not None:
return value
Expand All @@ -127,7 +120,7 @@ def _merge(self, configs: Iterable[DictConfig]):
def setup_configuration(
self,
conf: Union[DictConfig, dict, Any],
helpers: Dict[str, str] = {},
helpers: dict[str, str] = {},
*,
prefix: str = "",
):
Expand Down Expand Up @@ -196,7 +189,7 @@ def from_environ(self, prefixes: Iterable[str]) -> DictConfig:
return conf

@staticmethod
def _find_name(parts: List[str], conf: DictConfig) -> str:
def _find_name(parts: list[str], conf: DictConfig) -> str:
"""Find a name from parts, by trying joining with '.' (default) or '_'"""
if len(parts) < 2:
return "".join(parts)
Expand All @@ -205,7 +198,7 @@ def _find_name(parts: List[str], conf: DictConfig) -> str:
if name:
name += "_"
name += part
if name in conf.keys():
if name in conf:
sub_conf = conf.get(name)
if next_offset == len(parts):
return name
Expand Down
4 changes: 2 additions & 2 deletions alphaconf/internal/load_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Any, Tuple
from typing import Any

from omegaconf import DictConfig, OmegaConf

Expand All @@ -11,7 +11,7 @@
class TomlDecoderPrimitive(toml.TomlDecoder):
"""toml loader which reads dates as strings for compitability with JSON"""

def load_value(self, v: str, strictly_valid: bool = True) -> Tuple[Any, str]:
def load_value(self, v: str, strictly_valid: bool = True) -> tuple[Any, str]:
value, itype = super().load_value(v, strictly_valid)
# convert date, datetime, time using isoformat()
if itype in ('date', 'time'):
Expand Down
6 changes: 3 additions & 3 deletions alphaconf/invoke.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Union
from typing import Union

import invoke
from omegaconf import OmegaConf
Expand Down Expand Up @@ -52,13 +52,13 @@ def run_program(self):
return prog.run(argv)


def collection(variables: Dict = {}) -> invoke.Collection:
def collection(variables: dict = {}) -> invoke.Collection:
"""Create a new collection base on tasks in the variables"""
return invoke.Collection(*[v for v in variables.values() if isinstance(v, invoke.Task)])


def run(
__name__: str, namespace: Union[invoke.collection.Collection, Dict], **properties
__name__: str, namespace: Union[invoke.collection.Collection, dict], **properties
) -> InvokeApplication:
"""Create an invoke application and run it if __name__ is __main__"""
if isinstance(namespace, invoke.Collection):
Expand Down
2 changes: 1 addition & 1 deletion alphaconf/logging_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, *args, **kw):
def getMessage(self) -> str: # noqa: N802
msg = super().getMessage()
if self.context:
msg = "%s %s" % (self.context, msg)
msg = f"{self.context} {msg}"
return msg

def getRawMessage(self) -> str: # noqa: N802
Expand Down
11 changes: 8 additions & 3 deletions pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ cd "$(dirname "$0")"
[ -d .git ]

pre_commit() {
flake8
ruff check .
black --check .
isort --check-only .
mypy .
}

fix_all() {
black .
ruff check --fix-only .
}
format() {
black .
isort .
}

# Commands
Expand All @@ -23,6 +25,9 @@ case "${1:-run}" in
pre_commit
echo "All good to commit"
;;
fix)
fix_all
;;
format)
format
;;
Expand Down
Loading

0 comments on commit 436d2ff

Please sign in to comment.