Skip to content

Fix issue #13817: Fixed AttributeError in Argument.__repr__ when dest attribute is not set.#13824

Open
thartline35 wants to merge 17 commits intopytest-dev:mainfrom
thartline35:fix-issue-13817
Open

Fix issue #13817: Fixed AttributeError in Argument.__repr__ when dest attribute is not set.#13824
thartline35 wants to merge 17 commits intopytest-dev:mainfrom
thartline35:fix-issue-13817

Conversation

@thartline35
Copy link
Copy Markdown

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: Added hasattr() check for dest attribute
  • Added comprehensive test cases in testing/test_argparsing_repr_fix.py
  • Added changelog entry
  • Updated AUTHORS

All tests pass locally.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 18, 2025
Copy link
Copy Markdown
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good start, thanks

please use the NOT_SET constant we have for that

Comment thread src/_pytest/config/argparsing.py Outdated
Copy link
Copy Markdown
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests will need another round of review here. Will follow up with a more detailed review tomorrow, travelling without laptop right now.

My main concerns are:

  • They should be wherever existing tests are instead of a new file
  • The good case tests are most likely redundant as we have existing tests
  • Duplication can probably be reduced a lot via parametrization
  • not quite clear why suppressing the deprecation warning and accessing private API is needed, might be fine though, will need to check once back on laptop

Copy link
Copy Markdown
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not let self escape via exceptions in init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError with invalid flag added in pytest_addoption

3 participants