Skip to content

Instantly share code, notes, and snippets.

@kopyl
Last active May 30, 2021 12:23
Show Gist options
  • Select an option

  • Save kopyl/d8196d28abdf4fef2304dff764e743b6 to your computer and use it in GitHub Desktop.

Select an option

Save kopyl/d8196d28abdf4fef2304dff764e743b6 to your computer and use it in GitHub Desktop.
from __future__ import annotations
def return_func(
all_value: Optional = None,
reminder: bool = True
) -> Callable:
"""
Initialize:
returns = return_func()
Optional keyword adrguments:
all_value: value to return on all instances of decorator
reminder: print reminder to remove decorator
Usage:
@returns("Value to return")
def some_function(...):
...
@returns() # if all_value specified in initilization
def some_function(...):
...
"""
def returns(value: Optional = None) -> Callable:
def function(fn: Callable) -> Callable:
def wrapper(*args, **kwargs) -> Any:
if reminder:
function_name = fn.__name__
decorator_line = fn.__code__.co_firstlineno
print(
"Don't forget to REMOVE decorator on function",
fn.__name__,
"on line",
decorator_line
)
if all_value:
return all_value
return value
return wrapper
return function
return returns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment