From b56732b96204cf0f70d897f1fb4fa5ab6eee1151 Mon Sep 17 00:00:00 2001 From: Krzysztof Magusiak Date: Sun, 4 Aug 2024 22:15:02 +0200 Subject: [PATCH] Ruff and pre-commit update --- .github/workflows/lint-test.yaml | 16 +++---- .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 +- pre-commit | 33 +++++++++----- pyproject.toml | 62 +++++++++++++++++++-------- 8 files changed, 81 insertions(+), 49 deletions(-) diff --git a/.github/workflows/lint-test.yaml b/.github/workflows/lint-test.yaml index 233d5e3..2c5a2a0 100644 --- a/.github/workflows/lint-test.yaml +++ b/.github/workflows/lint-test.yaml @@ -10,19 +10,18 @@ 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" - 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 + ./pre-commit lint test: runs-on: ubuntu-latest strategy: @@ -30,15 +29,14 @@ jobs: # lowest, common (defaut ubuntu LTS), newest python-version: ["3.9", "3.10", "3.11"] 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/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..11f6690 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,17 @@ dotenv = ["python-dotenv"] invoke = ["invoke"] pydantic = ["pydantic>=2"] toml = ["toml"] +pinned = [ + "toml==0.10.2", +] +dev = [ + "mypy~=1.11", + "ruff==0.5.6", + "types-toml==0.10.2", +] +test = [ + "pytest==8.3.2", +] [project.urls] Homepage = "https://github.com/kmagusiak/alphaconf" @@ -46,38 +63,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