Skip to content

Plugin to add generated members #10461

@e4lam

Description

@e4lam

Question

Hi, I'm trying to write a plugin for generated members for our pybind11 classes (ie. C extension module) and seeking advice on the best way to go about it. In our code, we have an extends_class decorator like this:

def extends_class(cls):
    def decorator(f):
        setattr(cls, f.__name__, f)
    return decorator

So to add an generated member onto our pybind11 class, we do something like this:

from my_module import Foo
class FooExtension:
    @extends_class(Foo)
    def foobar(self):
        # do stuff

So I thought what I could do was write a plugin that finds the @extends_class(Foo) and then assign the decorated function into the ClassDef node for Foo. I got as far in my plugin to getting the decorated function node but then I couldn't figure out how to get at the ClassDef node for Foo. I can find the ImportFrom node for Foo (via the node root() method) but from there, I can't seem to find where the actual ast node tree is for my_module. Can you point me in the direction from here?

Looking at some of the astroid brain code, it seems that in some cases there's a different mechanism for this where it uses inference_tip() transform plugin instead. But I'm not sure if I can do that for this particular setup?

Any advice please? Thank you in advance!

Documentation for future user

It would be good if a lot more details about different techniques could be provided in the "Transform plugins" chapter of the "How To Guides".

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions