Skip to content

Commit d934756

Browse files
Move documentation to MkDocs (#24)
* Mkdocs documentation * Remove doc pre-commit * Remove uesless dependencies * Add content in main page * Add github action * Add site_url * Remove TODEL * Add code coloration * Add quick start * Add migration guide * Add API documentation
1 parent ddb4e6e commit d934756

14 files changed

+843
-702
lines changed

.github/workflows/documentation.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: documentation
2+
on:
3+
push:
4+
branches:
5+
- main
6+
permissions:
7+
contents: write
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Configure Git Credentials
14+
run: |
15+
git config user.name github-actions[bot]
16+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
17+
- uses: actions/setup-python@v5
18+
with:
19+
python-version: 3.x
20+
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
21+
- uses: actions/cache@v4
22+
with:
23+
key: mkdocs-material-${{ env.cache_id }}
24+
path: .cache
25+
restore-keys: |
26+
mkdocs-material-
27+
- run: pip install mkdocs-material
28+
- run: mkdocs gh-deploy --force

.pre-commit-config.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,3 @@ repos:
99
args: ["--diff", "src", "tests"]
1010
- id: ruff
1111
args: ["src", "tests"]
12-
13-
- repo: https://github.com/PyCQA/doc8
14-
rev: 0.10.1
15-
hooks:
16-
- id: doc8
17-
additional_dependencies:
18-
- toml

.readthedocs.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
API reference
2+
=============
3+
4+
The following document outlines betterproto's api. These classes should not be extended manually.
5+
6+
7+
## Message
8+
9+
::: betterproto.Message
10+
11+
::: betterproto.which_one_of
12+
13+
14+
## Enumerations
15+
16+
::: betterproto.Enum
17+
18+
::: betterproto.Casing

docs/api.rst

Lines changed: 0 additions & 31 deletions
This file was deleted.

docs/conf.py

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
1-
Welcome to betterproto's documentation!
2-
=======================================
1+
Home
2+
====
3+
4+
Welcome to betterproto2's documentation!
35

46
betterproto is a protobuf compiler and interpreter. It improves the experience of using
57
Protobuf and gRPC in Python, by generating readable, understandable, and idiomatic
68
Python code, using modern language features.
79

8-
9-
Features:
10-
~~~~~~~~~
10+
## Features
1111

1212
- Generated messages are both binary & JSON serializable
13-
- Messages use relevant python types, e.g. ``Enum``, ``datetime`` and ``timedelta``
14-
objects
13+
- Messages use relevant python types, e.g. ``Enum``, ``datetime`` and ``timedelta`` objects
1514
- ``async``/``await`` support for gRPC Clients and Servers
1615
- Generates modern, readable, idiomatic python code
17-
18-
Contents:
19-
~~~~~~~~~
20-
21-
.. toctree::
22-
:maxdepth: 2
23-
24-
quick-start
25-
api
26-
migrating
27-
28-
29-
If you still can't find what you're looking for, try in one of the following pages:
30-
31-
* :ref:`genindex`
32-
* :ref:`modindex`
33-
* :ref:`search`

docs/migrating.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
Migration Guide
2+
===============
3+
4+
## Google's protocolbuffers
5+
6+
betterproto has a mostly 1 to 1 drop in replacement for Google's protocolbuffers (after
7+
regenerating your protobufs of course) although there are some minor differences.
8+
9+
!!! note
10+
11+
betterproto implements the same basic methods including:
12+
13+
- `betterproto.Message.FromString`
14+
- `betterproto.Message.SerializeToString`
15+
16+
for compatibility purposes, however it is important to note that these are
17+
effectively aliases for `betterproto.Message.parse` and
18+
`betterproto.Message.__bytes__` respectively.
19+
20+
21+
## One-of Support
22+
23+
Protobuf supports grouping fields in a oneof clause. Only one of the fields in the group
24+
may be set at a given time. For example, given the proto:
25+
26+
```proto
27+
syntax = "proto3";
28+
29+
message Test {
30+
oneof foo {
31+
bool on = 1;
32+
int32 count = 2;
33+
string name = 3;
34+
}
35+
}
36+
```
37+
38+
You can use `betterproto.which_one_of(message, group_name)` to determine which of the
39+
fields was set. It returns a tuple of the field name and value, or a blank string and
40+
`None` if unset. Again this is a little different than the official Google code
41+
generator:
42+
43+
```python
44+
# Old way (official Google protobuf package)
45+
>>> message.WhichOneof("group")
46+
"foo"
47+
48+
# New way (this project)
49+
>>> betterproto.which_one_of(message, "group")
50+
("foo", "foo's value")
51+
```
52+
53+
54+
## Well-Known Google Types
55+
56+
Google provides several well-known message types like a timestamp, duration, and several
57+
wrappers used to provide optional zero value support. Each of these has a special JSON
58+
representation and is handled a little differently from normal messages. The Python
59+
mapping for these is as follows:
60+
61+
| Google Message | Python Type | Default |
62+
|-------------------------------|------------------------------------------------|--------------------------|
63+
| `google.protobuf.duration` | `datetime.timedelta` | `0` |
64+
| `google.protobuf.timestamp` | Timezone-aware `datetime.datetime` | `1970-01-01T00:00:00Z` |
65+
| `google.protobuf.*Value` | `Optional[...]` / `None` | `None` |
66+
| `google.protobuf.*` | `betterproto.lib.std.google.protobuf.*` | `None` |
67+
| `google.protobuf.*` | `betterproto.lib.pydantic.google.protobuf.*` | `None` |
68+
69+
For the wrapper types, the Python type corresponds to the wrapped type, e.g.
70+
``google.protobuf.BoolValue`` becomes ``Optional[bool]`` while
71+
``google.protobuf.Int32Value`` becomes ``Optional[int]``. All of the optional values
72+
default to None, so don't forget to check for that possible state.
73+
74+
Given:
75+
76+
```proto
77+
syntax = "proto3";
78+
79+
import "google/protobuf/duration.proto";
80+
import "google/protobuf/timestamp.proto";
81+
import "google/protobuf/wrappers.proto";
82+
83+
message Test {
84+
google.protobuf.BoolValue maybe = 1;
85+
google.protobuf.Timestamp ts = 2;
86+
google.protobuf.Duration duration = 3;
87+
}
88+
```
89+
90+
You can use it as such:
91+
92+
```python
93+
>>> t = Test().from_dict({"maybe": True, "ts": "2019-01-01T12:00:00Z", "duration": "1.200s"})
94+
>>> t
95+
Test(maybe=True, ts=datetime.datetime(2019, 1, 1, 12, 0, tzinfo=datetime.timezone.utc), duration=datetime.timedelta(seconds=1, microseconds=200000))
96+
97+
>>> t.ts - t.duration
98+
datetime.datetime(2019, 1, 1, 11, 59, 58, 800000, tzinfo=datetime.timezone.utc)
99+
100+
>>> t.ts.isoformat()
101+
'2019-01-01T12:00:00+00:00'
102+
103+
>>> t.maybe = None
104+
>>> t.to_dict()
105+
{'ts': '2019-01-01T12:00:00Z', 'duration': '1.200s'}
106+
```
107+
108+
109+
## [1.2.5] to [2.0.0b1]
110+
111+
### Updated package structures
112+
113+
Generated code now strictly follows the *package structure* of the `.proto` files.
114+
Consequently `.proto` files without a package will be combined in a single
115+
`__init__.py` file. To avoid overwriting existing `__init__.py` files, its best
116+
to compile into a dedicated subdirectory.
117+
118+
Upgrading:
119+
120+
- Remove your previously compiled `.py` files.
121+
- Create a new *empty* directory, e.g. `generated` or `lib/generated/proto` etc.
122+
- Regenerate your python files into this directory
123+
- Update import statements, e.g. `import ExampleMessage from generated`

0 commit comments

Comments
 (0)