Skip to content

Commit bd76042

Browse files
committed
python: export pytest.Metafunc for typing purposes
The type cannot be constructed directly, but is exported for use in type annotations, since it is reachable through existing public API.
1 parent 6d3a66d commit bd76042

File tree

8 files changed

+22
-8
lines changed

8 files changed

+22
-8
lines changed

changelog/7469.deprecation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ Directly constructing the following classes is now deprecated:
33
- ``_pytest.mark.structures.Mark``
44
- ``_pytest.mark.structures.MarkDecorator``
55
- ``_pytest.mark.structures.MarkGenerator``
6+
- ``_pytest.python.Metafunc``
67

78
These have always been considered private, but now issue a deprecation warning, which may become a hard error in pytest 7.0.0.

changelog/7469.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The newly-exported types are:
55
- ``pytest.Mark`` for :class:`marks <pytest.Mark>`.
66
- ``pytest.MarkDecorator`` for :class:`mark decorators <pytest.MarkDecorator>`.
77
- ``pytest.MarkGenerator`` for the :class:`pytest.mark <pytest.MarkGenerator>` singleton.
8+
- ``pytest.Metafunc`` for the :class:`metafunc <pytest.MarkGenerator>` argument to the `pytest_generate_tests <pytest.hookspec.pytest_generate_tests>` hook.
89

910
Constructing them directly is not supported; they are only meant for use in type annotations.
1011
Doing so will emit a deprecation warning, and may become a hard-error in pytest 7.0.

doc/en/deprecations.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ Metafunc.addcall
397397

398398
.. versionremoved:: 4.0
399399

400-
``_pytest.python.Metafunc.addcall`` was a precursor to the current parametrized mechanism. Users should use
401-
:meth:`_pytest.python.Metafunc.parametrize` instead.
400+
``Metafunc.addcall`` was a precursor to the current parametrized mechanism. Users should use
401+
:meth:`pytest.Metafunc.parametrize` instead.
402402

403403
Example:
404404

doc/en/funcarg_compare.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ There are several limitations and difficulties with this approach:
4747
2. parametrizing the "db" resource is not straight forward:
4848
you need to apply a "parametrize" decorator or implement a
4949
:py:func:`~hookspec.pytest_generate_tests` hook
50-
calling :py:func:`~python.Metafunc.parametrize` which
50+
calling :py:func:`~pytest.Metafunc.parametrize` which
5151
performs parametrization at the places where the resource
5252
is used. Moreover, you need to modify the factory to use an
5353
``extrakey`` parameter containing ``request.param`` to the
@@ -113,7 +113,7 @@ This new way of parametrizing funcarg factories should in many cases
113113
allow to re-use already written factories because effectively
114114
``request.param`` was already used when test functions/classes were
115115
parametrized via
116-
:py:func:`metafunc.parametrize(indirect=True) <_pytest.python.Metafunc.parametrize>` calls.
116+
:py:func:`metafunc.parametrize(indirect=True) <pytest.Metafunc.parametrize>` calls.
117117

118118
Of course it's perfectly fine to combine parametrization and scoping:
119119

doc/en/reference.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pytest.mark.parametrize
138138

139139
**Tutorial**: :doc:`parametrize`.
140140

141-
This mark has the same signature as :py:meth:`_pytest.python.Metafunc.parametrize`; see there.
141+
This mark has the same signature as :py:meth:`pytest.Metafunc.parametrize`; see there.
142142

143143

144144
.. _`pytest.mark.skip ref`:
@@ -870,7 +870,7 @@ Mark
870870
Metafunc
871871
~~~~~~~~
872872

873-
.. autoclass:: _pytest.python.Metafunc
873+
.. autoclass:: pytest.Metafunc()
874874
:members:
875875

876876
Module

src/_pytest/python.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from _pytest.config import ExitCode
5656
from _pytest.config import hookimpl
5757
from _pytest.config.argparsing import Parser
58+
from _pytest.deprecated import check_ispytest
5859
from _pytest.deprecated import FSCOLLECTOR_GETHOOKPROXY_ISINITPATH
5960
from _pytest.fixtures import FuncFixtureInfo
6061
from _pytest.main import Session
@@ -467,7 +468,12 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
467468
fixtureinfo = definition._fixtureinfo
468469

469470
metafunc = Metafunc(
470-
definition, fixtureinfo, self.config, cls=cls, module=module
471+
definition=definition,
472+
fixtureinfo=fixtureinfo,
473+
config=self.config,
474+
cls=cls,
475+
module=module,
476+
_ispytest=True,
471477
)
472478
methods = []
473479
if hasattr(module, "pytest_generate_tests"):
@@ -971,7 +977,11 @@ def __init__(
971977
config: Config,
972978
cls=None,
973979
module=None,
980+
*,
981+
_ispytest: bool = False,
974982
) -> None:
983+
check_ispytest(_ispytest)
984+
975985
#: Access to the underlying :class:`_pytest.python.FunctionDefinition`.
976986
self.definition = definition
977987

src/pytest/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from _pytest.python import Class
4141
from _pytest.python import Function
4242
from _pytest.python import Instance
43+
from _pytest.python import Metafunc
4344
from _pytest.python import Module
4445
from _pytest.python import Package
4546
from _pytest.python_api import approx
@@ -95,6 +96,7 @@
9596
"Mark",
9697
"MarkDecorator",
9798
"MarkGenerator",
99+
"Metafunc",
98100
"Module",
99101
"MonkeyPatch",
100102
"Package",

testing/python/metafunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DefinitionMock(python.FunctionDefinition):
4747
names = getfuncargnames(func)
4848
fixtureinfo: Any = FuncFixtureInfoMock(names)
4949
definition: Any = DefinitionMock._create(func, "mock::nodeid")
50-
return python.Metafunc(definition, fixtureinfo, config)
50+
return python.Metafunc(definition, fixtureinfo, config, _ispytest=True)
5151

5252
def test_no_funcargs(self) -> None:
5353
def function():

0 commit comments

Comments
 (0)