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