它在 PyCharm 2016 中有效,但在 PyCharm 2017 中无效:
“解决方案”:
PyCharm 2017.2.x 支持 打字
中的 NamedTuple
,我不再需要使用 .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/