Skip to content

Commit

Permalink
[wip] Convert the remaining '%' formatters to '.format'. Fixes pypa#6973
Browse files Browse the repository at this point in the history
.
  • Loading branch information
jaraco committed Mar 6, 2020
1 parent bf0da0f commit afbc230
Show file tree
Hide file tree
Showing 39 changed files with 306 additions and 270 deletions.
13 changes: 7 additions & 6 deletions docs/pip_sphinxext.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ def run(self):
class PipOptions(rst.Directive):

def _format_option(self, option, cmd_name=None):
if cmd_name:
bookmark_line = ".. _`%s_%s`:" % (cmd_name, option._long_opts[0])
else:
bookmark_line = ".. _`%s`:" % option._long_opts[0]
bookmark_line = (
".. _`{cmd_name}_{option.long_opts[0]}`:"
if cmd_name else
".. _`{option.long_opts[0]}`:"
).format(**locals())
line = ".. option:: "
if option._short_opts:
line += option._short_opts[0]
if option._short_opts and option._long_opts:
line += ", %s" % option._long_opts[0]
line += ", " + option._long_opts[0]
elif option._long_opts:
line += option._long_opts[0]
if option.takes_value():
Expand All @@ -60,7 +61,7 @@ def _format_option(self, option, cmd_name=None):
opt_help = option.help.replace('%default', str(option.default))
# fix paths with sys.prefix
opt_help = opt_help.replace(sys.prefix, "<sys.prefix>")
return [bookmark_line, "", line, "", " %s" % opt_help, ""]
return [bookmark_line, "", line, "", " " + opt_help, ""]

def _format_options(self, options, cmd_name=None):
for option in options:
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/cli/main_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def create_main_parser():

# create command listing for description
description = [''] + [
'%-27s %s' % (name, command_info.summary)
'{name:27} {command_info.summary}'.format(**locals())
for name, command_info in commands_dict.items()
]
parser.description = '\n'.join(description)
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def __init__(self, *args, **kwargs):
optparse.IndentedHelpFormatter.__init__(self, *args, **kwargs)

def format_option_strings(self, option):
return self._format_option_strings(option, ' <%s>', ', ')
return self._format_option_strings(option)

def _format_option_strings(self, option, mvarfmt=' <{}>', optsep=', '):
"""
Return a comma-separated list of option strings and metavars.
:param option: tuple of (short opt, long opt), e.g: ('-f', '--format')
:param mvarfmt: metavar format string - evaluated as mvarfmt % metavar
:param mvarfmt: metavar format string
:param optsep: separator
"""
opts = []
Expand Down
21 changes: 7 additions & 14 deletions src/pip/_internal/cli/progress_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def update(self):

class BlueEmojiBar(IncrementalBar):

suffix = "%(percent)d%%"
suffix = "{percent}%"
bar_prefix = " "
bar_suffix = " "
phases = (u"\U0001F539", u"\U0001F537", u"\U0001F535") # type: Any
Expand Down Expand Up @@ -203,8 +203,8 @@ class BaseDownloadProgressBar(WindowsMixin, InterruptibleMixin,
DownloadProgressMixin):

file = sys.stdout
message = "%(percent)d%%"
suffix = "%(downloaded)s %(download_speed)s %(pretty_eta)s"
message = "{percent}%"
suffix = "{downloaded} {download_speed} {pretty_eta}"

# NOTE: The "type: ignore" comments on the following classes are there to
# work around https://github.com/python/typing/issues/241
Expand Down Expand Up @@ -238,7 +238,7 @@ class DownloadProgressSpinner(WindowsMixin, InterruptibleMixin,
DownloadProgressMixin, Spinner):

file = sys.stdout
suffix = "%(downloaded)s %(download_speed)s"
suffix = "{downloaded} {download_speed}"

def next_phase(self): # type: ignore
if not hasattr(self, "_phaser"):
Expand All @@ -247,17 +247,10 @@ def next_phase(self): # type: ignore

def update(self):
# type: () -> None
message = self.message % self
message = self.message.format(vars(self))
phase = self.next_phase()
suffix = self.suffix % self
line = ''.join([
message,
" " if message else "",
phase,
" " if suffix else "",
suffix,
])

suffix = self.suffix.format(vars(self))
line = " ".join(filter(None, (message, phase, suffix)))
self.writeln(line)


Expand Down
10 changes: 5 additions & 5 deletions src/pip/_internal/cli/req_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ def get_requirements(
opts = {'name': self.name}
if options.find_links:
raise CommandError(
'You must give at least one requirement to %(name)s '
'(maybe you meant "pip %(name)s %(links)s"?)' %
dict(opts, links=' '.join(options.find_links)))
'You must give at least one requirement to {name} '
'(maybe you meant "pip {name} {links}"?)'.format(
**dict(opts, links=' '.join(options.find_links))))
else:
raise CommandError(
'You must give at least one requirement to %(name)s '
'(see "pip help %(name)s")' % opts)
'You must give at least one requirement to {name} '
'(see "pip help {name}")'.format(**opts))

return requirements

Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/cli/spinners.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def finish(self, final_status):
# type: (str) -> None
if self._finished:
return
self._update("finished with status '%s'" % (final_status,))
self._update("finished with status '{final_status}'".format(**locals()))
self._finished = True


Expand Down
14 changes: 6 additions & 8 deletions src/pip/_internal/commands/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pip._internal.utils.misc import get_prog

BASE_COMPLETION = """
# pip %(shell)s completion start%(script)s# pip %(shell)s completion end
# pip {shell} completion start{script}# pip {shell} completion end
"""

COMPLETION_SCRIPTS = {
Expand All @@ -21,7 +21,7 @@
COMP_CWORD=$COMP_CWORD \\
PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )
}
complete -o default -F _pip_completion %(prog)s
complete -o default -F _pip_completion {prog}
""",
'zsh': """
function _pip_completion {
Expand All @@ -32,7 +32,7 @@
COMP_CWORD=$(( cword-1 )) \\
PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null ))
}
compctl -K _pip_completion %(prog)s
compctl -K _pip_completion {prog}
""",
'fish': """
function __fish_complete_pip
Expand All @@ -43,7 +43,7 @@
set -lx PIP_AUTO_COMPLETE 1
string split \\ -- (eval $COMP_WORDS[1])
end
complete -fa "(__fish_complete_pip)" -c %(prog)s
complete -fa "(__fish_complete_pip)" -c {prog}
""",
}

Expand Down Expand Up @@ -85,11 +85,9 @@ def run(self, options, args):
shell_options = ['--' + shell for shell in sorted(shells)]
if options.shell in shells:
script = textwrap.dedent(
COMPLETION_SCRIPTS.get(options.shell, '') % {
'prog': get_prog(),
}
COMPLETION_SCRIPTS.get(options.shell, '').format(prog=get_prog())
)
print(BASE_COMPLETION % {'script': script, 'shell': options.shell})
print(BASE_COMPLETION.format(script=script, shell=options.shell))
else:
sys.stderr.write(
'ERROR: You must pass {}\n' .format(' or '.join(shell_options))
Expand Down
5 changes: 3 additions & 2 deletions src/pip/_internal/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ def print_results(hits, name_column_width=None, terminal_width=None):
summary = textwrap.wrap(summary, target_width)
summary = ('\n' + ' ' * (name_column_width + 3)).join(summary)

line = '%-*s - %s' % (name_column_width,
'%s (%s)' % (name, latest), summary)
line = '{name_latest:{name_column_width}} - {summary}'.format(
name_latest='{name} ({latest})'.format(**locals()),
**locals)
try:
write_output(line)
if name in installed_packages:
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def run(self, options, args):
reqs_to_uninstall[canonicalize_name(req.name)] = req
if not reqs_to_uninstall:
raise InstallationError(
'You must give at least one requirement to %(name)s (see '
'"pip help %(name)s")' % dict(name=self.name)
'You must give at least one requirement to {self.name} (see '
'"pip help {self.name}")'.format(**locals())
)

protect_pip_from_modification_on_windows(
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def filename(self):
return netloc

name = urllib_parse.unquote(name)
assert name, ('URL %r produced no filename' % self._url)
assert name, ('URL {self._url!r} produced no filename'.format(**locals()))
return name

@property
Expand Down
8 changes: 2 additions & 6 deletions src/pip/_internal/operations/install/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,9 @@ def is_entrypoint_wrapper(name):
del console[k]

# Generate the console and GUI entry points specified in the wheel
scripts_to_generate.extend(
'%s = %s' % kv for kv in console.items()
)
scripts_to_generate.extend(map('{} = {}'.format, console.items()))

gui_scripts_to_generate = [
'%s = %s' % kv for kv in gui.items()
]
gui_scripts_to_generate = list(map('{} = {}'.format, gui.items()))

generated_console_scripts = [] # type: List[str]

Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/req/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ def _get_url_from_path(path, name):
if is_installable_dir(path):
return path_to_url(path)
raise InstallationError(
"Directory %r is not installable. Neither 'setup.py' "
"nor 'pyproject.toml' found." % name
"Directory {name!r} is not installable. Neither 'setup.py' "
"nor 'pyproject.toml' found.".format(**locals())
)
if not is_archive_file(path):
return None
Expand Down Expand Up @@ -339,7 +339,7 @@ def parse_req_from_line(name, line_source):
# wheel file
if link.is_wheel:
wheel = Wheel(link.filename) # can raise InvalidWheelFilename
req_as_string = "%s==%s" % (wheel.name, wheel.version)
req_as_string = "{wheel.name}=={wheel.version}".format(**locals())
else:
# set the req to the egg fragment. when it's not there, this
# will become an 'unnamed' requirement
Expand Down
5 changes: 3 additions & 2 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def update_editable(self, obtain=True):
if self.link.scheme == 'file':
# Static paths don't get updated
return
assert '+' in self.link.url, "bad url: %r" % self.link.url
assert '+' in self.link.url, "bad url: {self.link.url!r}".format(**locals())
vc_type, url = self.link.url.split('+', 1)
vcs_backend = vcs.get_backend(vc_type)
if vcs_backend:
Expand Down Expand Up @@ -701,7 +701,8 @@ def _get_archive_name(self, path, parentdir, rootdir):
def _clean_zip_name(name, prefix):
# type: (str, str) -> str
assert name.startswith(prefix + os.path.sep), (
"name %r doesn't start with prefix %r" % (name, prefix)
"name {name!r} doesn't start with prefix {prefix!r}"
.format(**locals())
)
name = name[len(prefix) + 1:]
name = name.replace(os.path.sep, '/')
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/req/req_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def get_requirement(self, name):
if project_name in self.requirements:
return self.requirements[project_name]

raise KeyError("No project with the name %r" % name)
raise KeyError("No project with the name {name!r}".format(**locals()))

@property
def all_requirements(self):
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def backslashreplace_decode_fn(err):
raw_bytes = (err.object[i] for i in range(err.start, err.end))
# Python 2 gave us characters - convert to numeric bytes
raw_bytes = (ord(b) for b in raw_bytes)
return u"".join(u"\\x%x" % c for c in raw_bytes), err.end
return u"".join(map(u"\\x{:x}".format, raw_bytes)), err.end
codecs.register_error(
"backslashreplace_decode",
backslashreplace_decode_fn,
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def copy2_fixed(src, dest):
pass
else:
if is_socket_file:
raise shutil.SpecialFileError("`%s` is a socket" % f)
raise shutil.SpecialFileError("`{f}` is a socket".format(**locals()))

raise

Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def format(self, record):
if self.add_timestamp:
# TODO: Use Formatter.default_time_format after dropping PY2.
t = self.formatTime(record, "%Y-%m-%dT%H:%M:%S")
prefix = '%s,%03d ' % (t, record.msecs)
prefix = '{t},{record.msecs:03} '.format(**locals())
prefix += " " * get_indentation()
formatted = "".join([
prefix + line
Expand Down
8 changes: 4 additions & 4 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ def ask_password(message):
def format_size(bytes):
# type: (float) -> str
if bytes > 1000 * 1000:
return '%.1f MB' % (bytes / 1000.0 / 1000)
return '{:.1} MB'.format(bytes / 1000.0 / 1000)
elif bytes > 10 * 1000:
return '%i kB' % (bytes / 1000)
return '{} kB'.format(int(bytes / 1000))
elif bytes > 1000:
return '%.1f kB' % (bytes / 1000.0)
return '{:.1} kB'.format(bytes / 1000.0)
else:
return '%i bytes' % bytes
return '{} bytes'.format(int(bytes))


def is_installable_dir(path):
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def url_to_path(url):
Convert a file: URL to a path.
"""
assert url.startswith('file:'), (
"You can only turn file: urls into filenames (not %r)" % url)
"You can only turn file: urls into filenames (not {url!r})".format(**locals()))

_, netloc, path, _, _ = urllib_parse.urlsplit(url)

Expand All @@ -46,8 +46,8 @@ def url_to_path(url):
netloc = '\\\\' + netloc
else:
raise ValueError(
'non-local file URIs are not supported on this platform: %r'
% url
'non-local file URIs are not supported on this platform: {url!r}'
.format(**locals())
)

path = urllib_request.url2pathname(netloc + path)
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/vcs/subversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _get_svn_url_rev(cls, location):
elif data.startswith('<?xml'):
match = _svn_xml_url_re.search(data)
if not match:
raise ValueError('Badly formatted data: %r' % data)
raise ValueError('Badly formatted data: {data!r}'.format(**locals()))
url = match.group(1) # get repository URL
revs = [int(m.group(1)) for m in _svn_rev_re.finditer(data)] + [0]
else:
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/vcs/versioncontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,9 @@ def run_command(
# In other words, the VCS executable isn't available
if e.errno == errno.ENOENT:
raise BadCommand(
'Cannot find command %r - do you have '
'%r installed and in your '
'PATH?' % (cls.name, cls.name))
'Cannot find command {cls.name!r} - do you have '
'{cls.name!r} installed and in your '
'PATH?'.format(**locals()))
else:
raise # re-raise exception if a different error occurred

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ def not_code_files_and_folders(path, names):


def _common_wheel_editable_install(tmpdir_factory, common_wheels, package):
wheel_candidates = list(common_wheels.glob('%s-*.whl' % package))
wheel_candidates = list(common_wheels.glob('{package}-*.whl'.format(**locals())))
assert len(wheel_candidates) == 1, wheel_candidates
install_dir = Path(str(tmpdir_factory.mktemp(package))) / 'install'
Wheel(wheel_candidates[0]).install_as_egg(install_dir)
(install_dir / 'EGG-INFO').rename(install_dir / '%s.egg-info' % package)
(install_dir / 'EGG-INFO').rename(install_dir / '{package}.egg-info'.format(**locals()))
assert compileall.compile_dir(str(install_dir), quiet=1)
return install_dir

Expand Down
2 changes: 1 addition & 1 deletion tests/data/src/chattymodule/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from setuptools import setup

print("HELLO FROM CHATTYMODULE %s" % (sys.argv[1],))
print("HELLO FROM CHATTYMODULE {sys.argv[1]}".format(**locals()))
print(os.environ)
print(sys.argv)
if "--fail" in sys.argv:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_completion_not_files_after_nonexpecting_option(
(e.g. ``pip install``)
"""
res, env = autocomplete(
words=('pip install %s r' % cl_opts),
words=('pip install {cl_opts} r'.format(**locals())),
cword='2',
cwd=data.completion_paths,
)
Expand Down
Loading

0 comments on commit afbc230

Please sign in to comment.