Skip to content

Commit 076da92

Browse files
authored
Check python version that was used to install pre-commit venvs (apache#43282)
Since we moved to Python 3.9 and started usign 3.9+ only features line functools.cache, having Python 3.8 as default python version will result in virtualenvs created with outdated python. This pre-commit checks what is the global version of python3 and it will complain if it is lower than 3.9. This is done via explicit call of "python3", because this is what pre-commit does. Also global setting is needed because otherwise new virtualenvs created by pre-commit might be created using lower python version.
1 parent e49033d commit 076da92

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ repos:
168168
\.cfg$|\.conf$|\.ini$|\.ldif$|\.properties$|\.readthedocs$|\.service$|\.tf$|Dockerfile.*$
169169
- repo: local
170170
hooks:
171+
- id: check-min-python-version
172+
name: Check minimum Python version
173+
entry: ./scripts/ci/pre_commit/check_min_python_version.py
174+
language: python
175+
additional_dependencies: ['rich>=12.4.4']
176+
require_serial: true
171177
- id: update-common-sql-api-stubs
172178
name: Check and update common.sql API stubs
173179
entry: ./scripts/ci/pre_commit/update_common_sql_api_stubs.py

contributing-docs/08_static_code_checks.rst

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ require Breeze Docker image to be built locally.
194194
+-----------------------------------------------------------+--------------------------------------------------------+---------+
195195
| check-merge-conflict | Check that merge conflicts are not being committed | |
196196
+-----------------------------------------------------------+--------------------------------------------------------+---------+
197+
| check-min-python-version | Check minimum Python version | |
198+
+-----------------------------------------------------------+--------------------------------------------------------+---------+
197199
| check-newsfragments-are-valid | Check newsfragments are valid | |
198200
+-----------------------------------------------------------+--------------------------------------------------------+---------+
199201
| check-no-airflow-deprecation-in-providers | Do not use DeprecationWarning in providers | |

dev/breeze/doc/images/output_static-checks.svg

+1-1
Loading
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b4becd0ef113ac04210350ea8f9f98b9
1+
53b7f32a93cb7dec849138d404c47f6c

dev/breeze/src/airflow_breeze/pre_commit_ids.py

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"check-lazy-logging",
6363
"check-links-to-example-dags-do-not-use-hardcoded-versions",
6464
"check-merge-conflict",
65+
"check-min-python-version",
6566
"check-newsfragments-are-valid",
6667
"check-no-airflow-deprecation-in-providers",
6768
"check-no-providers-in-core-examples",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
from __future__ import annotations
19+
20+
import subprocess
21+
import sys
22+
from pathlib import Path
23+
24+
sys.path.insert(0, str(Path(__file__).parent.resolve()))
25+
26+
from common_precommit_utils import console
27+
28+
# update this version when we switch to a newer version of Python
29+
required_version = tuple(map(int, "3.9".split(".")))
30+
required_version_str = f"{required_version[0]}.{required_version[1]}"
31+
global_version = tuple(
32+
map(
33+
int,
34+
subprocess.run(
35+
[
36+
"python3",
37+
"-c",
38+
'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")',
39+
],
40+
capture_output=True,
41+
text=True,
42+
check=True,
43+
)
44+
.stdout.strip()
45+
.split("."),
46+
)
47+
)
48+
49+
50+
if global_version < required_version:
51+
console.print(f"[red]Python {required_version_str} or higher is required to install pre-commit.\n")
52+
console.print(f"[green]Current version is {global_version}\n")
53+
console.print(
54+
"[bright_yellow]Please follow those steps:[/]\n\n"
55+
f" * make sure that `python3 --version` is at least {required_version_str}\n"
56+
f" * run `pre-commit clean`\n"
57+
f" * run `pre-commit install --install-hooks`\n\n"
58+
)
59+
console.print(
60+
"There are various ways you can set `python3` to point to a newer version of Python.\n\n"
61+
f"For example run `pyenv global {required_version_str}` if you use pyenv, or\n"
62+
f"you can use venv with python {required_version_str} when you use pre-commit, or\n"
63+
"you can use `update-alternatives` if you use Ubuntu, or\n"
64+
"you can set `PATH` to point to the newer version of Python.\n\n"
65+
)
66+
sys.exit(1)
67+
else:
68+
console.print(f"Python version is sufficient: {required_version_str}")

0 commit comments

Comments
 (0)