diff --git a/src/compwa_policy/check_dev_files/precommit.py b/src/compwa_policy/check_dev_files/precommit.py index 923d77b8..2d5eca6a 100644 --- a/src/compwa_policy/check_dev_files/precommit.py +++ b/src/compwa_policy/check_dev_files/precommit.py @@ -10,11 +10,10 @@ from ruamel.yaml.scalarstring import DoubleQuotedScalarString from compwa_policy.errors import PrecommitError -from compwa_policy.utilities import CONFIG_PATH from compwa_policy.utilities.executor import Executor from compwa_policy.utilities.precommit.getters import find_repo from compwa_policy.utilities.precommit.struct import Hook -from compwa_policy.utilities.pyproject import Pyproject, get_constraints_file +from compwa_policy.utilities.python import has_constraint_files from compwa_policy.utilities.yaml import create_prettier_round_trip_yaml if TYPE_CHECKING: @@ -91,7 +90,7 @@ def _update_precommit_ci_commit_msg(precommit: ModifiablePrecommit) -> None: precommit_ci = precommit.document.get("ci") if precommit_ci is None: return - if __has_constraint_files(): + if has_constraint_files(): expected_msg = "MAINT: update pip constraints and pre-commit" else: expected_msg = "MAINT: autoupdate pre-commit hooks" @@ -103,15 +102,6 @@ def _update_precommit_ci_commit_msg(precommit: ModifiablePrecommit) -> None: precommit.append_to_changelog(msg) -def __has_constraint_files() -> bool: - if not CONFIG_PATH.pip_constraints.exists(): - return False - python_versions = Pyproject.load().get_supported_python_versions() - constraint_files = [get_constraints_file(v) for v in python_versions] - constraint_paths = [Path(path) for path in constraint_files if path is not None] - return any(path.exists() for path in constraint_paths) - - def _update_precommit_ci_skip(precommit: ModifiablePrecommit) -> None: precommit_ci = precommit.document.get("ci") if precommit_ci is None: diff --git a/src/compwa_policy/check_dev_files/vscode.py b/src/compwa_policy/check_dev_files/vscode.py index ea7c1403..279c1d8c 100644 --- a/src/compwa_policy/check_dev_files/vscode.py +++ b/src/compwa_policy/check_dev_files/vscode.py @@ -4,6 +4,7 @@ from compwa_policy.utilities import CONFIG_PATH, vscode from compwa_policy.utilities.executor import Executor +from compwa_policy.utilities.python import has_constraint_files def main(has_notebooks: bool) -> None: @@ -73,6 +74,11 @@ def _update_settings(has_notebooks: bool) -> None: if has_notebooks: do(_update_notebook_settings) do(_update_pytest_settings) + if has_constraint_files(): + do( + vscode.update_settings, + {"files.associations": {"**/.constraints/py*.txt": "pip-requirements"}}, + ) if CONFIG_PATH.envrc.exists(): do(vscode.update_settings, {"python.terminal.activateEnvironment": False}) diff --git a/src/compwa_policy/utilities/python.py b/src/compwa_policy/utilities/python.py new file mode 100644 index 00000000..7899f768 --- /dev/null +++ b/src/compwa_policy/utilities/python.py @@ -0,0 +1,13 @@ +from pathlib import Path # noqa: D100 + +from compwa_policy.utilities import CONFIG_PATH +from compwa_policy.utilities.pyproject import Pyproject, get_constraints_file + + +def has_constraint_files() -> bool: + if not CONFIG_PATH.pip_constraints.exists(): + return False + python_versions = Pyproject.load().get_supported_python_versions() + constraint_files = [get_constraints_file(v) for v in python_versions] + constraint_paths = [Path(path) for path in constraint_files if path is not None] + return any(path.exists() for path in constraint_paths)