Skip to content

Commit 56b3937

Browse files
committed
test switching skip to a class
checked on the following snippet ``` import pytest from typing import reveal_type print(pytest.skip) print(pytest.skip.Exception) reveal_type(pytest.skip) reveal_type(pytest.skip.Exception) reveal_type(pytest.skip(reason="whatever")) ``` before the change ``` $ uv run test_pytest_skip.py <function skip at 0x71122a118c20> <class 'Skipped'> Runtime type is 'function' Runtime type is 'type' Traceback (most recent call last): File "/code/playground_ty/testty/test_pytest_skip.py", line 9, in <module> reveal_type(pytest.skip(reason="whatever")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/code/ext/pytest/src/_pytest/outcomes.py", line 172, in skip raise Skipped(msg=reason, allow_module_level=allow_module_level) Skipped: whatever $ uv run -m mypy test_pytest_skip.py test_pytest_skip.py:7: note: Revealed type is "_pytest.outcomes._WithException[[reason: builtins.str =, *, allow_module_level: builtins.bool =], Never, def (msg: Union[builtins.str, None] =, pytrace: builtins.bool =, allow_module_level: builtins.bool =, *, _use_item_location: builtins.bool =) -> _pytest.outcomes.Skipped]" test_pytest_skip.py:8: note: Revealed type is "def (msg: Union[builtins.str, None] =, pytrace: builtins.bool =, allow_module_level: builtins.bool =, *, _use_item_location: builtins.bool =) -> _pytest.outcomes.Skipped" test_pytest_skip.py:9: note: Revealed type is "Never" Success: no issues found in 1 source file ``` after the change ``` $ uv run test_pytest_skip.py <_pytest.outcomes._Skip object at 0x71ee4f419dc0> <class 'Skipped'> Runtime type is '_Skip' Runtime type is 'type' Traceback (most recent call last): File "/code/playground_ty/testty/test_pytest_skip.py", line 9, in <module> reveal_type(pytest.skip(reason="whatever")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/code/ext/pytest/src/_pytest/outcomes.py", line 143, in __call__ raise Skipped(msg=reason, allow_module_level=allow_module_level) Skipped: whatever $ uv run -m mypy test_pytest_skip.py test_pytest_skip.py:7: note: Revealed type is "_pytest.outcomes._Skip" test_pytest_skip.py:8: note: Revealed type is "def (msg: Union[builtins.str, None] =, pytrace: builtins.bool =, allow_module_level: builtins.bool =, *, _use_item_location: builtins.bool =) -> _pytest.outcomes.Skipped" test_pytest_skip.py:9: note: Revealed type is "Never" ``` However, this is failing a unit test ``` $ tox -e py39-xdist def test_skip_simple(self): with pytest.raises(pytest.skip.Exception) as excinfo: pytest.skip("xxx") > assert excinfo.traceback[-1].frame.code.name == "skip" E AssertionError: assert '__call__' == 'skip' E E - skip E + __call__ testing/python/collect.py:1078: AssertionError ```
1 parent befc88e commit 56b3937

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

src/_pytest/outcomes.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -135,41 +135,52 @@ def exit(
135135
raise Exit(reason, returncode)
136136

137137

138-
@_with_exception(Skipped)
139-
def skip(
140-
reason: str = "",
141-
*,
142-
allow_module_level: bool = False,
143-
) -> NoReturn:
144-
"""Skip an executing test with the given message.
145-
146-
This function should be called only during testing (setup, call or teardown) or
147-
during collection by using the ``allow_module_level`` flag. This function can
148-
be called in doctests as well.
149-
150-
:param reason:
151-
The message to show the user as reason for the skip.
152-
153-
:param allow_module_level:
154-
Allows this function to be called at module level.
155-
Raising the skip exception at module level will stop
156-
the execution of the module and prevent the collection of all tests in the module,
157-
even those defined before the `skip` call.
158-
159-
Defaults to False.
160-
161-
:raises pytest.skip.Exception:
162-
The exception that is raised.
163-
164-
.. note::
165-
It is better to use the :ref:`pytest.mark.skipif ref` marker when
166-
possible to declare a test to be skipped under certain conditions
167-
like mismatching platforms or dependencies.
168-
Similarly, use the ``# doctest: +SKIP`` directive (see :py:data:`doctest.SKIP`)
169-
to skip a doctest statically.
170-
"""
171-
__tracebackhide__ = True
172-
raise Skipped(msg=reason, allow_module_level=allow_module_level)
138+
class _Skip:
139+
Exception = Skipped
140+
141+
def __call__(self, reason: str = "", allow_module_level: bool = False) -> NoReturn:
142+
__tracebackhide__ = True
143+
raise Skipped(msg=reason, allow_module_level=allow_module_level)
144+
145+
146+
skip = _Skip()
147+
148+
149+
# @_with_exception(Skipped)
150+
# def skip(
151+
# reason: str = "",
152+
# *,
153+
# allow_module_level: bool = False,
154+
# ) -> NoReturn:
155+
# """Skip an executing test with the given message.
156+
#
157+
# This function should be called only during testing (setup, call or teardown) or
158+
# during collection by using the ``allow_module_level`` flag. This function can
159+
# be called in doctests as well.
160+
#
161+
# :param reason:
162+
# The message to show the user as reason for the skip.
163+
#
164+
# :param allow_module_level:
165+
# Allows this function to be called at module level.
166+
# Raising the skip exception at module level will stop
167+
# the execution of the module and prevent the collection of all tests in the module,
168+
# even those defined before the `skip` call.
169+
#
170+
# Defaults to False.
171+
#
172+
# :raises pytest.skip.Exception:
173+
# The exception that is raised.
174+
#
175+
# .. note::
176+
# It is better to use the :ref:`pytest.mark.skipif ref` marker when
177+
# possible to declare a test to be skipped under certain conditions
178+
# like mismatching platforms or dependencies.
179+
# Similarly, use the ``# doctest: +SKIP`` directive (see :py:data:`doctest.SKIP`)
180+
# to skip a doctest statically.
181+
# """
182+
# __tracebackhide__ = True
183+
# raise Skipped(msg=reason, allow_module_level=allow_module_level)
173184

174185

175186
@_with_exception(Failed)

0 commit comments

Comments
 (0)