Skip to content

Fix AttributeError crash in Argument.dest on invalid option strings#14773

Closed
EternalRights wants to merge 1 commit into
pytest-dev:mainfrom
EternalRights:fix-argparsing-dest-attrerror
Closed

Fix AttributeError crash in Argument.dest on invalid option strings#14773
EternalRights wants to merge 1 commit into
pytest-dev:mainfrom
EternalRights:fix-argparsing-dest-attrerror

Conversation

@EternalRights

@EternalRights EternalRights commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Fix AttributeError crash in Argument.dest on invalid option strings

When pytest_addoption registers an option name that argparse rejects
(e.g. one starting with a digit), Action.init fails during
_set_opt_strings before self.dest is set. If Argument.dest then
reads self._action.dest, the secondary AttributeError hides the
original argparse error.

Fixed by using getattr(self._action, "dest", "") in the
dest property so a missing dest attribute does not cause a second
crash.

This complements #14429 by completing the fix RonnyPfannschmidt
originally requested on #13817.

Closes #13817

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 24, 2026
@The-Compiler

Copy link
Copy Markdown
Member

I'm confused by this, wasn't #13817 already fixed in #14429? I can't reproduce my example anymore, and your PR description ("When pytest_addoption registers an option name that argparse rejects
(e.g. one starting with a digit)") seems to say something entirely different to your test case.

@EternalRights

Copy link
Copy Markdown
Contributor Author

You're right — #14429 fixed the crash path. repr no longer touches
self.dest when _action isn't set.

Ronny's original comment was two things: guard repr, and safe
getattr for dest. I only did the first one. This closes the gap.

The test is synthetic because after #14429, repr is the only thing
that ever accesses .dest on a broken Argument. So there's no real crash
path left. But it's one line, and it's what Ronny asked for.

I'll update the description and rename the changelog (13817 is taken
by #14429, will use 14773 instead).

When a plugin registers an option with an invalid name (e.g. one
that starts with a digit), argparse's Action.__init__ raises before
the dest attribute is assigned. If Argument.dest then reads
self._action.dest, it raises a secondary AttributeError instead of
letting argparse report the real problem.

Use getattr with a "<missing>" fallback so a missing dest attribute
does not crash on access.

Closes pytest-dev#13817
@EternalRights
EternalRights force-pushed the fix-argparsing-dest-attrerror branch from 99e1c78 to 4d81b84 Compare July 24, 2026 08:38
@The-Compiler
The-Compiler force-pushed the main branch 2 times, most recently from cb2ee5b to ced9022 Compare July 24, 2026 09:22
@The-Compiler

Copy link
Copy Markdown
Member

Let's see what Ronny says about this. IMHO having a .dest attribute default to <missing> seems like quite the hack.

Comment thread testing/test_config.py

# Simulate the crash path: an option name that fails _set_opt_strings
# may result in an Action without a `dest` attribute.
class BrokenAction:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i need a p practical example of this failure - i never saw this in the wild

@EternalRights

Copy link
Copy Markdown
Contributor Author

Took another look — no real crash path. argparse.Action.init sets self.dest first, and add_argument either returns a complete action or raises before Argument is constructed. After #14429 the only path through repr is safe too. I'll close this.

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