diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 03174c0d2b1..5256544c5e6 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -847,8 +847,8 @@ rules: ## Development Commands ### Code Quality & Linting -- **Run all linters on all files**: `pre-commit run --all-files` -- **Run linters on staged files only**: `pre-commit run` +- **Run all linters on all files**: `prek run --all-files` +- **Run linters on staged files only**: `prek run` - **PyLint on everything** (slow): `pylint homeassistant` - **PyLint on specific folder**: `pylint homeassistant/components/my_integration` - **MyPy type checking (whole project)**: `mypy homeassistant/` diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1d513ba59e4..32dc9106b43 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -59,7 +59,6 @@ env: # 15 is the latest version # - 15.2 is the latest (as of 9 Feb 2023) POSTGRESQL_VERSIONS: "['postgres:12.14','postgres:15.2']" - PRE_COMMIT_CACHE: ~/.cache/pre-commit UV_CACHE_DIR: /tmp/uv-cache APT_CACHE_BASE: /home/runner/work/apt APT_CACHE_DIR: /home/runner/work/apt/cache @@ -83,7 +82,6 @@ jobs: integrations_glob: ${{ steps.info.outputs.integrations_glob }} integrations: ${{ steps.integrations.outputs.changes }} apt_cache_key: ${{ steps.generate_apt_cache_key.outputs.key }} - pre-commit_cache_key: ${{ steps.generate_pre-commit_cache_key.outputs.key }} python_cache_key: ${{ steps.generate_python_cache_key.outputs.key }} requirements: ${{ steps.core.outputs.requirements }} mariadb_groups: ${{ steps.info.outputs.mariadb_groups }} @@ -111,11 +109,6 @@ jobs: hashFiles('requirements_all.txt') }}-${{ hashFiles('homeassistant/package_constraints.txt') }}-${{ hashFiles('script/gen_requirements_all.py') }}" >> $GITHUB_OUTPUT - - name: Generate partial pre-commit restore key - id: generate_pre-commit_cache_key - run: >- - echo "key=pre-commit-${{ env.CACHE_VERSION }}-${{ - hashFiles('.pre-commit-config.yaml') }}" >> $GITHUB_OUTPUT - name: Generate partial apt restore key id: generate_apt_cache_key run: | @@ -244,8 +237,8 @@ jobs: echo "skip_coverage: ${skip_coverage}" echo "skip_coverage=${skip_coverage}" >> $GITHUB_OUTPUT - pre-commit: - name: Prepare pre-commit base + prek: + name: Run prek checks runs-on: *runs-on-ubuntu needs: [info] if: | @@ -254,147 +247,23 @@ jobs: && github.event.inputs.audit-licenses-only != 'true' steps: - *checkout - - &setup-python-default - name: Set up Python ${{ env.DEFAULT_PYTHON }} - id: python - uses: &actions-setup-python actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 - with: - python-version: ${{ env.DEFAULT_PYTHON }} - check-latest: true - - name: Restore base Python virtual environment - id: cache-venv - uses: &actions-cache actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 - with: - path: venv - key: &key-pre-commit-venv >- - ${{ runner.os }}-${{ runner.arch }}-${{ steps.python.outputs.python-version }}-venv-${{ - needs.info.outputs.pre-commit_cache_key }} - - name: Create Python virtual environment - if: steps.cache-venv.outputs.cache-hit != 'true' - run: | - python -m venv venv - . venv/bin/activate - python --version - pip install "$(grep '^uv' < requirements.txt)" - uv pip install "$(cat requirements_test.txt | grep pre-commit)" - - name: Restore pre-commit environment from cache - id: cache-precommit - uses: *actions-cache - with: - path: ${{ env.PRE_COMMIT_CACHE }} - lookup-only: true - key: &key-pre-commit-env >- - ${{ runner.os }}-${{ runner.arch }}-${{ steps.python.outputs.python-version }}-${{ - needs.info.outputs.pre-commit_cache_key }} - - name: Install pre-commit dependencies - if: steps.cache-precommit.outputs.cache-hit != 'true' - run: | - . venv/bin/activate - pre-commit install-hooks - - lint-ruff-format: - name: Check ruff-format - runs-on: *runs-on-ubuntu - needs: &needs-pre-commit - - info - - pre-commit - steps: - - *checkout - - *setup-python-default - - &cache-restore-pre-commit-venv - name: Restore base Python virtual environment - id: cache-venv - uses: &actions-cache-restore actions/cache/restore@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 - with: - path: venv - fail-on-cache-miss: true - key: *key-pre-commit-venv - - &cache-restore-pre-commit-env - name: Restore pre-commit environment from cache - id: cache-precommit - uses: *actions-cache-restore - with: - path: ${{ env.PRE_COMMIT_CACHE }} - fail-on-cache-miss: true - key: *key-pre-commit-env - - name: Run ruff-format - run: | - . venv/bin/activate - pre-commit run --hook-stage manual ruff-format --all-files --show-diff-on-failure - env: - RUFF_OUTPUT_FORMAT: github - - lint-ruff: - name: Check ruff - runs-on: *runs-on-ubuntu - needs: *needs-pre-commit - steps: - - *checkout - - *setup-python-default - - *cache-restore-pre-commit-venv - - *cache-restore-pre-commit-env - - name: Run ruff - run: | - . venv/bin/activate - pre-commit run --hook-stage manual ruff-check --all-files --show-diff-on-failure - env: - RUFF_OUTPUT_FORMAT: github - - lint-other: - name: Check other linters - runs-on: *runs-on-ubuntu - needs: *needs-pre-commit - steps: - - *checkout - - *setup-python-default - - *cache-restore-pre-commit-venv - - *cache-restore-pre-commit-env - - name: Register yamllint problem matcher run: | echo "::add-matcher::.github/workflows/matchers/yamllint.json" - - name: Run yamllint - run: | - . venv/bin/activate - pre-commit run --hook-stage manual yamllint --all-files --show-diff-on-failure - - name: Register check-json problem matcher run: | echo "::add-matcher::.github/workflows/matchers/check-json.json" - - name: Run check-json - run: | - . venv/bin/activate - pre-commit run --hook-stage manual check-json --all-files --show-diff-on-failure - - - name: Run prettier (fully) - if: needs.info.outputs.test_full_suite == 'true' - run: | - . venv/bin/activate - pre-commit run --hook-stage manual prettier --all-files --show-diff-on-failure - - - name: Run prettier (partially) - if: needs.info.outputs.test_full_suite == 'false' - shell: bash - run: | - . venv/bin/activate - shopt -s globstar - pre-commit run --hook-stage manual prettier --show-diff-on-failure --files {homeassistant,tests}/components/${{ needs.info.outputs.integrations_glob }}/{*,**/*} - - name: Register check executables problem matcher run: | echo "::add-matcher::.github/workflows/matchers/check-executables-have-shebangs.json" - - name: Run executables check - run: | - . venv/bin/activate - pre-commit run --hook-stage manual check-executables-have-shebangs --all-files --show-diff-on-failure - - name: Register codespell problem matcher run: | echo "::add-matcher::.github/workflows/matchers/codespell.json" - - name: Run codespell - run: | - . venv/bin/activate - pre-commit run --show-diff-on-failure --hook-stage manual codespell --all-files + - name: Run prek + uses: j178/prek-action@91fd7d7cf70ae1dee9f4f44e7dfa5d1073fe6623 # v1.0.11 + env: + PREK_SKIP: no-commit-to-branch,mypy,pylint,gen_requirements_all,hassfest,hassfest-metadata,hassfest-mypy-config + RUFF_OUTPUT_FORMAT: github lint-hadolint: name: Check ${{ matrix.file }} @@ -434,7 +303,7 @@ jobs: - &setup-python-matrix name: Set up Python ${{ matrix.python-version }} id: python - uses: *actions-setup-python + uses: &actions-setup-python actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 with: python-version: ${{ matrix.python-version }} check-latest: true @@ -447,7 +316,7 @@ jobs: env.HA_SHORT_VERSION }}-$(date -u '+%Y-%m-%dT%H:%M:%s')" >> $GITHUB_OUTPUT - name: Restore base Python virtual environment id: cache-venv - uses: *actions-cache + uses: &actions-cache actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 with: path: venv key: &key-python-venv >- @@ -562,7 +431,7 @@ jobs: steps: - &cache-restore-apt name: Restore apt cache - uses: *actions-cache-restore + uses: &actions-cache-restore actions/cache/restore@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1 with: path: *path-apt-cache fail-on-cache-miss: true @@ -579,7 +448,13 @@ jobs: -o Dir::State::Lists=${{ env.APT_LIST_CACHE_DIR }} \ libturbojpeg - *checkout - - *setup-python-default + - &setup-python-default + name: Set up Python ${{ env.DEFAULT_PYTHON }} + id: python + uses: *actions-setup-python + with: + python-version: ${{ env.DEFAULT_PYTHON }} + check-latest: true - &cache-restore-python-default name: Restore full Python ${{ env.DEFAULT_PYTHON }} virtual environment id: cache-venv @@ -782,9 +657,7 @@ jobs: - base - gen-requirements-all - hassfest - - lint-other - - lint-ruff - - lint-ruff-format + - prek - mypy steps: - *cache-restore-apt @@ -823,9 +696,7 @@ jobs: - base - gen-requirements-all - hassfest - - lint-other - - lint-ruff - - lint-ruff-format + - prek - mypy - prepare-pytest-full if: | @@ -949,9 +820,7 @@ jobs: - base - gen-requirements-all - hassfest - - lint-other - - lint-ruff - - lint-ruff-format + - prek - mypy if: | needs.info.outputs.lint_only != 'true' @@ -1066,9 +935,7 @@ jobs: - base - gen-requirements-all - hassfest - - lint-other - - lint-ruff - - lint-ruff-format + - prek - mypy if: | needs.info.outputs.lint_only != 'true' @@ -1202,9 +1069,7 @@ jobs: - base - gen-requirements-all - hassfest - - lint-other - - lint-ruff - - lint-ruff-format + - prek - mypy if: | needs.info.outputs.lint_only != 'true' diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0d0b9cd27ba..bfd14b20adb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -46,7 +46,7 @@ repos: # Run `python-typing-update` hook manually from time to time # to update python typing syntax. # Will require manual work, before submitting changes! - # pre-commit run --hook-stage manual python-typing-update --all-files + # prek run --hook-stage manual python-typing-update --all-files - id: python-typing-update stages: [manual] args: diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 50bb89daf38..ba90ecaeb16 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -45,7 +45,7 @@ { "label": "Ruff", "type": "shell", - "command": "pre-commit run ruff-check --all-files", + "command": "prek run ruff-check --all-files", "group": { "kind": "test", "isDefault": true @@ -57,9 +57,9 @@ "problemMatcher": [] }, { - "label": "Pre-commit", + "label": "Prek", "type": "shell", - "command": "pre-commit run --show-diff-on-failure", + "command": "prek run --show-diff-on-failure", "group": { "kind": "test", "isDefault": true diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 4b22d1284d7..ed6c697223f 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -16,7 +16,7 @@ from homeassistant.const import Platform if TYPE_CHECKING: # InferenceResult is available only from astroid >= 2.12.0 - # pre-commit should still work on out of date environments + # prek should still work on out of date environments from astroid.typing import InferenceResult _COMMON_ARGUMENTS: dict[str, list[str]] = { diff --git a/requirements_test.txt b/requirements_test.txt index 7126bb9371a..9ecf6cda367 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -15,7 +15,7 @@ librt==0.2.1 license-expression==30.4.3 mock-open==1.4.0 mypy-dev==1.19.0a4 -pre-commit==4.2.0 +prek==0.2.26 pydantic==2.12.2 pylint==4.0.1 pylint-per-file-ignores==1.4.0 diff --git a/script/hassfest/manifest.py b/script/hassfest/manifest.py index 05f89f250f6..de53164aed0 100644 --- a/script/hassfest/manifest.py +++ b/script/hassfest/manifest.py @@ -427,7 +427,7 @@ def validate(integrations: dict[str, Integration], config: Config) -> None: if config.action == "generate" and manifests_resorted: subprocess.run( [ - "pre-commit", + "prek", "run", "--hook-stage", "manual", diff --git a/script/lint b/script/lint index 26b6db705f1..5a7e9314b59 100755 --- a/script/lint +++ b/script/lint @@ -15,7 +15,7 @@ printf "%s\n" $files echo "==============" echo "LINT with ruff" echo "==============" -pre-commit run ruff-check --files $files +prek run ruff-check --files $files echo "================" echo "LINT with pylint" echo "================" diff --git a/script/lint_and_test.py b/script/lint_and_test.py index 44d9e5d8eb7..ed485f4b1ed 100755 --- a/script/lint_and_test.py +++ b/script/lint_and_test.py @@ -119,7 +119,7 @@ async def pylint(files): async def ruff(files): """Exec ruff.""" - _, log = await async_exec("pre-commit", "run", "ruff", "--files", *files) + _, log = await async_exec("prek", "run", "ruff", "--files", *files) res = [] for line in log.splitlines(): line = line.split(":") diff --git a/script/setup b/script/setup index 1fd61aa9b71..9b8984f4997 100755 --- a/script/setup +++ b/script/setup @@ -31,7 +31,7 @@ fi script/bootstrap -pre-commit install +prek install hass --script ensure_config -c config