Skip to content

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

Merged
rhettinger merged 1 commit into
python:masterfrom
rhettinger:improve_argparse
Nov 22, 2019
Merged

Defer import of shutil which is only needed for help and usage#17334
rhettinger merged 1 commit into
python:masterfrom
rhettinger:improve_argparse

Conversation

@rhettinger

Copy link
Copy Markdown
Contributor

Minor improvement that saves some downstream imports as well:

   shutil <- {os sys stat fnmatch collections errno}
   fnmatch <- {os posixpath re functools} 
   posixpath <- {os sys stat genericpatch}

Comment thread Lib/argparse.py

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 :)

@rhettinger rhettinger merged commit b4e5eea into python:master Nov 22, 2019
@rhettinger rhettinger deleted the improve_argparse branch November 22, 2019 06:51
jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
@blueyed

blueyed commented Jul 6, 2020

Copy link
Copy Markdown
Contributor

Note that it is still used via add_argument already (https://bugs.python.org/issue9348#msg373111).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip issue skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants