Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into kma/simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagusiak committed Jun 30, 2024
2 parents 2ea76ff + 436d2ff commit 7d20293
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 68 deletions.
23 changes: 0 additions & 23 deletions .flake8

This file was deleted.

8 changes: 5 additions & 3 deletions alphaconf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging
import re
from typing import Callable, MutableSequence, Optional, TypeVar
import warnings
from collections.abc import MutableSequence
from typing import Callable, Optional, TypeVar

from .frozendict import frozendict # noqa: F401 (expose)
from .frozendict import frozendict # (expose)
from .internal.configuration import Configuration
from .internal.load_file import read_configuration_file

Expand Down Expand Up @@ -160,4 +162,4 @@ def __alpha_configuration():

# Initialize configuration
__alpha_configuration()
__all__ = ["get", "setup_configuration", "frozendict"]
__all__ = ["get", "setup_configuration", "frozendict", "warnings"]
13 changes: 7 additions & 6 deletions alphaconf/cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sys
import logging
import argparse
from typing import Any, Callable, Optional, Sequence, TypeVar, Union
import logging
import sys
from collections.abc import Sequence
from typing import Callable, Optional, TypeVar, Union

from omegaconf import MissingMandatoryValue, OmegaConf

from . import initialize, setup_configuration
from .internal.load_file import read_configuration_file

T = TypeVar('T')
__all__ = ["run", "Application"]
__all__ = ["run"]
log = logging.getLogger(__name__)


Expand All @@ -27,7 +28,7 @@ class SelectConfigAction(ConfigAction):
def __call__(self, parser, namespace, values, option_string=None):
key, value = values.split('=') # XXX _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().__call__(parser, namespace, [arg], option_string)


Expand Down Expand Up @@ -98,7 +99,7 @@ def run(
:param config: Arguments passed to Application.__init__() and Application.setup_configuration()
:return: The result of main
"""
from . import get, _global_configuration
from . import _global_configuration, get

arg_parser = parser_create(main, **config, exit_on_error=should_exit)
try:
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
11 changes: 6 additions & 5 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 @@ -87,7 +88,7 @@ def get_configurations(
log.debug('Load configuration from %s', path)
yield load_file.read_configuration_file(path)
# Environment
prefixes: Optional[Tuple[str, ...]]
prefixes: tuple[str, ...] | None
if env_prefixes is True:
log.debug('Detecting accepted env prefixes')
default_keys = {str(k) for k in default_configuration}
Expand Down Expand Up @@ -249,7 +250,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 @@ -271,7 +272,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 @@ -314,7 +315,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
26 changes: 11 additions & 15 deletions alphaconf/internal/configuration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import copy
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 @@ -32,9 +28,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 @@ -51,7 +47,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 @@ -68,13 +64,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: Any = raise_on_missing) -> Any:
def get(self, key: Union[str, type], type=None, *, default: Any = raise_on_missing) -> Any:
"""Get a configuation value and cast to the correct type"""
if isinstance(key, _cla_type):
return self.__get_type(key, default=default)
Expand All @@ -98,7 +94,7 @@ def get(self, key: Union[str, Type], type=None, *, default: Any = raise_on_missi
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 @@ -123,7 +119,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 @@ -171,7 +167,7 @@ def add_helper(self, key, description):
self.helpers[key] = description

@staticmethod
def _find_name(parts: List[str], conf: DictConfig) -> str: # XXX move to application
def _find_name(parts: list[str], conf: DictConfig) -> str: # XXX move to application
"""Find a name from parts, by trying joining with '.' (default) or '_'"""
if len(parts) < 2:
return "".join(parts)
Expand All @@ -180,7 +176,7 @@ def _find_name(parts: List[str], conf: DictConfig) -> str: # XXX move to applic
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
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
34 changes: 30 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,38 @@ skip-string-normalization = 1
[tool.mypy]
ignore_missing_imports = true

[tool.isort]
profile = "black"
line_length = 100

[tools.setuptools]
packages = ["alphaconf"]

[tool.setuptools_scm]
local_scheme = "no-local-version"

[tool.ruff]
line-length = 100

[tool.ruff.lint]
# https://beta.ruff.rs/docs/rules/
select = [
#"C9", # mccabe
#"D", # documentation
"E",
"F",
"W",
"I", # isort
"N", # naming
"UP", # pyupdate
"C4", # comprehensions
"EXE",
"SIM",
"RUF",
]
ignore = [
"D102", # mission doc in public method, function
"D205", # blank line required betweeen summary and description
"D400", # first line should end with a period
"E731", # don't assign lambda
"SIM108", # simplify ITE by operator
]

[tool.ruff.lint.mccabe]
max-complexity = 10
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
setuptools
setuptools_scm
black>=22
flake8>=7
isort>=5
mypy
pytest
ruff>=0.5
pytest>=8
types-toml

# Libraries
Expand Down
2 changes: 1 addition & 1 deletion tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_log_exception(log, caplog):
log.error('err', exc_info=True)
rec = caplog.records[0]
exc_type, exc, tb = rec.exc_info
assert exc_type == ValueError and str(exc) == 'tvalue' and tb
assert exc_type is ValueError and str(exc) == 'tvalue' and tb


def test_log_format(log, caplog):
Expand Down

0 comments on commit 7d20293

Please sign in to comment.