Skip to content

Commit db29fd8

Browse files
committed
Add support for python 3.6
Changes: - Update config and docs to reference 3.6 - Add backports of dataclasses and datetime.fromisoformat - Support both 3.7 and 3.6 usages of undocumented __origin__ attribute on typing objects - Make github ci run tests for python 3.6 as well
1 parent dc7a3e9 commit db29fd8

File tree

9 files changed

+41
-404
lines changed

9 files changed

+41
-404
lines changed

.github/workflows/ci.yml

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,28 @@ name: CI
33
on: [push, pull_request]
44

55
jobs:
6-
build:
6+
7+
build-p36:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- uses: actions/setup-python@v1
13+
with:
14+
python-version: 3.6
15+
- uses: dschep/install-pipenv-action@v1
16+
- name: Install dependencies
17+
run: |
18+
sudo apt install protobuf-compiler libprotobuf-dev
19+
pipenv install --dev
20+
- name: Run tests
21+
run: |
22+
cp .env.default .env
23+
pipenv run pip install -e .
24+
pipenv run generate
25+
pipenv run test
26+
27+
build-p37:
728
runs-on: ubuntu-latest
829

930
steps:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.vscode/settings.json
33
.mypy_cache
44
.pytest_cache
5+
.python-version
56
betterproto/tests/*.bin
67
betterproto/tests/*_pb2.py
78
betterproto/tests/*.py

Pipfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ mypy = "*"
99
isort = "*"
1010
pytest = "*"
1111
rope = "*"
12+
v = {editable = true,version = "*"}
1213

1314
[packages]
1415
protobuf = "*"
1516
jinja2 = "*"
1617
grpclib = "*"
1718
stringcase = "*"
1819
black = "*"
20+
backports-datetime-fromisoformat = "*"
21+
dataclasses = "*"
1922

2023
[requires]
21-
python_version = "3.7"
24+
python_version = "3.6"
2225

2326
[scripts]
2427
plugin = "protoc --plugin=protoc-gen-custom=betterproto/plugin.py --custom_out=output"

Pipfile.lock

-396
This file was deleted.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This project aims to provide an improved experience when using Protobuf / gRPC i
66

77
- Protobuf 3 & gRPC code generation
88
- Both binary & JSON serialization is built-in
9-
- Python 3.7+ making use of:
9+
- Python 3.6+ making use of:
1010
- Enums
1111
- Dataclasses
1212
- `async`/`await`
@@ -296,7 +296,7 @@ datetime.datetime(2019, 1, 1, 11, 59, 58, 800000, tzinfo=datetime.timezone.utc)
296296

297297
## Development
298298

299-
First, make sure you have Python 3.7+ and `pipenv` installed, along with the official [Protobuf Compiler](https://github.com/protocolbuffers/protobuf/releases) for your platform. Then:
299+
First, make sure you have Python 3.6+ and `pipenv` installed, along with the official [Protobuf Compiler](https://github.com/protocolbuffers/protobuf/releases) for your platform. Then:
300300

301301
```sh
302302
# Get set up with the virtual env & dependencies

betterproto/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
import json
55
import struct
6+
import sys
67
from abc import ABC
78
from base64 import b64encode, b64decode
89
from datetime import datetime, timedelta, timezone
@@ -33,6 +34,13 @@
3334
if TYPE_CHECKING:
3435
from grpclib._protocols import IProtoMessage
3536

37+
if not (sys.version_info.major == 3 and sys.version_info.minor >= 7):
38+
# Apply backport of datetime.fromisoformat from 3.7
39+
from backports.datetime_fromisoformat import MonkeyPatch
40+
41+
MonkeyPatch.patch_fromisoformat()
42+
43+
3644
# Proto 3 data types
3745
TYPE_ENUM = "enum"
3846
TYPE_BOOL = "bool"
@@ -569,10 +577,10 @@ def _get_field_default(self, field: dataclasses.Field, meta: FieldMetadata) -> A
569577

570578
value: Any = 0
571579
if hasattr(t, "__origin__"):
572-
if t.__origin__ == dict:
580+
if t.__origin__ in (dict, Dict):
573581
# This is some kind of map (dict in Python).
574582
value = {}
575-
elif t.__origin__ == list:
583+
elif t.__origin__ in (list, List):
576584
# This is some kind of list (repeated) field.
577585
value = []
578586
elif t.__origin__ == Union and t.__args__[1] == type(None):
-2.97 KB
Binary file not shown.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.black]
2-
target-version = ['py37']
2+
target-version = ['py36']
33

44
[tool.isort]
55
multi_line_output = 3

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
exclude=["tests", "*.tests", "*.tests.*", "output", "output.*"]
1818
),
1919
package_data={"betterproto": ["py.typed", "templates/template.py"]},
20-
python_requires=">=3.7",
20+
python_requires=">=3.6",
2121
install_requires=["grpclib", "stringcase"],
2222
extras_require={"compiler": ["black", "jinja2", "protobuf"]},
2323
zip_safe=False,

0 commit comments

Comments
 (0)