Skip to content
Explanation

The lookup model

A lookup describes which objects are available from a particular source. A consumer searches it by Python type, asking for an object it can use without needing to know how that object was registered or which concrete class supplies it. The owner chooses what to expose, leaving the consumer to ask for what it needs. Keeping those decisions separate lets availability change without making every consumer responsible for maintaining it.

A type selects matching members

The searched type expresses what the consumer needs. If CsvExporter and PdfExporter both inherit from Exporter, objects of either class can match a query for Exporter. An object can also match several searched types through its inheritance relationships. Matching does not require the object's concrete class to be the exact type in the query, although the common interface does not promise structural protocol matching.

Unlike a dictionary keyed by type, a lookup can contain several members matching the same query. Registering another exporter need not replace an existing one. A consumer can retrieve all matching exporters or ask for a single instance.

Asking for one match is different from specifying which match should win. Fixed lookups preserve their supplied order, whereas GenericLookup uses set storage and does not guarantee registration order. When that choice matters, the application must choose a source or selection policy that expresses its needs.

Absence is a normal query outcome. lookup(Exporter) returns None when no instance is available, while lookup_expected(Exporter) makes that absence an error by raising NoneLookupError. A result for Exporter still exists when its collections are empty, so a consumer can retain the question before an exporter becomes available. Errors raised by a source or by object construction propagate. They do not mean that the query simply found no match. The precise query behaviour is in Queries, results and items.

Objects, descriptions and views

An instance is the object the consumer uses. An item describes one member and provides access to its instance. A result is a view of the members matching one searched type. These serve different needs: using an exporter, presenting its name in a chooser, and following the available exporters are different operations.

The distinction becomes useful with deferred members. A convertor can describe a member's type, identifier and display name before constructing its object. Inspecting the metadata can avoid construction until a consumer retrieves the instance. An item is not an additional application object of the searched type, and its identifier is not a dictionary key: the public query interface searches by type and has no lookup-by-identifier operation. See Defer object creation for the registration pattern.

A result keeps the searched type, rather than fixing a list of instances at the time of the query. A retained dynamic result can expose different members after its source changes. Each collection read describes the view at that time, so an earlier returned collection does not acquire the new members. This is why a result is a view, while a collection read from it is a snapshot. Keeping a result also allows a consumer to observe possible changes and read the view again. The retention requirements and notification timing limits are explained in Lifetime and change notifications.

Availability belongs to an owner

For a GenericLookup, the owner normally keeps an InstanceContent and gives consumers the associated lookup. The content supplies operations to add, remove or replace members, and the lookup supplies queries. One content object attaches to one generic lookup.

flowchart TB
    O[Lookup owner] -->|changes membership| C[InstanceContent]
    C -->|supplies members| L[GenericLookup]
    U[Consumer] -->|queries by type| L
    U -->|reads or observes| R[Result for a searched type]
    L -->|supplies matching view| R

This separation gives the application a place to decide when an object should be available. For example, a document owner might expose an exporter while the document supports export and remove it when that capability is no longer relevant. Consumers need only query the document's lookup. Removing a member withdraws its availability from that source without revoking references that consumers have already retrieved.

Ownership is an architectural convention rather than an access-control mechanism. The application decides which components receive the content handle, while consumers retain ordinary access to the objects they retrieve and to those objects' interfaces and behaviour.

A LookupProvider has a narrower responsibility: it supplies an associated lookup through get_lookup(). Providing that lookup does not by itself imply responsibility for its membership or permission to change it.

Manage membership shows the owner-side operations, and Extend Lookups covers implementing the interfaces.

Scope and change are separate choices

A local lookup exposes objects relevant to a particular document, selection or operation. Passing that lookup to a consumer makes the source of availability explicit. Lookup.get_default() instead provides the application's common lookup when no more specific source is selected. A local query does not automatically fall back to that default. An application can combine sources explicitly when it needs both.

Neither scope determines whether membership changes. A local lookup can be fixed, and an application-wide source can be dynamic. fixed() and singleton() choose membership at creation without freezing the member objects. GenericLookup allows owner-controlled changes, while EntryPointLookup loads installed package contributions at construction and then keeps fixed membership. Default resolution can select a contributed lookup or provider, so the returned source determines its membership behaviour. See Entry points and the default lookup for resolution and caching rules.

Combining sources or following one source

ProxyLookup combines several source lookups. For example, a consumer might see document-specific exporters together with application-wide exporters. A direct query searches sources in order, while result collections combine their matches. Combining sources does not transfer control of their memberships to the composite's owner: changing the source list and changing a source's members are separate responsibilities.

DelegatedLookup follows one provider-supplied source at a time. It is useful when the consumer should keep querying the same outer lookup while the selected document, and hence the source lookup, changes. The application must call lookup_updated() after changing what the provider supplies, because ordinary queries do not refresh it automatically. Retained outer results continue to represent their searched types across the source switch, so consumers can keep their observation attached to the outer view.

Composition answers “what is available across these sources?” Delegation answers “what is available from the selected source?” Both preserve the consumer's type-based query interface. Combine and switch lookups shows their use, and Composition and providers specifies ordering, refresh behaviour and source-management limits.