pandas Pylint 抱怨“参数‘cls’没有价值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35208443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:37:44  来源:igfitidea点击:

Pylint complains "no value for argument 'cls'"

pythonpandaspylint

提问by K.-Michael Aye

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

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

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

and pylintcomplains at the returnline with the E1120 'code-smell':

pylintreturn带有 E1120 '代码气味'的线路上抱怨:

No value for argument 'cls' in constructor call

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

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?

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

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

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

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

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

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

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

with Fanbeing 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...)

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

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

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

fancontainer = FanContainer.from_df(df)

and because I am overwriting the __init__in the FanContainerclass, 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.

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

采纳答案by ?ukasz Rogalski

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 clsin from_dfscope 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.

pylint的做出决议clsfrom_df范围对象是Container。类对象是可调用的(如函数),它们返回给定类的新实例。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.

在您的情况下,传递的参数不正确 -cls缺少第二个必需参数(恰好具有相同的名称- 但它存在于不同的分数中)。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_dfPyLint will warn about possible misuse.

跟进您的编辑:Pylint 不会运行您的代码。它静态地分析它。因为它可以像Container.from_dfPyLint那样调用它会警告可能的误用。

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)