Skip to content

Commit cde45b9

Browse files
authored
Merge pull request #14 from Lithe-IDEA/fix/python-keep-override-and-type-errors
fix(settings): 保留解释器覆盖路径和「显示类型错误」四选一,按 Pyrefly 1.4.1 翻译
2 parents cbda64c + 0c91477 commit cde45b9

22 files changed

Lines changed: 627 additions & 157 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
- name: Install pytest for the host suite
117117
run: python -m pip install --disable-pip-version-check pytest
118118
- name: Verify Python highlighting, run configurations, and dependencies on Lithe 0.8.9
119-
run: node scripts/e2e-host.mjs --ref 4d1b88f03f243356c855299464baa2647cc57226 --run '^TestPythonSupportPlugin(GrammarHighlighting|RunCurrentFile(PinnedInterpreter)?|ManualRunAndDependencies)$'
119+
run: node scripts/e2e-host.mjs --ref 4d1b88f03f243356c855299464baa2647cc57226 --run '^TestPythonSupportPlugin(GrammarHighlighting|RunCurrentFile(InterpreterOverride|PinnedInterpreter)?|ManualRunAndDependencies)$'
120120
- name: Run the full Python host suite on the pinned desktop
121121
env:
122122
GOFLAGS: -tags=vivahead

‎README.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ Pyrefly 和 debugpy 随插件包分发,Python 解释器、pytest、uv、conda
3131

3232
## 设置
3333

34-
**插件设置**(「语言与框架 › Python」,也在 设置 → 插件 → Python Support →「设置」),都立即生效:
34+
**插件设置**(「语言与框架 › Python」,也在 设置 → 插件 → Python Support →「设置」),除覆盖路径外都立即生效:
3535

3636
| 设置 | 默认 | 说明 |
3737
| --- | --- | --- |
3838
| 类型检查模式 | 默认 | Pyrefly 的预设:默认、自动、关闭、基础、宽松、严格、全部。项目有 `pyrefly.toml` 或 `[tool.pyrefly]` 时以项目配置为准;“自动”沿用附近的 mypy 或 Pyright 配置,没有时按“基础” |
39-
| 隐藏类型错误 | 关 | 所有文件都不显示类型错误,包括有自己 Pyrefly 配置的项目;悬停、跳转、补全照常。开启后类型检查模式选什么都不显示类型错误 |
39+
| 显示类型错误 | 始终开启 | 在哪些文件里显示类型错误:始终开启(没有 Pyrefly 配置的文件按类型检查模式)、仅已配置项目(只在 `pyrefly.toml` 或 `[tool.pyrefly]` 覆盖的文件里)、仅报告缺失导入、关闭(所有文件都不显示,包括有自己 Pyrefly 配置的项目)。Pyrefly 已经没有只报缺失导入的模式,所以“仅报告缺失导入”让没有 Pyrefly 配置的文件按“基础”检查:报告无法解析的导入和少数 Pyrefly 确定的错误。悬停、跳转、补全在各档都照常 |
4040
| 诊断范围 | 仅打开的文件 | 或者“整个工作区”:没打开的文件也报告错误级别的问题 |
4141
| 自动导入补全 | 开 | 补全列出还没导入的名字,确认时插入 `import` |
4242
| 自动导入路径风格 | 绝对导入 | 插入导入时用完整模块路径还是包内相对路径。也用于“移动符号”“转换模块”代码操作;关闭自动导入补全后只有这两个操作用它 |
4343
| 补全函数括号 | 关 | 补全函数或方法时加上 `()`,光标停在括号里 |
44+
| Python 解释器覆盖路径 | 空 | 填一个 Python 可执行文件后,构建任务、运行和 pytest 都用它,不再按下文的顺序查找;Pyrefly 分析和调试仍用查找到的解释器。修改后重新加载项目生效 |
4445

4546
## Python 解释器
4647

47-
Pyrefly 分析代码、运行、测试、调试、读取依赖、安装依赖都用同一个解释器。Lithe 按这个顺序找:
48+
Pyrefly 分析代码、运行、测试、调试、读取依赖、安装依赖都用同一个解释器,除非设置了“Python 解释器覆盖路径”:那时构建任务、运行和
49+
pytest 用覆盖路径,Pyrefly 和调试仍用下面找到的解释器。Lithe 按这个顺序找:
4850

4951
1. **项目环境**:为项目固定的解释器;项目里的 `.venv`、`venv`、`.conda`(项目要先被信任);项目的 `.python-version` 和
5052
`pyproject.toml` 的 `requires-python` 决定选哪个版本。
@@ -63,17 +65,17 @@ Python,固定也不行。受影响的有:
6365
在此之前请把环境建在项目里:`python3 -m venv .venv` 或 `uv venv`;Poetry 先运行 `poetry config virtualenvs.in-project true`,
6466
Pipenv 设置 `PIPENV_VENV_IN_PROJECT=1`。也可以固定 uv、pyenv、python.org 装的基础解释器,但那里没有环境里装的包。
6567

66-
1.0.0 的“Python 解释器覆盖”设置在 1.1.0 删除了:它只管运行,Pyrefly 却用工具链里的解释器,两边可能不一致。原来填过的路径不再生效,请在工具链页固定它。
68+
项目外的环境暂时也可以填进“Python 解释器覆盖路径”,让运行和 pytest 先用上它。
6769

6870
## 设置预览
6971

7072
两块预览由插件后端按界面语言、外观和设置窗口宽度画出。它们用示例代码,不读取打开的项目;截图里的路径和版本是测试数据。
7173

72-
**语言与框架 › Python**:顶部是当前用的 Python 解释器。下面四张卡片各对应一项设置:
74+
**语言与框架 › Python**:顶部是当前用的 Python 解释器;设置了覆盖路径时,下面多一行说明哪些流程改用它。下面四张卡片各对应一项设置:
7375

7476
| 卡片 | 画出什么 |
7577
| --- | --- |
76-
| 类型检查模式 | 示例代码在所选预设下报告哪些错误;隐藏类型错误开启时标为“已隐藏” |
78+
| 类型检查模式 | 示例代码在所选预设下报告哪些错误;示例没有 Pyrefly 配置,“显示类型错误”不是始终开启时,卡片按它画出实际效果 |
7779
| 诊断范围 | 一个小项目里哪些文件会被检查 |
7880
| 自动导入补全 | 补全列出未导入的 `greet`,确认后按绝对或相对路径插入导入;关闭时只补全已导入的名字 |
7981
| 补全函数括号 | 确认补全后是 `greet()` 还是 `greet` |
@@ -84,7 +86,7 @@ Pipenv 设置 `PIPENV_VENV_IN_PROJECT=1`。也可以固定 uv、pyenv、python.o
8486

8587
<img src="docs/assets/settings-previews/language-en-draft-light.png" alt="还没应用的修改先显示在预览里,英文浅色外观" width="720">
8688

87-
**构建工具 › Python**:画出上面三步查找顺序,以及解释器用在哪些流程。这一页没有设置项,换解释器要到工具链页。
89+
**构建工具 › Python**:先画覆盖路径(有没有设置、设置后谁用它),再画上面三步查找顺序,以及查找到的解释器用在哪些流程。这一页没有设置项,换解释器要到工具链页。
8890

8991
<img src="docs/assets/settings-previews/build-tools-zh-Hans-dark.png" alt="构建工具 › Python 画出 Lithe 查找 Python 解释器的顺序,深色外观" width="720">
9092

@@ -165,7 +167,7 @@ export LITHE_TEST_PYTHON_PLUGIN_ARCHIVE="$PWD/dist/python-support-1.1.0-darwin-a
165167
PYTEST_PYTHON=/path/to/python3.12 node scripts/e2e-host.mjs
166168
# 用同一个包检查 0.8.9:高亮、当前文件运行、固定解释器、手动运行
167169
PYTEST_PYTHON=/path/to/python3.12 node scripts/e2e-host.mjs --ref desktop-v0.8.9 \
168-
--run '^TestPythonSupportPlugin(GrammarHighlighting|RunCurrentFile(PinnedInterpreter)?|ManualRunAndDependencies)$'
170+
--run '^TestPythonSupportPlugin(GrammarHighlighting|RunCurrentFile(InterpreterOverride|PinnedInterpreter)?|ManualRunAndDependencies)$'
169171
```
170172

171173
插件包路径要写绝对路径。脚本把宿主检出到 `.artifacts/lithe-host`,复制 `e2e/host/desktop/` 下的测试后运行;`--source`、`--ref`、`--run`

‎backend/src/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod discovery;
44
mod inspect;
55
mod pyrefly;
66
mod requirements;
7+
mod type_errors;
78
mod view;
89

910
use lithe_plugin_sdk::Backend;

‎backend/src/pyrefly.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Produces the packaged Pyrefly launch and its settings using the host's interpreter hint.
22
3+
use crate::type_errors::Display;
34
use lithe_plugin_sdk::{Context, FunctionError, PluginError};
45
use serde_json::{json, Value};
56
use std::collections::VecDeque;
@@ -31,10 +32,22 @@ impl LaunchResolver {
3132
let settings = &params["settings"];
3233
let diagnostic_mode =
3334
enum_setting(settings, "diagnosticMode", &["openFilesOnly", "workspace"]);
35+
let preset = enum_setting(
36+
settings,
37+
"typeCheckingMode",
38+
&["default", "auto", "off", "basic", "legacy", "strict", "all"],
39+
);
40+
let display = Display::parse(enum_setting(
41+
settings,
42+
"displayTypeErrors",
43+
&Display::VALUES,
44+
))
45+
.expect("a declared value parses");
46+
let (preset, disabled) = display.pyrefly(preset);
3447
let mut python = json!({
3548
"pyrefly": {
36-
"typeCheckingMode": enum_setting(settings, "typeCheckingMode", &["default", "auto", "off", "basic", "legacy", "strict", "all"]),
37-
"disableTypeErrors": settings["disableTypeErrors"].as_bool().unwrap_or(false),
49+
"typeCheckingMode": preset,
50+
"disableTypeErrors": disabled,
3851
"diagnosticMode": diagnostic_mode,
3952
},
4053
"analysis": {

‎backend/src/type_errors.rs‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! The `displayTypeErrors` setting, and the Pyrefly settings it stands for.
2+
//!
3+
//! Pyrefly 1.4.1, the version `vendor-lock.json` packages, deprecates
4+
//! `python.pyrefly.displayTypeErrors` for `typeCheckingMode` and
5+
//! `disableTypeErrors`, and the new settings win whenever both are sent. The
6+
//! plugin always sends `typeCheckingMode`, so passing the old value on would
7+
//! leave only `force-off` in effect. The plugin keeps its four choices and
8+
//! sends Pyrefly the pair that does what each choice's title says instead.
9+
10+
/// Where type errors appear: the `displayTypeErrors` setting.
11+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12+
pub enum Display {
13+
/// `force-on`: every file, files without a Pyrefly config by the preset.
14+
AlwaysOn,
15+
/// `default`: only files a `pyrefly.toml` or `[tool.pyrefly]` covers.
16+
ConfiguredOnly,
17+
/// `error-missing-imports`. Pyrefly 0.64 removed this mode, so files
18+
/// without a Pyrefly config are checked with the `basic` preset, which
19+
/// reports unresolved imports and a few errors Pyrefly is sure of.
20+
MissingImports,
21+
/// `force-off`: no file, whatever its own Pyrefly config says.
22+
Off,
23+
}
24+
25+
impl Display {
26+
/// The declared option values, the default first.
27+
pub const VALUES: [&'static str; 4] =
28+
["force-on", "default", "error-missing-imports", "force-off"];
29+
30+
pub fn parse(value: &str) -> Option<Self> {
31+
Some(match value {
32+
"force-on" => Display::AlwaysOn,
33+
"default" => Display::ConfiguredOnly,
34+
"error-missing-imports" => Display::MissingImports,
35+
"force-off" => Display::Off,
36+
_ => return None,
37+
})
38+
}
39+
40+
/// The `typeCheckingMode` and `disableTypeErrors` Pyrefly gets, where
41+
/// `preset` is the `typeCheckingMode` setting. A preset reaches only the
42+
/// files no Pyrefly config covers; a configured project keeps its own.
43+
pub fn pyrefly(self, preset: &str) -> (&str, bool) {
44+
match self {
45+
Display::AlwaysOn => (preset, false),
46+
Display::ConfiguredOnly => ("off", false),
47+
Display::MissingImports => ("basic", false),
48+
Display::Off => (preset, true),
49+
}
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
fn every_declared_value_parses_and_nothing_else_does() {
59+
for value in Display::VALUES {
60+
assert!(Display::parse(value).is_some(), "{value}");
61+
}
62+
for value in ["", "on", "Force-On", "disable"] {
63+
assert_eq!(Display::parse(value), None, "{value}");
64+
}
65+
}
66+
67+
#[test]
68+
fn each_choice_sends_the_settings_that_do_what_its_title_says() {
69+
assert_eq!(Display::AlwaysOn.pyrefly("strict"), ("strict", false));
70+
assert_eq!(Display::ConfiguredOnly.pyrefly("strict"), ("off", false));
71+
assert_eq!(Display::MissingImports.pyrefly("strict"), ("basic", false));
72+
assert_eq!(Display::Off.pyrefly("strict"), ("strict", true));
73+
}
74+
}

‎backend/src/view/language.rs‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::scene::{
1717
code, column, group, pair, path, pill, rect, runs, squiggle, text, text_width, token, Node,
1818
CAPTION, STRONG,
1919
};
20-
use super::settings::{ImportFormat, Mode, Scope, Settings};
20+
use super::settings::{Display, ImportFormat, Mode, Scope, Settings};
2121
use super::words::Words;
2222
use serde_json::{json, Value};
2323

@@ -32,7 +32,7 @@ pub const TOOLS: [&str; 1] = ["python"];
3232
/// The settings the cards show; a draft of any of them marks the preview.
3333
pub const SETTINGS: [&str; 6] = [
3434
"typeCheckingMode",
35-
"disableTypeErrors",
35+
"displayTypeErrors",
3636
"diagnosticMode",
3737
"autoImportCompletions",
3838
"importFormat",
@@ -139,12 +139,21 @@ struct Card<'a> {
139139
preview: Vec<Node>,
140140
}
141141

142-
/// The region's nodes: the interpreter row, then the preview.
142+
/// The region's nodes: the interpreter row, then the preview. While the
143+
/// override path is set, a banner under the row says which commands use it
144+
/// instead.
143145
pub fn view(settings: &Settings, words: &Words, width: Option<f64>) -> Vec<Value> {
144-
let tools: Vec<Value> = TOOLS
146+
let mut tools: Vec<Value> = TOOLS
145147
.iter()
146148
.map(|tool| json!({ "type": "tool", "tool": tool }))
147149
.collect();
150+
if let Some(path) = &settings.interpreter_override {
151+
tools.push(json!({
152+
"type": "banner",
153+
"text": (words.override_banner)(path),
154+
"tone": "accent",
155+
}));
156+
}
148157
vec![
149158
json!({
150159
"type": "section",
@@ -174,15 +183,20 @@ fn mode_index(mode: Mode) -> usize {
174183
/// order the cards stand.
175184
fn values<'a>(settings: &Settings, words: &'a Words) -> [(&'a str, bool, &'a str); 4] {
176185
let switch = |on: bool| if on { words.on } else { words.off };
177-
let mode = if settings.hide_type_errors {
178-
(words.hidden, false, words.hidden_note)
179-
} else {
180-
let index = mode_index(settings.mode);
181-
(
182-
words.modes[index],
183-
settings.mode != Mode::Off,
184-
words.mode_notes[index],
185-
)
186+
let display =
187+
|index: usize, lit: bool| (words.displays[index], lit, words.display_notes[index]);
188+
let mode = match settings.display {
189+
Display::AlwaysOn => {
190+
let index = mode_index(settings.mode);
191+
(
192+
words.modes[index],
193+
settings.mode != Mode::Off,
194+
words.mode_notes[index],
195+
)
196+
}
197+
Display::ConfiguredOnly => display(0, false),
198+
Display::MissingImports => display(1, true),
199+
Display::Off => display(2, false),
186200
};
187201
let scope = match settings.scope {
188202
Scope::OpenFiles => (words.scope_open, false, words.scope_open_note),
@@ -235,7 +249,10 @@ pub fn canvas(settings: &Settings, words: &Words, width: Option<f64>) -> Value {
235249
value: mode.0,
236250
lit: mode.1,
237251
note: mode.2,
238-
changed: settings.any_changed(&["typeCheckingMode", "disableTypeErrors"]),
252+
// The preset decides this card only while type errors are always on.
253+
changed: settings.is_changed("displayTypeErrors")
254+
|| (settings.display == Display::AlwaysOn
255+
&& settings.is_changed("typeCheckingMode")),
239256
preview: type_check_preview(settings.reporting(), words, box_w),
240257
},
241258
Card {
@@ -435,7 +452,7 @@ fn reported(mode: Mode) -> (usize, Option<usize>) {
435452

436453
/// A function missing an annotation, a call with a string for a float, and a
437454
/// misspelled name, in a box `box_w` wide; under it what the preset in force
438-
/// reports. Nothing is reported while type errors are hidden or off.
455+
/// reports. Nothing is reported while the preset is off or type errors are.
439456
fn type_check_preview(reporting: Option<Mode>, words: &Words, box_w: f64) -> Vec<Node> {
440457
let mut nodes = numbered(vec![
441458
vec![
@@ -708,7 +725,7 @@ mod tests {
708725
let mut pairs: Vec<(&str, &str)> = w
709726
.modes
710727
.iter()
711-
.chain([&w.hidden])
728+
.chain(&w.displays)
712729
.map(|value| (w.mode_title, *value))
713730
.collect();
714731
pairs.extend([w.scope_open, w.scope_workspace].map(|value| (w.scope_title, value)));
@@ -734,8 +751,7 @@ mod tests {
734751
fn every_note_fits_the_narrowest_card() {
735752
for language in ["en", "zh-Hans"] {
736753
let w = words::for_language(language);
737-
let notes = w.mode_notes.iter().copied().chain([
738-
w.hidden_note,
754+
let notes = w.mode_notes.iter().chain(&w.display_notes).copied().chain([
739755
w.scope_open_note,
740756
w.scope_workspace_note,
741757
w.import_absolute_note,

0 commit comments

Comments
 (0)