From fa77bb674beb8d8095dab9f67a848350647ea46c Mon Sep 17 00:00:00 2001 From: Krzysztof Magusiak Date: Mon, 5 Aug 2024 08:04:07 +0200 Subject: [PATCH] Ruff and pre-commit update (#24) --- .github/workflows/lint-test.yaml | 20 ++++---- .github/workflows/python-publish.yaml | 4 +- .vscode/settings.json | 5 -- alphaconf/internal/application.py | 6 +-- alphaconf/internal/arg_parser.py | 2 +- alphaconf/internal/configuration.py | 2 +- alphaconf/internal/load_file.py | 2 +- pre-commit | 33 ++++++++----- pyproject.toml | 67 ++++++++++++++++++++------- requirements.txt | 16 ------- 10 files changed, 89 insertions(+), 68 deletions(-) delete mode 100644 requirements.txt diff --git a/.github/workflows/lint-test.yaml b/.github/workflows/lint-test.yaml index 233d5e3..d14add1 100644 --- a/.github/workflows/lint-test.yaml +++ b/.github/workflows/lint-test.yaml @@ -10,16 +10,15 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: # use minimum version here from setup.py - python-version: "3.9" + python-version: "3.12" - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install -r requirements.txt + python -m pip install -e .[pinned,dev] - name: Run pre-commit checks run: | ./pre-commit @@ -27,18 +26,17 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - # lowest, common (defaut ubuntu LTS), newest - python-version: ["3.9", "3.10", "3.11"] + # lowest, common (default ubuntu LTS), newest + python-version: ["3.9", "3.12"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install -r requirements.txt + python -m pip install -e .[pinned,dev,test] - name: Run tests run: | pytest diff --git a/.github/workflows/python-publish.yaml b/.github/workflows/python-publish.yaml index edefb29..f20dd12 100644 --- a/.github/workflows/python-publish.yaml +++ b/.github/workflows/python-publish.yaml @@ -21,12 +21,12 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: # don't use shallow checkout to determine an intermediary version correctly fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: '3.11' - name: Install dependencies diff --git a/.vscode/settings.json b/.vscode/settings.json index ac8a2b1..ebe7c49 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,4 @@ { - "black-formatter.args": [ - "--skip-string-normalization", - "--line-length", - "100" - ], "python.testing.pytestArgs": [ "tests" ], diff --git a/alphaconf/internal/application.py b/alphaconf/internal/application.py index 04aae10..c4c9f10 100644 --- a/alphaconf/internal/application.py +++ b/alphaconf/internal/application.py @@ -42,7 +42,7 @@ def __init__( self.argument_parser = self._build_argument_parser() def _build_argument_parser(self) -> arg_parser.ArgumentParser: - from .. import _global_configuration + from .. import _global_configuration # noqa: TID252 p = arg_parser.ArgumentParser(_global_configuration.helpers) arg_parser.configure_parser(p, app=self) @@ -162,7 +162,7 @@ def setup_configuration( env_prefixes: Union[bool, Iterable[str]] = True, resolve_configuration: bool = True, ): - from .. import _global_configuration as ctx_configuration + from .. import _global_configuration as ctx_configuration # noqa: TID252 from .dotenv_vars import try_dotenv try_dotenv(load_dotenv=load_dotenv) @@ -208,7 +208,7 @@ def masked_configuration( :param mask_keys: Which keys to mask :return: Configuration copy with masked values """ - from .. import SECRET_MASKS + from .. import SECRET_MASKS # noqa: TID252 config = cast(dict, OmegaConf.to_container(self.configuration.c)) if mask_secrets: diff --git a/alphaconf/internal/arg_parser.py b/alphaconf/internal/arg_parser.py index b810f04..ffd1bef 100644 --- a/alphaconf/internal/arg_parser.py +++ b/alphaconf/internal/arg_parser.py @@ -142,7 +142,7 @@ def _add_config(self, value: Union[list[str], DictConfig, dict, str]): if isinstance(value, list): self._config.extend(value) return - elif isinstance(value, DictConfig): + if isinstance(value, DictConfig): pass elif isinstance(value, dict): value = OmegaConf.create(value) diff --git a/alphaconf/internal/configuration.py b/alphaconf/internal/configuration.py index 9a8c6f5..c276f4b 100644 --- a/alphaconf/internal/configuration.py +++ b/alphaconf/internal/configuration.py @@ -202,7 +202,7 @@ def _find_name(parts: list[str], conf: DictConfig) -> str: sub_conf = conf.get(name) if next_offset == len(parts): return name - elif isinstance(sub_conf, DictConfig): + if isinstance(sub_conf, DictConfig): return name + "." + Configuration._find_name(parts[next_offset:], sub_conf) return ".".join([name, *parts[next_offset:]]) return ".".join(parts) diff --git a/alphaconf/internal/load_file.py b/alphaconf/internal/load_file.py index e34748f..502e780 100644 --- a/alphaconf/internal/load_file.py +++ b/alphaconf/internal/load_file.py @@ -35,7 +35,7 @@ def read_configuration_file(path: str) -> DictConfig: """ if path.endswith('.toml') and toml: config = toml.load(path, decoder=TomlDecoderPrimitive()) - return OmegaConf.create(config) + return OmegaConf.create(dict(config)) conf = OmegaConf.load(path) if not isinstance(conf, DictConfig): conf = OmegaConf.create({'config': conf}) diff --git a/pre-commit b/pre-commit index 8f39d4f..bdb5576 100755 --- a/pre-commit +++ b/pre-commit @@ -5,28 +5,39 @@ cd "$(dirname "$0")" [ -d .git ] || cd ../.. [ -d .git ] -pre_commit() { - ruff check . - black --check . +check() { + echo " ruff:" + ruff check + ruff format --check +} + +lint() { + check + echo " mypy:" mypy . } -fix_all() { - black . +fix() { ruff check --fix-only . } + format() { - black . + ruff format } # Commands case "${1:-run}" in - run|lint) - pre_commit - echo "All good to commit" + run|check) + check + echo " all good to commit." + ;; + lint) + lint ;; fix) - fix_all + echo "Fix all..." + fix + format ;; format) format @@ -42,5 +53,5 @@ case "${1:-run}" in ;; *) echo "Invalid argument: $*" - echo "Supported options: lint, format, install, uninstall" + echo "Supported options: lint, fix, format, install, uninstall" esac diff --git a/pyproject.toml b/pyproject.toml index c2dc939..475c47a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,11 +1,17 @@ [build-system] requires = [ - "setuptools>=61", - "setuptools-scm>=6", + "setuptools>=70", + "setuptools-scm>=8", "wheel", ] build-backend = "setuptools.build_meta" +[tools.setuptools] +packages = ["alphaconf"] + +[tool.setuptools_scm] +local_scheme = "no-local-version" + [project] name = "alphaconf" dynamic = ["version"] @@ -31,6 +37,22 @@ dotenv = ["python-dotenv"] invoke = ["invoke"] pydantic = ["pydantic>=2"] toml = ["toml"] +pinned = [ + "invoke==2.2.0", + "omegaconf==2.3.0", + "pydantic==2.7.4", + "pyyaml~=6.0", + "toml==0.10.2", +] +dev = [ + "mypy~=1.11", + "ruff==0.5.6", + "types-pyyaml~=6.0", + "types-toml>=0.10.8", +] +test = [ + "pytest==8.3.2", +] [project.urls] Homepage = "https://github.com/kmagusiak/alphaconf" @@ -46,38 +68,49 @@ skip-string-normalization = 1 [tool.mypy] ignore_missing_imports = true -[tools.setuptools] -packages = ["alphaconf"] - -[tool.setuptools_scm] -local_scheme = "no-local-version" - [tool.ruff] line-length = 100 +target-version = "py39" + +[tool.ruff.format] +quote-style="preserve" [tool.ruff.lint] # https://beta.ruff.rs/docs/rules/ select = [ + "C4", # flake8 comprehensions #"C9", # mccabe - #"D", # documentation - "E", - "F", - "W", + "COM", # flake8 commas + #"D", # pydocstyle, pydoclint + "E", # pycodestyle + "EXE", # flake8-executable + "F", # pyflakes "I", # isort + "LOG", # flake8 logging "N", # naming + "PLE", # pylint errors + "RET", # flake8 return + "RUF", # ruff specific + "SIM", # flake8 simplify + "TID", # flake8 tidy imports "UP", # pyupdate - "C4", # comprehensions - "EXE", - "SIM", - "RUF", + "W", # pycodestyle + # specific rules + "FIX003" # comments with XXX should become TODO or FIXME ] ignore = [ + "COM812", # trailing commas (because we use the ruff formatter) "D102", # mission doc in public method, function - "D205", # blank line required betweeen summary and description + "D205", # blank line required between summary and description "D400", # first line should end with a period "E731", # don't assign lambda "SIM108", # simplify ITE by operator + "SIM300", # yoda condition + "UP038", # isinstance must use union operator on types ] [tool.ruff.lint.mccabe] max-complexity = 10 + +[tool.ruff.lint.per-file-ignores] +"__init__.py" = ["F401"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index f2a92ef..0000000 --- a/requirements.txt +++ /dev/null @@ -1,16 +0,0 @@ -# Build tools -setuptools -setuptools_scm -black -mypy -ruff -pytest -types-toml - -# Libraries -omegaconf>=2 -colorama -pydantic>=2 -python-dotenv -invoke -toml