Skip to content

Commit 2d098cd

Browse files
committed
Document the protocol design in more detail
1 parent 0915a9d commit 2d098cd

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

variantlib/plugins/_subprocess.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def load_plugins(plugin_apis: list[str]) -> Generator[PluginType]:
4646
else:
4747
plugin_instance = plugin_callable
4848

49+
# We cannot use isinstance() here since some of the PluginType methods
50+
# are optional. Instead, we use @abstractmethod decorator to naturally
51+
# annotate required methods, and the remaining methods are optional.
4952
required_attributes = PluginType.__abstractmethods__
5053
if missing_attributes := required_attributes.difference(dir(plugin_instance)):
5154
raise TypeError(

variantlib/protocols.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@
77
# Must be standalone.
88
# =============================================================================== #
99

10+
"""
11+
Protocols for the plugin API
12+
13+
This file provides a number of protocols outlining the design of the plugin API.
14+
The classes serve a dual purpose: they are meant to help plugin authors
15+
visualize the plugin API, and they can also serve as abstract base classes
16+
for an actual plugin implementation. This module is intended to be fully
17+
self-contained and standalone, and it can be easily vendored into a plugin.
18+
19+
The central class is PluginType, which defined the API provided by the plugin.
20+
The actual API can be implemented as regular methods on a class (optionally
21+
subclassing PluginType), class methods, static methods or top-level functions
22+
in a module.
23+
24+
The methods declared as abstract here must be defined by the plugin.
25+
The remaining methods are optional. If they are not defined, the caller
26+
assumes a default value equivalent to the implementation provided in the class.
27+
This means that plugins do not need to define them even if they do not subclass
28+
the protocol classes.
29+
"""
30+
1031
from __future__ import annotations
1132

1233
from abc import abstractmethod

0 commit comments

Comments
 (0)