Skip to content

Commit 8635240

Browse files
committed
Expand docstrings to elaborate on the purpose and details of the interfaces.
1 parent 9ff0824 commit 8635240

File tree

1 file changed

+67
-13
lines changed

1 file changed

+67
-13
lines changed

importlib_metadata/__init__.py

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ class Distribution(DeprecatedNonAbstract):
360360
361361
Custom providers may derive from this class and define
362362
the abstract methods to provide a concrete implementation
363-
for their environment.
363+
for their environment. Some providers may opt to override
364+
the default implementation of some properties to bypass
365+
the file-reading mechanism.
364366
"""
365367

366368
@abc.abstractmethod
@@ -374,11 +376,10 @@ def read_text(self, filename) -> Optional[str]:
374376
375377
- METADATA: The distribution metadata including fields
376378
like Name and Version and Description.
377-
- entry_points.txt: A series of entry points defined by
378-
the Setuptools spec in an ini format with sections
379-
representing the groups.
380-
- RECORD: A record of files as installed by a typical
381-
installer.
379+
- entry_points.txt: A series of entry points as defined in
380+
`this spec <https://packaging.python.org/en/latest/specifications/entry-points/#file-format>`_.
381+
- RECORD: A record of files according to
382+
`this spec <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-record-file>`_.
382383
383384
A package may provide any set of files, including those
384385
not listed here or none at all.
@@ -454,7 +455,11 @@ def metadata(self) -> _meta.PackageMetadata:
454455
"""Return the parsed metadata for this Distribution.
455456
456457
The returned object will have keys that name the various bits of
457-
metadata. See PEP 566 for details.
458+
metadata per the
459+
`Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_.
460+
461+
Custom providers may provide the METADATA file or override this
462+
property.
458463
"""
459464
opt_text = (
460465
self.read_text('METADATA')
@@ -484,6 +489,12 @@ def version(self) -> str:
484489

485490
@property
486491
def entry_points(self) -> EntryPoints:
492+
"""
493+
Return EntryPoints for this distribution.
494+
495+
Custom providers may provide the ``entry_points.txt`` file
496+
or override this property.
497+
"""
487498
return EntryPoints._from_text_for(self.read_text('entry_points.txt'), self)
488499

489500
@property
@@ -496,6 +507,10 @@ def files(self) -> Optional[List[PackagePath]]:
496507
(i.e. RECORD for dist-info, or installed-files.txt or
497508
SOURCES.txt for egg-info) is missing.
498509
Result may be empty if the metadata exists but is empty.
510+
511+
Custom providers are recommended to provide a "RECORD" file (in
512+
``read_text``) or override this property to allow for callers to be
513+
able to resolve filenames provided by the package.
499514
"""
500515

501516
def make_file(name, hash=None, size_str=None):
@@ -523,7 +538,7 @@ def skip_missing_files(package_paths):
523538

524539
def _read_files_distinfo(self):
525540
"""
526-
Read the lines of RECORD
541+
Read the lines of RECORD.
527542
"""
528543
text = self.read_text('RECORD')
529544
return text and text.splitlines()
@@ -637,6 +652,9 @@ def _load_json(self, filename):
637652
class DistributionFinder(MetaPathFinder):
638653
"""
639654
A MetaPathFinder capable of discovering installed distributions.
655+
656+
Custom providers should implement this interface in order to
657+
supply metadata.
640658
"""
641659

642660
class Context:
@@ -684,11 +702,18 @@ def find_distributions(self, context=Context()) -> Iterable[Distribution]:
684702

685703
class FastPath:
686704
"""
687-
Micro-optimized class for searching a path for
688-
children.
705+
Micro-optimized class for searching a root for children.
706+
707+
Root is a path on the file system that may contain metadata
708+
directories either as natural directories or within a zip file.
689709
690710
>>> FastPath('').children()
691711
['...']
712+
713+
FastPath objects are cached and recycled for any given root.
714+
715+
>>> FastPath('foobar') is FastPath('foobar')
716+
True
692717
"""
693718

694719
@functools.lru_cache() # type: ignore
@@ -730,7 +755,18 @@ def lookup(self, mtime):
730755

731756

732757
class Lookup:
758+
"""
759+
A micro-optimized class for searching a (fast) path for metadata.
760+
"""
733761
def __init__(self, path: FastPath):
762+
"""
763+
Calculate all of the children representing metadata.
764+
765+
From the children in the path, calculate early all of the
766+
children that appear to represent metadata (infos) or legacy
767+
metadata (eggs).
768+
"""
769+
734770
base = os.path.basename(path.root).lower()
735771
base_is_egg = base.endswith(".egg")
736772
self.infos = FreezableDefaultDict(list)
@@ -751,7 +787,10 @@ def __init__(self, path: FastPath):
751787
self.infos.freeze()
752788
self.eggs.freeze()
753789

754-
def search(self, prepared):
790+
def search(self, prepared: Prepared):
791+
"""
792+
Yield all infos and eggs matching the Prepared query.
793+
"""
755794
infos = (
756795
self.infos[prepared.normalized]
757796
if prepared
@@ -767,13 +806,28 @@ def search(self, prepared):
767806

768807
class Prepared:
769808
"""
770-
A prepared search for metadata on a possibly-named package.
809+
A prepared search query for metadata on a possibly-named package.
810+
811+
Pre-calculates the normalization to prevent repeated operations.
812+
813+
>>> none = Prepared(None)
814+
>>> none.normalized
815+
>>> none.legacy_normalized
816+
>>> bool(none)
817+
False
818+
>>> sample = Prepared('Sample__Pkg-name.foo')
819+
>>> sample.normalized
820+
'sample_pkg_name_foo'
821+
>>> sample.legacy_normalized
822+
'sample__pkg_name.foo'
823+
>>> bool(sample)
824+
True
771825
"""
772826

773827
normalized = None
774828
legacy_normalized = None
775829

776-
def __init__(self, name):
830+
def __init__(self, name: Optional[str]):
777831
self.name = name
778832
if name is None:
779833
return

0 commit comments

Comments
 (0)