Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion 4 Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,9 @@ def consume_positionals(start_index):
required_actions = []
for action in self._actions:
if action not in seen_actions:
if action.required:
if (action.required and
action.default is None and
action.nargs not in (ZERO_OR_MORE, OPTIONAL)):
required_actions.append(_get_action_name(action))
else:
# Convert action default now instead of doing it before
Expand Down
7 changes: 7 additions & 0 deletions 7 Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6091,6 +6091,13 @@ def test_required_args(self):
'the following arguments are required: bar, baz',
self.parser.parse_args, [])

def test_required_and_optional(self):
self.parser.add_argument('bar')
self.parser.add_argument('baz', nargs='*')
self.assertRaisesRegex(argparse.ArgumentError,
'the following arguments are required: bar',
self.parser.parse_args, [])

def test_required_mutually_exclusive_args(self):
group = self.parser.add_mutually_exclusive_group(required=True)
group.add_argument('--bar')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes a bug in argparse where optional args are incorrectly processed as required args
Morty Proxy This is a proxified and sanitized view of the page, visit original site.