From 326ba21ed0a6a35b23f3f664832c39d86555fb2f Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Tue, 31 Dec 2019 19:43:15 -0600 Subject: [PATCH 1/3] py3: remove unneeded string casts --- dvc/cache.py | 2 +- dvc/path_info.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dvc/cache.py b/dvc/cache.py index cfdc6113c..e7e1d6fdc 100644 --- a/dvc/cache.py +++ b/dvc/cache.py @@ -53,7 +53,7 @@ def _make_remote_property(name): return Remote(self.repo, name=remote) - getter.__name__ = str(name) + getter.__name__ = name return cached_property(getter) diff --git a/dvc/path_info.py b/dvc/path_info.py index 0e2245cf2..dc64bc7a1 100644 --- a/dvc/path_info.py +++ b/dvc/path_info.py @@ -51,7 +51,7 @@ class PathInfo(pathlib.PurePath, _BasePath): return relpath(path) def __repr__(self): - return str("{}: '{}'").format(type(self).__name__, self) + return "{}: '{}'".format(type(self).__name__, self) # This permits passing it to file utils directly in Python 3.6+ # With Python 2.7, Python 3.5+ we are stuck with path_info.fspath for now From b844aea42cbb11e1c0508982ad3aa62952da2039 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Tue, 31 Dec 2019 20:04:39 -0600 Subject: [PATCH 2/3] py3: remove cast_bytes method --- dvc/daemon.py | 7 ++----- dvc/logger.py | 1 - dvc/scm/git/__init__.py | 3 +-- dvc/utils/__init__.py | 4 +--- dvc/utils/compat.py | 26 -------------------------- dvc/version.py | 2 +- tests/conftest.py | 5 ++--- tests/unit/test_version.py | 5 ++--- 8 files changed, 9 insertions(+), 44 deletions(-) diff --git a/dvc/daemon.py b/dvc/daemon.py index 98b8ec10f..ce5befce0 100644 --- a/dvc/daemon.py +++ b/dvc/daemon.py @@ -9,7 +9,6 @@ from subprocess import Popen from dvc.env import DVC_DAEMON from dvc.utils import fix_env from dvc.utils import is_binary -from dvc.utils.compat import cast_bytes_py2 logger = logging.getLogger(__name__) @@ -98,9 +97,7 @@ def daemon(args): env = fix_env() file_path = os.path.abspath(inspect.stack()[0][1]) - env[cast_bytes_py2("PYTHONPATH")] = cast_bytes_py2( - os.path.dirname(os.path.dirname(file_path)) - ) - env[cast_bytes_py2(DVC_DAEMON)] = cast_bytes_py2("1") + env["PYTHONPATH"] = os.path.dirname(os.path.dirname(file_path)) + env[DVC_DAEMON] = "1" _spawn(cmd, env) diff --git a/dvc/logger.py b/dvc/logger.py index 3f93f5640..024667053 100644 --- a/dvc/logger.py +++ b/dvc/logger.py @@ -7,7 +7,6 @@ import logging.handlers import colorama from dvc.progress import Tqdm -from dvc.utils.compat import RecursionError FOOTER = ( diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index 2ccb418bf..b214cb3ef 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -17,7 +17,6 @@ from dvc.utils import fix_env from dvc.utils import is_binary from dvc.utils import relpath from dvc.utils.fs import path_isin -from dvc.utils.compat import cast_bytes_py2 from dvc.utils.compat import open @@ -76,7 +75,7 @@ class Git(Base): # LD_LIBRARY_PATH that has been set by PyInstaller. # See [1] for more info. # [1] https://github.com/gitpython-developers/GitPython/issues/924 - env[cast_bytes_py2(ld_key)] = "" + env[ld_key] = "" try: tmp_repo = git.Repo.clone_from( diff --git a/dvc/utils/__init__.py b/dvc/utils/__init__.py index 7ef5b28ef..19c3988db 100644 --- a/dvc/utils/__init__.py +++ b/dvc/utils/__init__.py @@ -15,7 +15,6 @@ import nanotime from ruamel.yaml import YAML from shortuuid import uuid -from dvc.utils.compat import cast_bytes_py2 from dvc.utils.compat import fspath from dvc.utils.compat import fspath_py35 from dvc.utils.compat import makedirs as _makedirs @@ -205,8 +204,7 @@ def fix_env(env=None): lp_key = "LD_LIBRARY_PATH" lp_orig = env.get(lp_key + "_ORIG", None) if lp_orig is not None: - # NOTE: py2 doesn't like unicode strings in environ - env[cast_bytes_py2(lp_key)] = cast_bytes_py2(lp_orig) + env[lp_key] = lp_orig else: env.pop(lp_key, None) diff --git a/dvc/utils/compat.py b/dvc/utils/compat.py index 70e9c360e..23fd141e9 100644 --- a/dvc/utils/compat.py +++ b/dvc/utils/compat.py @@ -14,23 +14,6 @@ is_py2 = _ver[0] == 2 #: Python 3.x? is_py3 = _ver[0] == 3 -# simplified version of ipython_genutils/encoding.py -DEFAULT_ENCODING = sys.getdefaultencoding() - -if _ver[:2] < (3, 5): - RecursionError = RuntimeError -else: - RecursionError = RecursionError - - -def no_code(x, encoding=None): - return x - - -def encode(u, encoding=None): - encoding = encoding or DEFAULT_ENCODING - return u.encode(encoding, "replace") - def csv_reader(unicode_csv_data, dialect=None, **kwargs): """csv.reader doesn't support Unicode input, so need to use some tricks @@ -63,13 +46,6 @@ def utf_8_encoder(unicode_csv_data): yield line.encode("utf-8") -def cast_bytes(s, encoding=None): - """Source: https://github.com/ipython/ipython_genutils""" - if not isinstance(s, bytes): - return encode(s, encoding) - return s - - def _makedirs(name, mode=0o777, exist_ok=False): """Source: https://github.com/python/cpython/blob/ 3ce3dea60646d8a5a1c952469a2eb65f937875b3/Lib/os.py#L196-L226 @@ -118,7 +94,6 @@ if is_py2: numeric_types = (int, long, float) # noqa: F821 integer_types = (int, long) # noqa: F821 input = raw_input # noqa: F821 - cast_bytes_py2 = cast_bytes makedirs = _makedirs range = xrange # noqa: F821 FileNotFoundError = IOError @@ -174,7 +149,6 @@ elif is_py3: integer_types = (int,) # noqa: F821 input = input # noqa: F821 open = open # noqa: F821 - cast_bytes_py2 = no_code range = range # noqa: F821 FileNotFoundError = FileNotFoundError diff --git a/dvc/version.py b/dvc/version.py index a64f30165..be40680b9 100644 --- a/dvc/version.py +++ b/dvc/version.py @@ -49,7 +49,7 @@ def _is_release(dir_path, base_version): ["git", "describe", "--tags", "--exact-match"], cwd=dir_path, stderr=subprocess.STDOUT, - ).decode("utf-8") + ) tag = output.strip() return tag == base_version except subprocess.CalledProcessError: diff --git a/tests/conftest.py b/tests/conftest.py index 277477a90..7ae4ed571 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,15 +7,14 @@ from git.exc import GitCommandNotFound from dvc.remote.ssh.connection import SSHConnection from dvc.repo import Repo as DvcRepo -from dvc.utils.compat import cast_bytes_py2 from .basic_env import TestDirFixture, TestDvcGitFixture, TestGitFixture from .dir_helpers import * # noqa # Prevent updater and analytics from running their processes -os.environ[cast_bytes_py2("DVC_TEST")] = cast_bytes_py2("true") +os.environ["DVC_TEST"] = "true" # Ensure progress output even when not outputting to raw sys.stderr console -os.environ[cast_bytes_py2("DVC_IGNORE_ISATTY")] = cast_bytes_py2("true") +os.environ["DVC_IGNORE_ISATTY"] = "true" @pytest.fixture(autouse=True) diff --git a/tests/unit/test_version.py b/tests/unit/test_version.py index 5b33b4380..1d21c7205 100644 --- a/tests/unit/test_version.py +++ b/tests/unit/test_version.py @@ -3,7 +3,6 @@ import subprocess import mock import dvc.version -from dvc.utils.compat import cast_bytes def test_is_release(): @@ -13,10 +12,10 @@ def test_is_release(): assert ret is False m.side_effect = None - m.return_value = cast_bytes(dvc.version._BASE_VERSION) + m.return_value = dvc.version._BASE_VERSION ret = dvc.version._is_release(None, dvc.version._BASE_VERSION) assert ret - m.return_value = cast_bytes("630d1741c2d5dd89a3176bd15b63121b905d35c9") + m.return_value = "630d1741c2d5dd89a3176bd15b63121b905d35c9" ret = dvc.version._is_release(None, dvc.version._BASE_VERSION) assert ret is False From 1ef466b849e5a96311bf2561ad10d4af5843d7ff Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Tue, 31 Dec 2019 22:14:31 -0600 Subject: [PATCH 3/3] py3: remove convert_to_unicode noop --- dvc/rwlock.py | 9 ++------- dvc/stage.py | 4 +--- dvc/utils/compat.py | 12 ------------ 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/dvc/rwlock.py b/dvc/rwlock.py index b05d255db..9da97fe4b 100644 --- a/dvc/rwlock.py +++ b/dvc/rwlock.py @@ -9,12 +9,7 @@ from funcy.py3 import lmap, lfilter, lmapcat from .exceptions import DvcException from .lock import LockError -from .utils.compat import ( - convert_to_unicode, - FileNotFoundError, - JSONDecodeError, - str, -) +from .utils.compat import FileNotFoundError, JSONDecodeError, str from .utils.fs import relpath INFO_SCHEMA = {"pid": int, "cmd": str} @@ -44,7 +39,7 @@ def _edit_rwlock(lock_dir): try: with open(path, "r") as fobj: lock = json.load(fobj) - lock = SCHEMA(convert_to_unicode(lock)) + lock = SCHEMA(lock) except FileNotFoundError: lock = SCHEMA({}) except JSONDecodeError as exc: diff --git a/dvc/stage.py b/dvc/stage.py index 79127e261..a795a6852 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -421,10 +421,8 @@ class Stage(object): @staticmethod def validate(d, fname=None): - from dvc.utils.compat import convert_to_unicode - try: - Stage.COMPILED_SCHEMA(convert_to_unicode(d)) + Stage.COMPILED_SCHEMA(d) except MultipleInvalid as exc: raise StageFileFormatError(fname, exc) diff --git a/dvc/utils/compat.py b/dvc/utils/compat.py index 23fd141e9..00039b7fe 100644 --- a/dvc/utils/compat.py +++ b/dvc/utils/compat.py @@ -116,15 +116,6 @@ if is_py2: def __exit__(self, *args): self.close() - def convert_to_unicode(data): - if isinstance(data, builtin_str): - return str(data) - if isinstance(data, dict): - return dict(map(convert_to_unicode, data.items())) - if isinstance(data, (list, tuple)): - return type(data)(map(convert_to_unicode, data)) - return data - elif is_py3: import pathlib # noqa: F401 @@ -152,9 +143,6 @@ elif is_py3: range = range # noqa: F821 FileNotFoundError = FileNotFoundError - def convert_to_unicode(data): - return data - # Backport os.fspath() from Python 3.6 try: