Skip to content

Commit

Permalink
pkg_resources complete adapter types
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed May 13, 2024
1 parent 37f0392 commit 8f2a102
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
if TYPE_CHECKING:
from _typeshed import StrPath

T = TypeVar("T")
_T = TypeVar("_T")
# Type aliases
_NestedStr = Union[str, Iterable[Union[str, Iterable["_NestedStr"]]]]
_InstallerType = Callable[["Requirement"], Optional["Distribution"]]
Expand All @@ -135,9 +135,17 @@
_MetadataType = Optional["IResourceProvider"]
# Any object works, but let's indicate we expect something like a module (optionally has __loader__ or __file__)
_ModuleLike = Union[object, types.ModuleType]
_AdapterType = Callable[..., Any] # Incomplete
_ProviderFactoryType = Callable[[_ModuleLike], "IResourceProvider"]
_DistFinderType = Callable[[_T, str, bool], Iterable["Distribution"]]
_NSHandlerType = Callable[[_T, str, str, types.ModuleType], Optional[str]]
_AdapterT = TypeVar(
"_AdapterT", _DistFinderType[Any], _ProviderFactoryType, _NSHandlerType[Any]
)


# Use _typeshed.importlib.LoaderProtocol once available https://github.com/python/typeshed/pull/11890
_LoaderProtocol = Any # Incomplete
class _LoaderProtocol(Protocol):
def load_module(self, fullname: str, /) -> types.ModuleType: ...


_PEP440_FALLBACK = re.compile(r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.I)
Expand All @@ -156,7 +164,7 @@ class PEP440Warning(RuntimeWarning):
_state_vars: Dict[str, str] = {}


def _declare_state(vartype: str, varname: str, initial_value: T) -> T:
def _declare_state(vartype: str, varname: str, initial_value: _T) -> _T:
_state_vars[varname] = vartype
return initial_value

Expand Down Expand Up @@ -391,7 +399,7 @@ class UnknownExtra(ResolutionError):
"""Distribution doesn't have an "extra feature" of the given name"""


_provider_factories: Dict[Type[_ModuleLike], _AdapterType] = {}
_provider_factories: Dict[Type[_ModuleLike], _ProviderFactoryType] = {}

PY_MAJOR = '{}.{}'.format(*sys.version_info)
EGG_DIST = 3
Expand All @@ -402,7 +410,7 @@ class UnknownExtra(ResolutionError):


def register_loader_type(
loader_type: Type[_ModuleLike], provider_factory: _AdapterType
loader_type: Type[_ModuleLike], provider_factory: _ProviderFactoryType
):
"""Register `provider_factory` to make providers for `loader_type`
Expand Down Expand Up @@ -1525,7 +1533,7 @@ class NullProvider:

egg_name: Optional[str] = None
egg_info: Optional[str] = None
loader: Optional["_LoaderProtocol"] = None
loader: Optional[_LoaderProtocol] = None
module_path: Optional[str] # Some subclasses can have a None module_path

def __init__(self, module: _ModuleLike):
Expand Down Expand Up @@ -2110,12 +2118,12 @@ def __init__(self, importer: zipimport.zipimporter):
self._setup_prefix()


_distribution_finders: Dict[
type, Callable[[object, str, bool], Iterable["Distribution"]]
] = _declare_state('dict', '_distribution_finders', {})
_distribution_finders: Dict[type, _DistFinderType[Any]] = _declare_state(
'dict', '_distribution_finders', {}
)


def register_finder(importer_type: type, distribution_finder: _AdapterType):
def register_finder(importer_type: Type[_T], distribution_finder: _DistFinderType[_T]):
"""Register `distribution_finder` to find distributions in sys.path items
`importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
Expand Down Expand Up @@ -2289,15 +2297,17 @@ def resolve_egg_link(path):

register_finder(importlib.machinery.FileFinder, find_on_path)

_namespace_handlers: Dict[
type, Callable[[object, str, str, types.ModuleType], Optional[str]]
] = _declare_state('dict', '_namespace_handlers', {})
_namespace_handlers: Dict[type, _NSHandlerType[Any]] = _declare_state(
'dict', '_namespace_handlers', {}
)
_namespace_packages: Dict[Optional[str], List[str]] = _declare_state(
'dict', '_namespace_packages', {}
)


def register_namespace_handler(importer_type: type, namespace_handler: _AdapterType):
def register_namespace_handler(
importer_type: Type[_T], namespace_handler: _NSHandlerType[_T]
):
"""Register `namespace_handler` to declare namespace packages
`importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
Expand Down Expand Up @@ -2442,9 +2452,9 @@ def fixup_namespace_packages(path_item: str, parent: Optional[str] = None):


def file_ns_handler(
importer: Optional[importlib.abc.PathEntryFinder],
path_item,
packageName,
importer: object,
path_item: StrPath,
packageName: str,
module: types.ModuleType,
):
"""Compute an ns-package subpath for a filesystem or zipfile importer"""
Expand All @@ -2467,7 +2477,7 @@ def file_ns_handler(


def null_ns_handler(
importer: Optional[importlib.abc.PathEntryFinder],
importer: object,
path_item: Optional[str],
packageName: Optional[str],
module: Optional[_ModuleLike],
Expand Down Expand Up @@ -3334,7 +3344,7 @@ def _always_object(classes):
return classes


def _find_adapter(registry: Mapping[type, _AdapterType], ob: object):
def _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT:
"""Return an adapter factory for `ob` from `registry`"""
types = _always_object(inspect.getmro(getattr(ob, '__class__', type(ob))))
for t in types:
Expand Down

0 comments on commit 8f2a102

Please sign in to comment.