From 70310c9e8cfe591ff13f45ed35c6bed826a7d288 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Thu, 17 Mar 2022 01:01:17 +0100 Subject: [PATCH] pre-commit: add isort hook and apply (#354) --- .pre-commit-config.yaml | 5 ++++ benchmarks/benchmarks.py | 4 +-- pyproject.toml | 9 +++++++ src/betterproto/__init__.py | 18 ++++++++++--- src/betterproto/_types.py | 7 ++++- src/betterproto/_version.py | 1 + src/betterproto/casing.py | 1 + src/betterproto/compile/importing.py | 9 ++++++- src/betterproto/grpc/grpclib_client.py | 6 ++++- src/betterproto/grpc/grpclib_server.py | 6 ++++- src/betterproto/grpc/util/async_channel.py | 10 ++++++- .../lib/google/protobuf/__init__.py | 5 +++- src/betterproto/plugin/compiler.py | 1 + src/betterproto/plugin/main.py | 3 +-- src/betterproto/plugin/models.py | 27 ++++++++++++++++--- src/betterproto/plugin/parser.py | 18 ++++++++++--- tests/generate.py | 3 ++- tests/grpc/test_grpclib_client.py | 7 ++--- tests/grpc/test_stream_stream.py | 8 +++--- tests/grpc/thing_service.py | 10 ++++--- tests/inputs/enum/test_enum.py | 2 +- .../example_service/test_example_service.py | 6 ++++- .../test_google_impl_behavior_equivalence.py | 6 ++--- .../test_googletypes_response.py | 15 ++++++++--- .../test_googletypes_response_embedded.py | 1 + .../test_import_service_input_message.py | 1 + .../test_oneof_default_value_serialization.py | 5 ++-- .../test_proto3_field_presence.py | 6 ++++- .../test_proto3_field_presence_oneof.py | 2 +- .../test_repeated_duration_timestamp.py | 5 +++- tests/test_casing.py | 6 ++++- tests/test_deprecated.py | 5 +++- tests/test_features.py | 22 +++++++++++---- tests/test_get_ref_type.py | 5 +++- tests/test_inputs.py | 9 ++++++- tests/test_version.py | 5 +++- tests/util.py | 15 ++++++++--- 37 files changed, 216 insertions(+), 58 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4d54bcb..3d2fbf5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,6 +2,11 @@ ci: autofix_prs: false repos: + - repo: https://github.com/pycqa/isort + rev: 5.10.1 + hooks: + - id: isort + - repo: https://github.com/psf/black rev: 22.1.0 hooks: diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index 7eeb338..07b2360 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -1,8 +1,8 @@ -import betterproto from dataclasses import dataclass - from typing import List +import betterproto + @dataclass class TestMessage(betterproto.Message): diff --git a/pyproject.toml b/pyproject.toml index 0435ab5..4e71379 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,6 +100,15 @@ help = "Run tests with multiple pythons" 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 = ['py36'] diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index 0a46af6..e6af1bc 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -7,9 +7,16 @@ import sys import typing import warnings from abc import ABC -from base64 import b64decode, b64encode +from base64 import ( + b64decode, + b64encode, +) from copy import deepcopy -from datetime import datetime, timedelta, timezone +from datetime import ( + datetime, + timedelta, + timezone, +) from typing import ( Any, Callable, @@ -29,9 +36,14 @@ from dateutil.parser import isoparse from ._types import T from ._version import __version__ -from .casing import camel_case, safe_snake_case, snake_case +from .casing import ( + camel_case, + safe_snake_case, + snake_case, +) from .grpc.grpclib_client import ServiceStub + # Proto 3 data types TYPE_ENUM = "enum" TYPE_BOOL = "bool" diff --git a/src/betterproto/_types.py b/src/betterproto/_types.py index 26b7344..616d550 100644 --- a/src/betterproto/_types.py +++ b/src/betterproto/_types.py @@ -1,7 +1,12 @@ -from typing import TYPE_CHECKING, TypeVar +from typing import ( + TYPE_CHECKING, + TypeVar, +) + if TYPE_CHECKING: from grpclib._typing import IProtoMessage + from . import Message # Bound type variable to allow methods to return `self` of subclasses diff --git a/src/betterproto/_version.py b/src/betterproto/_version.py index efb5082..e40ec96 100644 --- a/src/betterproto/_version.py +++ b/src/betterproto/_version.py @@ -1,3 +1,4 @@ from pkg_resources import get_distribution + __version__ = get_distribution("betterproto").version diff --git a/src/betterproto/casing.py b/src/betterproto/casing.py index cd3c344..047f197 100644 --- a/src/betterproto/casing.py +++ b/src/betterproto/casing.py @@ -1,6 +1,7 @@ import keyword import re + # Word delimiters and symbols that will not be preserved when re-casing. # language=PythonRegExp SYMBOLS = "[^a-zA-Z0-9]*" diff --git a/src/betterproto/compile/importing.py b/src/betterproto/compile/importing.py index 6793a5b..a28f555 100644 --- a/src/betterproto/compile/importing.py +++ b/src/betterproto/compile/importing.py @@ -1,11 +1,18 @@ import os import re -from typing import Dict, List, Set, Tuple, Type +from typing import ( + Dict, + List, + Set, + Tuple, + Type, +) from ..casing import safe_snake_case from ..lib.google import protobuf as google_protobuf from .naming import pythonize_class_name + WRAPPER_TYPES: Dict[str, Type] = { ".google.protobuf.DoubleValue": google_protobuf.DoubleValue, ".google.protobuf.FloatValue": google_protobuf.FloatValue, diff --git a/src/betterproto/grpc/grpclib_client.py b/src/betterproto/grpc/grpclib_client.py index 960bd3d..28d47bd 100644 --- a/src/betterproto/grpc/grpclib_client.py +++ b/src/betterproto/grpc/grpclib_client.py @@ -15,7 +15,11 @@ from typing import ( import grpclib.const -from .._types import ST, T +from .._types import ( + ST, + T, +) + if TYPE_CHECKING: from grpclib.client import Channel diff --git a/src/betterproto/grpc/grpclib_server.py b/src/betterproto/grpc/grpclib_server.py index f124eb0..5c1f934 100644 --- a/src/betterproto/grpc/grpclib_server.py +++ b/src/betterproto/grpc/grpclib_server.py @@ -1,6 +1,10 @@ from abc import ABC from collections.abc import AsyncIterable -from typing import Any, Callable, Dict +from typing import ( + Any, + Callable, + Dict, +) import grpclib import grpclib.server diff --git a/src/betterproto/grpc/util/async_channel.py b/src/betterproto/grpc/util/async_channel.py index 5cb3f89..9f18dbf 100644 --- a/src/betterproto/grpc/util/async_channel.py +++ b/src/betterproto/grpc/util/async_channel.py @@ -1,5 +1,13 @@ import asyncio -from typing import AsyncIterable, AsyncIterator, Iterable, Optional, TypeVar, Union +from typing import ( + AsyncIterable, + AsyncIterator, + Iterable, + Optional, + TypeVar, + Union, +) + T = TypeVar("T") diff --git a/src/betterproto/lib/google/protobuf/__init__.py b/src/betterproto/lib/google/protobuf/__init__.py index e9b6de7..822b870 100644 --- a/src/betterproto/lib/google/protobuf/__init__.py +++ b/src/betterproto/lib/google/protobuf/__init__.py @@ -3,7 +3,10 @@ # plugin: python-betterproto import warnings from dataclasses import dataclass -from typing import Dict, List +from typing import ( + Dict, + List, +) import betterproto from betterproto.grpc.grpclib_server import ServiceBase diff --git a/src/betterproto/plugin/compiler.py b/src/betterproto/plugin/compiler.py index ba2284e..d262694 100644 --- a/src/betterproto/plugin/compiler.py +++ b/src/betterproto/plugin/compiler.py @@ -1,5 +1,6 @@ import os.path + try: # betterproto[compiler] specific dependencies import black diff --git a/src/betterproto/plugin/main.py b/src/betterproto/plugin/main.py index 8982321..62382e2 100755 --- a/src/betterproto/plugin/main.py +++ b/src/betterproto/plugin/main.py @@ -7,9 +7,8 @@ from betterproto.lib.google.protobuf.compiler import ( CodeGeneratorRequest, CodeGeneratorResponse, ) - -from betterproto.plugin.parser import generate_code from betterproto.plugin.models import monkey_patch_oneof_index +from betterproto.plugin.parser import generate_code def main() -> None: diff --git a/src/betterproto/plugin/models.py b/src/betterproto/plugin/models.py index 63161b7..1a7d2e3 100644 --- a/src/betterproto/plugin/models.py +++ b/src/betterproto/plugin/models.py @@ -33,13 +33,28 @@ reference to `A` to `B`'s `fields` attribute. import builtins import re import textwrap -from dataclasses import dataclass, field -from typing import Dict, Iterable, Iterator, List, Optional, Set, Type, Union +from dataclasses import ( + dataclass, + field, +) +from typing import ( + Dict, + Iterable, + Iterator, + List, + Optional, + Set, + Type, + Union, +) import betterproto from betterproto import which_one_of from betterproto.casing import sanitize_name -from betterproto.compile.importing import get_type_reference, parse_source_type_name +from betterproto.compile.importing import ( + get_type_reference, + parse_source_type_name, +) from betterproto.compile.naming import ( pythonize_class_name, pythonize_field_name, @@ -58,13 +73,17 @@ from betterproto.lib.google.protobuf import ( from betterproto.lib.google.protobuf.compiler import CodeGeneratorRequest from ..casing import sanitize_name -from ..compile.importing import get_type_reference, parse_source_type_name +from ..compile.importing import ( + get_type_reference, + parse_source_type_name, +) from ..compile.naming import ( pythonize_class_name, pythonize_field_name, pythonize_method_name, ) + # Create a unique placeholder to deal with # https://stackoverflow.com/questions/51575931/class-inheritance-in-python-3-7-dataclasses PLACEHOLDER = object() diff --git a/src/betterproto/plugin/parser.py b/src/betterproto/plugin/parser.py index 85332d2..e05e568 100644 --- a/src/betterproto/plugin/parser.py +++ b/src/betterproto/plugin/parser.py @@ -1,3 +1,15 @@ +import itertools +import pathlib +import sys +from typing import ( + TYPE_CHECKING, + Iterator, + List, + Set, + Tuple, + Union, +) + from betterproto.lib.google.protobuf import ( DescriptorProto, EnumDescriptorProto, @@ -11,10 +23,7 @@ from betterproto.lib.google.protobuf.compiler import ( CodeGeneratorResponseFeature, CodeGeneratorResponseFile, ) -import itertools -import pathlib -import sys -from typing import Iterator, List, Set, Tuple, TYPE_CHECKING, Union + from .compiler import outputfile_compiler from .models import ( EnumDefinitionCompiler, @@ -30,6 +39,7 @@ from .models import ( is_oneof, ) + if TYPE_CHECKING: from google.protobuf.descriptor import Descriptor diff --git a/tests/generate.py b/tests/generate.py index 12f79e2..afbc4e2 100755 --- a/tests/generate.py +++ b/tests/generate.py @@ -1,10 +1,10 @@ #!/usr/bin/env python import asyncio import os -from pathlib import Path import platform import shutil import sys +from pathlib import Path from typing import Set from tests.util import ( @@ -15,6 +15,7 @@ from tests.util import ( protoc, ) + # Force pure-python implementation instead of C++, otherwise imports # break things because we can't properly reset the symbol database. os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" diff --git a/tests/grpc/test_grpclib_client.py b/tests/grpc/test_grpclib_client.py index ba0b943..ce570e5 100644 --- a/tests/grpc/test_grpclib_client.py +++ b/tests/grpc/test_grpclib_client.py @@ -3,18 +3,19 @@ import sys import uuid import grpclib +import grpclib.client import grpclib.metadata import grpclib.server -import grpclib.client import pytest -from betterproto.grpc.util.async_channel import AsyncChannel from grpclib.testing import ChannelFor + +from betterproto.grpc.util.async_channel import AsyncChannel from tests.output_betterproto.service import ( DoThingRequest, DoThingResponse, GetThingRequest, + TestStub as ThingServiceClient, ) -from tests.output_betterproto.service import TestStub as ThingServiceClient from .thing_service import ThingService diff --git a/tests/grpc/test_stream_stream.py b/tests/grpc/test_stream_stream.py index 020262d..9a1e5b8 100644 --- a/tests/grpc/test_stream_stream.py +++ b/tests/grpc/test_stream_stream.py @@ -1,9 +1,11 @@ import asyncio +from dataclasses import dataclass +from typing import AsyncIterator + +import pytest + import betterproto from betterproto.grpc.util.async_channel import AsyncChannel -from dataclasses import dataclass -import pytest -from typing import AsyncIterator @dataclass diff --git a/tests/grpc/thing_service.py b/tests/grpc/thing_service.py index 835d6f1..1d7c27a 100644 --- a/tests/grpc/thing_service.py +++ b/tests/grpc/thing_service.py @@ -1,12 +1,14 @@ +from typing import Dict + +import grpclib +import grpclib.server + from tests.output_betterproto.service import ( - DoThingResponse, DoThingRequest, + DoThingResponse, GetThingRequest, GetThingResponse, ) -import grpclib -import grpclib.server -from typing import Dict class ThingService: diff --git a/tests/inputs/enum/test_enum.py b/tests/inputs/enum/test_enum.py index 3005c43..ead48a8 100644 --- a/tests/inputs/enum/test_enum.py +++ b/tests/inputs/enum/test_enum.py @@ -1,6 +1,6 @@ from tests.output_betterproto.enum import ( - Test, Choice, + Test, ) diff --git a/tests/inputs/example_service/test_example_service.py b/tests/inputs/example_service/test_example_service.py index 49c58f6..23b2e3b 100644 --- a/tests/inputs/example_service/test_example_service.py +++ b/tests/inputs/example_service/test_example_service.py @@ -1,7 +1,11 @@ -from typing import AsyncIterable, AsyncIterator +from typing import ( + AsyncIterable, + AsyncIterator, +) import pytest from grpclib.testing import ChannelFor + from tests.output_betterproto.example_service import ( ExampleRequest, ExampleResponse, diff --git a/tests/inputs/google_impl_behavior_equivalence/test_google_impl_behavior_equivalence.py b/tests/inputs/google_impl_behavior_equivalence/test_google_impl_behavior_equivalence.py index abe5d66..0297692 100644 --- a/tests/inputs/google_impl_behavior_equivalence/test_google_impl_behavior_equivalence.py +++ b/tests/inputs/google_impl_behavior_equivalence/test_google_impl_behavior_equivalence.py @@ -1,14 +1,14 @@ import pytest - from google.protobuf import json_format + import betterproto from tests.output_betterproto.google_impl_behavior_equivalence import ( - Test, Foo, + Test, ) from tests.output_reference.google_impl_behavior_equivalence.google_impl_behavior_equivalence_pb2 import ( - Test as ReferenceTest, Foo as ReferenceFoo, + Test as ReferenceTest, ) diff --git a/tests/inputs/googletypes_response/test_googletypes_response.py b/tests/inputs/googletypes_response/test_googletypes_response.py index ecb50dd..6e1ed29 100644 --- a/tests/inputs/googletypes_response/test_googletypes_response.py +++ b/tests/inputs/googletypes_response/test_googletypes_response.py @@ -1,9 +1,18 @@ -from typing import Any, Callable, Optional +from typing import ( + Any, + Callable, + Optional, +) + +import pytest import betterproto.lib.google.protobuf as protobuf -import pytest from tests.mocks import MockChannel -from tests.output_betterproto.googletypes_response import Input, TestStub +from tests.output_betterproto.googletypes_response import ( + Input, + TestStub, +) + test_cases = [ (TestStub.get_double, protobuf.DoubleValue, 2.5), diff --git a/tests/inputs/googletypes_response_embedded/test_googletypes_response_embedded.py b/tests/inputs/googletypes_response_embedded/test_googletypes_response_embedded.py index 34f6216..57ebce1 100644 --- a/tests/inputs/googletypes_response_embedded/test_googletypes_response_embedded.py +++ b/tests/inputs/googletypes_response_embedded/test_googletypes_response_embedded.py @@ -1,4 +1,5 @@ import pytest + from tests.mocks import MockChannel from tests.output_betterproto.googletypes_response_embedded import ( Input, diff --git a/tests/inputs/import_service_input_message/test_import_service_input_message.py b/tests/inputs/import_service_input_message/test_import_service_input_message.py index 4c69fc0..60c3e20 100644 --- a/tests/inputs/import_service_input_message/test_import_service_input_message.py +++ b/tests/inputs/import_service_input_message/test_import_service_input_message.py @@ -1,4 +1,5 @@ import pytest + from tests.mocks import MockChannel from tests.output_betterproto.import_service_input_message import ( NestedRequestMessage, diff --git a/tests/inputs/oneof_default_value_serialization/test_oneof_default_value_serialization.py b/tests/inputs/oneof_default_value_serialization/test_oneof_default_value_serialization.py index 0c928cb..29dd816 100644 --- a/tests/inputs/oneof_default_value_serialization/test_oneof_default_value_serialization.py +++ b/tests/inputs/oneof_default_value_serialization/test_oneof_default_value_serialization.py @@ -1,11 +1,12 @@ -import pytest import datetime +import pytest + import betterproto from tests.output_betterproto.oneof_default_value_serialization import ( - Test, Message, NestedMessage, + Test, ) diff --git a/tests/inputs/proto3_field_presence/test_proto3_field_presence.py b/tests/inputs/proto3_field_presence/test_proto3_field_presence.py index e07b283..3972d86 100644 --- a/tests/inputs/proto3_field_presence/test_proto3_field_presence.py +++ b/tests/inputs/proto3_field_presence/test_proto3_field_presence.py @@ -1,6 +1,10 @@ import json -from tests.output_betterproto.proto3_field_presence import Test, InnerTest, TestEnum +from tests.output_betterproto.proto3_field_presence import ( + InnerTest, + Test, + TestEnum, +) def test_null_fields_json(): diff --git a/tests/inputs/proto3_field_presence_oneof/test_proto3_field_presence_oneof.py b/tests/inputs/proto3_field_presence_oneof/test_proto3_field_presence_oneof.py index 0092a23..d5f69d0 100644 --- a/tests/inputs/proto3_field_presence_oneof/test_proto3_field_presence_oneof.py +++ b/tests/inputs/proto3_field_presence_oneof/test_proto3_field_presence_oneof.py @@ -1,7 +1,7 @@ from tests.output_betterproto.proto3_field_presence_oneof import ( - Test, InnerNested, Nested, + Test, WithOptional, ) diff --git a/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py b/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py index b1b13e5..efc3486 100644 --- a/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py +++ b/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py @@ -1,4 +1,7 @@ -from datetime import datetime, timedelta +from datetime import ( + datetime, + timedelta, +) from tests.output_betterproto.repeated_duration_timestamp import Test diff --git a/tests/test_casing.py b/tests/test_casing.py index ec60483..56cd8a9 100644 --- a/tests/test_casing.py +++ b/tests/test_casing.py @@ -1,6 +1,10 @@ import pytest -from betterproto.casing import camel_case, pascal_case, snake_case +from betterproto.casing import ( + camel_case, + pascal_case, + snake_case, +) @pytest.mark.parametrize( diff --git a/tests/test_deprecated.py b/tests/test_deprecated.py index 713af7d..84e08bd 100644 --- a/tests/test_deprecated.py +++ b/tests/test_deprecated.py @@ -2,7 +2,10 @@ import warnings import pytest -from tests.output_betterproto.deprecated import Message, Test +from tests.output_betterproto.deprecated import ( + Message, + Test, +) @pytest.fixture diff --git a/tests/test_features.py b/tests/test_features.py index 7dc3f35..39fa5c2 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -1,8 +1,18 @@ +from copy import ( + copy, + deepcopy, +) from dataclasses import dataclass -from copy import copy, deepcopy from datetime import datetime -from inspect import Parameter, signature -from typing import Dict, List, Optional +from inspect import ( + Parameter, + signature, +) +from typing import ( + Dict, + List, + Optional, +) import betterproto @@ -351,8 +361,10 @@ def test_recursive_message(): def test_recursive_message_defaults(): - from tests.output_betterproto.recursivemessage import Intermediate - from tests.output_betterproto.recursivemessage import Test as RecursiveMessage + from tests.output_betterproto.recursivemessage import ( + Intermediate, + Test as RecursiveMessage, + ) msg = RecursiveMessage(name="bob", intermediate=Intermediate(42)) diff --git a/tests/test_get_ref_type.py b/tests/test_get_ref_type.py index cbee4ca..4883ead 100644 --- a/tests/test_get_ref_type.py +++ b/tests/test_get_ref_type.py @@ -1,6 +1,9 @@ import pytest -from betterproto.compile.importing import get_type_reference, parse_source_type_name +from betterproto.compile.importing import ( + get_type_reference, + parse_source_type_name, +) @pytest.mark.parametrize( diff --git a/tests/test_inputs.py b/tests/test_inputs.py index 278a4da..2077fff 100644 --- a/tests/test_inputs.py +++ b/tests/test_inputs.py @@ -5,7 +5,13 @@ import os import sys from collections import namedtuple from types import ModuleType -from typing import Any, Dict, List, Set, Tuple +from typing import ( + Any, + Dict, + List, + Set, + Tuple, +) import pytest @@ -19,6 +25,7 @@ from tests.util import ( inputs_path, ) + # Force pure-python implementation instead of C++, otherwise imports # break things because we can't properly reset the symbol database. os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" diff --git a/tests/test_version.py b/tests/test_version.py index 05fe79e..461f366 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -1,7 +1,10 @@ -from betterproto import __version__ from pathlib import Path + import tomlkit +from betterproto import __version__ + + PROJECT_TOML = Path(__file__).joinpath("..", "..", "pyproject.toml").resolve() diff --git a/tests/util.py b/tests/util.py index 950cf7a..e3b43aa 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,11 +1,20 @@ import asyncio -from dataclasses import dataclass import importlib import os -from pathlib import Path import sys +from dataclasses import dataclass +from pathlib import Path from types import ModuleType -from typing import Callable, Dict, Generator, List, Optional, Tuple, Union +from typing import ( + Callable, + Dict, + Generator, + List, + Optional, + Tuple, + Union, +) + os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"