docs: fix misleading docstring about default vs skip=True in getoption#13879
docs: fix misleading docstring about default vs skip=True in getoption#13879midoriao wants to merge 3 commits intopytest-dev:mainfrom
Conversation
|
I'm curious how you came across this, is there actually a use case for passing both I'm also curious why |
|
I also don’t think there is a practical use case for specifying both at the same time. The intention of this PR is simply to correct a misleading description. I ran into this simply by reading the API reference and trying to understand what actual role of
As far as I can tell, this behavior was first discussed in detail in #10558. The conclusion there seemed to be that the behavior is established and not considered a bug, and that it should be documented clearly instead of changed. Given the unusual behavior, it might be clearer to keep the parameter descriptions simple and move the detail into a .. note:: section. Here is a possible wording: :param default:
Value to return when the option is undeclared (a declared option with value
``None`` is *not* considered undeclared).
:param skip:
If True, raise `pytest.skip` when the option is undeclared or has a None value.
.. note::
``default`` and ``skip=True`` use different rules for what counts as a
"missing value":
* ``default`` applies only when the option is undeclared.
* ``skip=True`` treats both an undeclared option and None value as missing.
When both parameters are used together, a provided ``default`` is returned
instead of skipping. This behavior exists for historical and backward-compatibility reasons.
See issue #10558 for background. |
This PR clarifies the
pytest.Config.getoptiondocstring to make the exceptional behavior ofdefaultwithskip=Trueexplicit.Also adds a small test verifying that behavior.
Stituation before this change
The previous docstring implied:
defaultparameter is ignored when the option is declared.But actual behavior is:
defaultis not ignored even if the option is declared, whenskip=Trueand the option hasNonevalue.This can be reproduced by the following test case (already existing in
testing/test_config.py):Rationale
The
getoption()behavior involves multiple conditions (whether the option is declared or not,Noneor not,default,skip), which previously made the docstring easy to misinterpret. This PR avoids multiple "note that ..." caveats and keeps each parameter’s description self-contained and readable.skip=Truespecial case in plain language.pytest_addoptionhook was removed, since this method’s behavior is independent of how the option was added.**declared**was removed.Nonevalue (context: Improve pytest.Config.getoption docstring #12886). The new wording states this condition explicitly.default=Noneandskip=True.Notes
No behavior change; documentation and test addition only.
Related historical issue: #10558