Skip to content

Commit 5bf697a

Browse files
ftruzzisobolevn
andauthored
make assert_trace work with @safe (#862) (#863)
* make assert_trace work with `@safe` (#862) * suppress flake8 violation * Some style fixes * make _DesiredFunctionFound a subclass of BaseException * fix flake8 failures Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 82dbf5d commit 5bf697a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See [0Ver](https://0ver.org/).
1313
- Makes `_Nothing` a singleton
1414
- Fixes typos in documentation
1515
- Refactor `flow` function
16+
- Fixes that `assert_trace` was not catching containers from `@safe`-wrapped functions
1617

1718
## 0.15.0 aka The initial HKT release
1819

returns/contrib/pytest/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,5 @@ def factory(self, *args, **kwargs):
208208
return wraps(original)(factory)
209209

210210

211-
class _DesiredFunctionFound(RuntimeError):
211+
class _DesiredFunctionFound(BaseException): # noqa: WPS418
212212
"""Exception to raise when expected function is found."""

tests/test_contrib/test_pytest/test_plugin_has_trace.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from returns.contrib.pytest import ReturnsAsserts
44
from returns.io import IOFailure, IOSuccess
5-
from returns.result import Failure, Success
5+
from returns.result import Failure, Success, safe
66

77

88
def _create_container_function(container_type, container_value):
@@ -15,6 +15,12 @@ def _create_container_function_intermediate(container_type, container_value):
1515
)
1616

1717

18+
@safe
19+
def _safe_decorated_function(return_failure: bool = False):
20+
if return_failure:
21+
raise ValueError('Function failed')
22+
23+
1824
@pytest.mark.parametrize('container_type', [ # noqa: WPS118
1925
Success,
2026
Failure,
@@ -71,3 +77,13 @@ def test_failed_assert_trace2(
7177
_create_container_function_intermediate( # type: ignore
7278
wrong_type, 1,
7379
)
80+
81+
82+
@pytest.mark.parametrize('container_type', [ # noqa: WPS118
83+
Success,
84+
Failure,
85+
])
86+
def test_safe_decorated_assert(container_type, returns: ReturnsAsserts):
87+
"""Test if our plugin will catch containers from @safe-wrapped functions."""
88+
with returns.assert_trace(container_type, _safe_decorated_function):
89+
_safe_decorated_function(return_failure=container_type is Failure)

0 commit comments

Comments
 (0)