Repository navigation
Use f-strings instead of str.format within importlib #82874
Description
Activity
importlib is full of str.format calls, modernizing it to use f-strings is a slight performance win and much more readable.
Did you want this one to land in 3.9?
- added3.10 (EOL)end of lifeend of lifeand removed3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life
on Oct 19, 2020 Status: Waiting until after the importlib.metadata and importlib.resources backports drop support for both Python 2.7 and 3.5 as keeping them in sync would be a pain for maintainers (per jaraco on our sprint importlib-any chat today)
Both importlib_metadata and importlib_resources have dropped support for Python 2.7 and 3.5, this should now be unblocked.
I just have merged a change which makes many C-style formatting as fast as f-strings (bpo-28307) and am working on supporting more format codes (%d, %x, %f, etc). Using C-style formatting can be a good option if you want performance and backward compatibility. It was faster than str.format() even without that optimization.
- added3.11only security fixesonly security fixesand removed3.10 (EOL)end of lifeend of life
on May 14, 2021 - added3.12only security fixesonly security fixesand removed3.11only security fixesonly security fixes
on Aug 26, 2022 #17058 did this for
Lib/importlib/_bootstrap.pyremaining things to consider (not all need to be converted):
$ grep -R '[.]format' Lib/importlib/ Lib/importlib/_bootstrap_external.py: path_tmp = '{}.{}'.format(path, id(path)) Lib/importlib/_bootstrap_external.py: raise ValueError('{!r} is not alphanumeric'.format(optimization)) Lib/importlib/_bootstrap_external.py: almost_filename = '{}.{}{}'.format(almost_filename, _OPT, optimization) Lib/importlib/_bootstrap_external.py: _warnings.warn(msg.format(portions[0]), ImportWarning) Lib/importlib/_bootstrap_external.py: raise ImportError('Non-code object in {!r}'.format(bytecode_path), Lib/importlib/_bootstrap_external.py: key = registry_key.format(fullname=fullname, Lib/importlib/_bootstrap_external.py: 'returns None'.format(module.__name__)) Lib/importlib/_bootstrap_external.py: return '_NamespacePath({!r})'.format(self._path) Lib/importlib/_bootstrap_external.py: new_name = '{}.{}'.format(name, suffix.lower()) Lib/importlib/_bootstrap_external.py: return 'FileFinder({!r})'.format(self.path) Lib/importlib/metadata/__init__.py: return text and map('"{}"'.format, text.splitlines()) Lib/importlib/resources/_adapters.py: "Invalid mode value '{}', only 'r' and 'rb' are supported".format(mode) Lib/importlib/_bootstrap.py: print(message.format(*args), file=sys.stderr) Lib/importlib/__init__.py: raise ValueError('{}.__loader__ is None'.format(name)) Lib/importlib/__init__.py: raise ValueError('{}.__loader__ is not set'.format(name)) from None Lib/importlib/__init__.py: raise ImportError('spec for {} missing loader'.format(name), Lib/importlib/__init__.py: raise TypeError(msg.format(name)) Lib/importlib/__init__.py: raise ImportError(msg.format(name), name=name) Lib/importlib/__init__.py: raise ImportError(msg.format(parent_name), Lib/importlib/util.py: raise ValueError('{}.__spec__ is not set'.format(name)) from None Lib/importlib/util.py: raise ValueError('{}.__spec__ is None'.format(name)) Lib/importlib/util.py: raise ValueError('{}.__spec__ is not set'.format(name)) from None Lib/importlib/util.py: raise ValueError('{}.__spec__ is None'.format(name))- added a commit that references this issue
on Oct 7, 2022 - added a commit that references this issue
on Oct 8, 2022
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: