From ab19b94811eadb3e8c05f16f39ca0a7f1012ebb3 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 8 Jan 2020 21:23:18 -0800 Subject: [PATCH] some manual py2 cleanups --- pre_commit/five.py | 5 +--- pre_commit/util.py | 9 +++---- requirements-dev.txt | 1 - setup.cfg | 1 - .../python_venv_hooks_repo/foo/__init__.py | 0 tests/color_test.py | 2 +- tests/commands/clean_test.py | 2 +- tests/commands/init_templatedir_test.py | 3 +-- tests/commands/install_uninstall_test.py | 3 +-- tests/commands/run_test.py | 2 +- tests/commands/try_repo_test.py | 3 +-- tests/conftest.py | 2 +- tests/envcontext_test.py | 6 ++--- tests/error_handler_test.py | 2 +- tests/languages/all_test.py | 9 +++---- tests/languages/docker_test.py | 2 +- tests/languages/helpers_test.py | 2 +- tests/languages/python_test.py | 2 +- tests/main_test.py | 2 +- tests/output_test.py | 3 ++- tests/repository_test.py | 2 +- tests/store_test.py | 2 +- tests/xargs_test.py | 25 +++---------------- 23 files changed, 31 insertions(+), 59 deletions(-) delete mode 100644 testing/resources/python_venv_hooks_repo/foo/__init__.py diff --git a/pre_commit/five.py b/pre_commit/five.py index 8d9e5767..7059b163 100644 --- a/pre_commit/five.py +++ b/pre_commit/five.py @@ -1,6 +1,3 @@ -import six - - def to_text(s): return s if isinstance(s, str) else s.decode('UTF-8') @@ -9,4 +6,4 @@ def to_bytes(s): return s if isinstance(s, bytes) else s.encode('UTF-8') -n = to_bytes if six.PY2 else to_text +n = to_text diff --git a/pre_commit/util.py b/pre_commit/util.py index 2c4d87ba..8c9751b4 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -80,7 +80,7 @@ class CalledProcessError(RuntimeError): self.stdout = stdout self.stderr = stderr - def to_bytes(self): + def __bytes__(self): def _indent_or_none(part): if part: return b'\n ' + part.replace(b'\n', b'\n ') @@ -97,11 +97,8 @@ class CalledProcessError(RuntimeError): b'stderr:', _indent_or_none(self.stderr), )) - def to_text(self): - return self.to_bytes().decode('UTF-8') - - __bytes__ = to_bytes - __str__ = to_text + def __str__(self): + return self.__bytes__().decode('UTF-8') def _cmd_kwargs(*cmd, **kwargs): diff --git a/requirements-dev.txt b/requirements-dev.txt index ba80df7f..9dfea92d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,6 +1,5 @@ -e . coverage -mock pytest pytest-env diff --git a/setup.cfg b/setup.cfg index daca858a..bf666de6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,7 +27,6 @@ install_requires = identify>=1.0.0 nodeenv>=0.11.1 pyyaml - six toml virtualenv>=15.2 importlib-metadata;python_version<"3.8" diff --git a/testing/resources/python_venv_hooks_repo/foo/__init__.py b/testing/resources/python_venv_hooks_repo/foo/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/color_test.py b/tests/color_test.py index 4c492814..4d98bd8d 100644 --- a/tests/color_test.py +++ b/tests/color_test.py @@ -1,6 +1,6 @@ import sys +from unittest import mock -import mock import pytest from pre_commit import envcontext diff --git a/tests/commands/clean_test.py b/tests/commands/clean_test.py index 22fe974c..955a6bc4 100644 --- a/tests/commands/clean_test.py +++ b/tests/commands/clean_test.py @@ -1,6 +1,6 @@ import os.path +from unittest import mock -import mock import pytest from pre_commit.commands.clean import clean diff --git a/tests/commands/init_templatedir_test.py b/tests/commands/init_templatedir_test.py index 12c6696a..010638d5 100644 --- a/tests/commands/init_templatedir_test.py +++ b/tests/commands/init_templatedir_test.py @@ -1,6 +1,5 @@ import os.path - -import mock +from unittest import mock import pre_commit.constants as C from pre_commit.commands.init_templatedir import init_templatedir diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 73d05300..feef316e 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -1,8 +1,7 @@ import os.path import re import sys - -import mock +from unittest import mock import pre_commit.constants as C from pre_commit.commands.install_uninstall import CURRENT_HASH diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 03962a7c..d271575e 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -2,8 +2,8 @@ import os.path import pipes import sys import time +from unittest import mock -import mock import pytest import pre_commit.constants as C diff --git a/tests/commands/try_repo_test.py b/tests/commands/try_repo_test.py index db2c47ba..fca0f3dd 100644 --- a/tests/commands/try_repo_test.py +++ b/tests/commands/try_repo_test.py @@ -1,8 +1,7 @@ import os.path import re import time - -import mock +from unittest import mock from pre_commit import git from pre_commit.commands.try_repo import try_repo diff --git a/tests/conftest.py b/tests/conftest.py index 0018cfd4..6993301e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,8 +2,8 @@ import functools import io import logging import os.path +from unittest import mock -import mock import pytest from pre_commit import output diff --git a/tests/envcontext_test.py b/tests/envcontext_test.py index 7c4bdddd..81f25e38 100644 --- a/tests/envcontext_test.py +++ b/tests/envcontext_test.py @@ -1,6 +1,6 @@ import os +from unittest import mock -import mock import pytest from pre_commit.envcontext import envcontext @@ -91,11 +91,11 @@ def test_exception_safety(): class MyError(RuntimeError): pass - env = {} + env = {'hello': 'world'} with pytest.raises(MyError): with envcontext([('foo', 'bar')], _env=env): raise MyError() - assert env == {} + assert env == {'hello': 'world'} def test_integration_os_environ(): diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py index 403dcfbd..fa2fc2d3 100644 --- a/tests/error_handler_test.py +++ b/tests/error_handler_test.py @@ -1,8 +1,8 @@ import os.path import re import sys +from unittest import mock -import mock import pytest from pre_commit import error_handler diff --git a/tests/languages/all_test.py b/tests/languages/all_test.py index e226d18f..5e8c8253 100644 --- a/tests/languages/all_test.py +++ b/tests/languages/all_test.py @@ -11,7 +11,6 @@ ArgSpec = functools.partial( inspect.FullArgSpec, varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={}, ) -getargspec = inspect.getfullargspec @pytest.mark.parametrize('language', all_languages) @@ -19,7 +18,7 @@ def test_install_environment_argspec(language): expected_argspec = ArgSpec( args=['prefix', 'version', 'additional_dependencies'], ) - argspec = getargspec(languages[language].install_environment) + argspec = inspect.getfullargpsec(languages[language].install_environment) assert argspec == expected_argspec @@ -31,19 +30,19 @@ def test_ENVIRONMENT_DIR(language): @pytest.mark.parametrize('language', all_languages) def test_run_hook_argpsec(language): expected_argspec = ArgSpec(args=['hook', 'file_args', 'color']) - argspec = getargspec(languages[language].run_hook) + argspec = inspect.getfullargpsec(languages[language].run_hook) assert argspec == expected_argspec @pytest.mark.parametrize('language', all_languages) def test_get_default_version_argspec(language): expected_argspec = ArgSpec(args=[]) - argspec = getargspec(languages[language].get_default_version) + argspec = inspect.getfullargpsec(languages[language].get_default_version) assert argspec == expected_argspec @pytest.mark.parametrize('language', all_languages) def test_healthy_argspec(language): expected_argspec = ArgSpec(args=['prefix', 'language_version']) - argspec = getargspec(languages[language].healthy) + argspec = inspect.getfullargpsec(languages[language].healthy) assert argspec == expected_argspec diff --git a/tests/languages/docker_test.py b/tests/languages/docker_test.py index 89e57000..9d69a13d 100644 --- a/tests/languages/docker_test.py +++ b/tests/languages/docker_test.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from pre_commit.languages import docker from pre_commit.util import CalledProcessError diff --git a/tests/languages/helpers_test.py b/tests/languages/helpers_test.py index 6f1232b4..b289f725 100644 --- a/tests/languages/helpers_test.py +++ b/tests/languages/helpers_test.py @@ -1,8 +1,8 @@ import multiprocessing import os import sys +from unittest import mock -import mock import pytest import pre_commit.constants as C diff --git a/tests/languages/python_test.py b/tests/languages/python_test.py index d806953e..da48e332 100644 --- a/tests/languages/python_test.py +++ b/tests/languages/python_test.py @@ -1,7 +1,7 @@ import os.path import sys +from unittest import mock -import mock import pytest import pre_commit.constants as C diff --git a/tests/main_test.py b/tests/main_test.py index 107a2e67..caccc9a6 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -1,7 +1,7 @@ import argparse import os.path +from unittest import mock -import mock import pytest import pre_commit.constants as C diff --git a/tests/output_test.py b/tests/output_test.py index 4c641c85..8b6d450c 100644 --- a/tests/output_test.py +++ b/tests/output_test.py @@ -1,4 +1,5 @@ -import mock +from unittest import mock + import pytest from pre_commit import color diff --git a/tests/repository_test.py b/tests/repository_test.py index 1f5521b8..43e0362c 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -2,9 +2,9 @@ import os.path import re import shutil import sys +from unittest import mock import cfgv -import mock import pytest import pre_commit.constants as C diff --git a/tests/store_test.py b/tests/store_test.py index 6fc8c058..bb64fead 100644 --- a/tests/store_test.py +++ b/tests/store_test.py @@ -1,7 +1,7 @@ import os.path import sqlite3 +from unittest import mock -import mock import pytest from pre_commit import git diff --git a/tests/xargs_test.py b/tests/xargs_test.py index c0bbe523..b999b1ee 100644 --- a/tests/xargs_test.py +++ b/tests/xargs_test.py @@ -2,10 +2,9 @@ import concurrent.futures import os import sys import time +from unittest import mock -import mock import pytest -import six from pre_commit import parse_shebang from pre_commit import xargs @@ -26,19 +25,10 @@ def test_environ_size(env, expected): @pytest.fixture -def win32_py2_mock(): +def win32_mock(): with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'): with mock.patch.object(sys, 'platform', 'win32'): - with mock.patch.object(six, 'PY2', True): - yield - - -@pytest.fixture -def win32_py3_mock(): - with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'): - with mock.patch.object(sys, 'platform', 'win32'): - with mock.patch.object(six, 'PY2', False): - yield + yield @pytest.fixture @@ -78,7 +68,7 @@ def test_partition_limits(): ) -def test_partition_limit_win32_py3(win32_py3_mock): +def test_partition_limit_win32(win32_mock): cmd = ('ninechars',) # counted as half because of utf-16 encode varargs = ('😑' * 5,) @@ -86,13 +76,6 @@ def test_partition_limit_win32_py3(win32_py3_mock): assert ret == (cmd + varargs,) -def test_partition_limit_win32_py2(win32_py2_mock): - cmd = ('ninechars',) - varargs = ('😑' * 5,) # 4 bytes * 5 - ret = xargs.partition(cmd, varargs, 1, _max_length=31) - assert ret == (cmd + varargs,) - - def test_partition_limit_linux(linux_mock): cmd = ('ninechars',) varargs = ('😑' * 5,)