77 TypeVar ,
88)
99
10+ from addon_toolkit .typing import DataclassInstance
11+
1012
1113DecoratorTarget = TypeVar ("DecoratorTarget" )
12- DeclarationDataclass = TypeVar ("DeclarationDataclass" )
1314
1415
1516@dataclasses .dataclass
16- class Declarator (Generic [DeclarationDataclass ]):
17+ class Declarator (Generic [DataclassInstance ]):
1718 """Declarator: add declarative metadata in python using decorators and dataclasses
1819
1920 define a dataclass with fields you want declared in your decorator, plus a field
@@ -48,15 +49,15 @@ class Declarator(Generic[DeclarationDataclass]):
4849 TwoPartGreetingDeclaration(a='kia', b='ora', on=<function _kia_ora at 0x...>)
4950 """
5051
51- declaration_dataclass : type [DeclarationDataclass ]
52+ declaration_dataclass : type [DataclassInstance ]
5253 field_for_target : str
5354 static_kwargs : dict [str , Any ] | None = None
5455
5556 # private storage linking a decorated class or function to data gleaned from its decorator
56- __declarations_by_target : weakref .WeakKeyDictionary [
57- object , DeclarationDataclass
58- ] = dataclasses . field (
59- default_factory = weakref . WeakKeyDictionary ,
57+ __declarations_by_target : weakref .WeakKeyDictionary [object , DataclassInstance ] = (
58+ dataclasses . field (
59+ default_factory = weakref . WeakKeyDictionary ,
60+ )
6061 )
6162
6263 def __post_init__ (self ) -> None :
@@ -69,7 +70,7 @@ def __post_init__(self) -> None:
6970 ), f'expected field "{ self .field_for_target } " on dataclass "{ self .declaration_dataclass } "'
7071
7172 def __call__ (
72- self , ** declaration_dataclass_kwargs
73+ self , ** declaration_dataclass_kwargs : Any
7374 ) -> Callable [[DecoratorTarget ], DecoratorTarget ]:
7475 """for using a Declarator as a decorator"""
7576
@@ -79,13 +80,13 @@ def _decorator(decorator_target: DecoratorTarget) -> DecoratorTarget:
7980
8081 return _decorator
8182
82- def with_kwargs (self , ** static_kwargs ) -> "Declarator[DeclarationDataclass ]" :
83+ def with_kwargs (self , ** static_kwargs : Any ) -> "Declarator[DataclassInstance ]" :
8384 """convenience for decorators that differ only by static field values"""
8485 # note: shared __declarations_by_target
8586 return dataclasses .replace (self , static_kwargs = static_kwargs )
8687
8788 def set_declaration (
88- self , declaration_target : DecoratorTarget , ** declaration_dataclass_kwargs
89+ self , declaration_target : DecoratorTarget , ** declaration_dataclass_kwargs : Any
8990 ) -> None :
9091 """create a declaration associated with the target
9192
@@ -98,14 +99,14 @@ def set_declaration(
9899 ** {self .field_for_target : declaration_target },
99100 )
100101
101- def get_declaration (self , target ) -> DeclarationDataclass :
102+ def get_declaration (self , target : DecoratorTarget ) -> DataclassInstance :
102103 try :
103104 return self .__declarations_by_target [target ]
104105 except KeyError :
105106 raise ValueError (f"no declaration found for { target } " )
106107
107108
108- class ClassDeclarator (Declarator [DeclarationDataclass ]):
109+ class ClassDeclarator (Declarator [DataclassInstance ]):
109110 """add declarative metadata to python classes using decorators
110111
111112 (same as Declarator but with additional methods that only make
@@ -157,13 +158,15 @@ class ClassDeclarator(Declarator[DeclarationDataclass]):
157158 SemanticVersionDeclaration(major=4, minor=2, patch=9, subj=<class 'addon_toolkit.declarator.MyLongLivedBaseClass'>)
158159 """
159160
160- def get_declaration_for_class_or_instance (self , type_or_object : type | object ):
161+ def get_declaration_for_class_or_instance (
162+ self , type_or_object : type | object
163+ ) -> DataclassInstance :
161164 _cls = (
162165 type_or_object if isinstance (type_or_object , type ) else type (type_or_object )
163166 )
164167 return self .get_declaration_for_class (_cls )
165168
166- def get_declaration_for_class (self , cls : type ):
169+ def get_declaration_for_class (self , cls : type ) -> DataclassInstance :
167170 for _cls in cls .__mro__ :
168171 try :
169172 return self .get_declaration (_cls )
0 commit comments