Skip to content

Commit 4110eba

Browse files
authored
python: restore formatting for ty-based modules (#476)
2 parents 75c5607 + 91cd108 commit 4110eba

2 files changed

Lines changed: 73 additions & 16 deletions

File tree

pkgs/modules/python/default.nix

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ let
5858
inherit pkgs python python-ld-library-path;
5959
};
6060

61-
ty = pkgs.callPackage ../../ty { };
62-
6361
sitecustomize = pkgs.callPackage ./sitecustomize.nix { };
6462

6563
uv = pkgs.callPackage ./uv {
@@ -79,8 +77,9 @@ in
7977
id = "python-${pythonVersion}";
8078
name = "Python Tools";
8179
displayVersion = pythonVersion;
80+
imports = [ (import ../ty) ];
8281
description = ''
83-
Development tools for Python. Includes Python interpreter, Pip, Poetry, and the ty language server.
82+
Development tools for Python. Includes Python interpreter, Pip, Poetry, the ty language server, and Ruff formatting.
8483
'';
8584

8685
replit.packages = [
@@ -103,13 +102,6 @@ in
103102
];
104103
};
105104

106-
replit.dev.languageServers.ty = {
107-
name = "ty";
108-
displayVersion = ty.version;
109-
language = "python3";
110-
start = "${ty}/bin/ty server";
111-
};
112-
113105
replit.dev.packagers.upmPython = {
114106
name = "Python packager";
115107
language = "python3";

pkgs/modules/ty/default.nix

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,66 @@
1-
{ pkgs, ... }:
1+
{ pkgs, lib, ... }:
22
let
33
ty = pkgs.callPackage ../../ty { };
4+
5+
formatter = import ../../formatter {
6+
inherit pkgs;
7+
};
8+
9+
run-ruff-format = pkgs.writeShellApplication {
10+
name = "run-ruff-format";
11+
runtimeInputs = [
12+
pkgs.bash
13+
pkgs.ruff
14+
];
15+
extraShellCheckFlags = [ "-x" ];
16+
text = ''
17+
#!/bin/bash
18+
19+
# Source the shared options parsing script
20+
source ${formatter}/bin/parse-formatter-options "$@"
21+
22+
if [[ "$apply" == "true" ]]; then
23+
if [[ "$stdinMode" == "true" ]]; then
24+
ruff format --stdin-filename "$file" - > "$file"
25+
else
26+
ruff format --quiet "$file"
27+
fi
28+
exit 0
29+
fi
30+
31+
if [[ "$stdinMode" == "true" ]]; then
32+
ruff format --stdin-filename "$file" -
33+
else
34+
ruff format --stdin-filename "$file" - < "$file"
35+
fi
36+
'';
37+
checkPhase = ''
38+
cat > test.py << 'EOF'
39+
def greet( name:str ) ->None:
40+
print( f"hello, {name}" )
41+
EOF
42+
43+
$out/bin/run-ruff-format -f test.py > output.py
44+
printf 'def greet(name: str) -> None:\n print(f"hello, {name}")\n' > expected.py
45+
if ! diff expected.py output.py; then
46+
echo "format output doesn't match expectation"
47+
exit 1
48+
fi
49+
50+
cp test.py applied.py
51+
$out/bin/run-ruff-format --apply -f applied.py
52+
if ! diff expected.py applied.py; then
53+
echo "apply format output doesn't match expectation"
54+
exit 1
55+
fi
56+
'';
57+
};
458
in
559
{
6-
id = "ty";
7-
name = "ty LSP";
8-
displayVersion = ty.version;
9-
description = ''
60+
id = lib.mkDefault "ty";
61+
name = lib.mkDefault "ty LSP";
62+
displayVersion = lib.mkDefault ty.version;
63+
description = lib.mkDefault ''
1064
Ty is an extremely fast Python type checker from Astral with an integrated language server.
1165
'';
1266
replit.dev.languageServers.ty = {
@@ -16,7 +70,18 @@ in
1670
start = "${ty}/bin/ty server";
1771
};
1872

73+
replit.dev.formatters.ruff = {
74+
name = "Ruff";
75+
displayVersion = pkgs.ruff.version;
76+
language = "python3";
77+
extensions = [ ".py" ];
78+
start = {
79+
args = [ "${run-ruff-format}/bin/run-ruff-format" ];
80+
};
81+
stdin = true;
82+
};
83+
1984
replit.env = {
20-
PATH = "${ty}/bin";
85+
PATH = lib.mkDefault "${ty}/bin";
2186
};
2287
}

0 commit comments

Comments
 (0)