8000 Create script to generate s2clientprotocol protobuf stubs (#11772) · python/typeshed@b3bfbef · GitHub
[go: up one dir, main page]

Skip to content

Commit b3bfbef

Browse files
authored
Create script to generate s2clientprotocol protobuf stubs (#11772)
Regenerate using mypy-protobuf 3.6
1 parent 0deaca4 commit b3bfbef

File tree

13 files changed

+708
-509
lines changed

13 files changed

+708
-509
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
# Based on scripts/generate_proto_stubs.sh.
3+
# Generates the protobuf stubs for the given s2clientprotocol version using mypy-protobuf.
4+
# Generally, new minor versions are a good time to update the stubs.
5+
6+
set -euxo pipefail
7+
8+
# Whenever you update S2CLIENT_PROTO_VERSION here, version should be updated
9+
# in stubs/s2clientprotocol/METADATA.toml and vice-versa.
10+
S2CLIENT_PROTO_VERSION=c04df4adbe274858a4eb8417175ee32ad02fd609
11+
MYPY_PROTOBUF_VERSION=3.6.0
12+
13+
REPO_ROOT="$(realpath "$(dirname "${BASH_SOURCE[0]}")"/..)"
14+
TMP_DIR="$(mktemp -d)"
15+
S2CLIENT_PROTO_FILENAME="$S2CLIENT_PROTO_VERSION.zip"
16+
S2CLIENT_PROTO_URL="https://github.com/Blizzard/s2client-proto/archive/$S2CLIENT_PROTO_FILENAME"
17+
S2CLIENT_PROTO_DIR="s2client-proto-$S2CLIENT_PROTO_VERSION"
18+
19+
cd "$TMP_DIR"
20+
echo "Working in $TMP_DIR"
21+
22+
# Fetch s2clientprotocol (which contains all the .proto files)
23+
wget "$S2CLIENT_PROTO_URL"
24+
unzip "$S2CLIENT_PROTO_FILENAME"
25+
26+
# Prepare virtualenv
27+
python3 -m venv .venv
28+
source .venv/bin/activate
29+
python3 -m pip install pre-commit mypy-protobuf=="$MYPY_PROTOBUF_VERSION"
30+
31+
# Remove existing pyi
32+
find "$REPO_ROOT/stubs/s2clientprotocol/" -name "*_pb2.pyi" -delete
33+
34+
# s2client works on very old protoc versions, down to 2.6. So we can use the system's protoc.
35+
PROTOC_VERSION=$(protoc --version)
36+
echo $PROTOC_VERSION
37+
protoc \
38+
--proto_path="$S2CLIENT_PROTO_DIR" \
39+
--mypy_out "relax_strict_optional_primitives:$REPO_ROOT/stubs/s2clientprotocol" \
40+
$S2CLIENT_PROTO_DIR/s2clientprotocol/*.proto \
41+
42+
PYTHON_S2CLIENT_PROTO_VERSION=$(
43+
grep -Pzo 'def game_version\(\):\n return ".+?"' $S2CLIENT_PROTO_DIR/s2clientprotocol/build.py \
44+
| tr '\n' ' ' \
45+
| cut -d '"' -f 2
46+
)
47+
48+
# Cleanup after ourselves, this is a temp dir, but it can still grow fast if run multiple times
49+
rm -rf "$TMP_DIR"
50+
# Must be in a git repository to run pre-commit
51+
cd "$REPO_ROOT"
52+
53+
sed --in-place="" \
54+
"s/extra_description = .*$/extra_description = \"\
55+
Partially generated using [mypy-protobuf==$MYPY_PROTOBUF_VERSION](https:\/\/github.com\/nipunn1313\/mypy-protobuf\/tree\/v$MYPY_PROTOBUF_VERSION) \
56+
and $PROTOC_VERSION \
57+
on [s2client-proto $PYTHON_S2CLIENT_PROTO_VERSION](https:\/\/github.com\/Blizzard\/s2client-proto\/tree\/$S2CLIENT_PROTO_VERSION)\"/" \
58+
stubs/s2clientprotocol/METADATA.toml
59+
60+
# use `|| true` so the script still continues even if a pre-commit hook
61+
# applies autofixes (which will result in a nonzero exit code)
62+
pre-commit run --files $(git ls-files -- "stubs/s2clientprotocol/**_pb2.pyi") || true
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# All modules ending with *_pb2 fail to import
2-
# The error message is "TypeError: Descriptors cannot be create directly"
2+
# The error message is "TypeError: Descriptors cannot be created directly.
3+
# If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0."
34
s2clientprotocol\..+_pb2

stubs/s2clientprotocol/METADATA.toml

Copy file name to clipboard
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Whenever you update version here, S2CLIENT_PROTO_VERSION should be updated
2+
# in scripts/sync_s2clientprotocol_protobuf_stubs.sh and vice-versa.
13
version = "5.*"
24
upstream_repository = "https://github.com/Blizzard/s2client-proto"
35
requires = ["types-protobuf"]
6+
extra_description = "Partially generated using [mypy-protobuf==3.6.0](https://github.com/nipunn1313/mypy-protobuf/tree/v3.6.0) and libprotoc 3.6.1 on [s2client-proto 5.0.12.91115.0](https://github.com/Blizzard/s2client-proto/tree/c04df4adbe274858a4eb8417175ee32ad02fd609)"

stubs/s2clientprotocol/s2clientprotocol/common_pb2.pyi

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@generated by mypy-protobuf. Do not edit manually!
33
isort:skip_file
44
"""
5+
56
import builtins
67
import sys
78
import typing
@@ -38,7 +39,7 @@ Protoss: Race.ValueType # 3
3839
Random: Race.ValueType # 4
3940
global___Race = Race
4041

41-
@typing_extensions.final
42+
@typing.final
4243
class AvailableAbility(google.protobuf.message.Message):
4344
DESCRIPTOR: google.protobuf.descriptor.Descriptor
4445

@@ -52,12 +53,12 @@ class AvailableAbility(google.protobuf.message.Message):
5253
ability_id: builtins.int | None = ...,
5354
requires_point: builtins.bool | None = ...,
5455
) -> None: ...
55-
def HasField(self, field_name: typing_extensions.Literal["ability_id", b"ability_id", "requires_point", b"requires_point"]) -> builtins.bool: ...
56-
def ClearField(self, field_name: typing_extensions.Literal["ability_id", b"ability_id", "requires_point", b"requires_point"]) -> None: ...
56+
def HasField(self, field_name: typing.Literal["ability_id", b"ability_id", "requires_point", b"requires_point"]) -> builtins.bool: ...
57+
def ClearField(self, field_name: typing.Literal["ability_id", b"ability_id", "requires_point", b"requires_point"]) -> None: ...
5758

5859
global___AvailableAbility = AvailableAbility
5960

60-
@typing_extensions.final
61+
@typing.final
6162
class ImageData(google.protobuf.message.Message):
6263
DESCRIPTOR: google.protobuf.descriptor.Descriptor
6364

@@ -66,24 +67,25 @@ class ImageData(google.protobuf.message.Message):
6667
DATA_FIELD_NUMBER: builtins.int
6768
bits_per_pixel: builtins.int
6869
"""Number of bits per pixel; 8 bits for a byte etc."""
70+
data: builtins.bytes
71+
"""Binary data; the size of this buffer in bytes is width * height * bits_per_pixel / 8."""
6972
@property
7073
def size(self) -> global___Size2DI:
7174
"""Dimension in pixels."""
72-
data: builtins.bytes
73-
"""Binary data; the size of this buffer in bytes is width * height * bits_per_pixel / 8."""
75+
7476
def __init__(
7577
self,
7678
*,
7779
bits_per_pixel: builtins.int | None = ...,
7880
size: global___Size2DI | None = ...,
7981
data: builtins.bytes | None = ...,
8082
) -> None: ...
81-
def HasField(self, field_name: typing_extensions.Literal["bits_per_pixel", b"bits_per_pixel", "data", b"data", "size", b"size"]) -> builtins.bool: ...
82-
def ClearField(self, field_name: typing_extensions.Literal["bits_per_pixel", b"bits_per_pixel", "data", b"data", "size", b"size"]) -> None: ...
83+
def HasField(self, field_name: typing.Literal["bits_per_pixel", b"bits_per_pixel", "data", b"data", "size", b"size"]) -> builtins.bool: ...
84+
def ClearField(self, field_name: typing.Literal["bits_per_pixel", b"bits_per_pixel", "data", b"data", "size", b"size"]) -> None: ...
8385

8486
global___ImageData = ImageData
8587

86-
@typing_extensions.final
88+
@typing.final
8789
class PointI(google.protobuf.message.Message):
8890
"""Point on the screen/minimap (e.g., 0..64).
8991
Note: bottom left of the screen is 0, 0.
@@ -101,12 +103,12 @@ class PointI(google.protobuf.message.Message):
101103
x: builtins.int | None = ...,
102104
y: builtins.int | None = ...,
103105
) -> None: ...
104-
def HasField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y"]) -> builtins.bool: ...
105-
def ClearField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y"]) -> None: ...
106+
def HasField(self, field_name: typing.Literal["x", b"x", "y", b"y"]) -> builtins.bool: ...
107+
def ClearField(self, field_name: typing.Literal["x", b"x", "y", b"y"]) -> None: ...
106108

107109
global___PointI = PointI
108110

109-
@typing_extensions.final
111+
@typing.final
110112
class RectangleI(google.protobuf.message.Message):
111113
"""Screen space rectangular area."""
112114

@@ -124,12 +126,12 @@ class RectangleI(google.protobuf.message.Message):
124126
p0: global___PointI | None = ...,
125127
p1: global___PointI | None = ...,
126128
) -> None: ...
127-
def HasField(self, field_name: typing_extensions.Literal["p0", b"p0", "p1", b"p1"]) -> builtins.bool: ...
128-
def ClearField(self, field_name: typing_extensions.Literal["p0", b"p0", "p1", b"p1"]) -> None: ...
129+
def HasField(self, field_name: typing.Literal["p0", b"p0", "p1", b"p1"]) -> builtins.bool: ...
130+
def ClearField(self, field_name: typing.Literal["p0", b"p0", "p1", b"p1"]) -> None: ...
129131

130132
global___RectangleI = RectangleI
131133

132-
@typing_extensions.final
134+
@typing.final
133135
class Point2D(google.protobuf.message.Message):
134136
"""Point on the game board, 0..255.
135137
Note: bottom left of the screen is 0, 0.
@@ -147,12 +149,12 @@ class Point2D(google.protobuf.message.Message):
147149
x: builtins.float | None = ...,
148150
y: builtins.float | None = ...,
149151
) -> None: ...
150-
def HasField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y"]) -> builtins.bool: ...
151-
def ClearField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y"]) -> None: ...
152+
def HasField(self, field_name: typing.Literal["x", b"x", "y", b"y"]) -> builtins.bool: ...
153+
def ClearField(self, field_name: typing.Literal["x", b"x", "y", b"y"]) -> None: ...
152154

153155
global___Point2D = Point2D
154156

155-
@typing_extensions.final
157+
@typing.final
156158
class Point(google.protobuf.message.Message):
157159
"""Point on the game board, 0..255.
158160
Note: bottom left of the screen is 0, 0.
@@ -173,12 +175,12 @@ class Point(google.protobuf.message.Message):
173175
y: builtins.float | None = ...,
174176
z: builtins.float | None = ...,
175177
) -> None: ...
176-
def HasField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y", "z", b"z"]) -> builtins.bool: ...
177-
def ClearField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y", "z", b"z"]) -> None: ...
178+
def HasField(self, field_name: typing.Literal["x", b"x", "y", b"y", "z", b"z"]) -> builtins.bool: ...
179+
def ClearField(self, field_name: typing.Literal["x", b"x", "y", b"y", "z", b"z"]) -> None: ...
178180

179181
global___Point = Point
180182

181-
@typing_extensions.final
183+
@typing.final
182184
class Size2DI(google.protobuf.message.Message):
183185
"""Screen dimensions."""
184186

@@ -194,7 +196,7 @@ class Size2DI(google.protobuf.message.Message):
194196
x: builtins.int | None = ...,
195197
y: builtins.int | None = ...,
196198
) -> None: ...
197-
def HasField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y"]) -> builtins.bool: ...
198-
def ClearField(self, field_name: typing_extensions.Literal["x", b"x", "y", b"y"]) -> None: ...
199+
def HasField(self, field_name: typing.Literal["x", b"x", "y", b"y"]) -> builtins.bool: ...
200+
def ClearField(self, field_name: typing.Literal["x", b"x", "y", b"y"]) -> None: ...
199201

200202
global___Size2DI = Size2DI

0 commit comments

Comments
 (0)
0