Skip to content

Commit 22bb6ab

Browse files
author
esblinov
committed
add checks for extra return statements
1 parent 2390b16 commit 22bb6ab

2 files changed

Lines changed: 9 additions & 17 deletions

File tree

transfunctions/decorators/superfunction.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,18 @@ async def async_option(flags: Dict[str, bool], args, kwargs, transformer) -> Non
7878

7979
def superfunction(*args: Callable, tilde_syntax: bool = True):
8080
def decorator(function):
81-
class NoReturns(NodeTransformer):
82-
def visit_Return(self, node: Return) -> Optional[Union[AST, List[AST]]]:
83-
raise WrongTransfunctionSyntaxError('A superfunction cannot contain a return statement.')
84-
8581
transformer = FunctionTransformer(
8682
function,
8783
currentframe().f_back.f_lineno,
8884
'superfunction',
89-
extra_transformers=[
90-
#NoReturns(),
91-
],
9285
)
9386

87+
if not tilde_syntax:
88+
class NoReturns(NodeTransformer):
89+
def visit_Return(self, node: Return) -> Optional[Union[AST, List[AST]]]:
90+
raise WrongTransfunctionSyntaxError('A superfunction cannot contain a return statement.')
91+
transformer.get_usual_function(addictional_transformers=[NoReturns()])
92+
9493
@wraps(function)
9594
def wrapper(*args, **kwargs):
9695
return UsageTracer(args, kwargs, transformer, tilde_syntax)

transfunctions/transformer.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class FunctionTransformer:
15-
def __init__(self, function: Callable, decorator_lineno: int, decorator_name: str, extra_transformers: Optional[List[NodeTransformer]] = None) -> None:
15+
def __init__(self, function: Callable, decorator_lineno: int, decorator_name: str) -> None:
1616
if isinstance(function, type(self)):
1717
raise DualUseOfDecoratorError(f"You cannot use the '{decorator_name}' decorator twice for the same function.")
1818
if not isfunction(function):
@@ -25,7 +25,6 @@ def __init__(self, function: Callable, decorator_lineno: int, decorator_name: st
2525
self.function = function
2626
self.decorator_lineno = decorator_lineno
2727
self.decorator_name = decorator_name
28-
self.extra_transformers = extra_transformers
2928
self.base_object = None
3029
self.cache: Dict[str, Callable] = {}
3130

@@ -42,8 +41,8 @@ def is_lambda(function: Callable) -> bool:
4241
lambda_example = lambda: 0 # noqa: E731
4342
return isinstance(function, type(lambda_example)) and function.__name__ == lambda_example.__name__
4443

45-
def get_usual_function(self):
46-
return self.extract_context('sync_context')
44+
def get_usual_function(self, addictional_transformers: Optional[List[NodeTransformer]] = None):
45+
return self.extract_context('sync_context', addictional_transformers=addictional_transformers)
4746

4847
def get_async_function(self):
4948
original_function = self.function
@@ -106,12 +105,6 @@ def extract_context(self, context_name: str, addictional_transformers: Optional[
106105
except OSError:
107106
source_code = dill_getsource(self.function)
108107

109-
if addictional_transformers is None:
110-
addictional_transformers = self.extra_transformers
111-
else:
112-
if self.extra_transformers is not None:
113-
addictional_transformers = addictional_transformers + self.extra_transformers
114-
115108
converted_source_code = self.clear_spaces_from_source_code(source_code)
116109
tree = parse(converted_source_code)
117110
original_function = self.function

0 commit comments

Comments
 (0)