You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In many cases when we create function wrappers, we would like to have a single way to write the wrapper rather than handling both generator and regular modes with a if/else.
Maybe we could add a multi_mode_handler flag ? Setting it to True would require the handler to be written in a way that is independent from the wrapped mode (to be found... maybe a coroutine ?).
The text was updated successfully, but these errors were encountered:
I propose to add a mode argument instead, and to provide a helper companion as_generator function.
defcreate_wrapper(f):
# use the helper to always have a generatorgenf=as_generator(f)
@with_signature(f, mode=f)defmy_handler( *args, **kwargs):
# <init code>gen=genf(*args, **kwargs)
forresingen:
# <intermediate code>yieldres# or yield anything else# <termination code>returnmy_handler
This would do the trick.
mode=f indicates that the generated function has the same mode than f (generator if generator, etc.). In that case the handler has to be itself a generator (nothing else is supported).
as_generator(f) is used to turn f into a generator if it is not already the case
The code above would work if foo is a normal function or a generator:
deffoo(a):
return"hello"create_wrapper(foo) # should be a normal functiondeffoo_g(a):
yield"hello"yield"again"create_wrapper(foo) # should be a generator
In many cases when we create function wrappers, we would like to have a single way to write the wrapper rather than handling both generator and regular modes with a if/else.
A good illustration is provided below:
https://github.com/smarie/python-pytest-harvest/blob/master/pytest_harvest/fixture_cache.py#L193
Maybe we could add a
multi_mode_handler
flag ? Setting it toTrue
would require the handler to be written in a way that is independent from the wrapped mode (to be found... maybe a coroutine ?).The text was updated successfully, but these errors were encountered: