Skip to content

Commit 9e63c66

Browse files
committed
Fix typing issues
1 parent 8dccaea commit 9e63c66

6 files changed

Lines changed: 554 additions & 500 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.

0 commit comments

Comments
 (0)