Skip to content

Commit affecbd

Browse files
authored
Introducing ruff as linter and formatter (basic setup) (open-telemetry#4223)
1 parent e32911b commit affecbd

File tree

144 files changed

+384
-741
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+384
-741
lines changed

.flake8

-35
This file was deleted.

.github/workflows/generate_workflows.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from pathlib import Path
22

33
from generate_workflows_lib import (
4-
generate_test_workflow,
5-
generate_lint_workflow,
64
generate_contrib_workflow,
7-
generate_misc_workflow
5+
generate_lint_workflow,
6+
generate_misc_workflow,
7+
generate_test_workflow,
88
)
99

1010
tox_ini_path = Path(__file__).parent.parent.parent.joinpath("tox.ini")

.github/workflows/misc_0.yml

+18
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,21 @@ jobs:
233233

234234
- name: Check workflows are up to date
235235
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)
236+
237+
ruff:
238+
name: ruff
239+
runs-on: ubuntu-latest
240+
steps:
241+
- name: Checkout repo @ SHA - ${{ github.sha }}
242+
uses: actions/checkout@v4
243+
244+
- name: Set up Python 3.10
245+
uses: actions/setup-python@v5
246+
with:
247+
python-version: "3.10"
248+
249+
- name: Install tox
250+
run: pip install tox
251+
252+
- name: Run tests
253+
run: tox -e ruff

.isort.cfg

-19
This file was deleted.

.pre-commit-config.yaml

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
repos:
2-
- repo: https://github.com/psf/black-pre-commit-mirror
3-
rev: 24.3.0
4-
hooks:
5-
- id: black
6-
language_version: python3.12
7-
- repo: https://github.com/pycqa/isort
8-
rev: 5.12.0
9-
hooks:
10-
- id: isort
11-
- repo: https://github.com/pycqa/flake8
12-
rev: '6.1.0'
13-
hooks:
14-
- id: flake8
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.6.9
5+
hooks:
6+
# Run the linter.
7+
- id: ruff
8+
args: ["--fix", "--show-fixes"]
9+
# Run the formatter.
10+
- id: ruff-format

CONTRIBUTING.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,15 @@ You can run `tox` with the following arguments:
5858
Python version
5959
- `tox -e spellcheck` to run a spellcheck on all the code
6060
- `tox -e lint-some-package` to run lint checks on `some-package`
61+
- `tox -e ruff` to run ruff linter and formatter checks against the entire codebase
6162

62-
`black` and `isort` are executed when `tox -e lint` is run. The reported errors can be tedious to fix manually.
63-
An easier way to do so is:
64-
65-
1. Run `.tox/lint/bin/black .`
66-
2. Run `.tox/lint/bin/isort .`
67-
68-
Or you can call formatting and linting in one command by [pre-commit](https://pre-commit.com/):
63+
`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:
6964

7065
```console
71-
$ pre-commit
66+
$ pip install pre-commit -c dev-requirements.txt
7267
```
7368

74-
You can also configure it to run lint tools automatically before committing with:
69+
and run this command inside the git repository:
7570

7671
```console
7772
$ pre-commit install

dev-requirements.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
pylint==3.2.1
2-
flake8==6.1.0
3-
isort==5.12.0
4-
black==24.3.0
52
httpretty==1.1.4
63
mypy==1.9.0
74
sphinx==7.1.2
@@ -20,3 +17,4 @@ psutil==5.9.6
2017
GitPython==3.1.41
2118
pre-commit==3.7.0; python_version >= '3.9'
2219
pre-commit==3.5.0; python_version < '3.9'
20+
ruff==0.6.9

docs/conf.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@
204204
.. |SCM_WEB| replace:: {s}
205205
.. |SCM_RAW_WEB| replace:: {sr}
206206
.. |SCM_BRANCH| replace:: {b}
207-
""".format(
208-
s=scm_web, sr=scm_raw_web, b=branch
209-
)
207+
""".format(s=scm_web, sr=scm_raw_web, b=branch)
210208

211209
# used to have links to repo files
212210
extlinks = {

docs/examples/auto-instrumentation/client.py

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
assert len(argv) == 2
3636

3737
with tracer.start_as_current_span("client"):
38-
3938
with tracer.start_as_current_span("client-server"):
4039
headers = {}
4140
inject(headers)

docs/examples/django/client.py

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434

3535
with tracer.start_as_current_span("client"):
36-
3736
with tracer.start_as_current_span("client-server"):
3837
headers = {}
3938
inject(headers)

docs/examples/django/instrumentation_example/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
1. Import the include() function: from django.urls import include, path
2727
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
2828
"""
29+
2930
from django.contrib import admin
3031
from django.urls import include, path
3132

docs/examples/django/manage.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
"""Django"s command-line utility for administrative tasks."""
17+
1718
import os
1819
import sys
1920

docs/examples/error_handler/error_handler_0/src/error_handler_0/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@
2121

2222
class ErrorHandler0(ErrorHandler, ZeroDivisionError):
2323
def _handle(self, error: Exception, *args, **kwargs):
24-
2524
logger.exception("ErrorHandler0 handling a ZeroDivisionError")

docs/examples/error_handler/error_handler_1/src/error_handler_1/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
# pylint: disable=too-many-ancestors
2323
class ErrorHandler1(ErrorHandler, IndexError, KeyError):
2424
def _handle(self, error: Exception, *args, **kwargs):
25-
2625
if isinstance(error, IndexError):
2726
logger.exception("ErrorHandler1 handling an IndexError")
2827

docs/examples/opentracing/rediscache.py

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def __call__(self, func):
3030
@wraps(func)
3131
def inner(*args, **kwargs):
3232
with self.tracer.start_active_span("Caching decorator") as scope1:
33-
3433
# Pickle the call args to get a canonical key. Don't do this in
3534
# prod!
3635
key = pickle.dumps((func.__qualname__, args, kwargs))

exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def translate_to_collector(spans: Sequence[ReadableSpan]):
144144

145145
if span.events:
146146
for event in span.events:
147-
148147
collector_annotation = trace_pb2.Span.TimeEvent.Annotation(
149148
description=trace_pb2.TruncatableString(value=event.name)
150149
)

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,31 @@
1818
from itertools import count
1919
from typing import (
2020
Any,
21-
Mapping,
22-
Optional,
23-
List,
2421
Callable,
25-
TypeVar,
2622
Dict,
2723
Iterator,
24+
List,
25+
Mapping,
26+
Optional,
27+
TypeVar,
2828
)
2929

30-
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
30+
from opentelemetry.proto.common.v1.common_pb2 import AnyValue as PB2AnyValue
3131
from opentelemetry.proto.common.v1.common_pb2 import (
32-
InstrumentationScope as PB2InstrumentationScope,
32+
ArrayValue as PB2ArrayValue,
3333
)
34-
from opentelemetry.proto.resource.v1.resource_pb2 import (
35-
Resource as PB2Resource,
34+
from opentelemetry.proto.common.v1.common_pb2 import (
35+
InstrumentationScope as PB2InstrumentationScope,
3636
)
37-
from opentelemetry.proto.common.v1.common_pb2 import AnyValue as PB2AnyValue
3837
from opentelemetry.proto.common.v1.common_pb2 import KeyValue as PB2KeyValue
3938
from opentelemetry.proto.common.v1.common_pb2 import (
4039
KeyValueList as PB2KeyValueList,
4140
)
42-
from opentelemetry.proto.common.v1.common_pb2 import (
43-
ArrayValue as PB2ArrayValue,
41+
from opentelemetry.proto.resource.v1.resource_pb2 import (
42+
Resource as PB2Resource,
4443
)
4544
from opentelemetry.sdk.trace import Resource
45+
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
4646
from opentelemetry.util.types import Attributes
4747

4848
_logger = logging.getLogger(__name__)

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
from collections import defaultdict
15-
from typing import Sequence, List
15+
from typing import List, Sequence
1616

1717
from opentelemetry.exporter.otlp.proto.common._internal import (
18+
_encode_attributes,
1819
_encode_instrumentation_scope,
1920
_encode_resource,
2021
_encode_span_id,
2122
_encode_trace_id,
2223
_encode_value,
23-
_encode_attributes,
2424
)
2525
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import (
2626
ExportLogsServiceRequest,
2727
)
28+
from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord
2829
from opentelemetry.proto.logs.v1.logs_pb2 import (
29-
ScopeLogs,
3030
ResourceLogs,
31+
ScopeLogs,
3132
)
32-
from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord
33-
3433
from opentelemetry.sdk._logs import LogData
3534

3635

0 commit comments

Comments
 (0)