Many minor code quality and readability improvements
MODIFY
dummyserver/handlers.py
dummyserver/handlers.py
@@ -107,7 +107,7 @@ class TestingApp(RequestHandler):
def certificate(self, request):
"""Return the requester's certificate."""
cert = request.get_ssl_certificate()
- subject = dict()
+ subject = {}
if cert is not None:
subject = {k: v for (k, v) in [y for z in cert["subject"] for y in z]}
return Response(json.dumps(subject))

MODIFY
dummyserver/testcase.py
dummyserver/testcase.py
@@ -182,7 +182,7 @@ class HTTPDummyProxyTestCase:
app, cls.io_loop, None, "http", cls.proxy_host
)
- upstream_ca_certs = cls.https_certs.get("ca_certs", None)
+ upstream_ca_certs = cls.https_certs.get("ca_certs")
app = web.Application(
[(r".*", ProxyHandler)], upstream_ca_certs=upstream_ca_certs
)

MODIFY
src/urllib3/contrib/securetransport.py
src/urllib3/contrib/securetransport.py
@@ -757,7 +757,7 @@ class SecureTransportContext:
@verify_mode.setter
def verify_mode(self, value):
- self._verify = True if value == ssl.CERT_REQUIRED else False
+ self._verify = value == ssl.CERT_REQUIRED
def set_default_verify_paths(self):
# So, this has to do something a bit weird. Specifically, what it does

MODIFY
src/urllib3/response.py
src/urllib3/response.py
@@ -272,7 +272,7 @@ class BaseHTTPResponse(io.IOBase):
for e in content_encoding.split(",")
if e.strip() in self.CONTENT_DECODERS
]
- if len(encodings):
+ if encodings:
self._decoder = _get_decoder(content_encoding)
def _decode(self, data: bytes, decode_content: bool, flush_decoder: bool) -> bytes:
@@ -864,7 +864,7 @@ class HTTPResponse(BaseHTTPResponse):
If the request that generated this response redirected, this method
will return the final redirect location.
"""
- if self.retries is not None and len(self.retries.history):
+ if self.retries is not None and self.retries.history:
return self.retries.history[-1].redirect_location
else:
return self._request_url

MODIFY
src/urllib3/util/retry.py
src/urllib3/util/retry.py
@@ -204,7 +204,7 @@ class Retry:
self.backoff_factor = backoff_factor
self.raise_on_redirect = raise_on_redirect
self.raise_on_status = raise_on_status
- self.history = history or tuple()
+ self.history = history or ()
self.respect_retry_after_header = respect_retry_after_header
self.remove_headers_on_redirect = frozenset(
[h.lower() for h in remove_headers_on_redirect]
@@ -274,8 +274,7 @@ class Retry:
retry_date = email.utils.mktime_tz(retry_date_tuple)
seconds = retry_date - time.time()
- if seconds < 0:
- seconds = 0
+ seconds = max(seconds, 0)
return seconds

MODIFY
src/urllib3/util/url.py
src/urllib3/util/url.py
@@ -241,7 +241,7 @@ def _remove_path_dot_segments(path: str) -> str:
if segment == ".":
continue
# Anything other than '..', should be appended to the output
- elif segment != "..":
+ if segment != "..":
output.append(segment)
# In this case segment == '..', if we can, we should pop the last
# element

Many minor code quality and readability improvements