[英]Partial stub in PyCharm
我想在我的項目中引入部分類型注釋。 例如重載。 我發現 pep561 引入了部分存根文件支持。
我使用 PyCharm 開發我的項目並添加相應的*.pyi
文件。 並得到了預期的信息,但 PyCharm 報告在 pyi 文件中找不到參考。
當pyi文件中沒有條目時,是否可以強制PyCharm查看原始py文件? 或者也許它也可以部分進入課堂?
├── 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
根據PEP 484 ,這是可能的,只需在.pyi
的頂層添加以下行(我檢查了它是否適用於 PyCharm 11.0.7,編輯:在 PyCharm 2020.3 中不起作用):
def __getattr__(name) -> Any: ...
其他庫(至少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.