Skip to content

Commit f41934a

Browse files
Use ruff instead of black+isort (#644)
* Use ruff * Update .gitignore * Format code * Use Ruff on generated code * Update pre-commit hook * Wrong commit * Remove wrong imports * Update hook * Format code * Target Python 3.8 * Reformat * Pin ruff version
1 parent 37fa3ab commit f41934a

File tree

12 files changed

+539
-559
lines changed

12 files changed

+539
-559
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ output
1818
.asv
1919
venv
2020
.devcontainer
21+
.ruff_cache

.pre-commit-config.yaml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ ci:
22
autofix_prs: false
33

44
repos:
5-
- repo: https://github.com/pycqa/isort
6-
rev: 5.11.5
5+
- repo: https://github.com/astral-sh/ruff-pre-commit
6+
rev: v0.7.4
77
hooks:
8-
- id: isort
9-
10-
- repo: https://github.com/psf/black
11-
rev: 23.1.0
12-
hooks:
13-
- id: black
14-
args: ["--target-version", "py310"]
8+
- id: ruff-format
9+
args: ["--diff", "src", "tests"]
10+
- id: ruff
11+
args: ["--select", "I", "src", "tests"]
1512

1613
- repo: https://github.com/PyCQA/doc8
1714
rev: 0.10.1

poetry.lock

Lines changed: 462 additions & 479 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ packages = [
1313

1414
[tool.poetry.dependencies]
1515
python = "^3.8"
16-
black = { version = ">=23.1.0", optional = true }
16+
# The Ruff version is pinned. To update it, also update it in .pre-commit-config.yaml
17+
ruff = { version = "~0.7.4", optional = true }
1718
grpclib = "^0.4.1"
1819
jinja2 = { version = ">=3.0.3", optional = true }
1920
python-dateutil = "^2.8"
20-
isort = { version = "^5.11.5", optional = true }
2121
typing-extensions = "^4.7.1"
2222
betterproto-rust-codec = { version = "0.1.1", optional = true }
2323

@@ -47,9 +47,16 @@ tomlkit = ">=0.7.0"
4747
protoc-gen-python_betterproto = "betterproto.plugin:main"
4848

4949
[tool.poetry.extras]
50-
compiler = ["black", "isort", "jinja2"]
50+
compiler = ["ruff", "jinja2"]
5151
rust-codec = ["betterproto-rust-codec"]
5252

53+
[tool.ruff]
54+
extend-exclude = ["tests/output_*"]
55+
target-version = "py38"
56+
57+
[tool.ruff.lint.isort]
58+
combine-as-imports = true
59+
lines-after-imports = 2
5360

5461
# Dev workflow tasks
5562

@@ -65,13 +72,29 @@ help = "Run tests"
6572
cmd = "mypy src --ignore-missing-imports"
6673
help = "Check types with mypy"
6774

68-
[tool.poe.tasks]
69-
_black = "black . --exclude tests/output_ --target-version py310"
70-
_isort = "isort . --extend-skip-glob 'tests/output_*/**/*'"
71-
7275
[tool.poe.tasks.format]
73-
sequence = ["_black", "_isort"]
74-
help = "Apply black and isort formatting to source code"
76+
sequence = ["_format", "_sort-imports"]
77+
help = "Format the source code, and sort the imports"
78+
79+
[tool.poe.tasks.check]
80+
sequence = ["_check-format", "_check-imports"]
81+
help = "Check that the source code is formatted and the imports sorted"
82+
83+
[tool.poe.tasks._format]
84+
cmd = "ruff format src tests"
85+
help = "Format the source code without sorting the imports"
86+
87+
[tool.poe.tasks._sort-imports]
88+
cmd = "ruff check --select I --fix src tests"
89+
help = "Sort the imports"
90+
91+
[tool.poe.tasks._check-format]
92+
cmd = "ruff format --diff src tests"
93+
help = "Check that the source code is formatted"
94+
95+
[tool.poe.tasks._check-imports]
96+
cmd = "ruff check --select I src tests"
97+
help = "Check that the imports are sorted"
7598

7699
[tool.poe.tasks.docs]
77100
cmd = "sphinx-build docs docs/build"
@@ -106,23 +129,6 @@ help = "Regenerate the types in betterproto.lib.std.google"
106129
shell = "poe generate && tox"
107130
help = "Run tests with multiple pythons"
108131

109-
[tool.poe.tasks.check-style]
110-
cmd = "black . --check --diff"
111-
help = "Check if code style is correct"
112-
113-
[tool.isort]
114-
py_version = 37
115-
profile = "black"
116-
force_single_line = false
117-
combine_as_imports = true
118-
lines_after_imports = 2
119-
include_trailing_comma = true
120-
force_grid_wrap = 2
121-
src_paths = ["src", "tests"]
122-
123-
[tool.black]
124-
target-version = ['py37']
125-
126132
[tool.doc8]
127133
paths = ["docs"]
128134
max_line_length = 88

src/betterproto/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
from types import UnionType as _types_UnionType
6767
else:
6868

69-
class _types_UnionType:
70-
...
69+
class _types_UnionType: ...
7170

7271

7372
# Proto 3 data types

src/betterproto/lib/pydantic/google/protobuf/__init__.py

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/lib/pydantic/google/protobuf/compiler/__init__.py

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/lib/std/google/protobuf/__init__.py

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/lib/std/google/protobuf/compiler/__init__.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/plugin/compiler.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import os.path
2+
import subprocess
23
import sys
34

45
from .module_validation import ModuleValidator
56

67

78
try:
89
# betterproto[compiler] specific dependencies
9-
import black
10-
import isort.api
1110
import jinja2
1211
except ImportError as err:
1312
print(
@@ -40,20 +39,17 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
4039

4140
code = body_template.render(output_file=output_file)
4241
code = header_template.render(output_file=output_file) + code
43-
code = isort.api.sort_code_string(
44-
code=code,
45-
show_diff=False,
46-
py_version=37,
47-
profile="black",
48-
combine_as_imports=True,
49-
lines_after_imports=2,
50-
quiet=True,
51-
force_grid_wrap=2,
52-
known_third_party=["grpclib", "betterproto"],
42+
43+
# Sort imports, delete unused ones
44+
code = subprocess.check_output(
45+
["ruff", "check", "--select", "I,F401", "--fix", "--silent", "-"],
46+
input=code,
47+
encoding="utf-8",
5348
)
54-
code = black.format_str(
55-
src_contents=code,
56-
mode=black.Mode(),
49+
50+
# Format the code
51+
code = subprocess.check_output(
52+
["ruff", "format", "-"], input=code, encoding="utf-8"
5753
)
5854

5955
# Validate the generated code.

tests/test_features.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,7 @@ class Truthy(betterproto.Message):
621621
2010-02-18T16:00:00.23334444
622622
2010-02-18T16:00:00,2283
623623
2009-05-19 143922
624-
2009-05-19 1439""".split(
625-
"\n"
626-
)
624+
2009-05-19 1439""".split("\n")
627625

628626

629627
def test_iso_datetime():

tests/test_streams.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_load_varint_file():
6262
stream.read(2) # Skip until first multi-byte
6363
assert betterproto.load_varint(stream) == (
6464
123456789,
65-
b"\x95\x9A\xEF\x3A",
65+
b"\x95\x9a\xef\x3a",
6666
) # Multi-byte varint
6767

6868

@@ -338,7 +338,7 @@ def run_java_single_varint(value: int, tmp_path) -> int:
338338

339339
def test_single_varint(compile_jar, tmp_path):
340340
single_byte = (1, b"\x01")
341-
multi_byte = (123456789, b"\x95\x9A\xEF\x3A")
341+
multi_byte = (123456789, b"\x95\x9a\xef\x3a")
342342

343343
# Write a single-byte varint to a file and have Java read it back
344344
returned = run_java_single_varint(single_byte[0], tmp_path)
@@ -351,8 +351,8 @@ def test_single_varint(compile_jar, tmp_path):
351351

352352
def test_multiple_varints(compile_jar, tmp_path):
353353
single_byte = (1, b"\x01")
354-
multi_byte = (123456789, b"\x95\x9A\xEF\x3A")
355-
over32 = (3000000000, b"\x80\xBC\xC1\x96\x0B")
354+
multi_byte = (123456789, b"\x95\x9a\xef\x3a")
355+
over32 = (3000000000, b"\x80\xbc\xc1\x96\x0b")
356356

357357
# Write two varints to the same file
358358
with open(tmp_path / "py_multiple_varints.out", "wb") as stream:

0 commit comments

Comments
 (0)