From 43e9a86394929310544362194ea3bf1aac34c088 Mon Sep 17 00:00:00 2001 From: Mark Mayo Date: Mon, 17 Apr 2023 12:02:39 +1200 Subject: [PATCH] some updates and fixes Didn't get around to it in my previous attempt, so doing it now. - updated deprecated .warn() to .warning() - unnecessary f-strings - imported sys to allow sys.exit calls instead of exit - simplified some not checks - replaced type with isinstance - --- pre_commit/git.py | 12 ++++++------ pre_commit/repository.py | 31 ++++++++++++++++--------------- testing/fixtures.py | 2 +- tests/error_handler_test.py | 2 +- tests/languages/docker_test.py | 2 +- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index 333dc7ba..bac2ae21 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -219,12 +219,12 @@ def check_for_cygwin_mismatch() -> None: if is_cygwin_python ^ is_cygwin_git: exe_type = {True: '(cygwin)', False: '(windows)'} - logger.warn( - f'pre-commit has detected a mix of cygwin python / git\n' - f'This combination is not supported, it is likely you will ' - f'receive an error later in the program.\n' - f'Make sure to use cygwin git+python while using cygwin\n' - f'These can be installed through the cygwin installer.\n' + logger.warning( + 'pre-commit has detected a mix of cygwin python / git\n' + 'This combination is not supported, it is likely you will ' + 'receive an error later in the program.\n' + 'Make sure to use cygwin git+python while using cygwin\n' + 'These can be installed through the cygwin installer.\n' f' - python {exe_type[is_cygwin_python]}\n' f' - git {exe_type[is_cygwin_git]}\n', ) diff --git a/pre_commit/repository.py b/pre_commit/repository.py index 040f238f..e5951bed 100644 --- a/pre_commit/repository.py +++ b/pre_commit/repository.py @@ -3,6 +3,7 @@ from __future__ import annotations import json import logging import os +import sys import shlex from typing import Any from typing import Sequence @@ -72,9 +73,9 @@ def _hook_install(hook: Hook) -> None: if hook.language == 'python_venv': logger.warning( f'`repo: {hook.src}` uses deprecated `language: python_venv`. ' - f'This is an alias for `language: python`. ' + 'This is an alias for `language: python`. ' f'Often `pre-commit autoupdate --repo {shlex.quote(hook.src)}` ' - f'will fix this.', + 'will fix this.', ) lang = languages[hook.language] @@ -99,8 +100,8 @@ def _hook_install(hook: Hook) -> None: if health_error: raise AssertionError( f'BUG: expected environment for {hook.language} to be healthy ' - f'immediately after install, please open an issue describing ' - f'your environment\n\n' + 'immediately after install, please open an issue describing ' + 'your environment\n\n' f'more info:\n\n{health_error}', ) @@ -129,9 +130,9 @@ def _hook( logger.error( f'The hook `{ret["id"]}` requires pre-commit version {version} ' f'but version {C.VERSION} is installed. ' - f'Perhaps run `pip install --upgrade pre-commit`.', + 'Perhaps run `pip install --upgrade pre-commit`.', ) - exit(1) + sys.exit(1) lang = ret['language'] if ret['language_version'] == C.DEFAULT: @@ -147,18 +148,18 @@ def _hook( logger.error( f'The hook `{ret["id"]}` specifies `language_version` but is ' f'using language `{lang}` which does not install an ' - f'environment. ' - f'Perhaps you meant to use a specific language?', + 'environment. ' + 'Perhaps you meant to use a specific language?', ) - exit(1) + sys.exit(1) if ret['additional_dependencies']: logger.error( f'The hook `{ret["id"]}` specifies `additional_dependencies` ' f'but is using language `{lang}` which does not install an ' - f'environment. ' - f'Perhaps you meant to use a specific language?', + 'environment. ' + 'Perhaps you meant to use a specific language?', ) - exit(1) + sys.exit(1) return ret @@ -200,10 +201,10 @@ def _cloned_repository_hooks( if hook['id'] not in by_id: logger.error( f'`{hook["id"]}` is not present in repository {repo}. ' - f'Typo? Perhaps it is introduced in a newer version? ' - f'Often `pre-commit autoupdate` fixes this.', + 'Typo? Perhaps it is introduced in a newer version? ' + 'Often `pre-commit autoupdate` fixes this.', ) - exit(1) + sys.exit(1) hook_dcts = [ _hook(by_id[hook['id']], hook, root_config=root_config) diff --git a/testing/fixtures.py b/testing/fixtures.py index 79a11605..0b064e2a 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -121,7 +121,7 @@ def read_config(directory, config_file=C.CONFIG_FILE): def write_config(directory, config, config_file=C.CONFIG_FILE): - if type(config) is not list and 'repos' not in config: + if not isinstance(config, list) and 'repos' not in config: assert isinstance(config, dict), config config = {'repos': [config]} with open(os.path.join(directory, config_file), 'w') as outfile: diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py index a79d9c1a..e8b76e42 100644 --- a/tests/error_handler_test.py +++ b/tests/error_handler_test.py @@ -201,7 +201,7 @@ def test_error_handler_read_only_filesystem(mock_store_dir, cap_out, capsys): # already been set up Store() - write = (stat.S_IWGRP | stat.S_IWOTH | stat.S_IWUSR) + write = stat.S_IWGRP | stat.S_IWOTH | stat.S_IWUSR os.chmod(mock_store_dir, os.stat(mock_store_dir).st_mode & ~write) with pytest.raises(SystemExit): diff --git a/tests/languages/docker_test.py b/tests/languages/docker_test.py index 836382a8..2e70fdcc 100644 --- a/tests/languages/docker_test.py +++ b/tests/languages/docker_test.py @@ -59,7 +59,7 @@ def test_docker_fallback_user(): getuid=invalid_attribute, getgid=invalid_attribute, ): - assert docker.get_docker_user() == () + assert not docker.get_docker_user() def test_in_docker_no_file():