Define Stable Plugin Extension Boundaries
Core idea
A plugin boundary describes a narrow extension contract, including expected input/output, compatibility and failure handling. A host can register explicitly approved implementations and reject unknown names or incompatible versions. A Protocol helps static checking but does not sandbox a plugin; installed plugin code executes with the host process permissions.
from typing import Protocolclass Renderer(Protocol): def __call__(self, value: int) -> str: ...def render_plain(value: int) -> str: return str(value)plugins: dict[str, Renderer] = {'plain': render_plain}print(plugins['plain'](12))A callable contract keeps the host independent of a specific renderer.
Expected output:
12Check declared interface versions
registry = {}def register(name, version, fn): if version != 1 or name in registry: raise ValueError('incompatible or duplicate') registry[name] = fnregister('text', 1, str)try: register('new', 2, str)except ValueError: print(sorted(registry))The incompatible declaration is rejected before use. A declared version alone does not verify implementation correctness.
Expected output:
['text']Common mistake
Do not automatically load every discovered entry point. Use approved distributions and a version policy; execute untrusted extensions only with appropriate isolation outside this example.
Practice
Add a second approved renderer and test duplicate registration, unknown names and incompatible versions. Document the callable contract.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Plugin boundaries
Check your understanding of plugin boundaries.
What is printed?
plugins={"plain":str}print(plugins["plain"](5))What is printed?
registry={"a":1}print("b" in registry)What should an extension contract include?
Does Protocol isolate untrusted code?