1{
2 stdenv,
3 lib,
4 buildPythonPackage,
5 fetchPypi,
6 pythonOlder,
7 pytestCheckHook,
8 aiohttp,
9 click,
10 colorama,
11 hatch-fancy-pypi-readme,
12 hatch-vcs,
13 hatchling,
14 ipython,
15 mypy-extensions,
16 packaging,
17 pathspec,
18 parameterized,
19 platformdirs,
20 tokenize-rt,
21 tomli,
22 typing-extensions,
23 uvloop,
24}:
25
26buildPythonPackage rec {
27 pname = "black";
28 version = "25.1.0";
29 format = "pyproject";
30
31 disabled = pythonOlder "3.8";
32
33 src = fetchPypi {
34 inherit pname version;
35 hash = "sha256-M0ltXNEiKtczkTUrSujaFSU8Xeibk6gLPiyNmhnsJmY=";
36 };
37
38 nativeBuildInputs = [
39 hatch-fancy-pypi-readme
40 hatch-vcs
41 hatchling
42 ];
43
44 propagatedBuildInputs = [
45 click
46 mypy-extensions
47 packaging
48 pathspec
49 platformdirs
50 ]
51 ++ lib.optionals (pythonOlder "3.11") [
52 tomli
53 typing-extensions
54 ];
55
56 optional-dependencies = {
57 colorama = [ colorama ];
58 d = [ aiohttp ];
59 uvloop = [ uvloop ];
60 jupyter = [
61 ipython
62 tokenize-rt
63 ];
64 };
65
66 # Necessary for the tests to pass on Darwin with sandbox enabled.
67 # Black starts a local server and needs to bind a local address.
68 __darwinAllowLocalNetworking = true;
69
70 nativeCheckInputs = [
71 pytestCheckHook
72 parameterized
73 ]
74 ++ lib.flatten (lib.attrValues optional-dependencies);
75
76 pytestFlags = [
77 "-Wignore::DeprecationWarning"
78 ];
79
80 preCheck = ''
81 export PATH="$PATH:$out/bin"
82
83 # The top directory /build matches black's DEFAULT_EXCLUDE regex.
84 # Make /build the project root for black tests to avoid excluding files.
85 touch ../.git
86 ''
87 + lib.optionalString stdenv.hostPlatform.isDarwin ''
88 # Work around https://github.com/psf/black/issues/2105
89 export TMPDIR="/tmp"
90 '';
91
92 disabledTests = [
93 # requires network access
94 "test_gen_check_output"
95 # broken on Python 3.13.4
96 # FIXME: remove this when fixed upstream
97 "test_simple_format[pep_701]"
98 ]
99 ++ lib.optionals stdenv.hostPlatform.isDarwin [
100 # fails on darwin
101 "test_expression_diff"
102 # Fail on Hydra, see https://github.com/NixOS/nixpkgs/pull/130785
103 "test_bpo_2142_workaround"
104 "test_skip_magic_trailing_comma"
105 ];
106 # multiple tests exceed max open files on hydra builders
107 doCheck = !(stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64);
108
109 meta = with lib; {
110 description = "Uncompromising Python code formatter";
111 homepage = "https://github.com/psf/black";
112 changelog = "https://github.com/psf/black/blob/${version}/CHANGES.md";
113 license = licenses.mit;
114 mainProgram = "black";
115 maintainers = with maintainers; [
116 sveitser
117 autophagy
118 ];
119 };
120}