Skip to content

Convenience syntax for adding things to plugin registry #172

@seibert

Description

@seibert

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions