VSCode Python 自动补全仅不适用于某些类

发布于 2025-01-15 22:49:58 字数 1210 浏览 1 评论 0原文

当在我的 python 项目中使用 VSCode 的自动完成功能时,除了一个类之外,它运行得很好,我不明白为什么。

该类来自模块 database,该模块总共包含 3 个类:DatabaseClientDatabaseCollection

DatabaseClient 有一个 get_database() 方法,该方法返回一个 Database 对象。
Database 有一个 get_collection() 方法,它返回一个 Collection 对象。
Collection 有多种方法。

自动完成功能按 DatabaseClient & 上的预期工作。 Database 对象,但根本不在 Collection 对象上。

下面的代码可以工作并给出预期结果(屏幕截图结果的第 15 行和第 20 行匹配),但在编辑时不会自动完成,如上所述。

import lib.database as database

print( database.DatabaseClient().get_database('business').get_collection('reviews').find() )

client = database.DatabaseClient()
db = client.get_database(client.list_database_names()[0])
coll = db.get_collection(db.list_collection_names()[0])
print(coll.find())

VSCode Python 自动完成不一致

尝试重新加载 VSCode,卸载并重新加载 python 扩展。
尝试卸载并重新安装 python 扩展。
尝试更改我的模块的名称。
尝试更改班级名称。
尝试更改 Collection 类的方法名称。
尝试使用单行命令行 3 并发现它是这样工作的。

When using autocompletion in VSCode in my python project, it works very well except for one class and I cannot figure out why.

The class is coming from a module database which contains 3 classes total: DatabaseClient, Database, Collection

DatabaseClient has a get_database() method which returns a Database object.
Database has a get_collection() method which returns a Collection object.
Collection has several methods.

The autocompletion works as intended on the DatabaseClient & Database objects, but not at all on the Collection object.

The code below works and give the expected results (line 15 and 20 of the screenshot results match), but not the autocompletion while editing as stated above.

import lib.database as database

print( database.DatabaseClient().get_database('business').get_collection('reviews').find() )

client = database.DatabaseClient()
db = client.get_database(client.list_database_names()[0])
coll = db.get_collection(db.list_collection_names()[0])
print(coll.find())

VSCode Python inconsistent autocompletion

Tried to reload VSCode, unload and reload the python extensions.
Tried to uninstall and re-install the python extensions.
Tried to change the name of my module.
Tried to change the name of the class.
Tried to change the name of the method of the Collection class.
Tried using single line command line 3 and noticed it worked this way.

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

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

发布评论

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

评论(1

两相知 2025-01-22 22:49:58

找到了解决方案。
问题来自于不使用方法头中的类型。
一旦我添加了正确的输入,自动完成功能就可以按预期在任何地方工作。

def get_database(self, database_name: str) -> Database:
def get_collection(self, collection_name: str) -> Collection:

Found out the solution.
The problem comes from not using typing in the method headers.
Once I added proper typing, the autocompletion worked everywhere as expected.

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