Pylance (VSCode) 未显示我的 Rust (PyO3, Maturin) 扩展的文档字符串

2 投票
1 回答
60 浏览
提问于 2025-04-14 16:32

在Rust中

use pyo3::prelude::*;

#[pyfunction]
/// Returns the answer to life, the universe, and all the rest
pub fn get_answer() -> usize {
  42
}

#[pymodule]
fn answers(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(get_answer, m)?)?;
    Ok(())
}

我在当前的Python环境中用 maturin develop 构建了,但我也试过用

maturin build && pip install [那个轮子去哪了]

在Python中

import answers

my_answer = answers.get_answer()

现在如果你在VSCode中找到那个Python代码,鼠标悬停在 get_answer 上,弹出的提示 不会 显示文档字符串。不过如果我去 ipython,明确请求 __doc__,它是存在的。

我漏掉了什么?

1 个回答

1

这个问题从去年年底就开始出现了。我试着把那个人说的话当成拉丁语来读,结果还算能理解一些。你可以检查一下你的配置设置,提问的人提到这可能是个Mac的问题,你是在用Mac吗?它在一个地方能正常工作而在另一个地方不行,这让我觉得可能是Pylance的问题,但我知道根据经验,这种情况不一定是这样。如果有新的信息,我会更新这个回答。

撰写回答