gpt4 book ai didi

python - 我如何在 .pyi 文件中声明原始模块中的部分字段?

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:26 24 4
gpt4 key购买 nike

它在 PyCharm 2016 中有效,但在 PyCharm 2017 中无效: enter image description here

“解决方案”:PyCharm 2017.2.x 支持 NamedTuple 来自 typing 并且我不需要再使用 .pyi 文件。

最佳答案

编辑#1:

我发现了这个问题:https://youtrack.jetbrains.com/issue/PY-18597并且有一个答案:

PyCharm 2017.1 EAP builds now always prefer stub files over Python files found on the Python path.

这意味着 Pycharm 将查找 .pyi 文件中存在的内容(如果存在这样的文件)并且它将使用此作为引用。所以你别无选择,只能在 .pyi 文件中声明所有函数。

您可以在 here 中找到一些关于如何避免潜在烦人的警告和错误的知识。

编辑#2:

是的,我可以确认 from ... import * 在 PyCharm 中没有按预期工作,这可能是一个错误,您可以为此打开一个问题(或者如果有人成功地完成了这项工作,请告诉我们怎么做)!

我根据这句话找到了解决方法:

Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form.

如果您:from file_a import a as a, b as b ... 那么所有内容都会按预期取消标记,但是 from file_a ... 会给您以下警告:

Import resolves to its containing file... (Ctrl+F1) 
This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

如解释的那样,不要 panic here这是一个关于模块名称可能被您的文件名遮蔽的警告(这不适用于您的情况!)

关于python - 我如何在 .pyi 文件中声明原始模块中的部分字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com