stubs — 类型存根贡献
注册 MicroPython 类型存根以支持代码补全。
self.stubs 提供类型存根贡献能力。Ui、Service、Data 插件均可用(贡献写入通常需要 data.write 权限,读取需要 data.read)。
方法
contribute(provider_id, profiles, kind?, version?, aliases?, metadata?, callback?)
注册类型存根。
self.stubs.contribute(
provider_id="my-stubs",
kind="micropython",
version="1.0.0",
profiles=[
{
"id": "generic",
"label": "Generic Stubs",
"path": "/path/to/generic",
"priority": 10,
},
],
aliases=["micropython"],
)参数:
provider_id(str) — 提供者 ID(唯一标识符)profiles(list[dict]) — 配置文件列表kind(str) — 存根类型,默认"micropython"version(str) — 版本号aliases(list[str]) — 别名列表metadata(dict) — 元数据
Profile 结构:
| 字段 | 类型 | 说明 |
|---|---|---|
id | str | 配置文件 ID |
label | str | 显示名称 |
path | str | 存根文件目录路径 |
priority | int | 优先级(越大越高) |
register_runtime(provider_id, profiles, ...)
注册运行时存根(可动态更新)。参数同 contribute()。
revoke(provider_id, callback?)
撤销已注册的存根。
self.stubs.revoke("my-stubs")get(provider_id, callback?)
查询已注册的存根。
self.stubs.get("my-stubs",
lambda **cb: print(cb)
)list(callback?)
列出所有已注册存根。
self.stubs.list(
lambda **cb: print(cb)
)resolve_layers(layers, callback?)
解析存根层级配置。
self.stubs.resolve_layers([
{"provider": "my-stubs", "profile": "generic"},
])完整示例
from pyrite_sdk.core.plugin import DataPlugin
class MicroPythonStubs(DataPlugin):
def on_contribute(self):
provider_id = "micropython-stubs"
root = self.path.plugin() / "stubs"
self.stubs.contribute(
provider_id=provider_id,
kind="micropython",
version="1.0.0",
profiles=[
{
"id": "generic",
"label": "MicroPython Generic",
"path": str(root / "generic"),
"priority": 10,
},
{
"id": "esp32",
"label": "MicroPython ESP32",
"path": str(root / "esp32"),
"priority": 20,
},
],
aliases=["micropython"],
)
# 自动配置层级
self.settings.micropython_stubs.get_layers(
lambda **cb: self._configure(cb, provider_id)
)
def _configure(self, result, provider_id):
current = result.get("data", {}).get("value", [])
if current:
self.message.warning("已检测到现有配置")
return
self.settings.micropython_stubs.set_enabled(True)
self.settings.micropython_stubs.set_layers([
{"provider": provider_id, "profile": "generic"},
{"provider": provider_id, "profile": "esp32"},
])
self.message.success("Stubs 已配置")