66import inspect
77import io
88import os
9- import sys
109import typing as t
1110from functools import lru_cache
1211from importlib import metadata
1312
14- if sys .version_info >= (3 , 10 ):
15- from importlib .metadata import packages_distributions as metadata_packages_distributions
16- else :
17-
18- def metadata_packages_distributions () -> t .Mapping [str , t .List [str ]]:
19- """Copied from Pyton 3.10 stdlib."""
20- pkg_to_dist = collections .defaultdict (list )
21- for dist in metadata .distributions ():
22- for pkg in (dist .read_text ("top_level.txt" ) or "" ).split ():
23- pkg_to_dist [pkg ].append (dist .metadata ["Name" ])
24- return dict (pkg_to_dist )
25-
26-
2713from plux .core .entrypoint import EntryPointDict , to_entry_point_dict
2814from plux .core .plugin import PluginSpec
2915
3016Distribution = metadata .Distribution
3117
3218
19+ def metadata_packages_distributions ():
20+ """Wrapper around ``importlib.metadata.packages_distributions``, which returns a mapping of top-level packages."""
21+ return metadata .packages_distributions ()
22+
23+
3324@lru_cache ()
34- def packages_distributions () -> t .Mapping [str , t . List [str ]]:
25+ def packages_distributions () -> t .Mapping [str , list [str ]]:
3526 """
3627 Cache wrapper around ``metadata.packages_distributions``, which returns a mapping of top-level packages to
3728 their distributions. This index is created by scanning all ``top_level.txt`` metadata files in the path.
@@ -53,7 +44,7 @@ def packages_distributions() -> t.Mapping[str, t.List[str]]:
5344 return distributions
5445
5546
56- def resolve_distribution_information (plugin_spec : PluginSpec ) -> t . Optional [ Distribution ] :
47+ def resolve_distribution_information (plugin_spec : PluginSpec ) -> Distribution | None :
5748 """
5849 Resolves for a PluginSpec the python distribution package it comes from. Currently, this raises an
5950 error for plugins that come from a namespace package (i.e., when a package is part of multiple
@@ -83,7 +74,7 @@ def entry_points_from_metadata_path(metadata_path: str) -> EntryPointDict:
8374
8475def resolve_entry_points (
8576 distributions : t .Iterable [Distribution ] = None ,
86- ) -> t . List [metadata .EntryPoint ]:
77+ ) -> list [metadata .EntryPoint ]:
8778 """
8879 Resolves all entry points using a combination of ``importlib.metadata``, and also follows entry points
8980 links in ``entry_points_editable.txt`` created by plux while building editable wheels.
@@ -120,7 +111,7 @@ def resolve_entry_points(
120111
121112def build_entry_point_index (
122113 entry_points : t .Iterable [metadata .EntryPoint ],
123- ) -> t . Dict [str , t . List [metadata .EntryPoint ]]:
114+ ) -> dict [str , list [metadata .EntryPoint ]]:
124115 """
125116 Organizes the given list of entry points into a dictionary that maps entry point groups to their
126117 respective entry points, which resembles the data structure of an ``entry_points.txt``.
@@ -138,24 +129,17 @@ def build_entry_point_index(
138129 return dict (result )
139130
140131
141- if sys .version_info >= (3 , 10 ):
142-
143- def parse_entry_points_text (text : str ) -> t .List [metadata .EntryPoint ]:
144- """
145- Parses the content of an ``entry_points.txt`` into a list of entry point objects.
146-
147- :param text: the string to parse
148- :return: a list of metadata EntryPoint objects
149- """
150- return metadata .EntryPoints ._from_text (text )
151-
152- else :
132+ def parse_entry_points_text (text : str ) -> list [metadata .EntryPoint ]:
133+ """
134+ Parses the content of an ``entry_points.txt`` into a list of entry point objects.
153135
154- def parse_entry_points_text (text : str ) -> t .List [metadata .EntryPoint ]:
155- return metadata .EntryPoint ._from_text (text )
136+ :param text: the string to parse
137+ :return: a list of metadata EntryPoint objects
138+ """
139+ return metadata .EntryPoints ._from_text (text )
156140
157141
158- def serialize_entry_points_text (index : t . Dict [str , t . List [metadata .EntryPoint ]]) -> str :
142+ def serialize_entry_points_text (index : dict [str , list [metadata .EntryPoint ]]) -> str :
159143 """
160144 Serializes an entry point index generated via ``build_entry_point_index`` into a string that can be
161145 written into an ``entry_point.txt``. Example::
@@ -185,7 +169,7 @@ class EntryPointsResolver:
185169 Interface for something that builds an entry point index.
186170 """
187171
188- def get_entry_points (self ) -> t . Dict [str , t . List [metadata .EntryPoint ]]:
172+ def get_entry_points (self ) -> dict [str , list [metadata .EntryPoint ]]:
189173 raise NotImplementedError
190174
191175
@@ -194,5 +178,5 @@ class MetadataEntryPointsResolver(EntryPointsResolver):
194178 Implementation that uses regular ``importlib.metadata`` methods to resolve entry points.
195179 """
196180
197- def get_entry_points (self ) -> t . Dict [str , t . List [metadata .EntryPoint ]]:
181+ def get_entry_points (self ) -> dict [str , list [metadata .EntryPoint ]]:
198182 return build_entry_point_index (resolve_entry_points (metadata .distributions ()))
0 commit comments