metagraph-dev / metagraph

Multi-target API for graph analytics with Dask

Home Page:https://metagraph.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convenience syntax for adding things to plugin registry

seibert opened this issue · comments

When writing tests, this pattern is common to create a registry with some plugins for testing:

    @abstract_algorithm('testing.scale')
    def testing_scale(a: Vector, scale: float)->Vector:  #pragma: no cover
        pass

    @concrete_algorithm('testing.scale', compiler="identity")
    def compiled_scale(a: NumpyVectorType, scale: float)->NumpyVectorType:
        return a * scale

    registry = PluginRegistry("test_subgraphs_plugin")
    registry.register(testing_scale)
    registry.register(compiled_scale)

I find myself often forgetting to actually register the plugins at the end. While there are good reasons for this format in actual plugins (which will likely use registry.register_from_modules()), I wish I had an alternate syntax like:

with PluginRegistry("test_subgraphs_plugin") as registry:
    @abstract_algorithm('testing.scale')
    def testing_scale(a: Vector, scale: float)->Vector:  #pragma: no cover
        pass

    @concrete_algorithm('testing.scale', compiler="identity")
    def compiled_scale(a: NumpyVectorType, scale: float)->NumpyVectorType:
        return a * scale

that would auto-register all the plugin-like things defined with the context manager block. Unfortunately, I think implementing this requires too much magic to behave as expected, like snapshotting locals() before and after the block to discover everything defined within it (which fails if a variable is redefined).

Since this is only for testing, perhaps a variant of register_from_modules(), like register_from_locals() which discovers everything in the local namespace of the fixture function (and is not recursive).