6
6
7
7
from .coder import Proto , proto_decode , proto_encode
8
8
9
- _ProtoTypes = Union [str , list , dict , bytes , int , float , bool , "ProtoStruct" ]
9
+ _ProtoBasicTypes = Union [str , list , dict , bytes , int , float , bool ]
10
+ _ProtoTypes = Union [_ProtoBasicTypes , "ProtoStruct" ]
10
11
11
12
T = TypeVar ("T" , str , list , dict , bytes , int , float , bool , "ProtoStruct" )
12
13
V = TypeVar ("V" )
13
14
NT : TypeAlias = Dict [int , Union [_ProtoTypes , "NT" ]]
14
15
AMT : TypeAlias = Dict [str , Tuple [Type [_ProtoTypes ], "ProtoField" ]]
15
16
DAMT : TypeAlias = Dict [str , "DelayAnnoType" ]
16
- DelayAnnoType = Union [str , type ( List [str ]) ]
17
+ DelayAnnoType = Union [str , List [str ]]
17
18
NoneType = type (None )
18
19
19
20
20
21
class ProtoField (Generic [T ]):
21
22
def __init__ (self , tag : int , default : T ):
22
23
if tag <= 0 :
23
24
raise ValueError ("Tag must be a positive integer" )
24
- self ._tag = tag
25
- self ._default = default
25
+ self ._tag : int = tag
26
+ self ._default : T = default
26
27
27
28
@property
28
29
def tag (self ) -> int :
@@ -91,11 +92,11 @@ class ProtoStruct:
91
92
92
93
def __init__ (self , * args , ** kwargs ):
93
94
undefined_params : List [str ] = []
94
- args = list (args )
95
+ args_list = list (args )
95
96
self ._resolve_annotations (self )
96
97
for name , (typ , field ) in self ._anno_map .items ():
97
98
if args :
98
- self ._set_attr (name , typ , args .pop (0 ))
99
+ self ._set_attr (name , typ , args_list .pop (0 ))
99
100
elif name in kwargs :
100
101
self ._set_attr (name , typ , kwargs .pop (name ))
101
102
else :
@@ -104,9 +105,7 @@ def __init__(self, *args, **kwargs):
104
105
else :
105
106
undefined_params .append (name )
106
107
if undefined_params :
107
- raise AttributeError (
108
- "Undefined parameters in '{}': {}" .format (self , undefined_params )
109
- )
108
+ raise AttributeError (f"Undefined parameters in { self } : { undefined_params } " )
110
109
111
110
def __init_subclass__ (cls , ** kwargs ):
112
111
cls ._proto_debug = kwargs .pop ("debug" ) if "debug" in kwargs else False
@@ -125,9 +124,7 @@ def _set_attr(self, name: str, data_typ: Type[V], value: V) -> None:
125
124
if isinstance (data_typ , GenericAlias ): # force ignore
126
125
pass
127
126
elif not isinstance (value , data_typ ) and value is not None :
128
- raise TypeError (
129
- "'{}' is not a instance of type '{}'" .format (value , data_typ )
130
- )
127
+ raise TypeError ("{value} is not a instance of type {data_typ}" )
131
128
setattr (self , name , value )
132
129
133
130
@classmethod
@@ -191,13 +188,13 @@ def _get_stored_mapping(self) -> Dict[str, NT]:
191
188
def _resolve_annotations (arg : Union [Type ["ProtoStruct" ], "ProtoStruct" ]) -> None :
192
189
for k , v in arg ._delay_anno_map .copy ().items ():
193
190
module = importlib .import_module (arg .__module__ )
194
- if hasattr (v , "__origin__" ): # resolve GenericAlias, such as list[str]
195
- arg ._anno_map [k ] = (v .__origin__ [module . __getattribute__ ( v .__args__ [0 ])], arg ._anno_map [k ][1 ])
196
- else :
197
- arg ._anno_map [k ] = (module . __getattribute__ ( v ), arg ._anno_map [k ][1 ])
191
+ if isinstance (v , GenericAlias ): # resolve GenericAlias, such as list[str]
192
+ arg ._anno_map [k ] = (v .__origin__ [getattr ( module , v .__args__ [0 ])], arg ._anno_map [k ][1 ])
193
+ if isinstance ( v , str ) :
194
+ arg ._anno_map [k ] = (getattr ( module , v ), arg ._anno_map [k ][1 ])
198
195
arg ._delay_anno_map .pop (k )
199
196
200
- def _encode (self , v : _ProtoTypes ) -> NT :
197
+ def _encode (self , v : _ProtoTypes ) -> _ProtoBasicTypes :
201
198
if isinstance (v , ProtoStruct ):
202
199
v = v .encode ()
203
200
return v
@@ -266,4 +263,3 @@ def decode(cls, data: bytes) -> Self:
266
263
267
264
return cls (** kwargs )
268
265
269
-
0 commit comments