3737class PackData :
3838 attr_name : str
3939 offset : int
40- type_descriptor : Packable
40+ type_descriptor : Type [ Packable ]
4141 optional : bool
4242
4343
@@ -54,27 +54,30 @@ class Packer(Packable):
5454 _packing_data : list [PackData ]
5555
5656 def __new__ (cls , * args , ** kwargs ) -> Self :
57+ assert cls .__base__ is not None , "Base class cannot be None"
58+
5759 instance = super ().__new__ (cls )
5860
5961 if getattr (cls , "_packing_data" , None ):
6062 return instance
6163
6264 cls ._packing_data = []
63- type_hints : list [ItemsView [str , Any ]]
65+ type_hints : list [tuple [str , Any ]]
6466
6567 try :
66- type_hints = get_type_hints (cls )
67- if not type_hints :
68+ _type_hints = get_type_hints (cls )
69+ if not _type_hints :
6870 raise PackerInvalidTypeHints (cls .__base__ )
69- type_hints = list (type_hints .items ())
71+ type_hints = list (_type_hints .items ())
7072 except TypeError :
7173 raise PackerInvalidTypeHints (cls .__base__ )
7274
7375 offset = 0
7476 last_origin = None
7577
7678 for i in type_hints :
77- attr , type_hint = i
79+ attr = i [0 ]
80+ type_hint = i [1 ]
7881
7982 origin = get_origin (type_hint )
8083 inner_types = get_args (type_hint )
@@ -98,13 +101,16 @@ def __new__(cls, *args, **kwargs) -> Self:
98101 offset += inner_type ._size
99102 last_origin = origin
100103
101- cls .pack , cls .unpack = create_pack_pair (cls .__base__ , cls ._packing_data )
102- cls ._size = offset
104+ pack_pair = create_pack_pair (cls .__base__ , cls ._packing_data )
105+
106+ setattr (cls , "pack" , pack_pair [0 ])
107+ setattr (cls , "unpack" , pack_pair [1 ])
108+ setattr (cls , "_size" , offset )
103109
104110 return instance
105111
106- def pack (self ) -> bytearray : ...
107- def unpack (self , _ : bytearray ) -> None : ...
112+ def pack (self ) -> bytearray : ... # type: ignore
113+ def unpack (self , _ : bytearray ) -> bool : ... # type: ignore
108114
109115
110116T = TypeVar ("T" )
0 commit comments