Skip to content

Remove redundant if check from argparser file. #8766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2019
Merged
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
6 changes: 2 additions & 4 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,10 +1469,8 @@ def _get_optional_kwargs(self, *args, **kwargs):

# strings starting with two prefix characters are long options
option_strings.append(option_string)
if option_string[0] in self.prefix_chars:
if len(option_string) > 1:
if option_string[1] in self.prefix_chars:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these checks are redundant.

Consider self.prefix_chars = {'-'}

The left hand side requires '--foo' whereas your updated code incorrectly allows X-foo

I do think these conditions could be combined, perhaps something like:

if (
        option_string[0] in self.prefix_chars and
        len(option_string) > 1 and
        option_string[1] in self.prefix_chars
):
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check the code from line no 1463. It checks for the condition option_string[0] in self.prefix_chars.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah indeed! Maybe use elif so that's more clear at a glance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do this.

if not option_string[0] in self.prefix_chars:
   # Some code. 
    raise
else:
     option_strings.append(option_string)
     if len(option_string) > 1 and option_string[1] in self.prefix_chars:
            long_option_strings.append(option_string)

Or the current code is also fine.

Copy link
Contributor Author

@sp1rs sp1rs Aug 17, 2018

Choose a reason for hiding this comment

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

@asottile This change looks good. No need for else. This looks cleaner. Please review.

long_option_strings.append(option_string)
if len(option_string) > 1 and option_string[1] in self.prefix_chars:
long_option_strings.append(option_string)

# infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
dest = kwargs.pop('dest', None)
Expand Down