Skip to content

Commit a7eb122

Browse files
committed
Remove Python 2 support
1 parent 3ce7cf0 commit a7eb122

3 files changed

Lines changed: 15 additions & 72 deletions

File tree

Dockerfile

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,9 @@ apk add --no-cache \
4646
python3
4747
pip3 install --break-system-packages \
4848
autoflake==1.7.8 \
49-
isort==5.13.2 \
5049
ruff==0.15.10 \
5150
'PyYAML>=6.0'
5251

53-
# Install Python dependencies
54-
python3 -m venv /black21-venv
55-
source /black21-venv/bin/activate
56-
pip3 install black==21.12b0 click==8.0.4
57-
deactivate
58-
echo 'source /black21-venv/bin/activate && black "$@"' > /usr/bin/black21
59-
chmod +x /usr/bin/black21
60-
6152
# Install Node dependencies
6253
npm install -g \
6354
@eslint/compat@2.0.5 \
@@ -113,15 +104,6 @@ find /usr/local/lib/node_modules -type d \
113104
apk del .build-deps
114105
rm -rf \
115106
/bin/coursier \
116-
/black21-venv/bin/Activate.ps1 \
117-
/black21-venv/bin/activate.csh \
118-
/black21-venv/bin/activate.fish \
119-
/black21-venv/bin/pip \
120-
/black21-venv/bin/pip3 \
121-
/black21-venv/bin/pip3.12 \
122-
/black21-venv/lib/python3.12/site-packages/pip \
123-
/black21-venv/lib/python3.12/site-packages/pkg_resources \
124-
/black21-venv/lib/python3.12/site-packages/setuptools \
125107
/root/.cache \
126108
/root/.npm \
127109
/usr/bin/lto-dump \

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ The main hook that runs several code formatters in parallel:
88

99
- [Prettier](https://github.com/prettier/prettier) v3.8.3 for CSS, HTML, JS, JSX, Markdown, Sass, TypeScript, XML, YAML
1010
- [ESLint](https://eslint.org/) v10.2.0 for JS, TypeScript
11-
- [Ruff](https://docs.astral.sh/ruff/) v0.15.10 for Python 3
12-
- [Black](https://github.com/psf/black) v21.12b0 for Python 2
13-
- [autoflake](https://github.com/myint/autoflake) v1.7.8 for Python <!-- TODO: Upgrade to v2+, restrict to Python 2, and reenable Ruff rule F401 once our Python 3 repos that were converted from Python 2 no longer use type hint comments: https://github.com/PyCQA/autoflake/issues/222#issuecomment-1419089254 -->
14-
- [isort](https://github.com/PyCQA/isort) v5.13.2 for Python 2
11+
- [Ruff](https://docs.astral.sh/ruff/) v0.15.10 for Python
12+
- [autoflake](https://github.com/myint/autoflake) v1.7.8 for Python <!-- TODO: Remove in favor of Ruff rule F401 once our Python 3 repos that were converted from Python 2 no longer use type hint comments: https://github.com/PyCQA/autoflake/issues/222#issuecomment-1419089254 -->
1513
- [google-java-format](https://github.com/google/google-java-format) v1.35.0 for Java
1614
- [ktfmt](https://github.com/facebookincubator/ktfmt) v0.62 for Kotlin
1715
- [gradle-dependencies-sorter](https://github.com/square/gradle-dependencies-sorter) v0.16 for Gradle Kotlin
@@ -25,7 +23,7 @@ The main hook that runs several code formatters in parallel:
2523
- [Taplo](https://taplo.tamasfe.dev/) v0.10.0 for TOML
2624
- Custom regex transformations (basically [sed](https://en.wikipedia.org/wiki/Sed)), for example:
2725
- Trimming trailing whitespace and newlines
28-
- Removing unnecessary `coding` pragmas and `object` base classes in Python 3
26+
- Removing unnecessary `coding` pragmas and `object` base classes in Python
2927
- Replacing empty Python collections like `list()` with literal equivalents
3028
- Replacing empty Kotlin collections like `arrayOf()` with `empty` equivalents
3129

@@ -63,7 +61,6 @@ Repo maintainers can declare these hooks in `.pre-commit-config.yaml`:
6361
# Code formatting hook
6462
- id: duolingo
6563
args: # Optional
66-
- --python-version=2 # Defaults to Python 3
6764
- --scala-version=3 # Defaults to Scala 2.12
6865
# Sync AI rules hook (for repos with Cursor AI rules)
6966
- id: sync-ai-rules

entry.ts

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,11 @@ const transformFile = (path: string, transform: (before: string) => string) =>
7979

8080
const enum HookName {
8181
Autoflake = "autoflake",
82-
Black = "Black",
8382
ClangFormat = "ClangFormat",
8483
EsLint = "ESLint",
8584
Gofmt = "gofmt",
8685
GoogleJavaFormat = "google-java-format",
8786
GradleDependenciesSorter = "gradle-dependencies-sorter",
88-
Isort = "isort",
8987
Ktfmt = "ktfmt",
9088
PackerFmt = "packer fmt",
9189
Prettier = "Prettier",
@@ -142,28 +140,6 @@ const HOOKS: Record<HookName, Hook> = {
142140
include: /\.py$/,
143141
runAfter: [HookName.Sed],
144142
},
145-
[HookName.Black]: {
146-
action: async (sources, args) =>
147-
args["python-version"]?.startsWith("2") &&
148-
run(
149-
// Black 21.x was the last major version with Python 2 support. It also
150-
// had a bug that requires pinning click==8.0.4. Both packages should
151-
// be removed once we drop Python 2 support.
152-
// https://github.com/psf/black/issues/2964
153-
"black21",
154-
"--fast",
155-
"--target-version",
156-
"py27",
157-
"--config",
158-
EMPTY_FILE,
159-
"--line-length",
160-
"100",
161-
"--quiet",
162-
...sources,
163-
),
164-
include: /\.py$/,
165-
runAfter: [HookName.Isort],
166-
},
167143
[HookName.ClangFormat]: {
168144
action: sources =>
169145
run(
@@ -213,16 +189,6 @@ const HOOKS: Record<HookName, Hook> = {
213189
include: /build\.gradle\.kts$/,
214190
runAfter: [HookName.Sed],
215191
},
216-
[HookName.Isort]: {
217-
// isort's automatic config file detection is broken
218-
// https://github.com/PyCQA/isort/issues/1907
219-
// https://github.com/samueljsb/qaz/pull/104
220-
action: (sources, args) =>
221-
args["python-version"]?.startsWith("2") &&
222-
run("isort", "--settings", "/.editorconfig", ...sources),
223-
include: /\.py$/,
224-
runAfter: [HookName.Autoflake],
225-
},
226192
[HookName.Ktfmt]: {
227193
action: async sources => {
228194
/** Try to avoid ktfmt OOMs presumably caused by too many input files */
@@ -287,10 +253,7 @@ const HOOKS: Record<HookName, Hook> = {
287253
runAfter: [HookName.Sed],
288254
},
289255
[HookName.Ruff]: {
290-
action: async (sources, args) => {
291-
if (args["python-version"]?.startsWith("2")) {
292-
return;
293-
}
256+
action: async sources => {
294257
// Sometimes Ruff requires multiple passes, which is ok since it's fast
295258
for (let i = 0; i < 2; ++i) {
296259
await run("ruff", "check", "--config", "/ruff.toml", ...sources);
@@ -329,7 +292,7 @@ const HOOKS: Record<HookName, Hook> = {
329292
// to ever actually be needed. At Duolingo, we determine the latter criterion
330293
// empirically by seeing how many existing violations our codebase contains
331294
[HookName.Sed]: {
332-
action: (sources, args) =>
295+
action: sources =>
333296
Promise.all(
334297
sources.map(source =>
335298
transformFile(source, data => {
@@ -369,14 +332,11 @@ const HOOKS: Record<HookName, Hook> = {
369332
"",
370333
);
371334

372-
// Transform Python 3
373-
if (!args["python-version"]?.startsWith("2")) {
374-
// Remove unnecessary encoding declarations
375-
data = data.replace(/^# -\*- coding: utf-?8.*?\n/gim, "");
335+
// Remove unnecessary encoding declarations
336+
data = data.replace(/^# -\*- coding: utf-?8.*?\n/gim, "");
376337

377-
// Remove unnecessary base class declarations
378-
data = data.replace(/(?<=^ *class \S+?)\(object\)(?=:)/gm, "");
379-
}
338+
// Remove unnecessary base class declarations
339+
data = data.replace(/(?<=^ *class \S+?)\(object\)(?=:)/gm, "");
380340
}
381341

382342
return data;
@@ -505,8 +465,6 @@ const prefixLines = (() => {
505465
/^\s*$/,
506466
// ktfmt spams this for every file that has no violations
507467
/\bDone formatting .+\.kts?$/,
508-
// Black 21.12b0 spams this when running on Python 2 source code
509-
/\bDEPRECATION: Python 2 support\b/,
510468
]
511469
.map(regex => regex.source)
512470
.join("|"),
@@ -536,6 +494,12 @@ const prefixLines = (() => {
536494
}
537495
}
538496

497+
// Python 2 is no longer supported
498+
if (args["python-version"]?.startsWith("2")) {
499+
console.error("This hook no longer supports Python 2.");
500+
process.exit(1);
501+
}
502+
539503
// Augment hook definitions to make them lockable
540504
interface LockableHook extends Hook {
541505
/** Upon resolution, indicates that this hook has completed */

0 commit comments

Comments
 (0)