PyCharm 中的部分存根 [英] Partial stub in PyCharm

查看:68
本文介绍了PyCharm 中的部分存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中引入部分类型注释.例如重载.我发现 pep561 引入了部分存根文件支持.

I would like to introduce partial type annotation to my project. For example for overloading. I found that pep561 introduce partial stub file support.

我使用 PyCharm 开发我的项目并添加相应的 *.pyi 文件.并得到了预期的信息,但 PyCharm 报告在 pyi 文件中找不到参考.

I develop my project with PyCharm and I add corresponding *.pyi file. And got expected information, but PyCharm reports that cannot find reference in pyi file.

当pyi文件中没有条目时,是否可以强制PyCharm查看原始py文件?或者也许它也可以部分进入课堂?

It is possible to force PyCharm to look to orginal py file when there is no entry in pyi file? Or maybe it is also doable with partial entry for class?

我创建示例项目来显示问题(原始项目太大):

I create sample project to show problem (orginal is to big):

├── main.py
└── pep561_test
    ├── __init__.py
    └── __init__.pyi

main.py

from pep561_test import AA, BB, CC

AA().test1(1)
AA().test1(True)
AA().test1('a')
AA().test2(1)

BB().test1(1)
BB().test2(1)

__init__.py

__init__.py

class AA:
    def test1(self, a):
        pass

    def test2(self, a):
        pass


class BB:
    def test1(self, a):
        pass

    def test2(self, a):
        pass


class CC:
    def test1(self, a):
        pass

    def test2(self, a):
        pass

__init__.pyi

__init__.pyi

class AA:
    def test1(self, a: int) -> int: ...

    def test1(self, a: bool) -> str: ...

    def test2(self, a):
        pass


class BB:
    def test1(self, a):
        pass

推荐答案

指示在 .py 中查找缺少的顶级引用 (CC)

根据 PEP 484,这是可能的, 只需在 .pyi 的顶层添加以下行(我检查了它是否适用于 PyCharm 11.0.7,在 PyCharm 2020.3 中不起作用):

Indicating to look in the .py for missing top-level reference (CC)

According to PEP 484, this is possible, simply add the following line at the top-level of your .pyi (I checked that it works with PyCharm 11.0.7, does not work in PyCharm 2020.3):

def __getattr__(name) -> Any: ...

指示在 .py 中查找缺少的属性/方法 (BB)

其他库(至少 typeshed)允许类似的语法将不完整的类存根与 .py

Indicating to look in the .py for missing attribute/method (BB)

Other libraries (at least typeshed) allow a similar syntax to merge an incomplete class stub with the class definition in the .py

class Foo:
    def __getattr__(self, name: str) -> Any: ...  # incomplete
    x: int
    y: str

然而,据我所知,这似乎不是 PEP 484 的一部分,也没有在 PyCharm(从 11.0.7 开始)中实现.我刚刚为此功能创建了一个 request:我偶然发现在寻找将我不完整的类存根与类定义合并的方法时,这个 stackoverflow 问题并得出结论认为它尚不可行.

However this does not seem to be part of PEP 484, and is not implemented in PyCharm (as of 11.0.7) as far as I know. I just created a request for this feature: I stumbled on this stackoverflow question while looking for a way to merge my incomplete class stub with the class definition and concluded that it is not feasible yet.

这篇关于PyCharm 中的部分存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆