Skip to content

Commit

Permalink
Merge pull request #72 from nat-n/always_black
Browse files Browse the repository at this point in the history
Make CI check formatting is black & append .j2 suffix to template.py
  • Loading branch information
nat-n authored May 27, 2020
2 parents a5effb2 + d7d277e commit 261e55b
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 140 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ on: [push, pull_request]

jobs:

check-formatting:
runs-on: ubuntu-latest

name: Consult black on python formatting

steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.7
- uses: dschep/install-pipenv-action@v1
- name: Install dependencies
run: |
pipenv install --dev --python ${pythonLocation}/python
- name: Run black
run: |
pipenv run black . --check --diff --exclude tests/output_
run-tests:
runs-on: ubuntu-latest

Expand Down
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ isort = "*"
pytest = "*"
pytest-asyncio = "*"
rope = "*"
v = {editable = true,version = "*"}

[packages]
protobuf = "*"
Expand Down
214 changes: 97 additions & 117 deletions Pipfile.lock

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ $ pipenv shell
$ pip install -e .
```

### Code style

This project enforces [black](https://github.com/psf/black) python code formatting.

Before commiting changes run:

```bash
pipenv run black .
```

To avoid merge conflicts later, non-black formatted python code will fail in CI.

### Tests

There are two types of tests:
Expand All @@ -324,7 +336,7 @@ Adding a standard test case is easy.

It will be picked up automatically when you run the tests.

- See also: [Standard Tests Development Guide](betterproto/tests/README.md)
- See also: [Standard Tests Development Guide](betterproto/tests/README.md)

#### Custom tests

Expand Down
8 changes: 6 additions & 2 deletions betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,9 @@ def _get_field_default(self, field: dataclasses.Field, meta: FieldMetadata) -> A
return self._betterproto.default_gen[field.name]()

@classmethod
def _get_field_default_gen(cls, field: dataclasses.Field, meta: FieldMetadata) -> Any:
def _get_field_default_gen(
cls, field: dataclasses.Field, meta: FieldMetadata
) -> Any:
t = cls._type_hint(field.name)

if hasattr(t, "__origin__"):
Expand Down Expand Up @@ -831,7 +833,9 @@ def to_dict(
else:
output[cased_name] = b64encode(v).decode("utf8")
elif meta.proto_type == TYPE_ENUM:
enum_values = list(self._betterproto.cls_by_field[field.name]) # type: ignore
enum_values = list(
self._betterproto.cls_by_field[field.name]
) # type: ignore
if isinstance(v, list):
output[cased_name] = [enum_values[e].name for e in v]
else:
Expand Down
40 changes: 24 additions & 16 deletions betterproto/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@

import google.protobuf.wrappers_pb2 as google_wrappers

WRAPPER_TYPES: Dict[str, Optional[Type]] = defaultdict(lambda: None, {
'google.protobuf.DoubleValue': google_wrappers.DoubleValue,
'google.protobuf.FloatValue': google_wrappers.FloatValue,
'google.protobuf.Int64Value': google_wrappers.Int64Value,
'google.protobuf.UInt64Value': google_wrappers.UInt64Value,
'google.protobuf.Int32Value': google_wrappers.Int32Value,
'google.protobuf.UInt32Value': google_wrappers.UInt32Value,
'google.protobuf.BoolValue': google_wrappers.BoolValue,
'google.protobuf.StringValue': google_wrappers.StringValue,
'google.protobuf.BytesValue': google_wrappers.BytesValue,
})


def get_ref_type(package: str, imports: set, type_name: str, unwrap: bool = True) -> str:
WRAPPER_TYPES: Dict[str, Optional[Type]] = defaultdict(
lambda: None,
{
"google.protobuf.DoubleValue": google_wrappers.DoubleValue,
"google.protobuf.FloatValue": google_wrappers.FloatValue,
"google.protobuf.Int64Value": google_wrappers.Int64Value,
"google.protobuf.UInt64Value": google_wrappers.UInt64Value,
"google.protobuf.Int32Value": google_wrappers.Int32Value,
"google.protobuf.UInt32Value": google_wrappers.UInt32Value,
"google.protobuf.BoolValue": google_wrappers.BoolValue,
"google.protobuf.StringValue": google_wrappers.StringValue,
"google.protobuf.BytesValue": google_wrappers.BytesValue,
},
)


def get_ref_type(
package: str, imports: set, type_name: str, unwrap: bool = True
) -> str:
"""
Return a Python type name for a proto type reference. Adds the import if
necessary. Unwraps well known type if required.
Expand Down Expand Up @@ -178,7 +183,7 @@ def generate_code(request, response):
lstrip_blocks=True,
loader=jinja2.FileSystemLoader("%s/templates/" % os.path.dirname(__file__)),
)
template = env.get_template("template.py")
template = env.get_template("template.py.j2")

output_map = {}
for proto_file in request.proto_file:
Expand Down Expand Up @@ -385,7 +390,10 @@ def generate_code(request, response):
).strip('"'),
"input_message": input_message,
"output": get_ref_type(
package, output["imports"], method.output_type, unwrap=False
package,
output["imports"],
method.output_type,
unwrap=False,
).strip('"'),
"client_streaming": method.client_streaming,
"server_streaming": method.server_streaming,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion betterproto/tests/inputs/service/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
grpclib.const.Cardinality.UNARY_UNARY,
DoThingRequest,
DoThingResponse,
),
)
}


Expand Down
4 changes: 3 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


def pytest_addoption(parser):
parser.addoption("--repeat", type=int, default=1, help="repeat the operation multiple times")
parser.addoption(
"--repeat", type=int, default=1, help="repeat the operation multiple times"
)


@pytest.fixture(scope="session")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
packages=find_packages(
exclude=["tests", "*.tests", "*.tests.*", "output", "output.*"]
),
package_data={"betterproto": ["py.typed", "templates/template.py"]},
package_data={"betterproto": ["py.typed", "templates/template.py.j2"]},
python_requires=">=3.6",
install_requires=[
'dataclasses; python_version<"3.7"',
Expand Down

0 comments on commit 261e55b

Please sign in to comment.