Komodo Python 自动完成:通过变量元数据进行类型推断?

发布于 2024-08-09 23:46:33 字数 413 浏览 6 评论 0原文

我正在使用 Komodo Edit 进行 Python 开发,并且我希望获得最好的退出自动完成。

如果我这样做:

a = A()
a.

我可以看到 A 的成员列表。

但是如果我这样做:

a = [A()]
b = a[0]
b.

它不起作用。我希望能够做到这一点:

a = [A()]
b = a[0]
"""b

Type: A
"""
b.

那么我如何告诉自动完成 b 是 A 类型?

I'm using Komodo Edit for Python development, and I want to get the best out of the auto complete.

If I do this:

a = A()
a.

I can see a list of members of A.

But if I do this:

a = [A()]
b = a[0]
b.

It does not work. I want to be able to do this:

a = [A()]
b = a[0]
"""b

Type: A
"""
b.

So how can I tell the auto complete that b is of type A?

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

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

发布评论

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

评论(2

旧故 2024-08-16 23:46:33

这并不能真正回答您的问题,但是使用 Wing IDE 您可以向类型分析器提供提示使用assert isinstance(b, A)。请参阅此处。我还没有找到用科莫多做到这一点的方法,尽管显然它是 在编写 PHP 或 JavaScript 时可以

更新

我找到了一种欺骗 Komodo 执行此操作的方法:

if 0: b=A()

这有效(至少在 Komodo 5.2 上)并且没有副作用,但肯定会让阅读您代码的人感到困惑。

This doesn't really answer your question, but with Wing IDE you can give hints to the type analyzer with assert isinstance(b, A). See here. I haven't found a way to do it with Komodo, though apparently it's possible when writing PHP or JavaScript.

Update:

I've found a way to trick Komodo into doing this:

if 0: b=A()

This works (at least on Komodo 5.2) and has no side effects, but is sure to confuse whoever reads your code.

旧伤慢歌 2024-08-16 23:46:33

我认为你在这方面不会有太多运气。问题在于,除了最简单的情况之外,在 Python 中静态推断变量的类型确实相当困难。通常,直到运行时才知道类型,因此不可能自动完成。

IDE 会进行一些静态分析来得出明显且最佳的猜测,但我敢打赌它甚至不会尝试容器中的元素。尽管我们可以算出 b 的类型为 A,但即使代码的微小变化也可能使其变得不可知,尤其是当它位于可变容器中时。

顺便说一句,我已经在完整的 Komodo IDE 上尝试过了,但效果并没有更好。我听说 Wing IDE 具有出色的代码完成功能,但我不确定它是否可以做得更好。

I don't think that you'll have much luck with this. The problem is that it's really quite difficult to statically infer the type of variables in Python except in the simplest of cases. Often the type isn't known until run-time and so auto completion isn't possible.

The IDE does some static analysis to work out the obvious and best guesses, but I'll bet it isn't even trying for elements in a container. Although we can work out that b is of type A even small variations to your code can make it unknowable, especially as it's in a mutable container.

Incidentally I've tried this on the full Komodo IDE and it's no better. I hear that Wing IDE has excellent code completion, but I wouldn't be sure it could do any better either.

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