Skip to content
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
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
22 changes: 22 additions & 0 deletions testing/test_parseopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,25 @@ 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)
assert repr(arg) == "Argument(<uninitialized>)"


def test_argument_repr_initialized(parser: parseopt.Parser) -> None:
"""Argument.__repr__ with properly initialized options."""
# Without type
parser.addoption("--myflag", dest="myflag", help="test flag")
option = parser._anonymous.options[-1]
assert repr(option) == "Argument(opts: ['--myflag'], dest: 'myflag', default: None)"

# With type
parser.addoption("--count", type=int, dest="count", help="count")
option = parser._anonymous.options[-1]
assert (
repr(option)
== "Argument(opts: ['--count'], dest: 'count', type: <class 'int'>, default: None)"
)
Loading