8000 Merge pull request #72 from nat-n/always_black · fixpoint/python-betterproto@261e55b · GitHub
[go: up one dir, main page]

Skip to content

Commit 261e55b

Browse files
authored
Merge pull request danielgtaylor#72 from nat-n/always_black
Make CI check formatting is black & append .j2 suffix to template.py
2 parents a5effb2 + d7d277e commit 261e55b

File tree

10 files changed

+163
-140
lines changed

10 files changed

+163
-140
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ on: [push, pull_request]
44

55
jobs:
66

7+
check-formatting:
8+
runs-on: ubuntu-latest
9+
10+
name: Consult black on python formatting
11+
12+
steps:
13+
- uses: actions/checkout@v1
14+
- uses: actions/setup-python@v1
15+
with:
16+
python-version: 3.7
17+
- uses: dschep/install-pipenv-action@v1
18+
- name: Install dependencies
19+
run: |
20+
pipenv install --dev --python ${pythonLocation}/python
21+
- name: Run black
22+
run: |
23+
pipenv run black . --check --diff --exclude tests/output_
24+
725
run-tests:
826
runs-on: ubuntu-latest
927

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ isort = "*"
1010
pytest = "*"
1111
pytest-asyncio = "*"
1212
rope = "*"
13-
v = {editable = true,version = "*"}
1413

1514
[packages]
1615
protobuf = "*"

Pipfile.lock

Lines changed: 97 additions & 117 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ $ pipenv shell
307307
$ pip install -e .
308308
```
309309

310+
### Code style
311+
312+
This project enforces [black](https://github.com/psf/black) python code formatting.
313+
314+
Before commiting changes run:
315+
316+
```bash
317+
pipenv run black .
318+
```
319+
320+
To avoid merge conflicts later, non-black formatted python code will fail in CI.
321+
310322
### Tests
311323

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

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

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

329341
#### Custom tests
330342

betterproto/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,9 @@ def _get_field_default(self, field: dataclasses.Field, meta: FieldMetadata) -> A
652652
return self._betterproto.default_gen[field.name]()
653653

654654
@classmethod
655-
def _get_field_default_gen(cls, field: dataclasses.Field, meta: FieldMetadata) -> Any:
655+
def _get_field_default_gen(
656+
cls, field: dataclasses.Field, meta: FieldMetadata
657+
) -> Any:
656658
t = cls._type_hint(field.name)
657659

658660
if hasattr(t, "__origin__"):
@@ -831,7 +833,9 @@ def to_dict(
831833
else:
832834
output[cased_name] = b64encode(v).decode("utf8")
833835
elif meta.proto_type == TYPE_ENUM:
834-
enum_values = list(self._betterproto.cls_by_field[field.name]) # type: ignore
836+
enum_values = list(
837+
self._betterproto.cls_by_field[field.name]
838+
) # type: ignore
835839
if isinstance(v, list):
836840
output[cased_name] = [enum_values[e].name for e in v]
837841
else:

betterproto/plugin.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@
2929

3030
import google.protobuf.wrappers_pb2 as google_wrappers
3131

32-
WRAPPER_TYPES: Dict[str, Optional[Type]] = defaultdict(lambda: None, {
33-
'google.protobuf.DoubleValue': google_wrappers.DoubleValue,
34-
'google.protobuf.FloatValue': google_wrappers.FloatValue,
35-
'google.protobuf.Int64Value': google_wrappers.Int64Value,
36-
'google.protobuf.UInt64Value': google_wrappers.UInt64Value,
37-
'google.protobuf.Int32Value': google_wrappers.Int32Value,
38-
'google.protobuf.UInt32Value': google_wrappers.UInt32Value,
39-
'google.protobuf.BoolValue': google_wrappers.BoolValue,
40-
'google.protobuf.StringValue': google_wrappers.StringValue,
41-
'google.protobuf.BytesValue': google_wrappers.BytesValue,
42-
})
43-
44-
45-
def get_ref_type(package: str, imports: set, type_name: str, unwrap: bool = True) -> str:
32+
WRAPPER_TYPES: Dict[str, Optional[Type]] = defaultdict(
33+
lambda: None,
34+
{
35+
"google.protobuf.DoubleValue": google_wrappers.DoubleValue,
36+
"google.protobuf.FloatValue": google_wrappers.FloatValue,
37+
"google.protobuf.Int64Value": google_wrappers.Int64Value,
38+
"google.protobuf.UInt64Value": google_wrappers.UInt64Value,
39+
"google.protobuf.Int32Value": google_wrappers.Int32Value,
40+
"google.protobuf.UInt32Value": google_wrappers.UInt32Value,
41+
"google.protobuf.BoolValue": google_wrappers.BoolValue,
42+
"google.protobuf.StringValue": google_wrappers.StringValue,
43+
"google.protobuf.BytesValue": google_wrappers.BytesValue,
44+
},
45+
)
46+
47+
48+
def get_ref_type(
49+
package: str, imports: set, type_name: str, unwrap: bool = True
50+
) -> str:
4651
"""
4752
Return a Python type name for a proto type reference. Adds the import if
4853
necessary. Unwraps well known type if required.
@@ -178,7 +183,7 @@ def generate_code(request, response):
178183
lstrip_blocks=True,
179184
loader=jinja2.FileSystemLoader("%s/templates/" % os.path.dirname(__file__)),
180185
)
181-
template = env.get_template("template.py")
186+
template = env.get_template("template.py.j2")
182187

183188
output_map = {}
184189
for proto_file in request.proto_file:
@@ -385,7 +390,10 @@ def generate_code(request, response):
385390
).strip('"'),
386391
"input_message": input_message,
387392
"output": get_ref_type(
388-
package, output["imports"], method.output_type, unwrap=False
393+
package,
394+
output["imports"],
395+
method.output_type,
396+
unwrap=False,
389397
).strip('"'),
390398
"client_streaming": method.client_streaming,
391399
"server_streaming": method.server_streaming,

betterproto/tests/inputs/service/test_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
3434
grpclib.const.Cardinality.UNARY_UNARY,
3535
DoThingRequest,
3636
DoThingResponse,
37-
),
37+
)
3838
}
3939

4040

conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33

44
def pytest_addoption(parser):
5-
parser.addoption("--repeat", type=int, default=1, help="repeat the operation multiple times")
5+
parser.addoption(
6+
"--repeat", type=int, default=1, help="repeat the operation multiple times"
7+
)
68

79

810
@pytest.fixture(scope="session")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
packages=find_packages(
1717
exclude=["tests", "*.tests", "*.tests.*", "output", "output.*"]
1818
),
19-
package_data={"betterproto": ["py.typed", "templates/template.py"]},
19+
package_data={"betterproto": ["py.typed", "templates/template.py.j2"]},
2020
python_requires=">=3.6",
2121
install_requires=[
2222
'dataclasses; python_version<"3.7"',

0 commit comments

Comments
 (0)
0