-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
Message.from_dict()
as a class and an instance method (#476)
* Make Message.from_dict() a class method Signed-off-by: Marek Pikuła <[email protected]> * Sync 1/2 of review comments * Sync other half * Update .pre-commit-config.yaml * Update __init__.py * Update utils.py * Update src/betterproto/__init__.py * Update .pre-commit-config.yaml * Update __init__.py * Update utils.py * Fix CI again * Fix failing formatting --------- Signed-off-by: Marek Pikuła <[email protected]> Co-authored-by: James Hilton-Balfe <[email protected]>
- Loading branch information
1 parent
02aa4e8
commit d9b7608
Showing
2 changed files
with
164 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from __future__ import annotations | ||
|
||
from typing import ( | ||
Any, | ||
Callable, | ||
Generic, | ||
Optional, | ||
Type, | ||
TypeVar, | ||
) | ||
|
||
from typing_extensions import ( | ||
Concatenate, | ||
ParamSpec, | ||
Self, | ||
) | ||
|
||
|
||
SelfT = TypeVar("SelfT") | ||
P = ParamSpec("P") | ||
HybridT = TypeVar("HybridT", covariant=True) | ||
|
||
|
||
class hybridmethod(Generic[SelfT, P, HybridT]): | ||
def __init__( | ||
self, | ||
func: Callable[ | ||
Concatenate[type[SelfT], P], HybridT | ||
], # Must be the classmethod version | ||
): | ||
self.cls_func = func | ||
self.__doc__ = func.__doc__ | ||
|
||
def instancemethod(self, func: Callable[Concatenate[SelfT, P], HybridT]) -> Self: | ||
self.instance_func = func | ||
return self | ||
|
||
def __get__( | ||
self, instance: Optional[SelfT], owner: Type[SelfT] | ||
) -> Callable[P, HybridT]: | ||
if instance is None or self.instance_func is None: | ||
# either bound to the class, or no instance method available | ||
return self.cls_func.__get__(owner, None) | ||
return self.instance_func.__get__(instance, owner) | ||
|
||
|
||
T_co = TypeVar("T_co") | ||
TT_co = TypeVar("TT_co", bound="type[Any]") | ||
|
||
|
||
class classproperty(Generic[TT_co, T_co]): | ||
def __init__(self, func: Callable[[TT_co], T_co]): | ||
self.__func__ = func | ||
|
||
def __get__(self, instance: Any, type: TT_co) -> T_co: | ||
return self.__func__(type) |