Skip to content

Commit 3be8e22

Browse files
authored
bpo-42403: Use @staticmethod in importlib (GH-23395)
Use @staticmethod on methods using @classmethod but don't use their cls parameter on the following classes: * BuiltinImporter * FrozenImporter * WindowsRegistryFinder * PathFinder Leave methods using @_requires_builtin or @_requires_frozen unchanged, since this decorator requires the wrapped method to have an extra parameter (cls or self).
1 parent a6109ef commit 3be8e22

File tree

4 files changed

+2355
-2356
lines changed

4 files changed

+2355
-2356
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,16 +761,16 @@ def find_module(cls, fullname, path=None):
761761
spec = cls.find_spec(fullname, path)
762762
return spec.loader if spec is not None else None
763763

764-
@classmethod
765-
def create_module(self, spec):
764+
@staticmethod
765+
def create_module(spec):
766766
"""Create a built-in module"""
767767
if spec.name not in sys.builtin_module_names:
768768
raise ImportError('{!r} is not a built-in module'.format(spec.name),
769769
name=spec.name)
770770
return _call_with_frames_removed(_imp.create_builtin, spec)
771771

772-
@classmethod
773-
def exec_module(self, module):
772+
@staticmethod
773+
def exec_module(module):
774774
"""Exec a built-in module"""
775775
_call_with_frames_removed(_imp.exec_builtin, module)
776776

@@ -831,8 +831,8 @@ def find_module(cls, fullname, path=None):
831831
"""
832832
return cls if _imp.is_frozen(fullname) else None
833833

834-
@classmethod
835-
def create_module(cls, spec):
834+
@staticmethod
835+
def create_module(spec):
836836
"""Use default semantics for module creation."""
837837

838838
@staticmethod

Lib/importlib/_bootstrap_external.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ class WindowsRegistryFinder:
754754
'\\Modules\\{fullname}\\Debug')
755755
DEBUG_BUILD = (_MS_WINDOWS and '_d.pyd' in EXTENSION_SUFFIXES)
756756

757-
@classmethod
758-
def _open_registry(cls, key):
757+
@staticmethod
758+
def _open_registry(key):
759759
try:
760760
return winreg.OpenKey(winreg.HKEY_CURRENT_USER, key)
761761
except OSError:
@@ -1219,8 +1219,8 @@ class _NamespaceLoader:
12191219
def __init__(self, name, path, path_finder):
12201220
self._path = _NamespacePath(name, path, path_finder)
12211221

1222-
@classmethod
1223-
def module_repr(cls, module):
1222+
@staticmethod
1223+
def module_repr(module):
12241224
"""Return repr for the module.
12251225
12261226
The method is deprecated. The import machinery does the job itself.
@@ -1261,8 +1261,8 @@ class PathFinder:
12611261

12621262
"""Meta path finder for sys.path and package __path__ attributes."""
12631263

1264-
@classmethod
1265-
def invalidate_caches(cls):
1264+
@staticmethod
1265+
def invalidate_caches():
12661266
"""Call the invalidate_caches() method on all path entry finders
12671267
stored in sys.path_importer_caches (where implemented)."""
12681268
for name, finder in list(sys.path_importer_cache.items()):
@@ -1271,8 +1271,8 @@ def invalidate_caches(cls):
12711271
elif hasattr(finder, 'invalidate_caches'):
12721272
finder.invalidate_caches()
12731273

1274-
@classmethod
1275-
def _path_hooks(cls, path):
1274+
@staticmethod
1275+
def _path_hooks(path):
12761276
"""Search sys.path_hooks for a finder for 'path'."""
12771277
if sys.path_hooks is not None and not sys.path_hooks:
12781278
_warnings.warn('sys.path_hooks is empty', ImportWarning)
@@ -1390,8 +1390,8 @@ def find_module(cls, fullname, path=None):
13901390
return None
13911391
return spec.loader
13921392

1393-
@classmethod
1394-
def find_distributions(cls, *args, **kwargs):
1393+
@staticmethod
1394+
def find_distributions(*args, **kwargs):
13951395
"""
13961396
Find distributions.
13971397

0 commit comments

Comments
 (0)