Repository: minio/minio-py
Pull request #906 (2020-05-04 16:25:06)

fix pylint errors in compat.py


Patch:
diff --git a/Makefile b/Makefile
index fbf442e..2d966f9 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 
 check:
 	@which pylint >/dev/null || pip install --user --upgrade pylint
-	@if python --version | grep -qi 'python 3'; then pylint --reports=no minio/copy_conditions.py minio/definitions.py minio/helpers.py minio/parsers.py minio/post_policy.py minio/signer.py minio/thread_pool.py minio/__init__.py minio/select; fi
+	@if python --version | grep -qi 'python 3'; then pylint --reports=no minio/compat.py minio/copy_conditions.py minio/definitions.py minio/helpers.py minio/parsers.py minio/post_policy.py minio/signer.py minio/thread_pool.py minio/__init__.py minio/select; fi
 	@if python --version | grep -qi 'python 3'; then pylint --reports=no minio/api.py; fi
 
 	@which isort >/dev/null || pip install --user --upgrade isort
diff --git a/minio/api.py b/minio/api.py
index 7ac4817..4e5c2d7 100644
--- a/minio/api.py
+++ b/minio/api.py
@@ -46,7 +46,7 @@ import urllib3
 # Internal imports
 from . import __title__, __version__
 from .compat import range  # pylint: disable=redefined-builtin
-from .compat import basestring, queryencode, urlsplit
+from .compat import basestring, quote, urlsplit
 from .credentials import (Chain, Credentials, EnvAWS, EnvMinio, IamEc2MetaData,
                           Static)
 from .definitions import Object, UploadPart
@@ -868,8 +868,7 @@ class Minio:  # pylint: disable=too-many-public-methods
             is_valid_sse_object(sse)
             headers.update(sse.marshal())
         # URI encoded, except '/' as per AWS S3 requirement
-        headers['X-Amz-Copy-Source'] = queryencode(
-            object_source).replace('%2F', '/')
+        headers['X-Amz-Copy-Source'] = quote(object_source)
 
         response = self._url_open('PUT',
                                   bucket_name=bucket_name,
diff --git a/minio/compat.py b/minio/compat.py
index 701e7d5..a601ee8 100644
--- a/minio/compat.py
+++ b/minio/compat.py
@@ -28,71 +28,71 @@ This module implements python 2.x and 3.x compatibility layer.
 import sys
 
 #: Python 2.x?
-_is_py2 = (sys.version_info[0] == 2)
-
-#: Python 3.x?
-_is_py3 = (sys.version_info[0] == 3)
-
-if _is_py2:
-    from Queue import Queue
-    queue = Queue
-
-    from Queue import Empty
-    queue_empty = Empty
-
-    from urllib import quote, unquote, urlencode
-
+PYTHON2 = (sys.version_info[0] == 2)
+
+if PYTHON2:
+    # pylint: disable=no-name-in-module
+    from urllib import quote as _quote
+    # pylint: disable=no-name-in-module
+    from urllib import unquote, urlencode
+    # pylint: disable=import-error
     from urlparse import urlsplit, parse_qs
 
     # Create missing types.
-    bytes = str
+    bytes = str  # pylint: disable=redefined-builtin, invalid-name
+
+    builtin_range = range  # pylint: disable=invalid-name
+    builtin_str = str  # pylint: disable=invalid-name
 
     # Update better types.
-    builtin_range = range
+    # pylint: disable=redefined-builtin, undefined-variable, invalid-name
     range = xrange
-    builtin_str = str
+    # pylint: disable=redefined-builtin, undefined-variable, invalid-name
     str = unicode
 
-    # Add missing imports
+    # Make importable.
+    # pylint: disable=self-assigning-variable, undefined-variable, invalid-name
     basestring = basestring
-elif _is_py3:
-    from queue import Queue
-    queue = Queue
+else:
+    from urllib.parse import quote as _quote  # pylint: disable=ungrouped-imports
+    # pylint: disable=ungrouped-imports
+    from urllib.parse import unquote, urlsplit, parse_qs, urlencode
 
-    from queue import Empty
-    queue_empty = Empty
+    # Create types to compat with python v2.
+    builtin_range = range  # pylint: disable=invalid-name
+    builtin_str = str  # pylint: disable=invalid-name
 
-    from urllib.parse import quote, unquote, urlsplit, parse_qs, urlencode
-    unquote = unquote  # to get rid of F401
-    urlencode = urlencode  # to get rid of F401
-    urlsplit = urlsplit  # to get rid of F401
-    parse_qs = parse_qs  # to get rid of F401
+    # Create missing types.
+    basestring = (str, bytes)  # pylint: disable=invalid-name
+    long = int  # pylint: disable=invalid-name
 
-    # Create types to compat with py2.
-    builtin_range = range
-    builtin_str = str
+    # Make importable.
+    bytes = bytes  # pylint: disable=self-assigning-variable, invalid-name
+    range = range  # pylint: disable=self-assigning-variable, invalid-name
+    str = str  # pylint: disable=self-assigning-variable, invalid-name
 
-    # Create missing types.
-    basestring = (str, bytes)
-    long = int
 
-    # Add missing imports
-    bytes = bytes
-    range = range
-    str = str
+# Make importable.
+# to get rid of F401. pylint: disable=self-assigning-variable, invalid-name
+unquote = unquote
+# to get rid of F401. pylint: disable=self-assigning-variable, invalid-name
+urlencode = urlencode
+# to get rid of F401. pylint: disable=self-assigning-variable, invalid-name
+urlsplit = urlsplit
+# to get rid of F401. pylint: disable=self-assigning-variable, invalid-name
+parse_qs = parse_qs
 
 
 # Note earlier versions of minio.compat exposed urllib.quote as urlencode
-def _quote(resource):
+def quote(resource):
     """
     This implementation of urllib.quote supports all unicode characters
 
     :param: resource: Resource value to be url encoded.
     """
-    if isinstance(resource, str):
-        return quote(resource.encode('utf-8'))
-
-    return quote(resource)
+    return _quote(
+        resource.encode('utf-8') if isinstance(resource, str) else resource,
+    )
 
 
 def queryencode(query):
@@ -101,4 +101,4 @@ def queryencode(query):
 
     :param: query: Query value to be url encoded.
     """
-    return _quote(query).replace('/', '%2F')
+    return quote(query).replace('/', '%2F')
diff --git a/minio/helpers.py b/minio/helpers.py
index 03cd217..7870d4e 100644
--- a/minio/helpers.py
+++ b/minio/helpers.py
@@ -44,8 +44,8 @@ from datetime import datetime
 import pytz
 
 # pylint: disable=redefined-builtin
-from .compat import (_is_py2, _is_py3, _quote, basestring, bytes, queryencode,
-                     str, urlsplit)
+from .compat import (PYTHON2, basestring, bytes, queryencode, quote, str,
+                     urlsplit)
 from .error import (InvalidArgumentError, InvalidBucketError,
                     InvalidEndpointError)
 
@@ -428,11 +428,7 @@ def is_valid_policy_type(policy):
     :return: True if policy parameter is of a valid type, 'string'.
     Raise :exc:`TypeError` otherwise.
     """
-    if _is_py3:
-        string_type = str
-    elif _is_py2:
-        string_type = basestring
-
+    string_type = basestring if PYTHON2 else str
     if not isinstance(policy, string_type):
         raise TypeError('policy can only be of type str')
 
@@ -605,7 +601,7 @@ def encode_object_name(object_name):
     :return: URL encoded input object name.
     """
     is_non_empty_string(object_name)
-    return _quote(object_name)
+    return quote(object_name)
 
 
 class Hasher:
diff --git a/minio/thread_pool.py b/minio/thread_pool.py
index 23aa8c0..62576c0 100644
--- a/minio/thread_pool.py
+++ b/minio/thread_pool.py
@@ -28,7 +28,12 @@ in parallel. Tasks results can also be retrieved.
 
 from threading import Thread
 
-from .compat import queue
+from .compat import PYTHON2
+
+if PYTHON2:
+    from Queue import Queue  # pylint: disable=import-error
+else:
+    from queue import Queue
 
 
 class Worker(Thread):
@@ -67,9 +72,9 @@ class ThreadPool:
     """ Pool of threads consuming tasks from a queue """
 
     def __init__(self, num_threads):
-        self.results_queue = queue()
-        self.exceptions_queue = queue()
-        self.tasks_queue = queue(num_threads)
+        self.results_queue = Queue()
+        self.exceptions_queue = Queue()
+        self.tasks_queue = Queue(num_threads)
         self.num_threads = num_threads
 
     def add_task(self, func, *args, **kargs):


Test patch:
diff --git a/tests/unit/minio_mocks.py b/tests/unit/minio_mocks.py
index 66a05db..4f00888 100644
--- a/tests/unit/minio_mocks.py
+++ b/tests/unit/minio_mocks.py
@@ -16,13 +16,13 @@
 
 from nose.tools import eq_
 
-from minio.compat import _is_py3
+from minio.compat import PYTHON2
 from minio.fold_case_dict import FoldCaseDict
 
-if _is_py3:
-    import http.client as httplib
-else:
+if PYTHON2:
     import httplib
+else:
+    import http.client as httplib
 
 
 class MockResponse(object):
diff --git a/tests/unit/sign_test.py b/tests/unit/sign_test.py
index 6b89db7..9fdbdab 100644
--- a/tests/unit/sign_test.py
+++ b/tests/unit/sign_test.py
@@ -21,7 +21,7 @@ from unittest import TestCase
 import pytz as pytz
 from nose.tools import eq_, raises
 
-from minio.compat import _quote, queryencode, urlsplit
+from minio.compat import queryencode, quote, urlsplit
 from minio.credentials import Credentials, Static
 from minio.error import InvalidArgumentError
 from minio.fold_case_dict import FoldCaseDict
@@ -166,19 +166,19 @@ class SignV4Test(TestCase):
 
 class UnicodeEncodeTest(TestCase):
     def test_unicode_quote(self):
-        eq_(_quote('/test/123/汉字'), '/test/123/%E6%B1%89%E5%AD%97')
+        eq_(quote('/test/123/汉字'), '/test/123/%E6%B1%89%E5%AD%97')
 
     def test_unicode_queryencode(self):
         eq_(queryencode('/test/123/汉字'), '%2Ftest%2F123%2F%E6%B1%89%E5%AD%97')
 
     def test_unicode_quote_u(self):
-        eq_(_quote(u'/test/123/汉字'), '/test/123/%E6%B1%89%E5%AD%97')
+        eq_(quote(u'/test/123/汉字'), '/test/123/%E6%B1%89%E5%AD%97')
 
     def test_unicode_queryencode_u(self):
         eq_(queryencode(u'/test/123/汉字'), '%2Ftest%2F123%2F%E6%B1%89%E5%AD%97')
 
     def test_unicode_quote_b(self):
-        eq_(_quote(b'/test/123/\xe6\xb1\x89\xe5\xad\x97'),
+        eq_(quote(b'/test/123/\xe6\xb1\x89\xe5\xad\x97'),
             '/test/123/%E6%B1%89%E5%AD%97')
 
     def test_unicode_queryencode_b(self):