GH-109653: Defer import of ``importlib.metadata._adapters`` (#109829)

* adapters

* Add comments for deferred imports with links to rationale.

* Add blurb

---------

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
Adam Turner 2024-03-21 03:49:10 +00:00 committed by GitHub
parent 667294d5b2
commit 6547330f4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 3 deletions

View File

@ -18,7 +18,7 @@ import itertools
import posixpath import posixpath
import collections import collections
from . import _adapters, _meta from . import _meta
from ._collections import FreezableDefaultDict, Pair from ._collections import FreezableDefaultDict, Pair
from ._functools import method_cache, pass_none from ._functools import method_cache, pass_none
from ._itertools import always_iterable, unique_everseen from ._itertools import always_iterable, unique_everseen
@ -456,6 +456,9 @@ class Distribution(DeprecatedNonAbstract):
Custom providers may provide the METADATA file or override this Custom providers may provide the METADATA file or override this
property. property.
""" """
# deferred for performance (python/cpython#109829)
from . import _adapters
opt_text = ( opt_text = (
self.read_text('METADATA') self.read_text('METADATA')
or self.read_text('PKG-INFO') or self.read_text('PKG-INFO')

View File

@ -12,8 +12,6 @@ import itertools
from typing import Union, Optional, cast from typing import Union, Optional, cast
from .abc import ResourceReader, Traversable from .abc import ResourceReader, Traversable
from ._adapters import wrap_spec
Package = Union[types.ModuleType, str] Package = Union[types.ModuleType, str]
Anchor = Package Anchor = Package
@ -109,6 +107,9 @@ def from_package(package: types.ModuleType):
Return a Traversable object for the given package. Return a Traversable object for the given package.
""" """
# deferred for performance (python/cpython#109829)
from ._adapters import wrap_spec
spec = wrap_spec(package) spec = wrap_spec(package)
reader = spec.loader.get_resource_reader(spec.name) reader = spec.loader.get_resource_reader(spec.name)
return reader.files() return reader.files()

View File

@ -0,0 +1,2 @@
Deferred select imports in importlib.metadata and importlib.resources for a
14% speedup.