Skip to content

Fix Argument.__repr__ crash when _action is not initialized#14429

Open
EternalRights wants to merge 2 commits intopytest-dev:mainfrom
EternalRights:fix-argparse-attrerror
Open

Fix Argument.__repr__ crash when _action is not initialized#14429
EternalRights wants to merge 2 commits intopytest-dev:mainfrom
EternalRights:fix-argparse-attrerror

Conversation

@EternalRights
Copy link
Copy Markdown
Contributor

@EternalRights EternalRights commented Apr 29, 2026

Ran into this while looking at #13817. When Argument.__init__ doesn't complete (e.g. if argparse.Action raises during construction), calling repr() on the partially-initialized Argument triggers a secondary AttributeError because self._action doesn't exist yet. This masks the original error message.

The fix uses getattr(self, "_action", None) in __repr__ and returns "Argument(<uninitialized>)" when _action is missing, as RonnyPfannschmidt suggested. This way the original exception message gets through instead of being buried under a secondary crash.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Apr 29, 2026
@EternalRights EternalRights force-pushed the fix-argparse-attrerror branch from 4f863ed to 4cd4ca7 Compare April 29, 2026 17:25
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.

1 participant