-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathmypy.nix
113 lines (111 loc) · 3.43 KB
/
mypy.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
lib,
pkgs,
config,
...
}:
let
escapePath = lib.replaceStrings [ "/" "." ] [ "-" "" ];
in
{
meta.maintainers = [ ];
options.programs.mypy = {
enable = lib.mkEnableOption "mypy";
package = lib.mkPackageOption pkgs "mypy" { };
directories = lib.mkOption {
description = "Directories to run mypy in";
type = lib.types.attrsOf (
lib.types.submodule (
{ name, ... }:
{
options = {
directory = lib.mkOption {
type = lib.types.str;
default = name;
description = "Directory to run mypy in";
};
extraPythonPackages = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [ ];
example = lib.literalMD "[ pkgs.python3.pkgs.requests ]";
description = "Extra packages to add to PYTHONPATH";
};
extraPythonPaths = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "./path/to/my/module" ];
description = ''
Extra paths to add to PYTHONPATH.
Paths are interpreted relative to the directory options and are added before extraPythonPackages.
'';
};
options = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "--ignore-missing-imports" ];
description = "Options to pass to mypy";
};
modules = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ "" ];
example = [
"mymodule"
"tests"
];
description = "Modules to check";
};
files = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "./**/tasks.py" ];
description = "Single files to check. Can be globs";
};
};
}
)
);
default = {
"" = { };
};
example = {
"." = {
modules = [
"mymodule"
"tests"
];
};
"./subdir" = { };
};
};
};
config = lib.mkIf config.programs.mypy.enable {
settings.formatter = lib.mapAttrs' (
name: cfg:
lib.nameValuePair "mypy-${escapePath name}" {
command = pkgs.bash;
options = [
"-eucx"
''
# to allow recursive globbing
shopt -s globstar
cd "${cfg.directory}"
export PYTHONPATH="${
lib.concatStringsSep ":" (
cfg.extraPythonPaths
++ lib.optional (cfg.extraPythonPackages != [ ]) (
pkgs.python3.pkgs.makePythonPath cfg.extraPythonPackages
)
)
}"
${lib.getExe config.programs.mypy.package} ${lib.escapeShellArgs cfg.options} ${lib.escapeShellArgs cfg.modules} ${builtins.toString cfg.files}
''
];
includes =
(builtins.map (
module: "${cfg.directory}/${module}${lib.optionalString (module != "") "/"}**/*.py"
) cfg.modules)
++ cfg.files;
}
) config.programs.mypy.directories;
};
}