Skip to content

Instantly share code, notes, and snippets.

@obestwalter
Created April 7, 2018 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obestwalter/7370b7d7517bbf8fd9e5425413ce48ea to your computer and use it in GitHub Desktop.
Save obestwalter/7370b7d7517bbf8fd9e5425413ce48ea to your computer and use it in GitHub Desktop.
Pythoncamp 2018 - Lightning talk: pluggy

Shameless plug: pluggy powers pytest and tox

Make your tool extensible by 3rd party plugins by provding a pub/sub mechanism for plugin writers

WARNING: I have no idea what I am doing (yet)

Where?

Package on pypi

pip install pluggy

Documentation on readthedocs

Who?

... uses it?

Why?

e.g. why not just subclass stuff

pluggy’s approach is meant to let a designer think carefully about which objects are explicitly needed by an extension writer. This is in contrast to subclass-based extension systems which may expose unnecessary state and behaviour or encourage tight coupling in overlying frameworks.

-- pluggy docs

How does it work?

  • creates a namespace that provides hook specifications
  • namespace created from module or class namespace
  • hookspecs are empty functions defining implementation name and footprint
  • hookspec namespace provides decorators to be attached to implementors registering them as callback in a list of implementations
  • provides a hook caller calling implementations and collecting result(s)

Slightly advanced

  • Inspection: e.g. get_plugins()
  • There are options to influence call order, making it optional or stop executing as soon as one implementations returns a result
  • a hook implementation can be marked as hook wrapper to serve as a context manager of all other hook implementations of the same hook
  • hooks can be marked as historic for lazy evalution
  • blocking registration of plugins is also possible

See it in action: tox

  • hook specifications tox/hookspecs.py
  • implements its own hooks, e.g. tox/config.py:tox_addpotion()

More Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment