diff --git a/poetry.lock b/poetry.lock index 302782d..6ccb6b5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -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"}, diff --git a/pyproject.toml b/pyproject.toml index 4e71379..093ad4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/src/betterproto/plugin/compiler.py b/src/betterproto/plugin/compiler.py index d262694..7542432 100644 --- a/src/betterproto/plugin/compiler.py +++ b/src/betterproto/plugin/compiler.py @@ -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(), ) diff --git a/src/betterproto/templates/template.py.j2 b/src/betterproto/templates/template.py.j2 index 8f72b5e..e615350 100644 --- a/src/betterproto/templates/template.py.j2 +++ b/src/betterproto/templates/template.py.j2 @@ -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 %}