Pylint插件警告特定功能使用?
如果使用特定功能,我想为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以访问
node
,node.value
指向正在订阅的内容。您可以直接检查它是否是nodes.dict
的实例,以捕获dict文字。对于nodes.name
或nodes.call.call
,您可以使用utils.safe_infer()
尝试查看推论值是否是词典。请参阅typechecker.visit_subscript()
中的模型。You have access to
node
, andnode.value
points to what is being subscripted. You can check directly if it's an instance ofnodes.Dict
to catch dict literals. For other types likenodes.Name
ornodes.Call
, you can useutils.safe_infer()
to try to see if the inferred value is a dictionary. See a model inTypeChecker.visit_subscript()
.