簡體   English   中英

PyCharm 中的部分存根

[英]Partial stub in PyCharm

我想在我的項目中引入部分類型注釋。 例如重載。 我發現 pep561 引入了部分存根文件支持。

我使用 PyCharm 開發我的項目並添加相應的*.pyi文件。 並得到了預期的信息,但 PyCharm 報告在 pyi 文件中找不到參考。

當pyi文件中沒有條目時,是否可以強制PyCharm查看原始py文件? 或者也許它也可以部分進入課堂?

我創建示例項目來顯示問題(原始的太大): 在“__init__.pyi”中找不到引用“CC”

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

主文件

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

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

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 中不起作用):

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

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

其他庫(至少typeshed )允許使用類似的語法將不完整的類存根與.py的類定義合並

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

然而,這似乎不是 PEP 484 的一部分,據我所知,它沒有在 PyCharm(從 11.0.7 開始)中實現。 我剛剛為此功能創建了一個請求:我在尋找將我不完整的類存根與類定義合並的方法時偶然發現了這個 stackoverflow 問題,並得出結論認為它尚不可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM