Skip to content

Can't load instances which define hook specs #19

@goodboy

Description

@goodboy

You currently can't instantiate a class which defines hook specs prior to loading

import pluggy

hookspec = pluggy.HookspecMarker('example')

class SpecSpace(object):
    @hookspec
    def myhook(self, stuff):
        """My custom hook spec.
        """

pluggy.PluginManager('example').add_hookspecs(SpecSpace())

because pluggy.varnames() doesn't successfully detect and remove the self name.

I realize this is an odd usage (and implied to be illegal based on the argname to add_hookspecs()) but it is conceivable. A contrived example might be where a user wishes to define the spec and the default hook implementation using the same function:

import pluggy

hookspec = pluggy.HookspecMarker('example')
hookimpl = pluggy.HookimplMarker('example')

class SpecSpace(object):
    def __init__(self, arg):
        self.arg = arg 

    @hookimpl
    @hookspec
    def hook1(self, stuff):
        """My custom hook spec.
        """
        if self.arg = 'doggy' and stuff == 'blah':
             # do this one thing
        else:
            # do this other thing

ss = SpecSpace('blah')
pm = pluggy.PluginManager('example')
pm.add_hookspecs(ss)
pm.register(ss)

I guess it's kind of silly...

Either way the implementation of pluggy.varnames is quite outdated and should be changed to more properly levarage the inspect module. If we prefer to not ever allow instances then a more explicit error should be thrown in PluginManager.add_hookspecs()

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