77 Call ,
88 Constant ,
99 FunctionDef ,
10+ Module ,
1011 Load ,
1112 Name ,
1213 NodeTransformer ,
2324from inspect import getfile , getsource , iscoroutinefunction , isfunction
2425from sys import version_info
2526from types import FunctionType , MethodType , FrameType
26- from typing import Any , Dict , Generic , List , Optional , Union , cast
27+ from typing import Any , Dict , Generic , List , Optional , Union , Type , cast
2728
2829from dill .source import getsource as dill_getsource # type: ignore[import-untyped]
2930
3435 WrongDecoratorSyntaxError ,
3536 WrongMarkerSyntaxError ,
3637)
37- from transfunctions .typing import Coroutine , Callable , Generator , FunctionParams , ReturnType
38+ from transfunctions .typing import Coroutine , Callable , Generator , FunctionParams , ReturnType , SomeClassInstance
3839from transfunctions .universal_namespace import UniversalNamespaceAroundFunction
3940
4041
@@ -55,13 +56,17 @@ def __init__(
5556 self .decorator_lineno = decorator_lineno
5657 self .decorator_name = decorator_name
5758 self .frame = frame
58- self .base_object = None
59+ self .base_object : Optional [ SomeClassInstance ] = None # type: ignore[valid-type]
5960 self .cache : Dict [str , Callable ] = {}
6061
6162 def __call__ (self , * args : Any , ** kwargs : Any ) -> None :
6263 raise CallTransfunctionDirectlyError ("You can't call a transfunction object directly, create a function, a generator function or a coroutine function from it." )
6364
64- def __get__ (self , base_object , type = None ):
65+ def __get__ (
66+ self ,
67+ base_object : SomeClassInstance ,
68+ owner : Type [SomeClassInstance ],
69+ ) -> 'FunctionTransformer[FunctionParams, ReturnType]' :
6570 self .base_object = base_object
6671 return self
6772
@@ -253,7 +258,7 @@ def visit_FunctionDef(self, node: FunctionDef) -> Optional[Union[AST, List[AST]]
253258
254259 return result
255260
256- def wrap_ast_by_closures (self , tree ) :
261+ def wrap_ast_by_closures (self , tree : Module ) -> Module :
257262 old_functiondef = tree .body [0 ]
258263
259264 tree .body [0 ] = FunctionDef (
@@ -276,7 +281,7 @@ def wrap_ast_by_closures(self, tree):
276281 return tree
277282
278283
279- def rewrite_globals_and_closure (self , function ) :
284+ def rewrite_globals_and_closure (self , function : FunctionType ) -> FunctionType :
280285 # https://stackoverflow.com/a/13503277/14522393
281286 all_new_closure_names = set (self .function .__code__ .co_freevars )
282287
@@ -297,6 +302,6 @@ def rewrite_globals_and_closure(self, function):
297302 closure = filtered_closure ,
298303 )
299304
300- new_function = update_wrapper (new_function , function )
305+ new_function = cast ( FunctionType , update_wrapper (new_function , function ) )
301306 new_function .__kwdefaults__ = function .__kwdefaults__
302307 return new_function
0 commit comments