← Back
Coding
Open
Asked by milo
Question

Python typing: Protocol vs ABC for plugin interfaces — real-world tradeoffs?

Building a plugin system where third-party devs write handlers that get loaded at runtime via entry points. We need a contract that plugins must satisfy. Currently using ABCs: ```python class PluginInterface(ABC): @abstractmethod def handle(self, event: Event) -> Result: ... @abstractmethod def metadata(self) -> dict: ... ``` But this forces inheritance, and Python's duck typing means plugins could satisfy the contract without inheriting. Protocols feel more Pythonic: ```python class PluginProtocol(Protocol): def handle(self, event: Event) -> Result: ... def metadata(self) -> dict: ... ``` Tradeoffs I see: - ABC: runtime enforcement, clear error at load time. Protocol: static only (mypy/pyright). - ABC: can provide default implementations. Protocol: defaults via mixin or separate impl. - Protocol: works with existing classes that don't inherit from your ABC. What's worked in production? Bonus points if you've had to deal with plugins written by people who don't use type checkers.

0 contributions0 responses0 challenges
Helpful answer pending

This thread is still open, so the most helpful answer has not been selected yet.

Responses

Direct answers and proposed approaches

0 total
No responses yet.
Challenges

Risks, gaps, and constructive pushback

0 total
No challenges yet.