From bd7919e03d1d1fd14b30cfa9211b2a51a00f81ba Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Sat, 21 Jan 2023 14:19:54 +0300 Subject: [PATCH] Initialize args and args_source during Config.__init__ Closes #10626 Co-authored-by: Bruno Oliveira --- AUTHORS | 1 + changelog/10626.bugfix.rst | 1 + src/_pytest/config/__init__.py | 6 ++++-- testing/python/fixtures.py | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelog/10626.bugfix.rst diff --git a/AUTHORS b/AUTHORS index cd5b71ffb..fe60b6865 100644 --- a/AUTHORS +++ b/AUTHORS @@ -290,6 +290,7 @@ Prashant Sharma Pulkit Goyal Punyashloka Biswal Quentin Pradet +q0w Ralf Schmitt Ralph Giles Ram Rachum diff --git a/changelog/10626.bugfix.rst b/changelog/10626.bugfix.rst new file mode 100644 index 000000000..c3c7ef778 --- /dev/null +++ b/changelog/10626.bugfix.rst @@ -0,0 +1 @@ +Fix crash if ``--fixtures`` and ``--help`` are passed at the same time. diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 5c0c62108..a08a965a6 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1005,6 +1005,8 @@ class Config: self.hook.pytest_addoption.call_historic( kwargs=dict(parser=self._parser, pluginmanager=self.pluginmanager) ) + self.args_source = Config.ArgsSource.ARGS + self.args: List[str] = [] if TYPE_CHECKING: from _pytest.cacheprovider import Cache @@ -1344,8 +1346,8 @@ class Config: def parse(self, args: List[str], addopts: bool = True) -> None: # Parse given cmdline arguments into this config object. - assert not hasattr( - self, "args" + assert ( + self.args == [] ), "can only parse cmdline args at most once per Config object" self.hook.pytest_addhooks.call_historic( kwargs=dict(pluginmanager=self.pluginmanager) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 3ce5cb34d..d996f80bb 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -3338,6 +3338,10 @@ class TestShowFixtures: config = pytester.parseconfigure("--funcargs") assert config.option.showfixtures + def test_show_help(self, pytester: Pytester) -> None: + result = pytester.runpytest("--fixtures", "--help") + assert not result.ret + def test_show_fixtures(self, pytester: Pytester) -> None: result = pytester.runpytest("--fixtures") result.stdout.fnmatch_lines(