Skip to content

Commit

Permalink
GH-109653: Defer import of importlib.metadata._adapters (#109829)
Browse files Browse the repository at this point in the history
* adapters

* Add comments for deferred imports with links to rationale.

* Add blurb

---------

Co-authored-by: Jason R. Coombs <[email protected]>
  • Loading branch information
AA-Turner and jaraco authored Mar 21, 2024
1 parent 667294d commit 6547330
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Lib/importlib/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import posixpath
import collections

from . import _adapters, _meta
from . import _meta
from ._collections import FreezableDefaultDict, Pair
from ._functools import method_cache, pass_none
from ._itertools import always_iterable, unique_everseen
Expand Down Expand Up @@ -456,6 +456,9 @@ def metadata(self) -> _meta.PackageMetadata:
Custom providers may provide the METADATA file or override this
property.
"""
# deferred for performance (python/cpython#109829)
from . import _adapters

opt_text = (
self.read_text('METADATA')
or self.read_text('PKG-INFO')
Expand Down
5 changes: 3 additions & 2 deletions Lib/importlib/resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from typing import Union, Optional, cast
from .abc import ResourceReader, Traversable

from ._adapters import wrap_spec

Package = Union[types.ModuleType, str]
Anchor = Package

Expand Down Expand Up @@ -109,6 +107,9 @@ def from_package(package: types.ModuleType):
Return a Traversable object for the given package.
"""
# deferred for performance (python/cpython#109829)
from ._adapters import wrap_spec

spec = wrap_spec(package)
reader = spec.loader.get_resource_reader(spec.name)
return reader.files()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deferred select imports in importlib.metadata and importlib.resources for a
14% speedup.

0 comments on commit 6547330

Please sign in to comment.