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): def to_text(s):
return s if isinstance(s, str) else s.decode('UTF-8') 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') 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.stdout = stdout
self.stderr = stderr self.stderr = stderr
def to_bytes(self): def __bytes__(self):
def _indent_or_none(part): def _indent_or_none(part):
if part: if part:
return b'\n ' + part.replace(b'\n', b'\n ') return b'\n ' + part.replace(b'\n', b'\n ')
@ -97,11 +97,8 @@ class CalledProcessError(RuntimeError):
b'stderr:', _indent_or_none(self.stderr), b'stderr:', _indent_or_none(self.stderr),
)) ))
def to_text(self): def __str__(self):
return self.to_bytes().decode('UTF-8') return self.__bytes__().decode('UTF-8')
__bytes__ = to_bytes
__str__ = to_text
def _cmd_kwargs(*cmd, **kwargs): def _cmd_kwargs(*cmd, **kwargs):

View file

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

View file

@ -27,7 +27,6 @@ install_requires =
identify>=1.0.0 identify>=1.0.0
nodeenv>=0.11.1 nodeenv>=0.11.1
pyyaml pyyaml
six
toml toml
virtualenv>=15.2 virtualenv>=15.2
importlib-metadata;python_version<"3.8" importlib-metadata;python_version<"3.8"

View file

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

View file

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

View file

@ -1,6 +1,5 @@
import os.path import os.path
from unittest import mock
import mock
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit.commands.init_templatedir import init_templatedir from pre_commit.commands.init_templatedir import init_templatedir

View file

@ -1,8 +1,7 @@
import os.path import os.path
import re import re
import sys import sys
from unittest import mock
import mock
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit.commands.install_uninstall import CURRENT_HASH from pre_commit.commands.install_uninstall import CURRENT_HASH

View file

@ -2,8 +2,8 @@ import os.path
import pipes import pipes
import sys import sys
import time import time
from unittest import mock
import mock
import pytest import pytest
import pre_commit.constants as C import pre_commit.constants as C

View file

@ -1,8 +1,7 @@
import os.path import os.path
import re import re
import time import time
from unittest import mock
import mock
from pre_commit import git from pre_commit import git
from pre_commit.commands.try_repo import try_repo from pre_commit.commands.try_repo import try_repo

View file

@ -2,8 +2,8 @@ import functools
import io import io
import logging import logging
import os.path import os.path
from unittest import mock
import mock
import pytest import pytest
from pre_commit import output from pre_commit import output

View file

@ -1,6 +1,6 @@
import os import os
from unittest import mock
import mock
import pytest import pytest
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
@ -91,11 +91,11 @@ def test_exception_safety():
class MyError(RuntimeError): class MyError(RuntimeError):
pass pass
env = {} env = {'hello': 'world'}
with pytest.raises(MyError): with pytest.raises(MyError):
with envcontext([('foo', 'bar')], _env=env): with envcontext([('foo', 'bar')], _env=env):
raise MyError() raise MyError()
assert env == {} assert env == {'hello': 'world'}
def test_integration_os_environ(): def test_integration_os_environ():

View file

@ -1,8 +1,8 @@
import os.path import os.path
import re import re
import sys import sys
from unittest import mock
import mock
import pytest import pytest
from pre_commit import error_handler from pre_commit import error_handler

View file

@ -11,7 +11,6 @@ ArgSpec = functools.partial(
inspect.FullArgSpec, varargs=None, varkw=None, defaults=None, inspect.FullArgSpec, varargs=None, varkw=None, defaults=None,
kwonlyargs=[], kwonlydefaults=None, annotations={}, kwonlyargs=[], kwonlydefaults=None, annotations={},
) )
getargspec = inspect.getfullargspec
@pytest.mark.parametrize('language', all_languages) @pytest.mark.parametrize('language', all_languages)
@ -19,7 +18,7 @@ def test_install_environment_argspec(language):
expected_argspec = ArgSpec( expected_argspec = ArgSpec(
args=['prefix', 'version', 'additional_dependencies'], args=['prefix', 'version', 'additional_dependencies'],
) )
argspec = getargspec(languages[language].install_environment) argspec = inspect.getfullargpsec(languages[language].install_environment)
assert argspec == expected_argspec assert argspec == expected_argspec
@ -31,19 +30,19 @@ def test_ENVIRONMENT_DIR(language):
@pytest.mark.parametrize('language', all_languages) @pytest.mark.parametrize('language', all_languages)
def test_run_hook_argpsec(language): def test_run_hook_argpsec(language):
expected_argspec = ArgSpec(args=['hook', 'file_args', 'color']) 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 assert argspec == expected_argspec
@pytest.mark.parametrize('language', all_languages) @pytest.mark.parametrize('language', all_languages)
def test_get_default_version_argspec(language): def test_get_default_version_argspec(language):
expected_argspec = ArgSpec(args=[]) expected_argspec = ArgSpec(args=[])
argspec = getargspec(languages[language].get_default_version) argspec = inspect.getfullargpsec(languages[language].get_default_version)
assert argspec == expected_argspec assert argspec == expected_argspec
@pytest.mark.parametrize('language', all_languages) @pytest.mark.parametrize('language', all_languages)
def test_healthy_argspec(language): def test_healthy_argspec(language):
expected_argspec = ArgSpec(args=['prefix', 'language_version']) expected_argspec = ArgSpec(args=['prefix', 'language_version'])
argspec = getargspec(languages[language].healthy) argspec = inspect.getfullargpsec(languages[language].healthy)
assert argspec == expected_argspec 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.languages import docker
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError

View file

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

View file

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

View file

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

View file

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

View file

@ -2,9 +2,9 @@ import os.path
import re import re
import shutil import shutil
import sys import sys
from unittest import mock
import cfgv import cfgv
import mock
import pytest import pytest
import pre_commit.constants as C import pre_commit.constants as C

View file

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

View file

@ -2,10 +2,9 @@ import concurrent.futures
import os import os
import sys import sys
import time import time
from unittest import mock
import mock
import pytest import pytest
import six
from pre_commit import parse_shebang from pre_commit import parse_shebang
from pre_commit import xargs from pre_commit import xargs
@ -26,18 +25,9 @@ def test_environ_size(env, expected):
@pytest.fixture @pytest.fixture
def win32_py2_mock(): def win32_mock():
with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'): with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'):
with mock.patch.object(sys, 'platform', 'win32'): 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
@ -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',) cmd = ('ninechars',)
# counted as half because of utf-16 encode # counted as half because of utf-16 encode
varargs = ('😑' * 5,) varargs = ('😑' * 5,)
@ -86,13 +76,6 @@ def test_partition_limit_win32_py3(win32_py3_mock):
assert ret == (cmd + varargs,) 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): def test_partition_limit_linux(linux_mock):
cmd = ('ninechars',) cmd = ('ninechars',)
varargs = ('😑' * 5,) varargs = ('😑' * 5,)