Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing ruff as linter and formatter (basic setup) #2921

Merged
merged 10 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions .flake8

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/misc_0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ jobs:

- name: Run tests
run: tox -e shellcheck

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 @@ -67,20 +67,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
2 changes: 1 addition & 1 deletion _template/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies = [
[project.entry-points.opentelemetry_instrumentor]
# REPLACE ME: the entrypoint for the instrumentor e.g
# sqlalchemy = "opentelemetry.instrumentation.sqlalchemy:SQLAlchemyInstrumentor"
<REPLACE ME> = "opentelemetry.instrumentation.<REPLACE>"
REPLACE_ME = "opentelemetry.instrumentation.<REPLACE>"

[project.urls]
# url of the instrumentation e.g
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.0.2
flake8==6.1.0
isort==5.12.0
black==24.3.0
httpretty==1.1.4
mypy==0.931
sphinx==7.1.2
Expand All @@ -19,3 +16,4 @@ ruamel.yaml==0.17.21
flaky==3.7.0
pre-commit==3.7.0; python_version >= '3.9'
pre-commit==3.5.0; python_version < '3.9'
ruff==0.6.9
3 changes: 1 addition & 2 deletions gen-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
astor==0.8.1
jinja2==3.1.4
markupsafe==2.0.1
isort
black
ruff==0.6.9
requests
tomli
tomli_w
Expand Down
48 changes: 38 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
[tool.black]
[tool.ruff]
# https://docs.astral.sh/ruff/configuration/
target-version = "py38"
line-length = 79
exclude = '''
(
\.git
| \.tox
| venv
| build
| dist
)
'''
extend-exclude = [
"_template",
"*_pb2*.py*",
]
output-format = "concise"

[tool.ruff.lint]
# https://docs.astral.sh/ruff/linter/#rule-selection
# pylint: https://github.com/astral-sh/ruff/issues/970
select = [
"I", # isort
"F", # pyflakes
"E", # pycodestyle errors
"W", # pycodestyle warnings
"PLC", # pylint convention
"PLE", # pylint error
"Q", # flake8-quotes
"A", # flake8-builtins
]
ignore = [
"E501", # line-too-long
]

[tool.ruff.lint.per-file-ignores]
"docs/**/*.*" = ["A001"]

[tool.ruff.lint.isort]
known-third-party = [
"psutil",
"pytest",
"redis",
"redis_opentracing",
"opencensus",
]
known-first-party = ["opentelemetry", "opentelemetry_example_app"]
57 changes: 30 additions & 27 deletions scripts/eachdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ def setup_instparser(instparser):
)

fmtparser = subparsers.add_parser(
"format", help="Formats all source code with black and isort.",
"format",
help="Formats all source code with black and isort.",
)
fmtparser.set_defaults(func=format_args)
fmtparser.add_argument(
Expand All @@ -248,7 +249,8 @@ def setup_instparser(instparser):
)

versionparser = subparsers.add_parser(
"version", help="Get the version for a release",
"version",
help="Get the version for a release",
)
versionparser.set_defaults(func=version_args)
versionparser.add_argument(
Expand All @@ -268,7 +270,8 @@ def setup_instparser(instparser):
)

findparser = subparsers.add_parser(
"find-package", help="Find package path.",
"find-package",
help="Find package path.",
)
findparser.set_defaults(func=find_package_args)
findparser.add_argument(
Expand All @@ -294,10 +297,7 @@ def find_targets_unordered(rootpath):
continue
if subdir.name.startswith(".") or subdir.name.startswith("venv"):
continue
if any(
(subdir / marker).exists()
for marker in ("pyproject.toml",)
):
if any((subdir / marker).exists() for marker in ("pyproject.toml",)):
yield subdir
else:
yield from find_targets_unordered(subdir)
Expand Down Expand Up @@ -520,23 +520,16 @@ def parse_subargs(parentargs, args):


def lint_args(args):
rootdir = str(find_projectroot())

runsubprocess(
args.dry_run,
("black", "--config", f"{rootdir}/pyproject.toml", ".")
+ (("--diff", "--check") if args.check_only else ()),
cwd=rootdir,
("ruff", "check", ".") + (() if args.check_only else ("--fix",)),
check=True,
)
runsubprocess(
args.dry_run,
("isort", "--settings-path", f"{rootdir}/.isort.cfg", ".")
+ (("--diff", "--check-only") if args.check_only else ()),
cwd=rootdir,
("ruff", "format") + (("--check",) if args.check_only else ()),
check=True,
)
runsubprocess(args.dry_run, ("flake8", "--config", f"{rootdir}/.flake8", rootdir), check=True)
execute_args(
parse_subargs(
args, ("exec", "pylint {}", "--all", "--mode", "lintroots")
Expand All @@ -545,7 +538,11 @@ def lint_args(args):
execute_args(
parse_subargs(
args,
("exec", "python scripts/check_for_valid_readme.py {}", "--all",),
(
"exec",
"python scripts/check_for_valid_readme.py {}",
"--all",
),
)
)

Expand Down Expand Up @@ -585,9 +582,7 @@ def update_changelogs(version):

## [{version}](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v{version}) - {today}

""".format(
version=version, today=today
)
""".format(version=version, today=today)
errors = False
try:
update_changelog("./CHANGELOG.md", version, new_entry)
Expand Down Expand Up @@ -634,7 +629,10 @@ def update_version_files(targets, version, packages):
print("updating version.py files")
targets = filter_packages(targets, packages)
update_files(
targets, "version.py", "__version__ .*", f'__version__ = "{version}"',
targets,
"version.py",
"__version__ .*",
f'__version__ = "{version}"',
)


Expand All @@ -652,7 +650,7 @@ def update_dependencies(targets, version, packages):
update_files(
targets,
"pyproject.toml",
fr"({package_name}.*)==(.*)",
rf"({package_name}.*)==(.*)",
r"\1== " + version + '",',
)

Expand Down Expand Up @@ -690,14 +688,18 @@ def release_args(args):
updated_versions = []

excluded = cfg["exclude_release"]["packages"].split()
targets = [target for target in targets if basename(target) not in excluded]
targets = [
target for target in targets if basename(target) not in excluded
]
for group in versions.split(","):
mcfg = cfg[group]
version = mcfg["version"]
updated_versions.append(version)
packages = None
if "packages" in mcfg:
packages = [pkg for pkg in mcfg["packages"].split() if pkg not in excluded]
packages = [
pkg for pkg in mcfg["packages"].split() if pkg not in excluded
]
print(f"update {group} packages to {version}")
update_dependencies(targets, version, packages)
update_version_files(targets, version, packages)
Expand All @@ -724,16 +726,15 @@ def format_args(args):
format_dir = str(find_projectroot())
if args.path:
format_dir = os.path.join(format_dir, args.path)
root_dir = str(find_projectroot())
runsubprocess(
args.dry_run,
("black", "--config", f"{root_dir}/pyproject.toml", "."),
("ruff", "check", "--fix"),
cwd=format_dir,
check=True,
)
runsubprocess(
args.dry_run,
("isort", "--settings-path", f"{root_dir}/.isort.cfg", "--profile", "black", "."),
("ruff", "format"),
xrmx marked this conversation as resolved.
Show resolved Hide resolved
cwd=format_dir,
check=True,
)
Expand Down Expand Up @@ -763,6 +764,7 @@ def version_args(args):
print("package not found")
sys.exit(1)


def find_package_args(args):
root = find_projectroot()
for package in find_targets_unordered(root):
Expand All @@ -774,6 +776,7 @@ def find_package_args(args):
print("package not found")
sys.exit(1)


def main():
args = parse_args()
args.func(args)
Expand Down
Loading
Loading