lambda 函数中的未定义变量

Undefined variable in lambda function

我有代码:

from functools import reduce

public_ids = [1,2,3,4,5]
filepath = '/path/to/file/'

rdd = sc.textFile(
    filepath
)

new_rdd = reduce(
    lambda a, b: a.filter(
        lambda x: b not in x
    ),
    public_ids,
    rdd
)

此代码假设根据 id 列表过滤 rdd 中的行。 rdd 是使用 spark 上下文 sc 的 textFile 方法从位于文件路径中的文件创建的。

此代码工作正常,但 pylint 引发错误:

E: Undefined variable 'b' (undefined-variable)

我认为我的编码方式不正确。我怎样才能更改它以便 pylint 不会再次引发错误?或者它只是 pylint 无法正确识别的结构?

很可能是 pylint 中的错误。

这是 similar bug report 2 年前的

foo = lambda x: lambda: x + 1 print(foo(1)())

correctly prints 2 when run, but pylint incorrectly reports

E: 1,24: Undefined variable 'x' (undefined-variable)

This is a regression from pylint 1.4.x.

这是一个 recent issue 于 2018 年 11 月 14 日报告的相同问题

The issue has been reported at #760 and fixed by #2274. However, the fix is merged only into pylint 2.x which supports only python >= 3.4 leaving us with the bug unresolved in the pylint 1.x series for python 2.

编辑

看起来你的误报可能与上面的问题略有不同,不过,我仍然认为这是一个错误。

我会尝试在他们的回购协议上创建一个问题,看看会发生什么 (如果您决定这样做,请在评论中 post link 以便我们关注。)