Skip to content

Commit 1aaf772

Browse files
authored
compiler: Run isort on compiled code (#355)
1 parent 70310c9 commit 1aaf772

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

poetry.lock

+20-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dataclasses = { version = "^0.7", python = ">=3.6, <3.7" }
1818
grpclib = "^0.4.1"
1919
jinja2 = { version = ">=2.11.2", optional = true }
2020
python-dateutil = "^2.8"
21+
isort = {version = "^5.10.1", optional = true}
2122

2223
[tool.poetry.dev-dependencies]
2324
asv = "^0.4.2"
@@ -42,7 +43,7 @@ pre-commit = "^2.17.0"
4243
protoc-gen-python_betterproto = "betterproto.plugin:main"
4344

4445
[tool.poetry.extras]
45-
compiler = ["black", "jinja2"]
46+
compiler = ["black", "isort", "jinja2"]
4647

4748

4849
# Dev workflow tasks

src/betterproto/plugin/compiler.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
try:
55
# betterproto[compiler] specific dependencies
66
import black
7+
import isort.api
78
import jinja2
89
except ImportError as err:
910
print(
@@ -32,7 +33,19 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
3233
)
3334
template = env.get_template("template.py.j2")
3435

36+
code = template.render(output_file=output_file)
37+
code = isort.api.sort_code_string(
38+
code=code,
39+
show_diff=False,
40+
py_version=37,
41+
profile="black",
42+
combine_as_imports=True,
43+
lines_after_imports=2,
44+
quiet=True,
45+
force_grid_wrap=2,
46+
known_third_party=["grpclib", "betterproto"],
47+
)
3548
return black.format_str(
36-
template.render(output_file=output_file),
49+
src_contents=code,
3750
mode=black.Mode(),
3851
)

src/betterproto/templates/template.py.j2

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ from typing import {% for i in output_file.typing_imports|sort %}{{ i }}{% if no
1616

1717
import betterproto
1818
from betterproto.grpc.grpclib_server import ServiceBase
19+
{% for i in output_file.imports|sort %}
20+
{{ i }}
21+
{% endfor %}
1922
{% if output_file.services %}
2023
import grpclib
2124
{% endif %}
@@ -216,7 +219,3 @@ class {{ service.py_name }}Base(ServiceBase):
216219
}
217220

218221
{% endfor %}
219-
220-
{% for i in output_file.imports|sort %}
221-
{{ i }}
222-
{% endfor %}

0 commit comments

Comments
 (0)