Pylint complains “no value for argument 'cls'”

ε祈祈猫儿з 提交于 2019-12-01 06:49:37

Typically this error is related to non-complaint function signatures.

Given your code:

class Container(object):
    def __init__(self, iterable, cls):
        self.content = [cls(item) for item in iterable]

    @classmethod
    def from_df(cls, df):
        rows = [i for _, i in df.iterrows()]
        return cls(rows)

Pylint resolves cls in from_df scope object to be Container. Class objects are callables (like functions) and they return new instance of given class. Pylint investigates constructor interface and checks if passed arguments are correct.

In your case passed arguments are incorrect - second required argument (which happens to have same name - cls - but it exists in different score) is missing. What's why Pylint yields error.

Follow up your edits: Pylint does not run your code. It statically analyzes it. Since it's possible to call it like Container.from_df PyLint will warn about possible misuse.

If constructor is never intended to use both arguments outside of your subclasses you may pass default argument and explicitly raise an exception:

class Container(object):
    def __init__(self, iterable, cls=None):
        if cls is None:
            raise NotImplementedError()
        self.content = [cls(item) for item in iterable]

    @classmethod
    def from_df(cls, df):
        rows = [i for _, i in df.iterrows()]
        return cls(rows)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!