Pylint 抱怨“参数‘cls’没有价值"; [英] Pylint complains "no value for argument 'cls'"

查看:69
本文介绍了Pylint 抱怨“参数‘cls’没有价值";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下类方法来从 pandas.DataFrame 而不是像这样的列表定义我的对象:

I have defined the following class-method to define my object from a pandas.DataFrame instead of from a list like so:

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

pylint 在带有 E1120 'code-smell' 的 return 行抱怨:

and pylint complains at the return line with the E1120 'code-smell':

构造函数调用中参数cls"没有值

No value for argument 'cls' in constructor call

我看不出有什么问题,而且它似乎有效.有没有其他人可能知道它可能有什么问题?

I can't see anything wrong with it, and it seems to work. Does anybody else maybe have an idea what could be wrong with it?

更新:呃,用户 rogalski 明白了(我认为):我对作为参数传入的类使用相同的变量名让我感到困惑:

Update: Ugh, user rogalski got it (I think): I confused myself by using the same variable name for a class that comes in as argument:

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

我这样做是因为我有不同类型的对象进入,而这个 Container 类是这个女儿的抽象版本:

I do this because I have different kind of objects coming in and this Container class is the abstract version of this daughter:

class FanContainer(Container):
    def __init__(self, iterable):
        super().__init__(iterable, Fan)

Fan 是需要包含"的几个类之一.Rogalski,想写一个答案,比如错误可能引用了 __init__ 构造函数的名称?干杯!(现在我必须挖掘为什么我的代码没有在这个问题上绊倒......)

with Fan being one of several classes that need to be 'contained'. Rogalski, want to write up an answer along the lines of saying that the error might reference a name of the __init__ constructor? Cheers! (Now I have to dig why my code isn't stumbling over this...)

更新 2才意识到我编码这个代码有多弱:我基本上是这样使用它的:

Update2 Only realizing know how feeble I have coded this: I am using this basically like so:

fancontainer = FanContainer.from_df(df)

并且因为我覆盖了 FanContainer 类中的 __init__,我想这就是为什么我的代码仍然有效的原因?所以,抽象的 __init__ 永远不会被直接调用,因为我从不调用 Container.from_df(df) 而只调用子类的 classmethods.猜猜这可以用不同的方式做得更漂亮.

and because I am overwriting the __init__ in the FanContainer class, I guess that's why my code still worked? So, the abstract __init__ is never being called directly, because I never call Container.from_df(df) but only the daughter classes' classmethods. Guess that can be done prettier a different way.

推荐答案

通常此错误与非投诉函数签名有关.

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

给定您的代码:

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 将 from_df 范围对象中的 cls 解析为 Container.类对象是可调用的(如函数),它们返回给定类的新实例.Pylint 调查构造函数接口并检查传递的参数是否正确.

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.

在您的情况下,传递的参数不正确 - 缺少第二个必需参数(恰好具有相同的名称 - cls - 但它存在于不同的分数中).Pylint 产生错误的原因是什么.

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.

跟进您的Pylint 不会运行您的代码.它静态地分析它.因为可以像 Container.from_df 一样调用它,所以 PyLint 会警告可能的误用.

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)

这篇关于Pylint 抱怨“参数‘cls’没有价值";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆