kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

bikeshed: Does `my_{argparse,shlex}.py` need to be excluded from black/isort/etc? #382

Closed tony closed 1 year ago

tony commented 1 year ago

@kislyuk Fine to make a PR to unexclude these?

My editor's setup (though not necessarily all contributors) wants to format them w/ black, and the diff looks harmless when #381's changes are considered:

diff of my_shlex.py ```diff diff --git a/argcomplete/my_shlex.py b/argcomplete/my_shlex.py index 9cf26e4..a28559d 100644 --- a/argcomplete/my_shlex.py +++ b/argcomplete/my_shlex.py @@ -29,10 +29,11 @@ try: except NameError: basestring = str + class shlex: "A lexical analyzer class for simple shell-like syntaxes." - def __init__(self, instream=None, infile=None, posix=False, - punctuation_chars=False): + + def __init__(self, instream=None, infile=None, posix=False, punctuation_chars=False): # Modified by argcomplete: 2/3 compatibility if isinstance(instream, basestring): instream = StringIO(instream) @@ -48,8 +49,7 @@ class shlex: else: self.eof = '' self.commenters = '#' - self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') + self.wordchars = 'abcdfeghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' # Modified by argcomplete: 2/3 compatibility # if self.posix: :...skipping... diff --git a/argcomplete/my_shlex.py b/argcomplete/my_shlex.py index 9cf26e4..a28559d 100644 --- a/argcomplete/my_shlex.py +++ b/argcomplete/my_shlex.py @@ -29,10 +29,11 @@ try: except NameError: basestring = str + class shlex: "A lexical analyzer class for simple shell-like syntaxes." - def __init__(self, instream=None, infile=None, posix=False, - punctuation_chars=False): + + def __init__(self, instream=None, infile=None, posix=False, punctuation_chars=False): # Modified by argcomplete: 2/3 compatibility if isinstance(instream, basestring): instream = StringIO(instream) @@ -48,8 +49,7 @@ class shlex: else: self.eof = '' self.commenters = '#' - self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') + self.wordchars = 'abcdfeghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' # Modified by argcomplete: 2/3 compatibility # if self.posix: # self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' @@ -76,7 +76,7 @@ class shlex: self._pushback_chars = deque() # these chars added because allowed in file names, args, wildcards self.wordchars += '~-./*?=' - #remove any punctuation chars from wordchars + # remove any punctuation chars from wordchars t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars)) self.wordchars = self.wordchars.translate(t) @@ -110,8 +110,7 @@ class shlex: self.instream.close() (self.infile, self.instream, self.lineno) = self.filestack.popleft() if self.debug: - print('shlex: popping to %s, line %d' \ - % (self.instream, self.lineno)) + print('shlex: popping to %s, line %d' % (self.instream, self.lineno)) self.state = ' ' def get_token(self): @@ -157,10 +156,9 @@ class shlex: if nextchar == '\n': self.lineno += 1 if self.debug >= 3: - print("shlex: in state %r I see character: %r" % (self.state, - nextchar)) + print("shlex: in state %r I see character: %r" % (self.state, nextchar)) if self.state is None: - self.token = '' # past end of file + self.token = '' # past end of file break elif self.state == ' ': if not nextchar: @@ -170,7 +168,7 @@ class shlex: if self.debug >= 2: print("shlex: I see whitespace in whitespace state") if self.token or (self.posix and quoted): - break # emit current token + break # emit current token else: continue elif nextchar in self.commenters: @@ -195,12 +193,12 @@ class shlex: else: self.token = nextchar if self.token or (self.posix and quoted): - break # emit current token + break # emit current token else: continue elif self.state in self.quotes: quoted = True - if not nextchar: # end of file + if not nextchar: # end of file if self.debug >= 2: print("shlex: I see EOF in quotes state") # XXX what error should be raised here? @@ -212,35 +210,33 @@ class shlex: break else: self.state = 'a' - elif (self.posix and nextchar in self.escape and self.state - in self.escapedquotes): + elif self.posix and nextchar in self.escape and self.state in self.escapedquotes: escapedstate = self.state self.state = nextchar else: self.token += nextchar elif self.state in self.escape: - if not nextchar: # end of file + if not nextchar: # end of file if self.debug >= 2: print("shlex: I see EOF in escape state") # XXX what error should be raised here? raise ValueError("No escaped character") # In posix shells, only the quote itself or the escape # character may be escaped within quotes. - if (escapedstate in self.quotes and - nextchar != self.state and nextchar != escapedstate): + if escapedstate in self.quotes and nextchar != self.state and nextchar != escapedstate: self.token += self.state self.token += nextchar self.state = escapedstate elif self.state in ('a', 'c'): if not nextchar: - self.state = None # end of file + self.state = None # end of file break elif nextchar in self.whitespace: if self.debug >= 2: print("shlex: I see whitespace in word state") self.state = ' ' if self.token or (self.posix and quoted): - break # emit current token + break # emit current token else: continue elif nextchar in self.commenters: @@ -249,7 +245,7 @@ class shlex: if self.posix: self.state = ' ' if self.token or (self.posix and quoted): - break # emit current token + break # emit current token else: continue elif self.posix and nextchar in self.quotes: @@ -265,8 +261,7 @@ class shlex: self._pushback_chars.append(nextchar) self.state = ' ' break - elif (nextchar in self.wordchars or nextchar in self.quotes - or self.whitespace_split): + elif nextchar in self.wordchars or nextchar in self.quotes or self.whitespace_split: self.token += nextchar # Modified by argcomplete: Record last wordbreak position if nextchar in self.wordbreaks: @@ -280,7 +275,7 @@ class shlex: print("shlex: I see punctuation in word state") self.state = ' ' if self.token or (self.posix and quoted): - break # emit current token + break # emit current token else: continue result = self.token ```
diff of my_argparse.py ```diff diff --git a/argcomplete/my_argparse.py b/argcomplete/my_argparse.py index 0a01545..98c2ab0 100644 --- a/argcomplete/my_argparse.py +++ b/argcomplete/my_argparse.py @@ -19,8 +19,7 @@ _num_consumed_args = {} def action_is_satisfied(action): - ''' Returns False if the parse would raise an error if no more arguments are given to this action, True otherwise. - ''' + '''Returns False if the parse would raise an error if no more arguments are given to this action, True otherwise.''' num_consumed_args = _num_consumed_args.get(action, 0) if action.nargs in [OPTIONAL, ZERO_OR_MORE, REMAINDER]: @@ -39,8 +38,7 @@ def action_is_satisfied(action): def action_is_open(action): - ''' Returns True if action could consume more arguments (i.e., its pattern is open). - ''' + '''Returns True if action could consume more arguments (i.e., its pattern is open).''' num_consumed_args = _num_consumed_args.get(action, 0) if action.nargs in [ZERO_OR_MORE, ONE_OR_MORE, PARSER, REMAINDER]: @@ -53,7 +51,7 @@ def action_is_open(action): def action_is_greedy(action, isoptional=False): - ''' Returns True if action will necessarily consume the next argument. + '''Returns True if action will necessarily consume the next argument. isoptional indicates whether the argument is an optional (starts with -). ''' num_consumed_args = _num_consumed_args.get(action, 0) @@ -67,7 +65,7 @@ def action_is_greedy(action, isoptional=False): class IntrospectiveArgumentParser(ArgumentParser): - ''' The following is a verbatim copy of ArgumentParser._parse_known_args (Python 2.7.3), + '''The following is a verbatim copy of ArgumentParser._parse_known_args (Python 2.7.3), except for the lines that contain the string "Added by argcomplete". ''' @@ -88,7 +86,7 @@ class IntrospectiveArgumentParser(ArgumentParser): for i, mutex_action in enumerate(mutex_group._group_actions): conflicts = action_conflicts.setdefault(mutex_action, []) conflicts.extend(group_actions[:i]) - conflicts.extend(group_actions[i + 1:]) + conflicts.extend(group_actions[i + 1 :]) # find all option indices, and determine the arg_string_pattern # which has an 'O' if there is an option at an index, @@ -140,8 +138,7 @@ class IntrospectiveArgumentParser(ArgumentParser): # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) - if argument_values is not SUPPRESS \ - or isinstance(action, _SubParsersAction): + if argument_values is not SUPPRESS or isinstance(action, _SubParsersAction): try: action(self, namespace, argument_values, option_string) except BaseException: @@ -256,14 +253,14 @@ class IntrospectiveArgumentParser(ArgumentParser): for action, arg_count in zip(positionals, arg_counts): # Added by argcomplete self.active_actions.append(action) # Added by argcomplete for action, arg_count in zip(positionals, arg_counts): - args = arg_strings[start_index: start_index + arg_count] + args = arg_strings[start_index : start_index + arg_count] start_index += arg_count - _num_consumed_args[action] = len(args) # Added by argcomplete + _num_consumed_args[action] = len(args) # Added by argcomplete take_action(action, args) # slice off the Positionals that we just parsed and return the # index at which the Positionals' string args stopped - positionals[:] = positionals[len(arg_counts):] + positionals[:] = positionals[len(arg_counts) :] return start_index # consume Positionals and Optionals alternately, until we have @@ -277,10 +274,7 @@ class IntrospectiveArgumentParser(ArgumentParser): while start_index <= max_option_string_index: # consume any Positionals preceding the next option - next_option_string_index = min([ - index - for index in option_string_indices - if index >= start_index]) + next_option_string_index = min([index for index in option_string_indices if index >= start_index]) if start_index != next_option_string_index: positionals_end_index = consume_positionals(start_index) @@ -331,9 +325,7 @@ class IntrospectiveArgumentParser(ArgumentParser): # if no actions were used, report the error else: - names = [_get_action_name(action) - for action in group._group_actions - if action.help is not SUPPRESS] + names = [_get_action_name(action) for action in group._group_actions if action.help is not SUPPRESS] msg = _('one of the arguments %s is required') self.error(msg % ' '.join(names)) ```
kislyuk commented 1 year ago

Yes, because it is a vendored copy of a file that was extracted from a third party distribution.

tony commented 1 year ago

@kislyuk Are you open to:

tony commented 1 year ago

re: above

porting in python 3.6+ standard library code?

Looks like you partially addressed in #362