You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse.ArgumentParser.parses_args does not honor exit_on_error=False when given unrecognized arguments #121018
Actually, this is more complex issue. error() is called in many other places, and it does not honor exit_on_error=False.
In most cases it can be replaced with raise ArgumentError. It is caught at higher level and error() is called if exit_on_error is true.
In some cases ValueError may be more appropriate exception than ArgumentError, but this is another issue.
Ah yes good catch. I think ArgumentError is meant to be raised by invalid arguments given by the end user. In cases where the error is caused by an improper setup of a parser or an option from the developer, a ValueError would indeed be more appropriate.
On second thought, I think it may be cleaner to simply put the if self.exit_on_error: ... else: raise ArgumentError logics directly in the error method, and call self.error only when it is caused by the end user.
There are already open issues for this bug: #85427 and #103498. It is my fault that I did not recognize duplicates.
Yes, putting the logic directly in the error method could simplify some cases, but we want exit_on_error=False to work even with user-overridden error method.
There are already open issues for this bug: #85427 and #103498. It is my fault that I did not recognize duplicates.
Ah you're right. I forgot to search for prior bug reports before filing mine. Sorry.
Yes, putting the logic directly in the error method could simplify some cases, but we want exit_on_error=False to work even with user-overridden error method.
Good point. Will keep the current approach then. Thanks!
Activity
gh-121018: Ensure ArgumentParser.parse_args with exit_on_error=False …
pythongh-121018: Ensure ArgumentParser.parse_args with exit_on_error=…
pythongh-121018: Ensure ArgumentParser.parse_args with exit_on_error=…
[3.13] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error…
[3.12] gh-121018: Ensure ArgumentParser.parse_args with exit_on_error…
pythongh-121018: Fix more cases of exiting in argparse when exit_on_e…
serhiy-storchaka commentedon Jun 26, 2024
Actually, this is more complex issue.
error()is called in many other places, and it does not honorexit_on_error=False.In most cases it can be replaced with
raise ArgumentError. It is caught at higher level anderror()is called ifexit_on_erroris true.In some cases ValueError may be more appropriate exception than ArgumentError, but this is another issue.
blhsing commentedon Jun 27, 2024
Ah yes good catch. I think
ArgumentErroris meant to be raised by invalid arguments given by the end user. In cases where the error is caused by an improper setup of a parser or an option from the developer, aValueErrorwould indeed be more appropriate.blhsing commentedon Jun 27, 2024
On second thought, I think it may be cleaner to simply put the
if self.exit_on_error: ... else: raise ArgumentErrorlogics directly in theerrormethod, and callself.erroronly when it is caused by the end user.serhiy-storchaka commentedon Jun 28, 2024
There are already open issues for this bug: #85427 and #103498. It is my fault that I did not recognize duplicates.
Yes, putting the logic directly in the error method could simplify some cases, but we want
exit_on_error=Falseto work even with user-overriddenerrormethod.gh-121018: Fix more cases of exiting in argparse when exit_on_error=F…
blhsing commentedon Jun 28, 2024
Ah you're right. I forgot to search for prior bug reports before filing mine. Sorry.
Good point. Will keep the current approach then. Thanks!
[3.13] pythongh-121018: Fix more cases of exiting in argparse when ex…
[3.12] pythongh-121018: Fix more cases of exiting in argparse when ex…
[3.12] gh-121018: Fix more cases of exiting in argparse when exit_on_…
[3.13] gh-121018: Fix more cases of exiting in argparse when exit_on_…
pythongh-121018: Ensure ArgumentParser.parse_args with exit_on_error=…
pythongh-121018: Fix more cases of exiting in argparse when exit_on_e…
gh-121018: Fix typo in NEWS entry (#121510)
[3.12] gh-121018: Fix typo in NEWS entry (GH-121510) (#121516)
pythongh-121018: Ensure ArgumentParser.parse_args with exit_on_error=…
pythongh-121018: Fix more cases of exiting in argparse when exit_on_e…
pythongh-121018: Fix typo in NEWS entry (python#121510)
pythongh-121018: Ensure ArgumentParser.parse_args with exit_on_error=…
pythongh-121018: Fix more cases of exiting in argparse when exit_on_e…
pythongh-121018: Fix typo in NEWS entry (python#121510)