Skip to content

Commit 0702cde

Browse files
committed
refactor: Remove most of _compat and python 2 support
1 parent f8643ec commit 0702cde

File tree

6 files changed

+19
-83
lines changed

6 files changed

+19
-83
lines changed

libvcs/_compat.py

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,17 @@
11
# flake8: NOQA
22
import sys
33

4-
PY2 = sys.version_info[0] == 2
5-
64
_identity = lambda x: x
5+
implements_to_string = _identity
76

7+
console_encoding = sys.__stdout__.encoding
88

9-
if PY2:
10-
unichr = unichr
11-
text_type = unicode
12-
string_types = (str, unicode)
13-
from urllib import urlretrieve
14-
15-
iterkeys = lambda d: d.iterkeys()
16-
itervalues = lambda d: d.itervalues()
17-
iteritems = lambda d: d.iteritems()
18-
19-
import ConfigParser as configparser
20-
import cPickle as pickle
21-
from cStringIO import StringIO as BytesIO
22-
from StringIO import StringIO
23-
24-
cmp = cmp
259

26-
input = raw_input
27-
from string import lower as ascii_lowercase
28-
29-
import urlparse
30-
31-
def console_to_str(s):
10+
def console_to_str(s):
11+
""" From pypa/pip project, pip.backwardwardcompat. License MIT. """
12+
try:
13+
return s.decode(console_encoding)
14+
except UnicodeDecodeError:
3215
return s.decode('utf_8')
33-
34-
def implements_to_string(cls):
35-
cls.__unicode__ = cls.__str__
36-
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
37-
return cls
38-
39-
40-
else:
41-
unichr = chr
42-
text_type = str
43-
string_types = (str,)
44-
45-
iterkeys = lambda d: iter(d.keys())
46-
itervalues = lambda d: iter(d.values())
47-
iteritems = lambda d: iter(d.items())
48-
49-
import configparser
50-
import pickle
51-
from io import BytesIO, StringIO
52-
53-
cmp = lambda a, b: (a > b) - (a < b)
54-
55-
input = input
56-
import urllib.parse as urllib
57-
import urllib.parse as urlparse
58-
from string import ascii_lowercase
59-
from urllib.request import urlretrieve
60-
61-
console_encoding = sys.__stdout__.encoding
62-
63-
implements_to_string = _identity
64-
65-
def console_to_str(s):
66-
""" From pypa/pip project, pip.backwardwardcompat. License MIT. """
67-
try:
68-
return s.decode(console_encoding)
69-
except UnicodeDecodeError:
70-
return s.decode('utf_8')
71-
except AttributeError: # for tests, #13
72-
return s
16+
except AttributeError: # for tests, #13
17+
return s

libvcs/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import logging
66
import os
77
from typing import NamedTuple
8+
from urllib import parse as urlparse
89

9-
from ._compat import implements_to_string, urlparse
1010
from .util import RepoLoggingAdapter, mkdir_p, run
1111

1212
logger = logging.getLogger(__name__)
@@ -34,7 +34,6 @@ def convert_pip_url(pip_url: str) -> VCSLocation:
3434
return VCSLocation(url=url, rev=rev)
3535

3636

37-
@implements_to_string
3837
class BaseRepo(RepoLoggingAdapter, object):
3938

4039
"""Base class for repositories.

libvcs/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import os
2020
import re
2121
from typing import NamedTuple, Optional
22+
from urllib import parse as urlparse
2223

2324
from . import exc
24-
from ._compat import urlparse
2525
from .base import BaseRepo, VCSLocation, convert_pip_url as base_convert_pip_url
2626

2727
logger = logging.getLogger(__name__)

libvcs/svn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import logging
1919
import os
2020
import re
21+
from urllib import parse as urlparse
2122

22-
from ._compat import urlparse
2323
from .base import BaseRepo, VCSLocation, convert_pip_url as base_convert_pip_url
2424

2525
logger = logging.getLogger(__name__)

tests/test_base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
"""tests for libvcs repo abstract base class."""
33
from __future__ import absolute_import, print_function, unicode_literals
44

5-
from libvcs._compat import text_type
65
from libvcs.base import BaseRepo, convert_pip_url
76
from libvcs.shortcuts import create_repo
87

98

109
def test_repr():
1110
repo = create_repo(url='file://path/to/myrepo', repo_dir='/hello/', vcs='git')
1211

13-
str_repo = text_type(repo)
12+
str_repo = str(repo)
1413
assert 'GitRepo' in str_repo
1514
assert 'hello' in str_repo
1615
assert '<GitRepo hello>' == str_repo
@@ -19,7 +18,7 @@ def test_repr():
1918
def test_repr_base():
2019
repo = BaseRepo(url='file://path/to/myrepo', repo_dir='/hello/')
2120

22-
str_repo = text_type(repo)
21+
str_repo = str(repo)
2322
assert 'Repo' in str_repo
2423
assert 'hello' in str_repo
2524
assert '<BaseRepo hello>' == str_repo

tests/test_git.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pytest
1010

1111
from libvcs import exc
12-
from libvcs._compat import PY2, string_types
1312
from libvcs.git import GitRemote, convert_pip_url as git_convert_pip_url, extract_status
1413
from libvcs.shortcuts import create_repo_from_pip_url
1514
from libvcs.util import run, which
@@ -81,7 +80,7 @@ def test_repo_update_handle_cases(tmpdir, git_remote, mocker):
8180

8281
def test_progress_callback(tmpdir, git_remote, mocker):
8382
def progress_callback_spy(output, timestamp):
84-
assert isinstance(output, string_types)
83+
assert isinstance(output, str)
8584
assert isinstance(timestamp, datetime.datetime)
8685

8786
progress_callback = mocker.Mock(
@@ -312,16 +311,10 @@ def test_extract_status():
312311
],
313312
)
314313
def test_extract_status_b(fixture, expected_result):
315-
if PY2:
316-
assert (
317-
extract_status(textwrap.dedent(fixture))._asdict().items()
318-
<= expected_result.items()
319-
)
320-
else:
321-
assert (
322-
extract_status(textwrap.dedent(fixture))._asdict().items()
323-
>= expected_result.items()
324-
)
314+
assert (
315+
extract_status(textwrap.dedent(fixture))._asdict().items()
316+
>= expected_result.items()
317+
)
325318

326319

327320
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)