From b2899e305715749bb4ec345219bd7c541d7a8996 Mon Sep 17 00:00:00 2001 From: Cordell Blakkan Date: Wed, 20 Mar 2024 13:58:13 -0400 Subject: [PATCH] Add ruff gh action --- .github/workflows/run-tests.yml | 1 + .pre-commit-config.yaml | 7 +++++++ Makefile | 9 ++++++++- README.md | 5 +++++ ruff.toml | 3 +++ yamale/schema/schema.py | 4 ++-- yamale/util.py | 25 +++++-------------------- yamale/yamale_testcase.py | 2 +- 8 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index c18b85c..b6b45f8 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -18,6 +18,7 @@ jobs: steps: - uses: actions/checkout@v2 + - uses: chartboost/ruff-action@v1 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..8a1b580 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,7 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.3 + hooks: + - id: ruff + args: ["--fix"] + - id: ruff-format diff --git a/Makefile b/Makefile index 49b2c9a..9daf6c0 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,10 @@ VERSION ?= $(shell cat yamale/VERSION) all: test +lint: + @ruff check --fix . + @ruff format . + test: @tox @@ -15,4 +19,7 @@ release: @git push --follow-tags --all @git push --tags -.PHONY: test tag coverage clean release +install-hooks: + @pre-commit install -f --install-hooks -t pre-commit + +.PHONY: test lint tag coverage clean release install-hooks diff --git a/README.md b/README.md index 4af77ce..afa957b 100644 --- a/README.md +++ b/README.md @@ -602,6 +602,11 @@ class TestYaml(YamaleTestCase): Developers ---------- +### Linting + Formatting +Yamale is formatted with [ruff](https://github.com/astral-sh/ruff). There is a github action enforcing +ruff formatting and linting rules. You can run this locally via `make lint` or by installing +the pre-commit hooks via `make install-hooks` + ### Testing Yamale uses [Tox](https://tox.readthedocs.org/en/latest/) to run its tests against multiple Python versions. To run tests, first checkout Yamale, install Tox, then run `make test` in Yamale's root diff --git a/ruff.toml b/ruff.toml index 46e6544..3e35689 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,2 +1,5 @@ line-length = 121 indent-width = 4 + +[lint] +ignore = ["F401", "F403"] diff --git a/yamale/schema/schema.py b/yamale/schema/schema.py index fe0972c..2b8af90 100644 --- a/yamale/schema/schema.py +++ b/yamale/schema/schema.py @@ -172,8 +172,8 @@ def _validate_any(self, validator, data, path, strict): def _validate_subset(self, validator, data, path, strict): def _internal_validate(internal_data): sub_errors = [] - for val in validator.validators: - err = self._validate(val, internal_data, path, strict) + for v in validator.validators: + err = self._validate(v, internal_data, path, strict) if not err: break sub_errors += err diff --git a/yamale/util.py b/yamale/util.py index 45c4ab3..eb1ab4f 100644 --- a/yamale/util.py +++ b/yamale/util.py @@ -1,27 +1,12 @@ -# ABCs for containers were moved to their own module -try: - from collections.abc import Mapping, Set, Sequence -except ImportError: - from collections import Mapping, Set, Sequence +from collections.abc import Mapping, Sequence -# Python 3 has no basestring, lets test it. -try: - basestring # attempt to evaluate basestring +def isstr(s): + return isinstance(s, str) - def isstr(s): - return isinstance(s, basestring) - def to_unicode(s): - return unicode(s) - -except NameError: - - def isstr(s): - return isinstance(s, str) - - def to_unicode(s): - return s +def to_unicode(s): + return s def is_list(obj): diff --git a/yamale/yamale_testcase.py b/yamale/yamale_testcase.py index 21ba6d7..49caea0 100644 --- a/yamale/yamale_testcase.py +++ b/yamale/yamale_testcase.py @@ -26,7 +26,7 @@ def validate(self, validators=None): if schema is None: return - if type(yaml) != list: + if not isinstance(yaml, list): yaml = [yaml] if base_dir is not None: