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

Use ruff instead of black+isort #644

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ output
.asv
venv
.devcontainer
.ruff_cache
15 changes: 6 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ ci:
autofix_prs: false

repos:
- repo: https://github.com/pycqa/isort
rev: 5.11.5
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.4
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
args: ["--target-version", "py310"]
- id: ruff-format
args: ["--diff", "src", "tests"]
- id: ruff
args: ["--select", "I", "src", "tests"]

- repo: https://github.com/PyCQA/doc8
rev: 0.10.1
Expand Down
941 changes: 462 additions & 479 deletions poetry.lock

Large diffs are not rendered by default.

58 changes: 32 additions & 26 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ packages = [

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

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

[tool.poetry.extras]
compiler = ["black", "isort", "jinja2"]
compiler = ["ruff", "jinja2"]
rust-codec = ["betterproto-rust-codec"]

[tool.ruff]
extend-exclude = ["tests/output_*"]
target-version = "py38"

[tool.ruff.lint.isort]
combine-as-imports = true
lines-after-imports = 2

# Dev workflow tasks

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

[tool.poe.tasks]
_black = "black . --exclude tests/output_ --target-version py310"
_isort = "isort . --extend-skip-glob 'tests/output_*/**/*'"

[tool.poe.tasks.format]
sequence = ["_black", "_isort"]
help = "Apply black and isort formatting to source code"
sequence = ["_format", "_sort-imports"]
help = "Format the source code, and sort the imports"

[tool.poe.tasks.check]
sequence = ["_check-format", "_check-imports"]
help = "Check that the source code is formatted and the imports sorted"

[tool.poe.tasks._format]
cmd = "ruff format src tests"
help = "Format the source code without sorting the imports"

[tool.poe.tasks._sort-imports]
cmd = "ruff check --select I --fix src tests"
help = "Sort the imports"

[tool.poe.tasks._check-format]
cmd = "ruff format --diff src tests"
help = "Check that the source code is formatted"

[tool.poe.tasks._check-imports]
cmd = "ruff check --select I src tests"
help = "Check that the imports are sorted"

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

[tool.poe.tasks.check-style]
cmd = "black . --check --diff"
help = "Check if code style is correct"

[tool.isort]
py_version = 37
profile = "black"
force_single_line = false
combine_as_imports = true
lines_after_imports = 2
include_trailing_comma = true
force_grid_wrap = 2
src_paths = ["src", "tests"]

[tool.black]
target-version = ['py37']

[tool.doc8]
paths = ["docs"]
max_line_length = 88
Expand Down
3 changes: 1 addition & 2 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
from types import UnionType as _types_UnionType
else:

class _types_UnionType:
...
class _types_UnionType: ...


# Proto 3 data types
Expand Down
12 changes: 6 additions & 6 deletions src/betterproto/lib/pydantic/google/protobuf/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/betterproto/lib/std/google/protobuf/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/betterproto/lib/std/google/protobuf/compiler/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 11 additions & 15 deletions src/betterproto/plugin/compiler.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os.path
import subprocess
import sys

from .module_validation import ModuleValidator


try:
# betterproto[compiler] specific dependencies
import black
import isort.api
import jinja2
except ImportError as err:
print(
Expand Down Expand Up @@ -40,20 +39,17 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:

code = body_template.render(output_file=output_file)
code = header_template.render(output_file=output_file) + code
code = isort.api.sort_code_string(
code=code,
show_diff=False,
py_version=37,
profile="black",
combine_as_imports=True,
lines_after_imports=2,
quiet=True,
force_grid_wrap=2,
known_third_party=["grpclib", "betterproto"],

# Sort imports, delete unused ones
code = subprocess.check_output(
["ruff", "check", "--select", "I,F401", "--fix", "--silent", "-"],
input=code,
encoding="utf-8",
)
code = black.format_str(
src_contents=code,
mode=black.Mode(),

# Format the code
code = subprocess.check_output(
["ruff", "format", "-"], input=code, encoding="utf-8"
)

# Validate the generated code.
Expand Down
4 changes: 1 addition & 3 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,7 @@ class Truthy(betterproto.Message):
2010-02-18T16:00:00.23334444
2010-02-18T16:00:00,2283
2009-05-19 143922
2009-05-19 1439""".split(
"\n"
)
2009-05-19 1439""".split("\n")


def test_iso_datetime():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_load_varint_file():
stream.read(2) # Skip until first multi-byte
assert betterproto.load_varint(stream) == (
123456789,
b"\x95\x9A\xEF\x3A",
b"\x95\x9a\xef\x3a",
) # Multi-byte varint


Expand Down Expand Up @@ -338,7 +338,7 @@ def run_java_single_varint(value: int, tmp_path) -> int:

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

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

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

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