Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing for load and select #483

Merged
merged 4 commits into from Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions importlib_metadata/__init__.py
Expand Up @@ -33,7 +33,7 @@
from importlib import import_module
from importlib.abc import MetaPathFinder
from itertools import starmap
from typing import Iterable, List, Mapping, Optional, Set, cast
from typing import Any, Iterable, List, Mapping, Match, Optional, Set, cast

__all__ = [
'Distribution',
Expand Down Expand Up @@ -175,12 +175,12 @@ class EntryPoint:
def __init__(self, name: str, value: str, group: str) -> None:
vars(self).update(name=name, value=value, group=group)

def load(self):
def load(self) -> Any:
"""Load the entry point from its definition. If only a module
is indicated by the value, return that module. Otherwise,
return the named object.
"""
match = self.pattern.match(self.value)
match = cast(Match, self.pattern.match(self.value))
module = import_module(match.group('module'))
attrs = filter(None, (match.group('attr') or '').split('.'))
return functools.reduce(getattr, attrs, module)
Expand Down Expand Up @@ -275,7 +275,7 @@ def __repr__(self):
"""
return '%s(%r)' % (self.__class__.__name__, tuple(self))

def select(self, **params):
def select(self, **params) -> EntryPoints:
"""
Select entry points from self that match the
given parameters (typically group and/or name).
Expand Down Expand Up @@ -769,6 +769,7 @@ class Lookup:
"""
A micro-optimized class for searching a (fast) path for metadata.
"""

def __init__(self, path: FastPath):
"""
Calculate all of the children representing metadata.
Expand Down