99from typing_extensions import Final , final
1010
1111if TYPE_CHECKING :
12- from returns .interfaces .specific .result import ResultBasedN
12+ from returns .interfaces .failable import FailableN
13+ from returns .interfaces .specific .result import ResultLikeN
1314
1415_ERROR_FIELD : Final = '_error_handled'
1516_ERROR_HANDLERS : Final = (
2122)
2223
2324_FunctionType = TypeVar ('_FunctionType' , bound = Callable )
24- _ResultCallableType = TypeVar (
25- '_ResultCallableType ' , bound = Callable [..., 'ResultBasedN ' ],
25+ _ReturnsResultType = TypeVar (
26+ '_ReturnsResultType ' , bound = Callable [..., 'ResultLikeN ' ],
2627)
2728
2829
29- def pytest_configure (config ) -> None :
30- """
31- Hook to be executed on import.
32-
33- We use it define custom markers.
34- """
35- config .addinivalue_line (
36- 'markers' ,
37- """
38- returns_lawful: all tests under `check_all_laws` is marked this way,
39- use `-m "not returns_lawful"` to skip them.
40- """ ,
41- )
42-
43-
44- class _DesiredFunctionFound (RuntimeError ):
45- """Exception to raise when expected function is found."""
46-
47-
4830@final
49- class _ReturnsAsserts (object ):
31+ class ReturnsAsserts (object ):
5032 """Class with helpers assertions to check containers."""
5133
5234 __slots__ = ()
5335
54- def is_error_handled (self , container ) -> bool :
36+ def is_error_handled (self , container : 'FailableN' ) -> bool :
5537 """Ensures that container has its error handled in the end."""
5638 return bool (getattr (container , _ERROR_FIELD , False ))
5739
5840 @contextmanager
5941 def has_trace (
6042 self ,
61- trace_type : _ResultCallableType ,
43+ trace_type : _ReturnsResultType ,
6244 function_to_search : _FunctionType ,
6345 ) -> Iterator [None ]:
46+ """
47+ Ensures that a given function was called during execution.
48+
49+ Use it to determine where the failure happened.
50+ """
6451 old_tracer = sys .gettrace ()
6552 sys .settrace (partial (_trace_function , trace_type , function_to_search ))
6653
@@ -76,6 +63,27 @@ def has_trace(
7663 sys .settrace (old_tracer )
7764
7865
66+ @pytest .fixture (scope = 'session' )
67+ def returns (_patch_containers ) -> ReturnsAsserts : # noqa: WPS442
68+ """Returns our own class with helpers assertions to check containers."""
69+ return ReturnsAsserts ()
70+
71+
72+ def pytest_configure (config ) -> None :
73+ """
74+ Hook to be executed on import.
75+
76+ We use it define custom markers.
77+ """
78+ config .addinivalue_line (
79+ 'markers' ,
80+ """
81+ returns_lawful: all tests under `check_all_laws` is marked this way,
82+ use `-m "not returns_lawful"` to skip them.
83+ """ ,
84+ )
85+
86+
7987@pytest .fixture (scope = 'session' )
8088def _patch_containers () -> None :
8189 """
@@ -92,12 +100,6 @@ def _patch_containers() -> None:
92100 _patch_error_handling (_ERRORS_COPIERS , _PatchedContainer .copy_handler )
93101
94102
95- @pytest .fixture (scope = 'session' )
96- def returns (_patch_containers ) -> _ReturnsAsserts : # noqa: WPS442
97- """Returns our own class with helpers assertions to check containers."""
98- return _ReturnsAsserts ()
99-
100-
101103def _patch_error_handling (methods , patch_handler ) -> None :
102104 for container in _PatchedContainer .containers_to_patch ():
103105 for method in methods :
@@ -107,7 +109,7 @@ def _patch_error_handling(methods, patch_handler) -> None:
107109
108110
109111def _trace_function (
110- trace_type : _ResultCallableType ,
112+ trace_type : _ReturnsResultType ,
111113 function_to_search : _FunctionType ,
112114 frame : FrameType ,
113115 event : str ,
@@ -192,3 +194,7 @@ def factory(self, *args, **kwargs):
192194 )
193195 return original_result
194196 return wraps (original )(factory )
197+
198+
199+ class _DesiredFunctionFound (RuntimeError ):
200+ """Exception to raise when expected function is found."""
0 commit comments