Skip to content

Commit 468ceaf

Browse files
ofekichard26JelleZijlstra
authored
Switch build backend to Hatchling (psf#3233)
This implements PEP 621, obviating the need for `setup.py`, `setup.cfg`, and `MANIFEST.in`. The build backend Hatchling (of which I am a maintainer in the PyPA) is now used as that is the default in the official Python packaging tutorial. Hatchling is available on all the major distribution channels such as Debian, Fedora, and many more. ## Python support The earliest supported Python 3 version of Hatchling is 3.7, therefore I've also set that as the minimum here. Python 3.6 is EOL and other build backends like flit-core and setuptools also dropped support. Python 3.6 accounted for 3-4% of downloads in the last month. ## Plugins Configuration is now completely static with the help of 3 plugins: ### Readme hynek's hatch-fancy-pypi-readme allows for the dynamic construction of the readme which was previously coded up in `setup.py`. Now it's simply: ```toml [tool.hatch.metadata.hooks.fancy-pypi-readme] content-type = "text/markdown" fragments = [ { path = "README.md" }, { path = "CHANGES.md" }, ] ``` ### Versioning hatch-vcs is currently just a wrapper around setuptools-scm (which despite the legacy naming is actually now decoupled from setuptools): ```toml [tool.hatch.version] source = "vcs" [tool.hatch.build.hooks.vcs] version-file = "src/_black_version.py" template = ''' version = "{version}" ''' ``` ### mypyc hatch-mypyc offers many benefits over the existing approach: - No need to manually select files for inclusion - Avoids the need for the current CI workaround for mypyc/mypyc#946 - Intermediate artifacts (like `build/`) from setuptools and mypyc itself no longer clutter the project directory - Runtime dependencies required at build time no longer need to be manually redeclared as this is a built-in option of Hatchling Co-authored-by: Richard Si <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 4b4680a commit 468ceaf

File tree

13 files changed

+140
-195
lines changed

13 files changed

+140
-195
lines changed

.github/mypyc-requirements.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/workflows/diff_shades.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: diff-shades
33
on:
44
push:
55
branches: [main]
6-
paths: ["src/**", "setup.*", "pyproject.toml", ".github/workflows/*"]
6+
paths: ["src/**", "pyproject.toml", ".github/workflows/*"]
77

88
pull_request:
9-
paths: ["src/**", "setup.*", "pyproject.toml", ".github/workflows/*"]
9+
paths: ["src/**", "pyproject.toml", ".github/workflows/*"]
1010

1111
concurrency:
1212
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
@@ -41,6 +41,7 @@ jobs:
4141
needs: configure
4242
runs-on: ubuntu-latest
4343
env:
44+
HATCH_BUILD_HOOKS_ENABLE: "1"
4445
# Clang is less picky with the C code it's given than gcc (and may
4546
# generate faster binaries too).
4647
CC: clang-12
@@ -64,7 +65,6 @@ jobs:
6465
run: |
6566
python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip
6667
python -m pip install click packaging urllib3
67-
python -m pip install -r .github/mypyc-requirements.txt
6868
# After checking out old revisions, this might not exist so we'll use a copy.
6969
cat scripts/diff_shades_gha_helper.py > helper.py
7070
git config user.name "diff-shades-gha"
@@ -83,8 +83,7 @@ jobs:
8383
GITHUB_TOKEN: ${{ github.token }}
8484
run: >
8585
${{ matrix.baseline-setup-cmd }}
86-
&& python setup.py --use-mypyc bdist_wheel
87-
&& python -m pip install dist/*.whl && rm build dist -r
86+
&& python -m pip install .
8887
8988
- name: Analyze baseline revision
9089
if: steps.baseline-cache.outputs.cache-hit != 'true'
@@ -97,8 +96,7 @@ jobs:
9796
GITHUB_TOKEN: ${{ github.token }}
9897
run: >
9998
${{ matrix.target-setup-cmd }}
100-
&& python setup.py --use-mypyc bdist_wheel
101-
&& python -m pip install dist/*.whl
99+
&& python -m pip install .
102100
103101
- name: Analyze target revision
104102
run: >

.github/workflows/fuzz.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
strategy:
2323
fail-fast: false
2424
matrix:
25-
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
25+
python-version: ["3.7", "3.8", "3.9", "3.10"]
2626

2727
steps:
2828
- uses: actions/checkout@v3

.github/workflows/pypi_upload.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ jobs:
6161
uses: pypa/[email protected]
6262
env:
6363
CIBW_ARCHS_MACOS: "${{ matrix.macos_arch }}"
64-
# This isn't supported in pyproject.toml which makes sense (but is annoying).
65-
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.6.2"
6664

6765
- name: Upload wheels as workflow artifacts
6866
uses: actions/upload-artifact@v2

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
strategy:
3232
fail-fast: false
3333
matrix:
34-
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "pypy-3.7", "pypy-3.8"]
34+
python-version: ["3.7", "3.8", "3.9", "3.10", "pypy-3.7", "pypy-3.8"]
3535
os: [ubuntu-latest, macOS-latest, windows-latest]
3636

3737
steps:

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
<!-- Include any especially major or disruptive changes here -->
88

9+
- Runtime support for Python 3.6 has been removed. Formatting 3.6 code will still be
10+
supported until further notice.
11+
912
### Stable style
1013

1114
<!-- Changes that affect Black's stable style -->
@@ -28,6 +31,8 @@
2831

2932
<!-- Changes to how Black is packaged, such as dependency requirements -->
3033

34+
- Hatchling is now used as the build backend. This will not have any effect for users
35+
who install Black with its wheels from PyPI. (#3233)
3136
- Faster compiled wheels are now available for CPython 3.11 (#3276)
3237

3338
### Parser

MANIFEST.in

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/faq.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ disabled-by-default counterpart W504. E203 should be disabled while changes are
8686

8787
## Which Python versions does Black support?
8888

89-
Currently the runtime requires Python 3.6-3.10. Formatting is supported for files
90-
containing syntax from Python 3.3 to 3.10. We promise to support at least all Python
89+
Currently the runtime requires Python 3.7-3.11. Formatting is supported for files
90+
containing syntax from Python 3.3 to 3.11. We promise to support at least all Python
9191
versions that have not reached their end of life. This is the case for both running
9292
_Black_ and formatting code.
9393

9494
Support for formatting Python 2 code was removed in version 22.0. While we've made no
9595
plans to stop supporting older Python 3 minor versions immediately, their support might
9696
also be removed some time in the future without a deprecation period.
9797

98+
Runtime support for 3.6 was removed in version 22.9.0.
99+
98100
## Why does my linter or typechecker complain after I format my code?
99101

100102
Some linters and other tools use magical comments (e.g., `# noqa`, `# type: ignore`) to

docs/usage_and_configuration/the_basics.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ code in compliance with many other _Black_ formatted projects.
204204

205205
[PEP 518](https://www.python.org/dev/peps/pep-0518/) defines `pyproject.toml` as a
206206
configuration file to store build system requirements for Python projects. With the help
207-
of tools like [Poetry](https://python-poetry.org/) or
208-
[Flit](https://flit.readthedocs.io/en/latest/) it can fully replace the need for
209-
`setup.py` and `setup.cfg` files.
207+
of tools like [Poetry](https://python-poetry.org/),
208+
[Flit](https://flit.readthedocs.io/en/latest/), or
209+
[Hatch](https://hatch.pypa.io/latest/) it can fully replace the need for `setup.py` and
210+
`setup.cfg` files.
210211

211212
### Where _Black_ looks for the file
212213

pyproject.toml

Lines changed: 117 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
[tool.black]
99
line-length = 88
10-
target-version = ['py36', 'py37', 'py38']
10+
target-version = ['py37', 'py38']
1111
include = '\.pyi?$'
1212
extend-exclude = '''
1313
/(
@@ -26,18 +26,128 @@ preview = true
2626
# NOTE: You don't need this in your own Black configuration.
2727

2828
[build-system]
29-
requires = ["setuptools>=45.0", "setuptools_scm[toml]>=6.3.1", "wheel"]
30-
build-backend = "setuptools.build_meta"
29+
requires = ["hatchling>=1.8.0", "hatch-vcs", "hatch-fancy-pypi-readme"]
30+
build-backend = "hatchling.build"
31+
32+
[project]
33+
name = "black"
34+
description = "The uncompromising code formatter."
35+
license = "MIT"
36+
requires-python = ">=3.7"
37+
authors = [
38+
{ name = "Łukasz Langa", email = "[email protected]" },
39+
]
40+
keywords = [
41+
"automation",
42+
"autopep8",
43+
"formatter",
44+
"gofmt",
45+
"pyfmt",
46+
"rustfmt",
47+
"yapf",
48+
]
49+
classifiers = [
50+
"Development Status :: 5 - Production/Stable",
51+
"Environment :: Console",
52+
"Intended Audience :: Developers",
53+
"License :: OSI Approved :: MIT License",
54+
"Operating System :: OS Independent",
55+
"Programming Language :: Python",
56+
"Programming Language :: Python :: 3 :: Only",
57+
"Programming Language :: Python :: 3.7",
58+
"Programming Language :: Python :: 3.8",
59+
"Programming Language :: Python :: 3.9",
60+
"Programming Language :: Python :: 3.10",
61+
"Topic :: Software Development :: Libraries :: Python Modules",
62+
"Topic :: Software Development :: Quality Assurance",
63+
]
64+
dependencies = [
65+
"click>=8.0.0",
66+
"mypy_extensions>=0.4.3",
67+
"pathspec>=0.9.0",
68+
"platformdirs>=2",
69+
"tomli>=1.1.0; python_full_version < '3.11.0a7'",
70+
"typed-ast>=1.4.2; python_version < '3.8' and implementation_name == 'cpython'",
71+
"typing_extensions>=3.10.0.0; python_version < '3.10'",
72+
]
73+
dynamic = ["readme", "version"]
74+
75+
[project.optional-dependencies]
76+
colorama = ["colorama>=0.4.3"]
77+
uvloop = ["uvloop>=0.15.2"]
78+
d = [
79+
"aiohttp>=3.7.4",
80+
]
81+
jupyter = [
82+
"ipython>=7.8.0",
83+
"tokenize-rt>=3.2.0",
84+
]
85+
86+
[project.scripts]
87+
black = "black:patched_main"
88+
blackd = "blackd:patched_main [d]"
89+
90+
[project.urls]
91+
Changelog = "https://github.com/psf/black/blob/main/CHANGES.md"
92+
Homepage = "https://github.com/psf/black"
93+
94+
[tool.hatch.metadata.hooks.fancy-pypi-readme]
95+
content-type = "text/markdown"
96+
fragments = [
97+
{ path = "README.md" },
98+
{ path = "CHANGES.md" },
99+
]
100+
101+
[tool.hatch.version]
102+
source = "vcs"
103+
104+
[tool.hatch.build.hooks.vcs]
105+
version-file = "src/_black_version.py"
106+
template = '''
107+
version = "{version}"
108+
'''
109+
110+
[tool.hatch.build.targets.sdist]
111+
exclude = ["/profiling"]
112+
113+
[tool.hatch.build.targets.wheel]
114+
only-include = ["src"]
115+
sources = ["src"]
116+
117+
[tool.hatch.build.targets.wheel.hooks.mypyc]
118+
enable-by-default = false
119+
dependencies = [
120+
"hatch-mypyc>=0.13.0",
121+
"mypy==0.971",
122+
# Required stubs to be removed when the packages support PEP 561 themselves
123+
"types-typed-ast>=1.4.2",
124+
]
125+
require-runtime-dependencies = true
126+
exclude = [
127+
# There's no good reason for blackd to be compiled.
128+
"/src/blackd",
129+
# Not performance sensitive, so save bytes + compilation time:
130+
"/src/blib2to3/__init__.py",
131+
"/src/blib2to3/pgen2/__init__.py",
132+
"/src/black/output.py",
133+
"/src/black/concurrency.py",
134+
"/src/black/files.py",
135+
"/src/black/report.py",
136+
# Breaks the test suite when compiled (and is also useless):
137+
"/src/black/debug.py",
138+
# Compiled modules can't be run directly and that's a problem here:
139+
"/src/black/__main__.py",
140+
]
141+
options = { debug_level = "0" }
31142

32143
[tool.cibuildwheel]
33144
build-verbosity = 1
34145
# So these are the environments we target:
35-
# - Python: CPython 3.6+ only
146+
# - Python: CPython 3.7+ only
36147
# - Architecture (64-bit only): amd64 / x86_64, universal2, and arm64
37148
# - OS: Linux (no musl), Windows, and macOS
38149
build = "cp3*-*"
39150
skip = ["*-manylinux_i686", "*-musllinux_*", "*-win32", "pp-*"]
40-
before-build = ["pip install -r .github/mypyc-requirements.txt"]
41151
# This is the bare minimum needed to run the test suite. Pulling in the full
42152
# test_requirements.txt would download a bunch of other packages not necessary
43153
# here and would slow down the testing step a fair bit.
@@ -49,7 +159,7 @@ test-extras = ["d"," jupyter"]
49159
test-skip = ["*-macosx_arm64", "*-macosx_universal2:arm64"]
50160

51161
[tool.cibuildwheel.environment]
52-
BLACK_USE_MYPYC = "1"
162+
HATCH_BUILD_HOOKS_ENABLE = "1"
53163
MYPYC_OPT_LEVEL = "3"
54164
MYPYC_DEBUG_LEVEL = "0"
55165
# The dependencies required to build wheels with mypyc aren't specified in
@@ -61,28 +171,17 @@ AIOHTTP_NO_EXTENSIONS = "1"
61171

62172
[tool.cibuildwheel.linux]
63173
before-build = [
64-
"pip install -r .github/mypyc-requirements.txt",
65174
"yum install -y clang gcc",
66175
]
67176

68177
[tool.cibuildwheel.linux.environment]
69-
BLACK_USE_MYPYC = "1"
178+
HATCH_BUILD_HOOKS_ENABLE = "1"
70179
MYPYC_OPT_LEVEL = "3"
71180
MYPYC_DEBUG_LEVEL = "0"
72-
PIP_NO_BUILD_ISOLATION = "no"
73181
# Black needs Clang to compile successfully on Linux.
74182
CC = "clang"
75183
AIOHTTP_NO_EXTENSIONS = "1"
76184

77-
[tool.cibuildwheel.windows]
78-
# For some reason, (compiled) mypyc is failing to start up with "ImportError: DLL load
79-
# failed: A dynamic link library (DLL) initialization routine failed." on Windows for
80-
# at least 3.6. Let's just use interpreted mypy[c].
81-
# See also: https://github.com/mypyc/mypyc/issues/819.
82-
before-build = [
83-
"pip install -r .github/mypyc-requirements.txt --no-binary mypy"
84-
]
85-
86185
[tool.isort]
87186
atomic = true
88187
profile = "black"

0 commit comments

Comments
 (0)