some manual py2 cleanups

This commit is contained in:
Anthony Sottile
2020-01-08 21:23:18 -08:00
parent 30c1e8289f
commit ab19b94811
23 changed files with 31 additions and 59 deletions

View File

@@ -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

View File

@@ -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):

View File

@@ -1,6 +1,5 @@
-e .
coverage
mock
pytest
pytest-env

View File

@@ -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"

View File

@@ -1,6 +1,6 @@
import sys
from unittest import mock
import mock
import pytest
from pre_commit import envcontext

View File

@@ -1,6 +1,6 @@
import os.path
from unittest import mock
import mock
import pytest
from pre_commit.commands.clean import clean

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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():

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +1,4 @@
import mock
from unittest import mock
from pre_commit.languages import docker
from pre_commit.util import CalledProcessError

View File

@@ -1,8 +1,8 @@
import multiprocessing
import os
import sys
from unittest import mock
import mock
import pytest
import pre_commit.constants as C

View File

@@ -1,7 +1,7 @@
import os.path
import sys
from unittest import mock
import mock
import pytest
import pre_commit.constants as C

View File

@@ -1,7 +1,7 @@
import argparse
import os.path
from unittest import mock
import mock
import pytest
import pre_commit.constants as C

View File

@@ -1,4 +1,5 @@
import mock
from unittest import mock
import pytest
from pre_commit import color

View File

@@ -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

View File

@@ -1,7 +1,7 @@
import os.path
import sqlite3
from unittest import mock
import mock
import pytest
from pre_commit import git

View File

@@ -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,)