Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-30152: Reduce the number of imports for argparse.#1269
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
eccaf6979b085f30c87cd46c77d3d8a622cdad5bae578af9048a2abf502bf6ac449538025688dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -84,15 +84,12 @@ | ||
| import collections as _collections | ||
| import copy as _copy | ||
| import os as _os | ||
| import re as _re | ||
| import sys as _sys | ||
| import textwrap as _textwrap | ||
| from gettext import gettext as _, ngettext | ||
| SUPPRESS = '==SUPPRESS==' | ||
| OPTIONAL = '?' | ||
| @@ -137,10 +134,16 @@ def _get_args(self): | ||
| return [] | ||
| def _ensure_value(namespace, name, value): | ||
| if getattr(namespace, name, None) is None: | ||
| setattr(namespace, name, value) | ||
| return getattr(namespace, name) | ||
| def _copy_items(items): | ||
| if items is None: | ||
| return [] | ||
| # The copy module is used only in the 'append' and 'append_const' | ||
| # actions, and it is needed only when the default value isn't a list. | ||
| # Delay its import for speeding up the common case. | ||
| if type(items) is list: | ||
| return items[:] | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not items.copy()? MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is shorter. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And it's faster. | ||
| import copy | ||
| return copy.copy(items) | ||
| # =============== | ||
| @@ -619,12 +622,17 @@ def _iter_indented_subactions(self, action): | ||
| def _split_lines(self, text, width): | ||
| text = self._whitespace_matcher.sub(' ', text).strip() | ||
| return _textwrap.wrap(text, width) | ||
| # The textwrap module is used only for formatting help. | ||
| # Delay its import for speeding up the common usage of argparse. | ||
| import textwrap | ||
| return textwrap.wrap(text, width) | ||
| def _fill_text(self, text, width, indent): | ||
| text = self._whitespace_matcher.sub(' ', text).strip() | ||
| return _textwrap.fill(text, width, initial_indent=indent, | ||
| subsequent_indent=indent) | ||
| import textwrap | ||
| return textwrap.fill(text, width, | ||
| initial_indent=indent, | ||
| subsequent_indent=indent) | ||
| def _get_help_string(self, action): | ||
| return action.help | ||
| @@ -952,7 +960,8 @@ def __init__(self, | ||
| metavar=metavar) | ||
| def __call__(self, parser, namespace, values, option_string=None): | ||
| items = _copy.copy(_ensure_value(namespace, self.dest, [])) | ||
| items = getattr(namespace, self.dest, None) | ||
| items = _copy_items(items) | ||
| items.append(values) | ||
| setattr(namespace, self.dest, items) | ||
| @@ -978,7 +987,8 @@ def __init__(self, | ||
| metavar=metavar) | ||
| def __call__(self, parser, namespace, values, option_string=None): | ||
| items = _copy.copy(_ensure_value(namespace, self.dest, [])) | ||
| items = getattr(namespace, self.dest, None) | ||
| items = _copy_items(items) | ||
| items.append(self.const) | ||
| setattr(namespace, self.dest, items) | ||
| @@ -1000,8 +1010,10 @@ def __init__(self, | ||
| help=help) | ||
| def __call__(self, parser, namespace, values, option_string=None): | ||
| new_count = _ensure_value(namespace, self.dest, 0) + 1 | ||
| setattr(namespace, self.dest, new_count) | ||
| count = getattr(namespace, self.dest, None) | ||
| if count is None: | ||
| count = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use getattr(namespace, self.dest, 0) and not have the if test? Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If think the current code is equivalent to | ||
| setattr(namespace, self.dest, count + 1) | ||
| class _HelpAction(Action): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| import sys | ||
| from types import MappingProxyType, DynamicClassAttribute | ||
| from functools import reduce | ||
| from operator import or_ as _or_ | ||
| # try _collections first to reduce startup cost | ||
| try: | ||
| @@ -744,11 +742,10 @@ def __xor__(self, other): | ||
| def __invert__(self): | ||
| members, uncovered = _decompose(self.__class__, self._value_) | ||
| inverted_members = [ | ||
| m for m in self.__class__ | ||
| if m not in members and not m._value_ & self._value_ | ||
| ] | ||
| inverted = reduce(_or_, inverted_members, self.__class__(0)) | ||
| inverted = self.__class__(0) | ||
| for m in self.__class__: | ||
| if m not in members and not (m._value_ & self._value_): | ||
| inverted = inverted | m | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how this change is related to removing imports. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| return self.__class__(inverted) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not isinstance(items, list)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
listsubclass can override__copy__or__reduce__.