Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bettet document how ‘self’ argument is handled #128

Closed
notestaff opened this issue Mar 21, 2018 · 10 comments
Closed

bettet document how ‘self’ argument is handled #128

notestaff opened this issue Mar 21, 2018 · 10 comments

Comments

@notestaff
Copy link

It is optional, right? When registering a class with hook impls, the methods can omit the ‘self’ argument and not be @staticmethod , and things will work; in fact, decorating with @staticmethod might cause problems. Correct? Would help to clarify the docs.

@goodboy
Copy link
Contributor

goodboy commented Mar 21, 2018

Hey @notestaff the implementation for args introspection is here.

When registering a class with hook impls, the methods can omit the ‘self’ argument and not be @staticmethod , and things will work;

Python will likely throw an error if you tried to do this. But i'm not sure exactly what you mean; provide an example if you want.

decorating with @staticmethod might cause problems. Correct?

No, afaik this should work fine.
If not do submit a bug report and test demonstrating the issue :)

I do agree the docs could be better.
I'll put up a PR asap.

@goodboy
Copy link
Contributor

goodboy commented Mar 21, 2018

Yeah afaik:

class doggy:
    def bark();
        park

doggy().bark()

gives:

----> 6 doggy().bark()
TypeError: bark() takes 0 positional arguments but 1 was given

So not sure what you mean regarding the no self arg without the class func being marked with staticmethod/classmethod.

@notestaff
Copy link
Author

In https://pluggy.readthedocs.io/en/latest/#call-time-order , SomeOtherPlugin.setup_project() takes no self, and is not marked with staticmethod.

@nicoddemus
Copy link
Member

@notestaff that's a bug in the docs, opened #129 with a fix. Is that enough to close this issue?

@goodboy
Copy link
Contributor

goodboy commented Mar 22, 2018

@notestaff yeah definitely just a typo. That code couldn't run in Python as demonstrated above :)

@notestaff
Copy link
Author

maybe also copy the info about self from https://github.com/pytest-dev/pluggy/blob/master/pluggy/hooks.py#L109 to the main docs? Normally hookimpls can't have args that the hookspec doesn't, but 'self' is an exception to that, correct?

@goodboy
Copy link
Contributor

goodboy commented Apr 16, 2018

@nicoddemus mind adding that little extra docs in your PR?

@pirate
Copy link

pirate commented Sep 27, 2024

Is there any way to tell pluggy to leave classmethods bound to their original class? (I was confused, it does this already, see other comment below)

i.e. whenever a hookimpl is called, pass the classit was originally discovered on to it when running it?

I want to store some static class variables on the classes that are used to namespace my hookimpl, and I'd love for the hook funcs to be able to access their respective class attrs, as it would let you do simple (static/import-time) inheritance e.g.:

css_plugins.py:

from pathlib import Path

from my_plugin_system import hookimpl


class DefaultCSSPlugin:
    css_file: Path = 'some/path/to/default.css'

	@hookimpl
    @classmethod
    def get_page_css(cls, page) -> str:
        return Path(cls.css_file).read_text()

class LightModePlugin(DefaultCSSPlugin):
    css_file = 'some/path/to/lightmode.css'

class DarkModePlugin(DefaultCSSPlugin):
    css_file = 'some/path/to/darkmode.css'

Example use:

from .css_plugins import DefaultCSSPlugin, LightModePlugin, DarkModePlugin

pm.register(DefaultCSSPlugin)
pm.register(LightModePlugin)
pm.register(DarkModePlugin)

# this should register 3 implementations of get_page_css

@RonnyPfannschmidt
Copy link
Member

@pirate your example needs a explanation, it's unclear what you want to happen

@pirate
Copy link

pirate commented Sep 27, 2024

Ah sorry this was a red-herring, the issue I was encountering was actually caused by this: #536 (the plugin being a pydantic object broke plugin registration)

Pluggy already preserves the self/class correctly, I was just confused by the AttributeError and it made me think it was swapping out self for something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants