From 5017c97c3911ed7bc76ddc0672563231cc28ea35 Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Sat, 12 Oct 2024 21:53:02 -0300 Subject: [PATCH] trying ruff Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --- .github/workflows/misc_0.yml | 18 +++++++++++++++ .pre-commit-config.yaml | 21 +++++++---------- CONTRIBUTING.md | 13 ++++------- dev-requirements.txt | 1 + pyproject.toml | 44 ++++++++++++++++++++++-------------- tox.ini | 20 ++++++++-------- 6 files changed, 67 insertions(+), 50 deletions(-) diff --git a/.github/workflows/misc_0.yml b/.github/workflows/misc_0.yml index fbb06fd4743..00fb99e9605 100644 --- a/.github/workflows/misc_0.yml +++ b/.github/workflows/misc_0.yml @@ -228,3 +228,21 @@ jobs: - name: Check workflows are up to date run: git diff --exit-code || (echo 'Generated workflows are out of date, run "tox -e generate-workflows" and commit the changes in this PR.' && exit 1) + + ruff: + name: ruff + runs-on: ubuntu-latest + steps: + - name: Checkout repo @ SHA - ${{ github.sha }} + uses: actions/checkout@v4 + + - name: Set up Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install tox + run: pip install tox + + - name: Run tests + run: tox -e ruff diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b01b7ce4d7b..725bda5f886 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,14 +1,9 @@ repos: - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.3.0 - hooks: - - id: black - language_version: python3.12 - - repo: https://github.com/pycqa/isort - rev: 5.12.0 - hooks: - - id: isort - - repo: https://github.com/pycqa/flake8 - rev: '6.1.0' - hooks: - - id: flake8 +- repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.6.9 + hooks: + # Run the linter. + - id: ruff + # Run the formatter. + - id: ruff-format diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 81376baeba5..a2defe8b659 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -58,20 +58,15 @@ You can run `tox` with the following arguments: Python version - `tox -e spellcheck` to run a spellcheck on all the code - `tox -e lint-some-package` to run lint checks on `some-package` +- `tox -e ruff` to run ruff linter and formatter checks against the entire codebase -`black` and `isort` are executed when `tox -e lint` is run. The reported errors can be tedious to fix manually. -An easier way to do so is: - -1. Run `.tox/lint/bin/black .` -2. Run `.tox/lint/bin/isort .` - -Or you can call formatting and linting in one command by [pre-commit](https://pre-commit.com/): +`ruff check` and `ruff format` are executed when `tox -e ruff` is run. We strongly recommend you to configure [pre-commit](https://pre-commit.com/) locally to run `ruff` automatically before each commit by installing it as git hooks. You just need to [install pre-commit](https://pre-commit.com/#install) in your environment: ```console -$ pre-commit +$ pip install pre-commit -c dev-requirements.txt ``` -You can also configure it to run lint tools automatically before committing with: +and run this command inside the git repository: ```console $ pre-commit install diff --git a/dev-requirements.txt b/dev-requirements.txt index 849dec4e66c..11ff43fe05d 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -20,3 +20,4 @@ psutil==5.9.6 GitPython==3.1.41 pre-commit==3.7.0; python_version >= '3.9' pre-commit==3.5.0; python_version < '3.9' +ruff==0.6.9 diff --git a/pyproject.toml b/pyproject.toml index 873076c802c..2a4125b0823 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,20 +1,3 @@ -[tool.black] -line-length = 79 -exclude = ''' -( - /( # generated files - .tox| - venv| - venv.*| - .venv.*| - target.*| - .*/build/lib/.*| - exporter/opentelemetry-exporter-zipkin-proto-http/src/opentelemetry/exporter/zipkin/proto/http/v2/gen| - opentelemetry-proto/src/opentelemetry/proto/.*/.*| - scripts - )/ -) -''' [tool.pytest.ini_options] addopts = "-rs -v" log_cli = true @@ -27,3 +10,30 @@ include = [ "opentelemetry-sdk/src", "opentelemetry-semantic-conventions/src", ] + +[tool.ruff] +# https://docs.astral.sh/ruff/configuration/ +target-version = "py38" +line-length = 79 +extend-exclude = [ + "*_pb2*.py*", +] +fix = true +output-format = "concise" + +[tool.ruff.lint] +# https://docs.astral.sh/ruff/linter/#rule-selection +# pylint: https://github.com/astral-sh/ruff/issues/970 +select = ["E4", "E7", "E9", "F", "W", "Q", "I", "PLE"] +fixable = ["I", "F", "Q", "W"] +ignore = ["F401", "E501"] # TODO: fix all of them later + +[tool.ruff.lint.isort] +known-third-party = [ + "psutil", + "pytest", + "redis", + "redis_opentracing", + "opencensus", +] +known-first-party = ["opentelemetry", "opentelemetry_example_app"] diff --git a/tox.ini b/tox.ini index 973158af1f1..1722837688d 100644 --- a/tox.ini +++ b/tox.ini @@ -111,6 +111,7 @@ envlist = public-symbols-check shellcheck generate-workflows + ruff [testenv] deps = @@ -329,17 +330,6 @@ commands = ; implicit Any due to unfollowed import would result). mypyinstalled: mypy --install-types --non-interactive --namespace-packages opentelemetry-api/tests/mypysmoke.py --strict -[testenv:lint] -basepython: python3 -recreate = True -deps = - -r dev-requirements.txt - -commands = - black --config {toxinidir}/pyproject.toml {toxinidir} --diff --check - isort --settings-path {toxinidir}/.isort.cfg {toxinidir} --diff --check-only - flake8 --config {toxinidir}/.flake8 {toxinidir} - [testenv:spellcheck] basepython: python3 recreate = True @@ -461,3 +451,11 @@ commands_pre = commands = pyright --version pyright + +[testenv:ruff] +basepython: python3 +deps = + -c {toxinidir}/dev-requirements.txt + pre-commit +commands = + pre-commit run -a --color=always