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
 -
This commit is contained in:
Mark Mayo 2023-04-17 12:02:39 +12:00
parent 6896025288
commit 43e9a86394
5 changed files with 25 additions and 24 deletions

View file

@ -219,12 +219,12 @@ def check_for_cygwin_mismatch() -> None:
if is_cygwin_python ^ is_cygwin_git: if is_cygwin_python ^ is_cygwin_git:
exe_type = {True: '(cygwin)', False: '(windows)'} exe_type = {True: '(cygwin)', False: '(windows)'}
logger.warn( logger.warning(
f'pre-commit has detected a mix of cygwin python / git\n' 'pre-commit has detected a mix of cygwin python / git\n'
f'This combination is not supported, it is likely you will ' 'This combination is not supported, it is likely you will '
f'receive an error later in the program.\n' 'receive an error later in the program.\n'
f'Make sure to use cygwin git+python while using cygwin\n' 'Make sure to use cygwin git+python while using cygwin\n'
f'These can be installed through the cygwin installer.\n' 'These can be installed through the cygwin installer.\n'
f' - python {exe_type[is_cygwin_python]}\n' f' - python {exe_type[is_cygwin_python]}\n'
f' - git {exe_type[is_cygwin_git]}\n', f' - git {exe_type[is_cygwin_git]}\n',
) )

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import json import json
import logging import logging
import os import os
import sys
import shlex import shlex
from typing import Any from typing import Any
from typing import Sequence from typing import Sequence
@ -72,9 +73,9 @@ def _hook_install(hook: Hook) -> None:
if hook.language == 'python_venv': if hook.language == 'python_venv':
logger.warning( logger.warning(
f'`repo: {hook.src}` uses deprecated `language: python_venv`. ' 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'Often `pre-commit autoupdate --repo {shlex.quote(hook.src)}` '
f'will fix this.', 'will fix this.',
) )
lang = languages[hook.language] lang = languages[hook.language]
@ -99,8 +100,8 @@ def _hook_install(hook: Hook) -> None:
if health_error: if health_error:
raise AssertionError( raise AssertionError(
f'BUG: expected environment for {hook.language} to be healthy ' f'BUG: expected environment for {hook.language} to be healthy '
f'immediately after install, please open an issue describing ' 'immediately after install, please open an issue describing '
f'your environment\n\n' 'your environment\n\n'
f'more info:\n\n{health_error}', f'more info:\n\n{health_error}',
) )
@ -129,9 +130,9 @@ def _hook(
logger.error( logger.error(
f'The hook `{ret["id"]}` requires pre-commit version {version} ' f'The hook `{ret["id"]}` requires pre-commit version {version} '
f'but version {C.VERSION} is installed. ' 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'] lang = ret['language']
if ret['language_version'] == C.DEFAULT: if ret['language_version'] == C.DEFAULT:
@ -147,18 +148,18 @@ def _hook(
logger.error( logger.error(
f'The hook `{ret["id"]}` specifies `language_version` but is ' f'The hook `{ret["id"]}` specifies `language_version` but is '
f'using language `{lang}` which does not install an ' f'using language `{lang}` which does not install an '
f'environment. ' 'environment. '
f'Perhaps you meant to use a specific language?', 'Perhaps you meant to use a specific language?',
) )
exit(1) sys.exit(1)
if ret['additional_dependencies']: if ret['additional_dependencies']:
logger.error( logger.error(
f'The hook `{ret["id"]}` specifies `additional_dependencies` ' f'The hook `{ret["id"]}` specifies `additional_dependencies` '
f'but is using language `{lang}` which does not install an ' f'but is using language `{lang}` which does not install an '
f'environment. ' 'environment. '
f'Perhaps you meant to use a specific language?', 'Perhaps you meant to use a specific language?',
) )
exit(1) sys.exit(1)
return ret return ret
@ -200,10 +201,10 @@ def _cloned_repository_hooks(
if hook['id'] not in by_id: if hook['id'] not in by_id:
logger.error( logger.error(
f'`{hook["id"]}` is not present in repository {repo}. ' f'`{hook["id"]}` is not present in repository {repo}. '
f'Typo? Perhaps it is introduced in a newer version? ' 'Typo? Perhaps it is introduced in a newer version? '
f'Often `pre-commit autoupdate` fixes this.', 'Often `pre-commit autoupdate` fixes this.',
) )
exit(1) sys.exit(1)
hook_dcts = [ hook_dcts = [
_hook(by_id[hook['id']], hook, root_config=root_config) _hook(by_id[hook['id']], hook, root_config=root_config)

View file

@ -121,7 +121,7 @@ def read_config(directory, config_file=C.CONFIG_FILE):
def write_config(directory, config, 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 assert isinstance(config, dict), config
config = {'repos': [config]} config = {'repos': [config]}
with open(os.path.join(directory, config_file), 'w') as outfile: with open(os.path.join(directory, config_file), 'w') as outfile:

View file

@ -201,7 +201,7 @@ def test_error_handler_read_only_filesystem(mock_store_dir, cap_out, capsys):
# already been set up # already been set up
Store() 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) os.chmod(mock_store_dir, os.stat(mock_store_dir).st_mode & ~write)
with pytest.raises(SystemExit): with pytest.raises(SystemExit):

View file

@ -59,7 +59,7 @@ def test_docker_fallback_user():
getuid=invalid_attribute, getuid=invalid_attribute,
getgid=invalid_attribute, getgid=invalid_attribute,
): ):
assert docker.get_docker_user() == () assert not docker.get_docker_user()
def test_in_docker_no_file(): def test_in_docker_no_file():