mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(run): Python run configuration type (#4126)
Adds PyCharm's Python configuration: a script or a -m module with arguments, command (interpreter or uv/poetry) and runner arguments; a plain python/python3 runs on the project interpreter as before. Existing python configurations (detected, imported, run from the context menu) open in this form when their command line maps to a script or module. projectPython prefers a detected python configuration over one made by hand. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
This commit is contained in:
co-authored by
wildcatDownstairs
parent
8959f7aa5d
commit
d7351dca1d
@@ -397,6 +397,17 @@ describe("context configuration", () => {
|
||||
expect(contextConfig("tools/gen.py", uv)).toMatchObject({ command: "uv", args: ["run", "python", "tools/gen.py"] });
|
||||
});
|
||||
|
||||
it("prefers a detected Python config's interpreter over one made by hand", () => {
|
||||
const configs = [
|
||||
cfg({ id: "mine", kind: "python", command: "python3", args: ["tool.py"], detected: false }),
|
||||
cfg({ id: "found", kind: "python", command: "uv", args: ["run", "python", "-m", "app"], detected: true }),
|
||||
];
|
||||
expect(contextConfig("tools/gen.py", configs)).toMatchObject({
|
||||
command: "uv",
|
||||
args: ["run", "python", "tools/gen.py"],
|
||||
});
|
||||
});
|
||||
|
||||
it("takes the interpreter from a Django runserver config without its arguments", () => {
|
||||
const runserver = (command: string, args: string[]) =>
|
||||
cfg({ id: "django:runserver", kind: "python", command, args: [...args, "manage.py", "runserver"] });
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
import type { RunConfig } from "@/platform/ipc";
|
||||
import { djangoLabel, djangoTestsOf, hasDjangoTests } from "./djangoTypes";
|
||||
import { systemPython } from "./pythonTypes";
|
||||
import { phpunitArgs } from "./testing/phpunit";
|
||||
import { goTestParts } from "./goArguments";
|
||||
import { goBuildType } from "./goTypes";
|
||||
@@ -122,7 +123,8 @@ export function projectPython(configs: RunConfig[], relPath?: string): { command
|
||||
configs.filter((c) => ["python", "pytest", "django-test"].includes(c.kind)),
|
||||
relPath,
|
||||
);
|
||||
const py = configs.find((c) => c.kind === "python");
|
||||
// A detected configuration says what the project uses; one made by hand only what it runs itself.
|
||||
const py = configs.find((c) => c.kind === "python" && c.detected) ?? configs.find((c) => c.kind === "python");
|
||||
if (py) {
|
||||
// The interpreter part ends where the program starts: `-m`, or the script (`manage.py`). What follows
|
||||
// (`runserver`, `test`…) belongs to that program, never to the interpreter.
|
||||
@@ -144,11 +146,6 @@ export function projectPython(configs: RunConfig[], relPath?: string): { command
|
||||
return null;
|
||||
}
|
||||
|
||||
/** `python` on Windows (where `python3` is not a command), `python3` elsewhere. */
|
||||
function systemPython(): string {
|
||||
return isWindows() ? "python" : "python3";
|
||||
}
|
||||
|
||||
/**
|
||||
* PyCharm runs pytest for a `test_*.py` file even when the project declares no
|
||||
* test setup: `python -m pytest`, with the project's interpreter when detected.
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Python (PyCharm's "Python" configuration): a script or a `-m` module with its
|
||||
* arguments. The command is the interpreter (`python3`, `.venv/bin/python`, `uv`),
|
||||
* the runner args its leading arguments (`run python` for uv / poetry, `-u`). A plain
|
||||
* `python` / `python3` runs on the project interpreter: the backend swaps it at launch.
|
||||
*/
|
||||
import type { RunConfigTypeContribution } from "@/workbench/contributions";
|
||||
import { formatArgs, parseArgs } from "./configs";
|
||||
import { isWindows } from "./jvmTypes";
|
||||
|
||||
/** `python` on Windows (where `python3` is not a command), `python3` elsewhere. */
|
||||
export const systemPython = () => (isWindows() ? "python" : "python3");
|
||||
|
||||
export const pythonType: RunConfigTypeContribution = {
|
||||
id: "python",
|
||||
label: "Python",
|
||||
icon: "file-python",
|
||||
description: "Run a Python script or module",
|
||||
// A getter: the host's OS is known only once the project is open.
|
||||
get defaults() {
|
||||
return { mode: "script", command: systemPython() };
|
||||
},
|
||||
debugAdapter: "debugpy",
|
||||
fields: [
|
||||
{
|
||||
key: "mode",
|
||||
label: "Execute",
|
||||
control: "select",
|
||||
options: [
|
||||
{ value: "script", label: "Script file" },
|
||||
{ value: "module", label: "Module" },
|
||||
],
|
||||
},
|
||||
{
|
||||
key: "script",
|
||||
label: "Script path",
|
||||
mono: true,
|
||||
required: true,
|
||||
browse: "file",
|
||||
placeholder: "main.py",
|
||||
visible: (p) => p.mode !== "module",
|
||||
},
|
||||
{
|
||||
key: "module",
|
||||
label: "Module",
|
||||
mono: true,
|
||||
required: true,
|
||||
placeholder: "uvicorn",
|
||||
visible: (p) => p.mode === "module",
|
||||
},
|
||||
{ key: "args", label: "Arguments", mono: true, placeholder: "Space separated; quote with ' or \"" },
|
||||
{
|
||||
key: "command",
|
||||
label: "Command",
|
||||
mono: true,
|
||||
placeholder: "python3",
|
||||
required: true,
|
||||
hint: "e.g. python3, .venv/bin/python, uv, poetry",
|
||||
},
|
||||
{
|
||||
key: "runnerArgs",
|
||||
label: "Runner arguments",
|
||||
mono: true,
|
||||
placeholder: "run python",
|
||||
hint: "e.g. `run python` for uv/poetry; empty calls the interpreter itself",
|
||||
},
|
||||
],
|
||||
commandLine(p) {
|
||||
const module = p.mode === "module";
|
||||
const target = (module ? p.module : p.script)?.trim();
|
||||
const program = !target ? [] : module ? ["-m", target] : [target];
|
||||
return {
|
||||
command: p.command?.trim() || systemPython(),
|
||||
args: [...parseArgs(p.runnerArgs ?? ""), ...program, ...parseArgs(p.args ?? "")],
|
||||
};
|
||||
},
|
||||
paramsFrom(c) {
|
||||
if (c.kind !== "python") return null;
|
||||
// The program starts at `-m` or the script; what precedes it belongs to the interpreter.
|
||||
const at = c.args.findIndex((a) => a === "-m" || /\.pyw?$/.test(a));
|
||||
if (at < 0) return null;
|
||||
const module = c.args[at] === "-m";
|
||||
const target = c.args[module ? at + 1 : at];
|
||||
if (!target) return null;
|
||||
return {
|
||||
mode: module ? "module" : "script",
|
||||
[module ? "module" : "script"]: target,
|
||||
args: formatArgs(c.args.slice(at + (module ? 2 : 1))),
|
||||
command: c.command,
|
||||
runnerArgs: formatArgs(c.args.slice(0, at)),
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -355,6 +355,47 @@ describe("configuration types", () => {
|
||||
expect(rake(["-T"])).toMatchObject({ options: "-T", task: "" });
|
||||
});
|
||||
|
||||
it("Python runs a script or module and round-trips detected command lines", () => {
|
||||
const python = typeFor("python")!;
|
||||
expect(newConfigOfType(python, [])).toMatchObject({
|
||||
kind: "python",
|
||||
command: "python3",
|
||||
args: [],
|
||||
isTest: false,
|
||||
debugAdapter: "debugpy",
|
||||
});
|
||||
expect(missingParams(newConfigOfType(python, []), python)).toHaveProperty("script");
|
||||
expect(
|
||||
python.commandLine({ mode: "module", module: " uvicorn ", script: "ignored.py", args: "app:app --reload" }),
|
||||
).toEqual({ command: "python3", args: ["-m", "uvicorn", "app:app", "--reload"] });
|
||||
|
||||
const lines: Array<{ command: string; args: string[]; params: Record<string, string> }> = [
|
||||
{
|
||||
command: ".venv/bin/python",
|
||||
args: ["-u", "src/my app.py", "--port", "8000"],
|
||||
params: { mode: "script", script: "src/my app.py", runnerArgs: "-u", args: "--port 8000" },
|
||||
},
|
||||
{
|
||||
command: "uv",
|
||||
args: ["run", "python", "manage.py", "runserver"],
|
||||
params: { mode: "script", script: "manage.py", runnerArgs: "run python", args: "runserver" },
|
||||
},
|
||||
{
|
||||
command: "python3",
|
||||
args: ["-X", "utf8", "-m", "pkg.cli", "serve"],
|
||||
params: { mode: "module", module: "pkg.cli", runnerArgs: "-X utf8", args: "serve" },
|
||||
},
|
||||
];
|
||||
for (const { command, args, params } of lines) {
|
||||
const config = { ...newConfigOfType(python, []), command, args, params: undefined };
|
||||
expect(formParams(config, python)).toMatchObject({ command, ...params });
|
||||
}
|
||||
// No script or module to show in the form: it stays a plain command.
|
||||
expect(
|
||||
formParams({ ...newConfigOfType(python, []), args: ["-c", "print(1)"], params: undefined }, python),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("Django tests round-trip commandLine / paramsFrom and always report to the test tree", () => {
|
||||
const django = typeFor("django-test")!;
|
||||
expect(django.debugAdapter).toBe("debugpy");
|
||||
|
||||
@@ -27,6 +27,7 @@ import { goBuildType, goTestType } from "./goTypes";
|
||||
import { dotnetProjectType, dotnetTestType } from "./dotnetTypes";
|
||||
import { rakeType, rspecType, rubyScriptType } from "./rubyTypes";
|
||||
import { djangoTestType } from "./djangoTypes";
|
||||
import { pythonType } from "./pythonTypes";
|
||||
import { phpunitType } from "./phpunitTypes";
|
||||
import { UNITY_CONFIG_KIND } from "./unity";
|
||||
|
||||
@@ -491,6 +492,7 @@ export const BUILTIN_TYPES: readonly RunConfigTypeContribution[] = [
|
||||
gradle,
|
||||
maven,
|
||||
godot,
|
||||
pythonType,
|
||||
pytest,
|
||||
rubyScriptType,
|
||||
rakeType,
|
||||
|
||||
@@ -2456,5 +2456,6 @@
|
||||
"Load Maven Project": "Load Maven Project",
|
||||
"Skip": "Skip",
|
||||
"Resize Markdown preview": "Resize Markdown preview",
|
||||
"Run a Python script or module": "Run a Python script or module",
|
||||
"Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.": "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed."
|
||||
}
|
||||
|
||||
@@ -5749,6 +5749,7 @@
|
||||
"Archive ({count})": "归档({count})",
|
||||
"Delete ({count})…": "删除({count})…",
|
||||
"Run pytest tests": "运行 pytest 测试",
|
||||
"Run a Python script or module": "运行 Python 脚本或模块",
|
||||
"e.g. pytest, uv, poetry, .venv/bin/pytest, python": "例如 pytest、uv、poetry、.venv/bin/pytest、python",
|
||||
"Runner arguments": "运行器实参",
|
||||
"e.g. `run pytest` for uv/poetry, `-m pytest` for python; empty calls the command itself": "例如 uv/poetry 使用 `run pytest`,python 使用 `-m pytest`;留空则直接调用命令本身",
|
||||
|
||||
@@ -258,6 +258,7 @@ const UNTRANSLATED = new Set<string>([
|
||||
"go env GOPATH",
|
||||
"kebab-case",
|
||||
"KEY=value; OTHER=value",
|
||||
"main.py",
|
||||
"my-mcp-server",
|
||||
"mysite.settings_test",
|
||||
"NAME",
|
||||
@@ -280,6 +281,7 @@ const UNTRANSLATED = new Set<string>([
|
||||
"run python",
|
||||
"snake_case",
|
||||
"tests",
|
||||
"uvicorn",
|
||||
'{ "section": { "key": "value" } }',
|
||||
// Identifiers that only look like text: icon names, error codes, categories, console output
|
||||
"app",
|
||||
|
||||
Reference in New Issue
Block a user