python-decorators

Using custom decorators with Slack Bolt Python SDK

Using custom decorators with Slack Bolt Python SDK Question: Is it possible to use my own python decorators with the Bolt SDK? I tried the code below, and it’s not working: @it_platform @app.command("/help") def action_help(): # * Log to Cloudwatch logging.info("The user triggered the command /help") I’m getting the error Unhandled request ({‘type’: None, ‘command’: …

Total answers: 1

Use decorator to add an argument to the decorated function

Use decorator to add an argument to the decorated function Question: I have a function foo that logs some information during its execution: import logging logging.basicConfig() def foo(x): for i in range(10): logging.info(f"This is iteration number {i}.") x += i return x Question: Is it possible to create a decorator log_or_not so that I can …

Total answers: 2

Capturing a function call as dict

Capturing a function call as dict Question: I’m trying to write a python function decorator, and part of my implementation is that I need to capture the function call and look through the supplied values. I already have the function signature by using inspect.signature, but I’m unsure how to compose it with passed arguments. Say …

Total answers: 1

How can I provide a non-fixture pytest parameter via a custom decorator?

How can I provide a non-fixture pytest parameter via a custom decorator? Question: We have unit tests running via Pytest, which use a custom decorator to start up a context-managed mock echo server before each test, and provide its address to the test as an extra parameter. This works on Python 2. However, if we …

Total answers: 3

Why does the decorator return the type None?

Why does the decorator return the type None? Question: Help me understand why this decorator does not add a value to the end of the list that is formed in the do_list () function. I get the result of the function working with the decorator None def dec_do_list(func): def wrapper(arg: int): result = func(arg).append(4) return …

Total answers: 1

mypy: Untyped decorator makes function "my_method" untyped

mypy: Untyped decorator makes function "my_method" untyped Question: When I try using a decorator that I defined in another package, mypy fails with the error message Untyped decorator makes function "my_method" untyped. How should I define my decorator to make sure this passes? from mypackage import mydecorator @mydecorator def my_method(date: int) -> str: … Asked …

Total answers: 1

python decorator wraps twice?

python decorator wraps twice? Question: Encounter a strange problem with python decorator by accident, basically when I directly call decorator function, the original function being wrapped got wrapped twice. Some simple code to show the problem: def my_deco(f): def wrapper(*args): print(‘some code before’) f(*args) print(‘some code after’) return wrapper @my_deco def original_func(text): print (f’original func …

Total answers: 2

How to add a custom decorator to a FastAPI route?

How to add a custom decorator to a FastAPI route? Question: I want to add an auth_required decorator to my endpoints. (Please consider that this question is about decorators, not middleware) So a simple decorator looks like this: def auth_required(func): def wrapper(*args, **kwargs): if user_ctx.get() is None: raise HTTPException(…) return func(*args, **kwargs) return wrapper So …

Total answers: 4

Context manager as a decorator with access to the yielded object

Context manager as a decorator with access to the yielded object Question: I have a context manager for an object that can be used similar to the open context manager, e.g. with MyContextManager as cm: cm.do_something() I know that a simple context manager can be made into a decorator if using contextlib.ContextDecorator to create it. …

Total answers: 2

How a decorator can register a function without a formal call?

How a decorator can register a function without a formal call? Question: I am looking at one of the examples from here, in particular at the following one: import random PLUGINS = dict() def register(func): “””Register a function as a plug-in””” PLUGINS[func.__name__] = func return func @register def say_hello(name): return f”Hello {name}” @register def be_awesome(name): …

Total answers: 2