Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up various refactorings #6165

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
:license: Apache 2.0, see LICENSE for more details.
"""

import contextlib
import warnings

import urllib3
Expand Down Expand Up @@ -94,9 +95,10 @@ def _check_cryptography(cryptography_version):
return

if cryptography_version < [1, 3, 4]:
warning = "Old version of cryptography ({}) may cause slowdown.".format(
cryptography_version
warning = (
f"Old version of cryptography ({cryptography_version}) may cause slowdown."
)

warnings.warn(warning, RequestsDependencyWarning)


Expand All @@ -107,17 +109,19 @@ def _check_cryptography(cryptography_version):
)
except (AssertionError, ValueError):
warnings.warn(
"urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
"version!".format(
urllib3.__version__, chardet_version, charset_normalizer_version
(
f"urllib3 ({urllib3.__version__}) or "
+ f"chardet ({chardet_version})/charset_normalizer "
+ f"({charset_normalizer_version}) doesn't match a supported version!"
),
RequestsDependencyWarning,
)


# Attempt to enable urllib3's fallback for SNI support
# if the standard library doesn't support SNI or the
# 'ssl' library isn't available.
try:
with contextlib.suppress(ImportError):
try:
import ssl
except ImportError:
Expand All @@ -132,8 +136,6 @@ def _check_cryptography(cryptography_version):
from cryptography import __version__ as cryptography_version

_check_cryptography(cryptography_version)
except ImportError:
pass

# urllib3's DependencyWarnings should be silenced.
from urllib3.exceptions import DependencyWarning
Expand Down
4 changes: 1 addition & 3 deletions requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,7 @@ def send(
f"Invalid timeout {timeout}. Pass a (connect, read) timeout tuple, "
f"or a single float to set both timeouts to the same value."
)
elif isinstance(timeout, TimeoutSauce):
pass
else:
elif not isinstance(timeout, TimeoutSauce):
timeout = TimeoutSauce(connect=timeout, read=timeout)

try:
Expand Down
4 changes: 2 additions & 2 deletions requests/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def _basic_auth_str(username, password):
if isinstance(password, str):
password = password.encode("latin1")

authstr = "Basic " + to_native_string(
b64encode(b":".join((username, password))).strip()
authstr = (
f'Basic {to_native_string(b64encode(b":".join((username, password))).strip())}'
)

return authstr
Expand Down
16 changes: 7 additions & 9 deletions requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,12 @@ def prepare_body(self, data, files, json=None):
# Multi-part file uploads.
if files:
(body, content_type) = self._encode_files(files, data)
else:
if data:
body = self._encode_params(data)
if isinstance(data, basestring) or hasattr(data, "read"):
content_type = None
else:
content_type = "application/x-www-form-urlencoded"
elif data:
body = self._encode_params(data)
if isinstance(data, basestring) or hasattr(data, "read"):
content_type = None
else:
content_type = "application/x-www-form-urlencoded"

self.prepare_content_length(body)

Expand Down Expand Up @@ -813,8 +812,7 @@ def generate():
# Special case for urllib3.
if hasattr(self.raw, "stream"):
try:
for chunk in self.raw.stream(chunk_size, decode_content=True):
yield chunk
yield from self.raw.stream(chunk_size, decode_content=True)
except ProtocolError as e:
raise ChunkedEncodingError(e)
except DecodeError as e:
Expand Down
9 changes: 4 additions & 5 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).
"""

import contextlib
import os
import sys
import time
Expand Down Expand Up @@ -720,7 +722,7 @@ def send(self, request, **kwargs):
if allow_redirects:
# Redirect resolving generator.
gen = self.resolve_redirects(r, request, **kwargs)
history = [resp for resp in gen]
history = list(gen)
else:
history = []

Expand All @@ -734,13 +736,10 @@ def send(self, request, **kwargs):

# If redirects aren't being followed, store the response on the Request for Response.next().
if not allow_redirects:
try:
with contextlib.suppress(StopIteration):
r._next = next(
self.resolve_redirects(r, request, yield_requests=True, **kwargs)
)
except StopIteration:
pass

if not stream:
r.content

Expand Down
20 changes: 8 additions & 12 deletions requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def get_netrc_auth(url, raise_errors=False):
else:
netrc_locations = (f"~/{f}" for f in NETRC_FILES)

try:
# suppressed for App Engine
with contextlib.suppress(ImportError, AttributeError):
from netrc import NetrcParseError, netrc

netrc_path = None
Expand Down Expand Up @@ -243,10 +244,6 @@ def get_netrc_auth(url, raise_errors=False):
if raise_errors:
raise

# App Engine hackiness.
except (ImportError, AttributeError):
pass


def guess_filename(obj):
"""Tries to guess the filename of the given object."""
Expand Down Expand Up @@ -840,16 +837,15 @@ def select_proxy(url, proxies):
return proxies.get(urlparts.scheme, proxies.get("all"))

proxy_keys = [
urlparts.scheme + "://" + urlparts.hostname,
f"{urlparts.scheme}://{urlparts.hostname}",
urlparts.scheme,
"all://" + urlparts.hostname,
f"all://{urlparts.hostname}",
"all",
]
proxy = None
for proxy_key in proxy_keys:
if proxy_key in proxies:
proxy = proxies[proxy_key]
break

proxy = next(
(proxies[proxy_key] for proxy_key in proxy_keys if proxy_key in proxies), None
)

return proxy

Expand Down