From ab866363d3c2729ba8c60c04e7921383e072fba2 Mon Sep 17 00:00:00 2001 From: Sanket Saurav Date: Mon, 29 Apr 2019 11:34:38 -0700 Subject: [PATCH] Fix issues to improve adherence to best practices Also added `.deepsource.toml` for continuous quality analysis --- .deepsource.toml | 20 ++++++++ requests3/_structures.py | 4 +- requests3/core/_http/_async/response.py | 8 +-- .../core/_http/_backends/sync_backend.py | 1 - .../core/_http/_backends/twisted_backend.py | 10 ++-- requests3/core/_http/_sync/response.py | 8 +-- requests3/core/_http/exceptions.py | 50 +++++++++---------- .../ssl_match_hostname/_implementation.py | 6 +-- requests3/core/_http/util/selectors.py | 2 +- requests3/http_auth.py | 10 ++-- requests3/http_models.py | 1 - requests3/http_utils.py | 2 +- 12 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 00000000..28015212 --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,20 @@ +# generated by deepsource.io +version = 1 + +test_patterns = [ + 'tests/*.py' +] + +exclude_patterns = [ + '*/**/*_compat.py', + 'requests3/core/_http/packages/six.py', + 'setup.py' +] + +[[analyzers]] +name = "python" +enabled = true +runtime_version = "3.x.x" + + [analyzers.meta] + max_line_length = 100 diff --git a/requests3/_structures.py b/requests3/_structures.py index ffdaba5c..9c6ce141 100644 --- a/requests3/_structures.py +++ b/requests3/_structures.py @@ -8,7 +8,7 @@ Data structures that power Requests. import collections -from ._basics import basestring, OrderedDict +from ._basics import basestring class CaseInsensitiveDict(collections.MutableMapping): @@ -38,7 +38,7 @@ class CaseInsensitiveDict(collections.MutableMapping): behavior is undefined. """ - __slots__ = "_store" + __slots__ = ["_store"] def __init__(self, data=None, **kwargs): self._store = collections.OrderedDict() diff --git a/requests3/core/_http/_async/response.py b/requests3/core/_http/_async/response.py index b6e2be15..a965777c 100644 --- a/requests3/core/_http/_async/response.py +++ b/requests3/core/_http/_async/response.py @@ -449,9 +449,9 @@ class HTTPResponse(io.IOBase): def readinto(self, b): # This method is required for `io` module compatibility. temp = self.read(len(b)) - if len(temp) == 0: + + if not temp: return 0 - else: - b[: len(temp)] = temp - return len(temp) + b[: len(temp)] = temp + return len(temp) diff --git a/requests3/core/_http/_backends/sync_backend.py b/requests3/core/_http/_backends/sync_backend.py index ade0d68e..fc4a27f3 100644 --- a/requests3/core/_http/_backends/sync_backend.py +++ b/requests3/core/_http/_backends/sync_backend.py @@ -1,5 +1,4 @@ import errno -import select import socket import ssl from ..util.connection import create_connection diff --git a/requests3/core/_http/_backends/twisted_backend.py b/requests3/core/_http/_backends/twisted_backend.py index 86dbe766..f3baa849 100644 --- a/requests3/core/_http/_backends/twisted_backend.py +++ b/requests3/core/_http/_backends/twisted_backend.py @@ -238,16 +238,16 @@ class TwistedSocket: # the most that we ever generate. (If *we* were cancelled, then there # will be 2 CancelledErrors, and that's fine; in that case we want to # preserve 1 of them and then re-raise it.) - for i in range(len(failures)): - if isinstance(failures[i].value, CancelledError): - del failures[i] + for idx, failure in enumerate(failures): + if isinstance(failure.value, CancelledError): + del failures[idx] break # Now whatever's left is what we need to re-raise - if len(failures) == 0: + if not failures: return - elif len(failures) == 1: + if len(failures) == 1: failures[0].raiseException() else: raise DoubleError(*failures) diff --git a/requests3/core/_http/_sync/response.py b/requests3/core/_http/_sync/response.py index ec5de3b0..a3835021 100644 --- a/requests3/core/_http/_sync/response.py +++ b/requests3/core/_http/_sync/response.py @@ -449,9 +449,9 @@ class HTTPResponse(io.IOBase): def readinto(self, b): # This method is required for `io` module compatibility. temp = self.read(len(b)) - if len(temp) == 0: + + if not temp: return 0 - else: - b[: len(temp)] = temp - return len(temp) + b[: len(temp)] = temp + return len(temp) diff --git a/requests3/core/_http/exceptions.py b/requests3/core/_http/exceptions.py index 287df15b..8c62a4fa 100644 --- a/requests3/core/_http/exceptions.py +++ b/requests3/core/_http/exceptions.py @@ -3,17 +3,17 @@ from __future__ import absolute_import # Base Exceptions class HTTPError(Exception): - "Base exception used by this module." + """Base exception used by this module.""" pass class HTTPWarning(Warning): - "Base warning used by this module." + """Base warning used by this module.""" pass class PoolError(HTTPError): - "Base exception for errors caused within a pool." + """Base exception for errors caused within a pool.""" def __init__(self, pool, message): self.pool = pool @@ -25,7 +25,7 @@ class PoolError(HTTPError): class RequestError(PoolError): - "Base exception for PoolErrors that have associated URLs." + """Base exception for PoolErrors that have associated URLs.""" def __init__(self, pool, url, message): self.url = url @@ -37,22 +37,22 @@ class RequestError(PoolError): class SSLError(HTTPError): - "Raised when SSL certificate fails in an HTTPS connection." + """Raised when SSL certificate fails in an HTTPS connection.""" pass class ProxyError(HTTPError): - "Raised when the connection to a proxy fails." + """Raised when the connection to a proxy fails.""" pass class DecodeError(HTTPError): - "Raised when automatic decoding based on Content-Type fails." + """Raised when automatic decoding based on Content-Type fails.""" pass class ProtocolError(HTTPError): - "Raised when something unexpected happens mid-request/response." + """Raised when something unexpected happens mid-request/response.""" pass @@ -94,39 +94,39 @@ class TimeoutError(HTTPError): class ReadTimeoutError(TimeoutError, RequestError): - "Raised when a socket timeout occurs while receiving data from a server" + """Raised when a socket timeout occurs while receiving data from a server""" pass # This timeout error does not have a URL attached and needs to inherit from the # base HTTPError class ConnectTimeoutError(TimeoutError): - "Raised when a socket timeout occurs while connecting to a server" + """Raised when a socket timeout occurs while connecting to a server""" pass class NewConnectionError(ConnectTimeoutError, PoolError): - "Raised when we fail to establish a new connection. Usually ECONNREFUSED." + """Raised when we fail to establish a new connection. Usually ECONNREFUSED.""" pass class EmptyPoolError(PoolError): - "Raised when a pool runs out of connections and no more are allowed." + """Raised when a pool runs out of connections and no more are allowed.""" pass class ClosedPoolError(PoolError): - "Raised when a request enters a pool after the pool has been closed." + """Raised when a request enters a pool after the pool has been closed.""" pass class LocationValueError(ValueError, HTTPError): - "Raised when there is something wrong with a given URL input." + """Raised when there is something wrong with a given URL input.""" pass class LocationParseError(LocationValueError): - "Raised when get_host or similar fails to parse the URL input." + """Raised when get_host or similar fails to parse the URL input.""" def __init__(self, location): message = "Failed to parse: %s" % location @@ -135,38 +135,38 @@ class LocationParseError(LocationValueError): class ResponseError(HTTPError): - "Used as a container for an error reason supplied in a MaxRetryError." + """Used as a container for an error reason supplied in a MaxRetryError.""" GENERIC_ERROR = "too many error responses" SPECIFIC_ERROR = "too many {status_code} error responses" class SecurityWarning(HTTPWarning): - "Warned when perfoming security reducing actions" + """Warned when perfoming security reducing actions""" pass class SubjectAltNameWarning(SecurityWarning): - "Warned when connecting to a host with a certificate missing a SAN." + """Warned when connecting to a host with a certificate missing a SAN.""" pass class InsecureRequestWarning(SecurityWarning): - "Warned when making an unverified HTTPS request." + """Warned when making an unverified HTTPS request.""" pass class SystemTimeWarning(SecurityWarning): - "Warned when system time is suspected to be wrong" + """Warned when system time is suspected to be wrong""" pass class InsecurePlatformWarning(SecurityWarning): - "Warned when certain SSL configuration is not available on a platform." + """Warned when certain SSL configuration is not available on a platform.""" pass class SNIMissingWarning(HTTPWarning): - "Warned when making a HTTPS request without SNI available." + """Warned when making a HTTPS request without SNI available.""" pass @@ -195,7 +195,7 @@ class BadVersionError(ProtocolError): class ProxySchemeUnknown(AssertionError, ValueError): - "ProxyManager does not support the supplied scheme" + """ProxyManager does not support the supplied scheme""" # TODO(t-8ch): Stop inheriting from AssertionError in v2.0. def __init__(self, scheme): @@ -204,7 +204,7 @@ class ProxySchemeUnknown(AssertionError, ValueError): class HeaderParsingError(HTTPError): - "Raised by assert_header_parsing, but we convert it to a log.warning statement." + """Raised by assert_header_parsing, but we convert it to a log.warning statement.""" def __init__(self, defects, unparsed_data): message = "%s, unparsed data: %r" % (defects or "Unknown", unparsed_data) @@ -212,7 +212,7 @@ class HeaderParsingError(HTTPError): class UnrewindableBodyError(HTTPError): - "urllib3 encountered an error when trying to rewind a body" + """urllib3 encountered an error when trying to rewind a body""" pass diff --git a/requests3/core/_http/packages/ssl_match_hostname/_implementation.py b/requests3/core/_http/packages/ssl_match_hostname/_implementation.py index 304d3116..2771bd2d 100644 --- a/requests3/core/_http/packages/ssl_match_hostname/_implementation.py +++ b/requests3/core/_http/packages/ssl_match_hostname/_implementation.py @@ -107,14 +107,14 @@ def match_hostname(cert, hostname): try: # Divergence from upstream: ipaddress can't handle byte str host_ip = ipaddress.ip_address(_to_unicode(hostname)) - except ValueError: - # Not an IP address (common case) - host_ip = None except UnicodeError: # Divergence from upstream: Have to deal with ipaddress not taking # byte strings. addresses should be all ascii, so we consider it not # an ipaddress in this case host_ip = None + except ValueError: + # Not an IP address (common case) + host_ip = None except AttributeError: # Divergence from upstream: Make ipaddress library optional if ipaddress is None: diff --git a/requests3/core/_http/util/selectors.py b/requests3/core/_http/util/selectors.py index 92d31796..6cbb082b 100644 --- a/requests3/core/_http/util/selectors.py +++ b/requests3/core/_http/util/selectors.py @@ -328,7 +328,7 @@ if hasattr(select, "select"): def select(self, timeout=None): # Selecting on empty lists on Windows errors out. - if not len(self._readers) and not len(self._writers): + if not (self._readers or self._writers): return [] timeout = None if timeout is None else max(timeout, 0.0) diff --git a/requests3/http_auth.py b/requests3/http_auth.py index 181188f2..8db2368f 100644 --- a/requests3/http_auth.py +++ b/requests3/http_auth.py @@ -6,17 +6,17 @@ requests.auth This module contains the authentication handlers for Requests. """ +import hashlib import os import re -import time -import hashlib import threading - +import time +import warnings from base64 import b64encode -from ._basics import urlparse, str, basestring -from .http_cookies import extract_cookies_to_jar +from ._basics import basestring, str, urlparse from ._internal_utils import to_native_string +from .http_cookies import extract_cookies_to_jar from .http_utils import parse_dict_header CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded" diff --git a/requests3/http_models.py b/requests3/http_models.py index 543229bc..eb6708fd 100644 --- a/requests3/http_models.py +++ b/requests3/http_models.py @@ -8,7 +8,6 @@ This module contains the primary objects that power Requests. import datetime import codecs -import sys from typing import Mapping, Callable # Import encoding now, to avoid implicit import later. diff --git a/requests3/http_utils.py b/requests3/http_utils.py index 26d20442..482989ae 100644 --- a/requests3/http_utils.py +++ b/requests3/http_utils.py @@ -8,6 +8,7 @@ that are also useful for external consumption. """ import codecs +import collections import contextlib import io import os @@ -15,7 +16,6 @@ import re import cgi import socket import struct -import sys import tempfile import warnings import typing