Skip to content

Defer import of shutil which is only needed for help and usage #17334

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
Nov 22, 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: 3 additions & 3 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@

import os as _os
import re as _re
import shutil as _shutil
import sys as _sys

from gettext import gettext as _, ngettext
Expand Down Expand Up @@ -167,7 +166,8 @@ def __init__(self,

# default setting for width
if width is None:
width = _shutil.get_terminal_size().columns
import shutil
width = shutil.get_terminal_size().columns
width -= 2

self._prog = prog
Expand Down Expand Up @@ -264,7 +264,7 @@ def add_argument(self, action):
invocations.append(get_invocation(subaction))

# update the maximum item length
invocation_length = max([len(s) for s in invocations])
invocation_length = max(map(len, invocations))
Copy link
Member

Choose a reason for hiding this comment

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

@rhettinger Looks awesome, is there any performance difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was either this or a switch to a generation expression to save memory. Depending on the number of invocations, the map version has fewer global lookups and fewer turns around the ceval loop. The win is likely very minor though. Mostly, it reads a little better than before.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation :)

action_length = invocation_length + self._current_indent
self._action_max_length = max(self._action_max_length,
action_length)
Expand Down