returns
returns copied to clipboard
`assert_trace` does not work correctly with functions that use the same decorator
Bug report
What's wrong
Our assert_trace from pytest plugin doesn't work correctly when two or more different functions are using the same decorator, it not considers that those functions are different! See the example below:
def test_two_functios_using_same_decorator(returns: ReturnsAsserts):
def decorator(func):
def decorated(*args, **kwargs):
return func(*args, **kwargs)
return decorated
@decorator
def func_one():
return Success(42)
@decorator
def func_two():
return Success(42)
with returns.assert_trace(Success, func_one):
func_two()
That test should fail because we want to assert the Success creation inside func_one, but I'm calling func_two and the test is passing!
How is that should be
Our pytest plugin should consider that those functions are different!
System information
pythonversion: 3.9returnsversion: 0.16.0
What if we use @wraps on def decorated? 🤔
Also doesn't work, I tested with safe too (safe uses wraps)
wraps just keep some metadata like name and docstring. But if we get the code from them they are the same
I know how to solve this issue! Reading the Python docs I discovered that wrapped functions has the __wrapped__ attribute which we can use to get the "original" function!
The users just need to pay attention to use the wraps decorator inside their decorators!
Docs: https://docs.python.org/3/library/functools.html#functools.update_wrapper
Let's add this to the docs :)
I made the fix and broke other part 😆
The thing is, fixing this (with the actual code ive made) we won't be able to handle creations within decorators cause we're getting the most inner function, @safe won't work for example:
@safe
def _safe_decorated_function(return_failure: bool = False):
if return_failure:
raise ValueError('Function failed')
def test_something():
with returns.assert_trace(container_type, _safe_decorated_function):
_safe_decorated_function(True)
Failure is being created inside safe and not _safe_decorated_function
Suggestions?
Suggestions?
No :(