Skip to content

Commit

Permalink
ruff fix (upgrade to 3.9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagusiak committed Jun 30, 2024
1 parent c268557 commit cbb3a8d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 38 deletions.
3 changes: 2 additions & 1 deletion alphaconf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
from .internal.application import Application
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
4 changes: 2 additions & 2 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
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
19 changes: 10 additions & 9 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 @@ -127,16 +128,16 @@ 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
24 changes: 10 additions & 14 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,7 +48,7 @@ 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: ...
Expand All @@ -69,13 +65,13 @@ def get(
@overload
def get(
self,
key: Type[T],
key: type[T],
type: None = None,
*,
default: Union[T, RaiseOnMissingType] = raise_on_missing,
) -> 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 @@ -99,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 @@ -124,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 @@ -193,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 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

0 comments on commit cbb3a8d

Please sign in to comment.