Pylint插件警告特定功能使用?

发布于 2025-01-25 02:22:08 字数 459 浏览 0 评论 0原文

如果使用特定功能,我想为Pylint(或可能是Mypy)插件(可能是Mypy?)插件。更重要的是,我想警告当使用括号操作员而不是GET方法访问字典时:

mydict = { "foo": 3 }

# warning: direct dictionary access
mydict["bar"]

# no warning:
mydict.get("bar", None)

使用官方文档,我设法弄清楚我必须实现覆盖范围的检查器 visit_subscript。但是,内部visit_subscript,我无法访问左侧的类型,因此我无法确定是否访问了dict list,我不想为这些警告。

有没有一种方法可以在Pylint中获取对下标的推断类型(如果有)?还是有另一种编写此类插件的方法?

I want to have a pylint (or possibly mypy?) plugin to warn if a specific function is used. More to the point, I want to warn when a dictionary is accessed using the brackets operator instead of the get method:

mydict = { "foo": 3 }

# warning: direct dictionary access
mydict["bar"]

# no warning:
mydict.get("bar", None)

With the official docs, I managed to figure out that I have to implement a checker that overrides visit_subscript. However, inside visit_subscript, I don't have access to the type of the left-hand side, so I cannot determine if a Dict is being accessed or, say, a List, and I don't want warnings for those.

Is there a way to get the inferred type (if available) for the subscript in pylint? Or is there an alternative way to write such a plugin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2025-02-01 02:22:08

您可以访问nodenode.value指向正在订阅的内容。您可以直接检查它是否是nodes.dict的实例,以捕获dict文字。对于nodes.namenodes.call.call,您可以使用utils.safe_infer()尝试查看推论值是否是词典。请参阅typechecker.visit_subscript()中的模型。

You have access to node, and node.value points to what is being subscripted. You can check directly if it's an instance of nodes.Dict to catch dict literals. For other types like nodes.Name or nodes.Call, you can use utils.safe_infer() to try to see if the inferred value is a dictionary. See a model in TypeChecker.visit_subscript().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文