[code-quality] Pylint Plugin help: Transform class locals to instances of provided types

Tyler Fox tbttfox at gmail.com
Wed Oct 18 21:08:59 EDT 2017


I've got a class that does some metaclass magic. Some class members are
passed a type, and the metaclass turns those objects into instances of the
type.  How can I set that up in a pylint plugin?  I've made it as far as
identifying the members and the type names, but I have no idea what to do
next.  Any help would be greatly appreciated

(If this doesn't work, maybe there's a way to just remove the type
inference for those members? No idea how to do that either)

from astroid import MANAGER
from astroid import nodes, node_classes

# Removed a bunch of safety code for the sake of brevity and clarity
def transform_myClass(node):
    for key, value in node.locals.items():
        val = value[0]
        s = val.statement().value
        # ArgumentObj is the class that stores the types
        if s.func.name == 'ArgumentObj':
            for child in s.get_children():
                if isinstance(child, node_classes.Keyword):
                    # allowedTypes is the kwarg that recieves a *TUPLE* of
types
                    # The first one gets instantiated, but the others are
allowed
                    if child.arg == 'allowedTypes':
                        typeNames = child.value # A tuple of names of type
objects

                        #### And here is where I have no idea what to do
next ####

MANAGER.register_transform(nodes.ClassDef, transform_myClass)

~T.Fox
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/code-quality/attachments/20171019/1819f96d/attachment.html>


More information about the code-quality mailing list