Skip to content

Commit 754f34a

Browse files
Generate py.typed files (#131)
* Fix typing issues * Add py.typed file * Cancel dependencies update
1 parent 8dccaea commit 754f34a

6 files changed

Lines changed: 30 additions & 21 deletions

File tree

betterproto2/src/betterproto2/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
from __future__ import annotations
22

3-
__all__ = ["__version__", "check_compiler_version", "classproperty", "unwrap", "MessagePool", "validators"]
3+
__all__ = [
4+
"__version__",
5+
"check_compiler_version",
6+
"classproperty",
7+
"staticproperty",
8+
"unwrap",
9+
"MessagePool",
10+
"validators",
11+
]
412

513
import dataclasses
614
import enum as builtin_enum
@@ -36,7 +44,7 @@
3644
from .casing import camel_case, safe_snake_case, snake_case
3745
from .enum_ import Enum as Enum
3846
from .grpc.grpclib_client import ServiceStub as ServiceStub
39-
from .utils import classproperty
47+
from .utils import classproperty, staticproperty
4048

4149
if TYPE_CHECKING:
4250
from _typeshed import SupportsRead, SupportsWrite
@@ -1015,7 +1023,7 @@ def parse(cls, data: bytes) -> Self:
10151023

10161024
# For compatibility with other libraries.
10171025
@classmethod
1018-
def FromString(cls: type[T], data: bytes) -> T:
1026+
def FromString(cls: type[T], s: bytes) -> T:
10191027
"""
10201028
Parse the binary encoded Protobuf into this message instance. This
10211029
returns the instance itself and is therefore assignable and chainable.
@@ -1035,7 +1043,7 @@ def FromString(cls: type[T], data: bytes) -> T:
10351043
:class:`Message`
10361044
The initialized message.
10371045
"""
1038-
return cls.parse(data)
1046+
return cls.parse(s)
10391047

10401048
def to_dict(
10411049
self,

betterproto2/src/betterproto2/_types.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from typing import (
2-
TYPE_CHECKING,
3-
TypeVar,
4-
)
1+
from typing import TYPE_CHECKING, TypeVar
52

63
if TYPE_CHECKING:
74
from grpclib._typing import IProtoMessage # type: ignore[reportPrivateImportUsage]

betterproto2/src/betterproto2/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def __get__(self, instance: Any, type: TT_co) -> T_co:
1818
T = TypeVar("T")
1919

2020

21+
class staticproperty(Generic[T]): # Should be applied after @staticmethod
22+
def __init__(self, fget: Callable[[], T]) -> None:
23+
self.fget = fget
24+
25+
def __get__(self, instance: Any, owner: type[Any]) -> T:
26+
return self.fget()
27+
28+
2129
def unwrap(x: T | None) -> T:
2230
"""
2331
Unwraps an optional value, returning the value if it exists, or raises a ValueError if the value is None.

betterproto2_compiler/pyproject.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ test = [
4646
package = true
4747
default-groups = "all"
4848

49-
# [tool.hatch.build.targets.sdist]
50-
# include = ["src/betterproto2_compiler"]
51-
52-
# [tool.hatch.build.targets.wheel]
53-
# include = ["src/betterproto2_compiler"]
54-
55-
# [tool.hatch.build.targets.wheel.sources]
56-
# "src/betterproto2_compiler" = "betterproto2_compiler"
57-
5849
[build-system]
5950
requires = ["hatchling"]
6051
build-backend = "hatchling.build"

betterproto2_compiler/src/betterproto2_compiler/plugin/parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse:
171171
)
172172
)
173173

174+
response.file.append(CodeGeneratorResponseFile(name="py.typed", content=""))
175+
174176
if settings.google_protobuf_descriptors:
175177
response.file.append(
176178
CodeGeneratorResponseFile(

betterproto2_compiler/src/betterproto2_compiler/templates/template.py.j2

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class {{ enum.py_name | add_to_all }}(betterproto2.Enum):
88

99
{% if output_file.settings.google_protobuf_descriptors %}
1010
{# Add descriptor class property to be more drop-in compatible with other libraries. #}
11-
@betterproto2.classproperty
12-
def DESCRIPTOR(self) -> EnumDescriptor:
11+
@betterproto2.staticproperty
12+
@staticmethod
13+
def DESCRIPTOR() -> EnumDescriptor:
1314
return {{ enum.descriptor_name }}.enum_types_by_name['{{ enum.prefixed_proto_name }}']
1415
{% endif %}
1516

@@ -76,8 +77,9 @@ class {{ message.py_name | add_to_all }}(betterproto2.Message):
7677

7778
{% if output_file.settings.google_protobuf_descriptors %}
7879
{# Add descriptor class property to be more drop-in compatible with other libraries. #}
79-
@betterproto2.classproperty
80-
def DESCRIPTOR(self) -> Descriptor:
80+
@betterproto2.staticproperty
81+
@staticmethod
82+
def DESCRIPTOR() -> Descriptor:
8183
return {{ message.descriptor_name }}.message_types_by_name['{{ message.prefixed_proto_name }}']
8284
{% endif %}
8385

@@ -179,6 +181,7 @@ class {{ (service.py_name + "Base") | add_to_all }}(ServiceBase):
179181
async def __rpc_{{ method.py_name }}(self, stream: "grpclib.server.Stream[{{ method.py_input_message_type }}, {{ method.py_output_message_type }}]") -> None:
180182
{% if not method.client_streaming %}
181183
request = await stream.recv_message()
184+
assert request is not None
182185
{% else %}
183186
request = stream.__aiter__()
184187
{% endif %}

0 commit comments

Comments
 (0)