Skip to content
Reference

Entry points and the default lookup

EntryPointLookup constructs members from contributions registered by installed Python packages. Lookup.get_default() uses that mechanism to resolve the application's common lookup. Both are available from the package root:

Import discovery interfaces
from lookups import EntryPointLookup, Lookup

See Public API for the exported surface and Queries, results and items for the shared query interface.

Entry-point lookup

Registration metadata

An entry point associates a name with an import target within a named group. For example, a contributing package can declare this mapping in pyproject.toml:

pyproject.toml
[project.entry-points."my_application.roles"]
reports = "my_package.reports:create_report_writer"
Field Example Meaning
Group my_application.roles The group selected by EntryPointLookup(group).
Name reports The contribution's name in package metadata. It is not the searched type or a lookup item identifier.
Target my_package.reports:create_report_writer The object loaded from the named module and then called without arguments.

The target can be a class or another callable. It must accept a no-argument call and return a non-None member.

The package must be installed with its entry-point metadata available to the Python process. Placing a module on the import path alone does not register a contribution.

Construction, queries and lifetime

EntryPointLookup(group: str) reads the installed metadata for that group, loads every target and calls each target immediately. It passes the returned objects to SimpleLookup, which retains them strongly with fixed membership. A missing group, or a group with no contributions, produces an empty lookup.

Query installed contributions
from lookups import EntryPointLookup

lookup = EntryPointLookup("example.unregistered.roles")
assert lookup.lookup(object) is None  # Assumes no installed package uses this group.
assert lookup.lookup_all(object) == ()

Queries use the common type-based interface. Entry-point names do not provide an additional name-based query API. Its query and result interfaces follow SimpleLookup.

Membership stays fixed after construction. Installing another package does not refresh an existing entry-point lookup, and there is no refresh method. Objects within the lookup can still change internally.

Contribution code runs during construction

Loading can import modules, and calling a target executes contribution code before any lookup query. Import, loading and target-call errors propagate, so there is no per-contribution error isolation. A non-callable target or one requiring arguments fails at the call, and a returned None is rejected by SimpleLookup with ValueError('None cannot be a lookup member').

EntryPointLookup

Bases: SimpleLookup

Load every contribution in a named entry-point group at construction.

Each target is loaded and called with no arguments immediately. Membership then remains fixed, so installing another package does not refresh this lookup. Loading and constructor errors propagate, with no per-contribution isolation.

Methods:

__init__
__init__(group)

Discover and construct the group's installed contributions.

Parameters:

Name Type Description Default
group str

Entry-point group name. A missing or empty group gives an empty lookup. The default lookup uses lookup.default.

required

Each target must be callable without arguments and return a non-None member.

Default lookup resolution

Lookup.get_default() takes no arguments and returns a Lookup. On initial resolution it constructs an EntryPointLookup for lookup.default, with singular lookup. Registration uses the same group/name/target shape:

pyproject.toml
[project.entry-points."lookup.default"]
roles = "my_package.roles:create_roles"

The target's return type determines which branch is selected. Resolution follows this order:

Condition Returned lookup and caching
A default provider has already been selected Call that provider's get_lookup() and return its answer. This takes precedence over a cached lookup.
A default lookup has already been cached, without a selected provider Return the cached lookup.
Initial discovery finds a contributed Lookup that also implements LookupProvider Cache that object as both lookup and provider, call its get_lookup() and return the answer. Later default requests call the same provider again.
Initial discovery finds a contributed Lookup without LookupProvider Cache and return that lookup.
No contributed Lookup exists, but a LookupProvider does Construct, cache and return a DelegatedLookup around that provider. Its constructor calls get_lookup() immediately.
Neither a Lookup nor a LookupProvider exists Cache and return a ProxyLookup over the entry-point lookup, exposing its ordinary contributed members.

All contributions are loaded and called before the initial type-based selection, including contributions that the selected default does not expose. A contributed Lookup takes precedence over a separate provider, so discovery does not combine all contributed lookups into one composite.

When the group is empty, the final branch still returns a cached ProxyLookup, whose queries find no members. An empty group does not make get_default() return None.

Provider refresh and limits

A selected object implementing both Lookup and LookupProvider can supply a different lookup on each default request. By contrast, a provider-only contribution produces a cached DelegatedLookup: later get_default() calls return that wrapper without re-reading the provider. Its source changes only through the delegated lookup's explicit lookup_updated() operation.

Selection between competing default lookups or providers has no defined priority policy. Entry-point names do not establish which package wins. Concurrent initial resolution also has no general guarantee.

Loading, construction and provider errors propagate to the caller.

Default resolution has these configuration boundaries:

Concern Supported boundary
Default selection No public reset or set-default API, or supported configuration file for changing the default group.
Execution scope Default resolution is independent of experimental GlobalLookup.
Internal controls _DEFAULT_* attributes are test controls, not application configuration.
Test groups lookups.test_* groups are fixtures, not application extension contracts.