From 4cd4ca750db56a77e0498d5764e68dce533b261d Mon Sep 17 00:00:00 2001 From: EternalRights <3147268827@qq.com> Date: Thu, 30 Apr 2026 01:01:50 +0800 Subject: [PATCH 1/2] fix Argument.__repr__ crash when _action is not initialized --- changelog/13817.bugfix.rst | 1 + src/_pytest/config/argparsing.py | 5 ++++- testing/test_parseopt.py | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 changelog/13817.bugfix.rst diff --git a/changelog/13817.bugfix.rst b/changelog/13817.bugfix.rst new file mode 100644 index 00000000000..438706b0033 --- /dev/null +++ b/changelog/13817.bugfix.rst @@ -0,0 +1 @@ +Fixed a secondary `AttributeError` masking the original error when an option argument fails to initialize. \ No newline at end of file diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index 4536709134b..f2ec53374af 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -306,10 +306,13 @@ def type(self) -> Any | None: return self._action.type def __repr__(self) -> str: + action = getattr(self, "_action", None) + if action is None: + return "Argument()" args: list[str] = [] args += ["opts: " + repr(self.names())] args += ["dest: " + repr(self.dest)] - if self._action.type: + if action.type: args += ["type: " + repr(self.type)] args += ["default: " + repr(self.default)] return "Argument({})".format(", ".join(args)) diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index f56deed8b5d..8c802617011 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -341,3 +341,28 @@ def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) result = pytester.run("bash", str(script), arg) result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"]) + + +def test_argument_repr_uninitialized() -> None: + """Argument.__repr__ should not crash if _action is not set yet.""" + arg = parseopt.Argument.__new__(parseopt.Argument) + result = repr(arg) + assert result == "Argument()" + + +def test_argument_repr_initialized(parser: parseopt.Parser) -> None: + """Argument.__repr__ works normally when properly initialized.""" + parser.addoption("--myflag", dest="myflag", help="test flag") + option = parser._anonymous.options[-1] + result = repr(option) + assert "opts:" in result + assert "dest:" in result + + +def test_argument_repr_with_type(parser: parseopt.Parser) -> None: + """Argument.__repr__ includes type when set.""" + parser.addoption("--count", type=int, dest="count", help="count") + option = parser._anonymous.options[-1] + result = repr(option) + assert "type:" in result + assert "int" in result From e786bca51096b60427b1a2503b2ff62b11976c05 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Apr 2026 17:26:18 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/13817.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/13817.bugfix.rst b/changelog/13817.bugfix.rst index 438706b0033..08c9a6a53c3 100644 --- a/changelog/13817.bugfix.rst +++ b/changelog/13817.bugfix.rst @@ -1 +1 @@ -Fixed a secondary `AttributeError` masking the original error when an option argument fails to initialize. \ No newline at end of file +Fixed a secondary `AttributeError` masking the original error when an option argument fails to initialize.