compiler: Run isort on compiled code (#355)

This commit is contained in:
Arun Babu Neelicattu 2022-03-18 23:29:42 +01:00 committed by GitHub
parent 70310c9e8c
commit 1aaf7728cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

22
poetry.lock generated
View File

@ -379,6 +379,20 @@ category = "dev"
optional = false
python-versions = "*"
[[package]]
name = "isort"
version = "5.10.1"
description = "A Python utility / library to sort Python imports."
category = "main"
optional = true
python-versions = ">=3.6.1,<4.0"
[package.extras]
pipfile_deprecated_finder = ["pipreqs", "requirementslib"]
requirements_deprecated_finder = ["pipreqs", "pip-api"]
colors = ["colorama (>=0.4.3,<0.5.0)"]
plugins = ["setuptools"]
[[package]]
name = "jinja2"
version = "3.0.3"
@ -925,12 +939,12 @@ docs = ["sphinx", "jaraco.packaging (>=8.2)", "rst.linker (>=1.9)"]
testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest-cov", "pytest-enabler (>=1.0.1)", "jaraco.itertools", "func-timeout", "pytest-black (>=0.3.7)", "pytest-mypy"]
[extras]
compiler = ["black", "jinja2"]
compiler = ["black", "isort", "jinja2"]
[metadata]
lock-version = "1.1"
python-versions = ">=3.6.2,<4.0"
content-hash = "d710a0f85f0cfd8b891a15f61015df3eae2e33a9d060be27592b9632c6edd023"
content-hash = "2891bac64691f1030eb74c847cd5a8d9b43598b108cf03db80a987b168d89a22"
[metadata.files]
alabaster = [
@ -1289,6 +1303,10 @@ iniconfig = [
{file = "iniconfig-1.1.1-py2.py3-none-any.whl", hash = "sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3"},
{file = "iniconfig-1.1.1.tar.gz", hash = "sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32"},
]
isort = [
{file = "isort-5.10.1-py3-none-any.whl", hash = "sha256:6f62d78e2f89b4500b080fe3a81690850cd254227f27f75c3a0c491a1f351ba7"},
{file = "isort-5.10.1.tar.gz", hash = "sha256:e8443a5e7a020e9d7f97f1d7d9cd17c88bcb3bc7e218bf9cf5095fe550be2951"},
]
jinja2 = [
{file = "Jinja2-3.0.3-py3-none-any.whl", hash = "sha256:077ce6014f7b40d03b47d1f1ca4b0fc8328a692bd284016f806ed0eaca390ad8"},
{file = "Jinja2-3.0.3.tar.gz", hash = "sha256:611bb273cd68f3b993fabdc4064fc858c5b47a973cb5aa7999ec1ba405c87cd7"},

View File

@ -18,6 +18,7 @@ dataclasses = { version = "^0.7", python = ">=3.6, <3.7" }
grpclib = "^0.4.1"
jinja2 = { version = ">=2.11.2", optional = true }
python-dateutil = "^2.8"
isort = {version = "^5.10.1", optional = true}
[tool.poetry.dev-dependencies]
asv = "^0.4.2"
@ -42,7 +43,7 @@ pre-commit = "^2.17.0"
protoc-gen-python_betterproto = "betterproto.plugin:main"
[tool.poetry.extras]
compiler = ["black", "jinja2"]
compiler = ["black", "isort", "jinja2"]
# Dev workflow tasks

View File

@ -4,6 +4,7 @@ import os.path
try:
# betterproto[compiler] specific dependencies
import black
import isort.api
import jinja2
except ImportError as err:
print(
@ -32,7 +33,19 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
)
template = env.get_template("template.py.j2")
code = template.render(output_file=output_file)
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"],
)
return black.format_str(
template.render(output_file=output_file),
src_contents=code,
mode=black.Mode(),
)

View File

@ -16,6 +16,9 @@ from typing import {% for i in output_file.typing_imports|sort %}{{ i }}{% if no
import betterproto
from betterproto.grpc.grpclib_server import ServiceBase
{% for i in output_file.imports|sort %}
{{ i }}
{% endfor %}
{% if output_file.services %}
import grpclib
{% endif %}
@ -216,7 +219,3 @@ class {{ service.py_name }}Base(ServiceBase):
}
{% endfor %}
{% for i in output_file.imports|sort %}
{{ i }}
{% endfor %}