Skip to content

Commit

Permalink
Introducing ruff as linter and formatter (basic setup) (#4223)
Browse files Browse the repository at this point in the history
  • Loading branch information
emdneto authored Oct 15, 2024
1 parent e32911b commit affecbd
Show file tree
Hide file tree
Showing 144 changed files with 384 additions and 741 deletions.
35 changes: 0 additions & 35 deletions .flake8

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/generate_workflows.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pathlib import Path

from generate_workflows_lib import (
generate_test_workflow,
generate_lint_workflow,
generate_contrib_workflow,
generate_misc_workflow
generate_lint_workflow,
generate_misc_workflow,
generate_test_workflow,
)

tox_ini_path = Path(__file__).parent.parent.parent.joinpath("tox.ini")
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/misc_0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,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
19 changes: 0 additions & 19 deletions .isort.cfg

This file was deleted.

22 changes: 9 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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
args: ["--fix", "--show-fixes"]
# Run the formatter.
- id: ruff-format
13 changes: 4 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
pylint==3.2.1
flake8==6.1.0
isort==5.12.0
black==24.3.0
httpretty==1.1.4
mypy==1.9.0
sphinx==7.1.2
Expand All @@ -20,3 +17,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
4 changes: 1 addition & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@
.. |SCM_WEB| replace:: {s}
.. |SCM_RAW_WEB| replace:: {sr}
.. |SCM_BRANCH| replace:: {b}
""".format(
s=scm_web, sr=scm_raw_web, b=branch
)
""".format(s=scm_web, sr=scm_raw_web, b=branch)

# used to have links to repo files
extlinks = {
Expand Down
1 change: 0 additions & 1 deletion docs/examples/auto-instrumentation/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
assert len(argv) == 2

with tracer.start_as_current_span("client"):

with tracer.start_as_current_span("client-server"):
headers = {}
inject(headers)
Expand Down
1 change: 0 additions & 1 deletion docs/examples/django/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@


with tracer.start_as_current_span("client"):

with tracer.start_as_current_span("client-server"):
headers = {}
inject(headers)
Expand Down
1 change: 1 addition & 0 deletions docs/examples/django/instrumentation_example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
"""

from django.contrib import admin
from django.urls import include, path

Expand Down
1 change: 1 addition & 0 deletions docs/examples/django/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

"""Django"s command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@

class ErrorHandler0(ErrorHandler, ZeroDivisionError):
def _handle(self, error: Exception, *args, **kwargs):

logger.exception("ErrorHandler0 handling a ZeroDivisionError")
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
# pylint: disable=too-many-ancestors
class ErrorHandler1(ErrorHandler, IndexError, KeyError):
def _handle(self, error: Exception, *args, **kwargs):

if isinstance(error, IndexError):
logger.exception("ErrorHandler1 handling an IndexError")

Expand Down
1 change: 0 additions & 1 deletion docs/examples/opentracing/rediscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __call__(self, func):
@wraps(func)
def inner(*args, **kwargs):
with self.tracer.start_active_span("Caching decorator") as scope1:

# Pickle the call args to get a canonical key. Don't do this in
# prod!
key = pickle.dumps((func.__qualname__, args, kwargs))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def translate_to_collector(spans: Sequence[ReadableSpan]):

if span.events:
for event in span.events:

collector_annotation = trace_pb2.Span.TimeEvent.Annotation(
description=trace_pb2.TruncatableString(value=event.name)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@
from itertools import count
from typing import (
Any,
Mapping,
Optional,
List,
Callable,
TypeVar,
Dict,
Iterator,
List,
Mapping,
Optional,
TypeVar,
)

from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.proto.common.v1.common_pb2 import AnyValue as PB2AnyValue
from opentelemetry.proto.common.v1.common_pb2 import (
InstrumentationScope as PB2InstrumentationScope,
ArrayValue as PB2ArrayValue,
)
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as PB2Resource,
from opentelemetry.proto.common.v1.common_pb2 import (
InstrumentationScope as PB2InstrumentationScope,
)
from opentelemetry.proto.common.v1.common_pb2 import AnyValue as PB2AnyValue
from opentelemetry.proto.common.v1.common_pb2 import KeyValue as PB2KeyValue
from opentelemetry.proto.common.v1.common_pb2 import (
KeyValueList as PB2KeyValueList,
)
from opentelemetry.proto.common.v1.common_pb2 import (
ArrayValue as PB2ArrayValue,
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as PB2Resource,
)
from opentelemetry.sdk.trace import Resource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.util.types import Attributes

_logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import defaultdict
from typing import Sequence, List
from typing import List, Sequence

from opentelemetry.exporter.otlp.proto.common._internal import (
_encode_attributes,
_encode_instrumentation_scope,
_encode_resource,
_encode_span_id,
_encode_trace_id,
_encode_value,
_encode_attributes,
)
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import (
ExportLogsServiceRequest,
)
from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord
from opentelemetry.proto.logs.v1.logs_pb2 import (
ScopeLogs,
ResourceLogs,
ScopeLogs,
)
from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord

from opentelemetry.sdk._logs import LogData


Expand Down
Loading

0 comments on commit affecbd

Please sign in to comment.