33import os
44import re
55import abc
6- import csv
76import sys
87import json
98import zipp
1918import posixpath
2019import collections
2120
22- from .compat . py311 import relative_fix
23- from . import _adapters , _meta , _py39compat
21+ from . import _meta
22+ from .compat import py39 , py311
2423from ._collections import FreezableDefaultDict , Pair
2524from ._compat import (
2625 NullFinder ,
3433from importlib import import_module
3534from importlib .abc import MetaPathFinder
3635from itertools import starmap
37- from typing import Iterable , List , Mapping , Optional , Set , cast
36+ from typing import Any , Iterable , List , Mapping , Match , Optional , Set , cast
3837
3938__all__ = [
4039 'Distribution' ,
@@ -176,12 +175,12 @@ class EntryPoint:
176175 def __init__ (self , name : str , value : str , group : str ) -> None :
177176 vars (self ).update (name = name , value = value , group = group )
178177
179- def load (self ):
178+ def load (self ) -> Any :
180179 """Load the entry point from its definition. If only a module
181180 is indicated by the value, return that module. Otherwise,
182181 return the named object.
183182 """
184- match = self .pattern .match (self .value )
183+ match = cast ( Match , self .pattern .match (self .value ) )
185184 module = import_module (match .group ('module' ))
186185 attrs = filter (None , (match .group ('attr' ) or '' ).split ('.' ))
187186 return functools .reduce (getattr , attrs , module )
@@ -276,12 +275,12 @@ def __repr__(self):
276275 """
277276 return '%s(%r)' % (self .__class__ .__name__ , tuple (self ))
278277
279- def select (self , ** params ):
278+ def select (self , ** params ) -> EntryPoints :
280279 """
281280 Select entry points from self that match the
282281 given parameters (typically group and/or name).
283282 """
284- return EntryPoints (ep for ep in self if _py39compat .ep_matches (ep , ** params ))
283+ return EntryPoints (ep for ep in self if py39 .ep_matches (ep , ** params ))
285284
286285 @property
287286 def names (self ) -> Set [str ]:
@@ -462,6 +461,9 @@ def metadata(self) -> _meta.PackageMetadata:
462461 Custom providers may provide the METADATA file or override this
463462 property.
464463 """
464+ # deferred for performance (python/cpython#109829)
465+ from . import _adapters
466+
465467 opt_text = (
466468 self .read_text ('METADATA' )
467469 or self .read_text ('PKG-INFO' )
@@ -523,6 +525,10 @@ def make_file(name, hash=None, size_str=None):
523525
524526 @pass_none
525527 def make_files (lines ):
528+ # Delay csv import, since Distribution.files is not as widely used
529+ # as other parts of importlib.metadata
530+ import csv
531+
526532 return starmap (make_file , csv .reader (lines ))
527533
528534 @pass_none
@@ -564,7 +570,7 @@ def _read_files_egginfo_installed(self):
564570 return
565571
566572 paths = (
567- relative_fix ((subdir / name ).resolve ())
573+ py311 . relative_fix ((subdir / name ).resolve ())
568574 .relative_to (self .locate_file ('' ).resolve (), walk_up = True )
569575 .as_posix ()
570576 for name in text .splitlines ()
@@ -873,8 +879,9 @@ class MetadataPathFinder(NullFinder, DistributionFinder):
873879 of Python that do not have a PathFinder find_distributions().
874880 """
875881
882+ @classmethod
876883 def find_distributions (
877- self , context = DistributionFinder .Context ()
884+ cls , context = DistributionFinder .Context ()
878885 ) -> Iterable [PathDistribution ]:
879886 """
880887 Find distributions.
@@ -884,7 +891,7 @@ def find_distributions(
884891 (or all names if ``None`` indicated) along the paths in the list
885892 of directories ``context.path``.
886893 """
887- found = self ._search_paths (context .name , context .path )
894+ found = cls ._search_paths (context .name , context .path )
888895 return map (PathDistribution , found )
889896
890897 @classmethod
@@ -895,6 +902,7 @@ def _search_paths(cls, name, paths):
895902 path .search (prepared ) for path in map (FastPath , paths )
896903 )
897904
905+ @classmethod
898906 def invalidate_caches (cls ) -> None :
899907 FastPath .__new__ .cache_clear ()
900908
@@ -992,7 +1000,7 @@ def version(distribution_name: str) -> str:
9921000
9931001_unique = functools .partial (
9941002 unique_everseen ,
995- key = _py39compat .normalized_name ,
1003+ key = py39 .normalized_name ,
9961004)
9971005"""
9981006Wrapper for ``distributions`` to return unique distributions by name.
0 commit comments