Skip to content
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

GH-82874: Use f-strings instead of str.format within importlib.__init__.py #94016

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
19 changes: 9 additions & 10 deletions Lib/importlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ def find_loader(name, path=None):
try:
loader = sys.modules[name].__loader__
if loader is None:
raise ValueError('{}.__loader__ is None'.format(name))
raise ValueError(f"{name}.__loader__ is None")
else:
return loader
except KeyError:
pass
except AttributeError:
raise ValueError('{}.__loader__ is not set'.format(name)) from None
raise ValueError(f"{name}.__loader__ is not set") from None

spec = _bootstrap._find_spec(name, path)
# We won't worry about malformed specs (missing attributes).
if spec is None:
return None
if spec.loader is None:
if spec.submodule_search_locations is None:
raise ImportError('spec for {} missing loader'.format(name),
raise ImportError(f"spec for {name} missing loader",
name=name)
raise ImportError('namespace packages do not have loaders',
name=name)
Expand All @@ -117,8 +117,8 @@ def import_module(name, package=None):
if name.startswith('.'):
if not package:
msg = ("the 'package' argument is required to perform a relative "
"import for {!r}")
raise TypeError(msg.format(name))
f"import for {name!r}")
raise TypeError(msg)
for character in name:
if character != '.':
break
Expand All @@ -144,8 +144,8 @@ def reload(module):
raise TypeError("reload() argument must be a module")

if sys.modules.get(name) is not module:
msg = "module {} not in sys.modules"
raise ImportError(msg.format(name), name=name)
msg = f"module {name} not in sys.modules"
raise ImportError(msg, name=name)
if name in _RELOADING:
return _RELOADING[name]
_RELOADING[name] = module
Expand All @@ -155,9 +155,8 @@ def reload(module):
try:
parent = sys.modules[parent_name]
except KeyError:
msg = "parent {!r} not in sys.modules"
raise ImportError(msg.format(parent_name),
name=parent_name) from None
msg = f"parent {parent_name!r} not in sys.modules"
raise ImportError(msg, name=parent_name) from None
else:
pkgpath = parent.__path__
else:
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Nicolas Chauvat
Jerry Chen
Michael Chermside
Ingrid Cheung
Adam Chhina
Terry Chia
Albert Chin-A-Young
Adal Chiriliuc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prefer f-strings to ``.format`` in importlib.__init__.py.