Skip to content

Make InvocationParams.args a tuple #6008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/6008.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``Config.InvocationParams.args`` is now always a ``tuple`` to better convey that it should be
immutable and avoid accidental modifications.
11 changes: 5 additions & 6 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_config(args=None, plugins=None):
config = Config(
pluginmanager,
invocation_params=Config.InvocationParams(
args=args, plugins=plugins, dir=Path().resolve()
args=args or (), plugins=plugins, dir=Path().resolve()
),
)

Expand Down Expand Up @@ -654,7 +654,7 @@ class Config:

Contains the following read-only attributes:

* ``args``: list of command-line arguments as passed to ``pytest.main()``.
* ``args``: tuple of command-line arguments as passed to ``pytest.main()``.
* ``plugins``: list of extra plugins, might be None.
* ``dir``: directory where ``pytest.main()`` was invoked from.
"""
Expand All @@ -667,13 +667,13 @@ class InvocationParams:

.. note::

Currently the environment variable PYTEST_ADDOPTS is also handled by
pytest implicitly, not being part of the invocation.
Note that the environment variable ``PYTEST_ADDOPTS`` and the ``addopts``
ini option are handled by pytest, not being included in the ``args`` attribute.

Plugins accessing ``InvocationParams`` must be aware of that.
"""

args = attr.ib()
args = attr.ib(converter=tuple)
plugins = attr.ib()
dir = attr.ib(type=Path)

Expand Down Expand Up @@ -938,7 +938,6 @@ def parse(self, args, addopts=True):
assert not hasattr(
self, "args"
), "can only parse cmdline args at most once per Config object"
assert self.invocation_params.args == args
self.hook.pytest_addhooks.call_historic(
kwargs=dict(pluginmanager=self.pluginmanager)
)
Expand Down
9 changes: 7 additions & 2 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
from _pytest.compat import importlib_metadata
from _pytest.config import _iter_rewritable_modules
from _pytest.config import Config
from _pytest.config.exceptions import UsageError
from _pytest.config.findpaths import determine_setup
from _pytest.config.findpaths import get_common_ancestor
Expand Down Expand Up @@ -456,7 +457,7 @@ def test_invocation_params_args(self, _sys_snapshot):

config = Config.fromdictargs(option_dict, args)
assert config.args == ["a", "b"]
assert config.invocation_params.args == args
assert config.invocation_params.args == tuple(args)
assert config.option.verbose == 4
assert config.option.capture == "no"

Expand Down Expand Up @@ -1235,14 +1236,18 @@ class DummyPlugin:
call = calls[0]
config = call.item.config

assert config.invocation_params.args == [p, "-v"]
assert config.invocation_params.args == (p, "-v")
assert config.invocation_params.dir == Path(str(testdir.tmpdir))

plugins = config.invocation_params.plugins
assert len(plugins) == 2
assert plugins[0] is plugin
assert type(plugins[1]).__name__ == "Collect" # installed by testdir.inline_run()

# args cannot be None
with pytest.raises(TypeError):
Config.InvocationParams(args=None, plugins=None, dir=Path())


@pytest.mark.parametrize(
"plugin",
Expand Down