Skip to content

Commit 74ac32c

Browse files
committed
Add strict ini option to enable all other strictness options
The `--strict` option is undeprecated and enables `strict`. Fix #13823.
1 parent d361da7 commit 74ac32c

File tree

13 files changed

+93
-30
lines changed

13 files changed

+93
-30
lines changed

changelog/13823.feature.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Added a :confval:`strict` configuration option to enable all strictness-related options.
2+
3+
When set to ``True``, the :confval:`strict` option currently enables :confval:`strict_config`,
4+
:confval:`strict_markers`, :confval:`strict_xfail`, and :confval:`strict_parametrization_ids`.
5+
6+
The individual strictness options can be explicitly set to override the global :confval:`strict` setting.
7+
8+
If new strictness options are added in the future, they will also be automatically enabled by :confval:`strict`.
9+
Therefore, we only recommend setting ``strict=True`` if you're using a locked version of pytest.

doc/en/deprecations.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,18 +589,20 @@ removed in pytest 8 (deprecated since pytest 2.4.0):
589589
- ``parser.addoption(..., type="int/string/float/complex")`` - use ``type=int`` etc. instead.
590590

591591

592-
The ``--strict`` command-line option
593-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
592+
The ``--strict`` command-line option (reintroduced)
593+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
594594

595595
.. deprecated:: 6.2
596-
.. versionremoved:: 8.0
596+
.. versionchanged:: 9.0
597597

598598
The ``--strict`` command-line option has been deprecated in favor of ``--strict-markers``, which
599599
better conveys what the option does.
600600

601-
We have plans to maybe in the future to reintroduce ``--strict`` and make it an encompassing
602-
flag for all strictness related options (``--strict-markers`` and ``--strict-config``
603-
at the moment, more might be introduced in the future).
601+
In version 8.1, we accidentally un-deprecated ``--strict``.
602+
603+
In version 9.0, we changed ``--strict`` to make it set the new :confval:`strict`
604+
configuration option. It now enables all strictness related options (including
605+
:confval:`strict_markers`).
604606

605607

606608
.. _cmdline-preparse-deprecated:

doc/en/reference/reference.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,25 @@ passed multiple times. The expected format is ``name=value``. For example::
20662066
"auto" can be used to explicitly use the global verbosity level.
20672067

20682068

2069+
.. confval:: strict
2070+
2071+
If set to ``True``, enables all strictness options: :confval:`strict_config`, :confval:`strict_markers`,
2072+
:confval:`strict_xfail`, and :confval:`strict_parametrization_ids`.
2073+
2074+
If you explicitly set an individual strictness option, it takes precedence over ``strict``.
2075+
2076+
.. note::
2077+
If new strictness options are added to pytest in the future, they will also be enabled by ``strict``.
2078+
We therefore only recommend using this option when using a locked version of pytest.
2079+
2080+
.. code-block:: ini
2081+
2082+
[pytest]
2083+
strict = True
2084+
2085+
.. versionadded:: 9.0
2086+
2087+
20692088
.. confval:: strict_xfail
20702089

20712090
If set to ``True``, tests marked with ``@pytest.mark.xfail`` that actually succeed will by default fail the
@@ -2078,6 +2097,8 @@ passed multiple times. The expected format is ``name=value``. For example::
20782097
[pytest]
20792098
strict_xfail = True
20802099
2100+
You can also enable this option via the :confval:`strict` option.
2101+
20812102
.. versionchanged:: 9.0
20822103
Renamed from ``xfail_strict`` to ``strict_xfail``.
20832104
``xfail_strict`` is accepted as an alias for ``strict_xfail``.
@@ -2092,6 +2113,8 @@ passed multiple times. The expected format is ``name=value``. For example::
20922113
[pytest]
20932114
strict_config = True
20942115
2116+
You can also enable this option via the :confval:`strict` option.
2117+
20952118

20962119
.. confval:: strict_markers
20972120

@@ -2102,6 +2125,8 @@ passed multiple times. The expected format is ``name=value``. For example::
21022125
[pytest]
21032126
strict_markers = True
21042127
2128+
You can also enable this option via the :confval:`strict` option.
2129+
21052130

21062131
.. confval:: strict_parametrization_ids
21072132

@@ -2115,6 +2140,8 @@ passed multiple times. The expected format is ``name=value``. For example::
21152140
[pytest]
21162141
strict_parametrization_ids = True
21172142
2143+
You can also enable this option via the :confval:`strict` option.
2144+
21182145
For example,
21192146

21202147
.. code-block:: python

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,7 @@ norecursedirs = [
378378
"build",
379379
"dist",
380380
]
381-
strict_xfail = true
382-
strict_parametrization_ids = true
383-
strict_markers = true
384-
strict_config = true
381+
strict = true
385382
filterwarnings = [
386383
"error",
387384
"default:Using or importing the ABCs:DeprecationWarning:unittest2.*",

src/_pytest/config/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,11 @@ def _validate_plugins(self) -> None:
15151515
)
15161516

15171517
def _warn_or_fail_if_strict(self, message: str) -> None:
1518-
strict_config = self.getini("strict_config")
1518+
if self.hasini("strict_config"):
1519+
strict_config = self.getini("strict_config")
1520+
else:
1521+
strict_config = self.getini("strict")
1522+
15191523
if strict_config:
15201524
raise UsageError(message)
15211525

src/_pytest/main.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def pytest_addoption(parser: Parser) -> None:
9090
)
9191
group.addoption(
9292
"--strict",
93-
action="store_true",
94-
help="(Deprecated) alias to --strict-markers",
93+
action=OverrideIniAction,
94+
ini_option="strict",
95+
ini_value="true",
96+
help="Enables the strict option",
9597
)
9698
parser.addini(
9799
"strict_config",
@@ -107,6 +109,13 @@ def pytest_addoption(parser: Parser) -> None:
107109
type="bool",
108110
default=False,
109111
)
112+
parser.addini(
113+
"strict",
114+
"Enables all strictness options, currently: "
115+
"strict_config, strict_markers, strict_xfail, strict_parametrization_ids",
116+
type="bool",
117+
default=False,
118+
)
110119

111120
group = parser.getgroup("pytest-warnings")
112121
group.addoption(

src/_pytest/mark/structures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,10 @@ def __getattr__(self, name: str) -> MarkDecorator:
585585
__tracebackhide__ = True
586586
fail(f"Unknown '{name}' mark, did you mean 'parametrize'?")
587587

588-
strict_markers = (
589-
self._config.getini("strict_markers") or self._config.option.strict
590-
)
588+
if self._config.hasini("strict_markers"):
589+
strict_markers = self._config.getini("strict_markers")
590+
else:
591+
strict_markers = self._config.getini("strict")
591592
if strict_markers:
592593
fail(
593594
f"{name!r} not found in `markers` configuration option",

src/_pytest/python.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,10 @@ def make_unique_parameterset_ids(self) -> list[str | _HiddenParam]:
964964

965965
def _strict_parametrization_ids_enabled(self) -> bool:
966966
if self.config:
967-
return bool(self.config.getini("strict_parametrization_ids"))
967+
if self.config.hasini("strict_parametrization_ids"):
968+
return bool(self.config.getini("strict_parametrization_ids"))
969+
else:
970+
return bool(self.config.getini("strict"))
968971
return False
969972

970973
def _resolve_ids(self) -> Iterable[str | _HiddenParam]:

src/_pytest/skipping.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,12 @@ def evaluate_xfail_marks(item: Item) -> Xfail | None:
214214
"""Evaluate xfail marks on item, returning Xfail if triggered."""
215215
for mark in item.iter_markers(name="xfail"):
216216
run = mark.kwargs.get("run", True)
217-
strict = mark.kwargs.get("strict", item.config.getini("strict_xfail"))
217+
if "strict" in mark.kwargs:
218+
strict = mark.kwargs["strict"]
219+
elif item.config.hasini("strict_xfail"):
220+
strict = item.config.getini("strict_xfail")
221+
else:
222+
strict = item.config.getini("strict")
218223
raises = mark.kwargs.get("raises", None)
219224
if "condition" not in mark.kwargs:
220225
conditions = mark.args

testing/test_collection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,15 +2726,17 @@ def test_1(): pass
27262726
),
27272727
],
27282728
)
2729+
@pytest.mark.parametrize("option_name", ["strict_parametrization_ids", "strict"])
27292730
def test_strict_parametrization_ids(
27302731
pytester: Pytester,
27312732
x_y: Sequence[tuple[int, int]],
27322733
expected_duplicates: Sequence[str],
2734+
option_name: str,
27332735
) -> None:
27342736
pytester.makeini(
2735-
"""
2737+
f"""
27362738
[pytest]
2737-
strict_parametrization_ids = true
2739+
{option_name} = true
27382740
"""
27392741
)
27402742
pytester.makepyfile(

0 commit comments

Comments
 (0)