pytest
pytest copied to clipboard
Fix issue #13817: Fixed ``AttributeError`` in ``Argument.__repr__`` when ``dest`` attribute is not set.
Description
Fixes #13817
When an invalid option name is provided to pytest_addoption() (e.g., missing the -- prefix), pytest correctly detects the error but then fails while trying to display the error message. This results in an AttributeError that obscures the original, helpful validation error.
Root Cause
The Argument.__repr__() method unconditionally accesses self.dest, which hasn't been set yet when initialization is interrupted by a validation error in _set_opt_strings().
The Fix
Added a defensive hasattr() check for dest in __repr__, following the same pattern already used for the type and default attributes.
Before
AttributeError: 'Argument' object has no attribute 'dest'
After
ArgumentError: invalid long option string 'shuffle': must start with --, followed by non-dash
Changes
- Modified
src/_pytest/config/argparsing.py: Addedhasattr()check fordestattribute - Added comprehensive test cases in
testing/test_argparsing_repr_fix.py - Added changelog entry
- Updated AUTHORS
All tests pass locally.