Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog/13817.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a secondary `AttributeError` masking the original error when an option argument fails to initialize.
5 changes: 4 additions & 1 deletion src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(<uninitialized>)"
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))
Expand Down
25 changes: 25 additions & 0 deletions testing/test_parseopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(<uninitialized>)"


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
Loading