Skip to content

Commit 4ee6dff

Browse files
author
esblinov
committed
some typing shit
1 parent 3405dba commit 4ee6dff

5 files changed

Lines changed: 29 additions & 15 deletions

File tree

transfunctions/decorators/superfunction.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from ast import AST, NodeTransformer, Return
33
from functools import wraps
44
from inspect import currentframe
5-
from typing import Any, Dict, Generic, List, Optional, Union, overload
5+
from typing import Any, Dict, Generic, List, Optional, Union, Type, overload, cast
6+
from types import FrameType, TracebackType
67

78
from displayhooks import not_display
89

@@ -60,7 +61,7 @@ def __invert__(self) -> ReturnType:
6061
def send(self, value: Any) -> Any:
6162
return self.coroutine.send(value)
6263

63-
def throw(self, exception_type: Any, value: Any = None, traceback: Any = None) -> None: # pragma: no cover
64+
def throw(self, exception_type: Type[BaseException], value: Any = None, traceback: Optional[TracebackType] = None) -> None: # pragma: no cover
6465
pass
6566

6667
def close(self) -> None: # pragma: no cover
@@ -111,9 +112,9 @@ def superfunction( # type: ignore[misc]
111112
def decorator(function: Callable[FunctionParams, ReturnType]) -> Callable[FunctionParams, UsageTracer[FunctionParams, ReturnType]]:
112113
transformer = FunctionTransformer(
113114
function,
114-
currentframe().f_back.f_lineno, # type: ignore[union-attr]
115+
cast(FrameType, cast(FrameType, currentframe()).f_back).f_lineno,
115116
"superfunction",
116-
currentframe().f_back,
117+
cast(FrameType, cast(FrameType, currentframe()).f_back),
117118
)
118119

119120
if not tilde_syntax:
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from inspect import currentframe
2+
from typing import cast
3+
from types import FrameType
24

35
from transfunctions.transformer import FunctionTransformer
46
from transfunctions.typing import Callable, FunctionParams, ReturnType
@@ -7,4 +9,9 @@
79
def transfunction(
810
function: Callable[FunctionParams, ReturnType],
911
) -> FunctionTransformer[FunctionParams, ReturnType]:
10-
return FunctionTransformer(function, currentframe().f_back.f_lineno, "transfunction", currentframe().f_back) # type: ignore[union-attr]
12+
return FunctionTransformer(
13+
function,
14+
cast(FrameType, cast(FrameType, currentframe()).f_back).f_lineno,
15+
"transfunction",
16+
cast(FrameType, cast(FrameType, currentframe()).f_back),
17+
) # type: ignore[union-attr]

transfunctions/transformer.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Call,
88
Constant,
99
FunctionDef,
10+
Module,
1011
Load,
1112
Name,
1213
NodeTransformer,
@@ -23,7 +24,7 @@
2324
from inspect import getfile, getsource, iscoroutinefunction, isfunction
2425
from sys import version_info
2526
from 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

2829
from dill.source import getsource as dill_getsource # type: ignore[import-untyped]
2930

@@ -34,7 +35,7 @@
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
3839
from 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

transfunctions/typing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020

2121
ReturnType = TypeVar('ReturnType')
2222
FunctionParams = ParamSpec('FunctionParams')
23+
SomeClassInstance = TypeVar('SomeClassInstance')
2324

2425
if sys.version_info >= (3, 9):
2526
IterableWithResults = Iterable[ReturnType]
2627
else:
2728
IterableWithResults = Iterable
2829

29-
__all__ = ('ParamSpec', 'TypeAlias', 'Callable', 'Coroutine', 'Generator', 'ReturnType', 'FunctionParams', 'IterableWithResults')
30+
__all__ = ('ParamSpec', 'TypeAlias', 'Callable', 'Coroutine', 'Generator', 'ReturnType', 'FunctionParams', 'IterableWithResults', 'SomeClassInstance')

transfunctions/universal_namespace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Any
1+
from typing import Dict, Optional, Any
22
from types import FrameType
33
import builtins
44

@@ -7,7 +7,7 @@ class Nothing:
77
pass
88

99
class UniversalNamespaceAroundFunction(dict):
10-
def __init__(self, function, frame: FrameType) -> None:
10+
def __init__(self, function, frame: Optional[FrameType]) -> None:
1111
self.function = function
1212
self.frame = frame
1313
self.results: Dict[str, Any] = {}

0 commit comments

Comments
 (0)