Skip to content

Commit

Permalink
Fix flake8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Apr 27, 2022
1 parent 6ae0f59 commit 0e51d67
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 65 deletions.
18 changes: 8 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
exclude: tests/test_lowlevel.py
- id: black
exclude: tests/test_lowlevel.py
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
hooks:
- id: pyupgrade
args: [--py37-plus]
# TODO: Add flake8 changes after we're happy
# with above formatting changes.
#- repo: https://gitlab.com/pycqa/flake8
# rev: 4.0.1
# hooks:
# - id: flake8
- id: pyupgrade
args: [--py37-plus]
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tool.isort]
profile = "black"
src_paths = ["requests", "test"]
honor_noqa = true

[tool.pytest.ini_options]
addopts = "-p no:warnings --doctest-modules"
doctest_optionflags = "NORMALIZE_WHITESPACE ELLIPSIS"
minversion = "6.2"
testpaths = [
"requests",
"tests",
]
3 changes: 0 additions & 3 deletions pytest.ini

This file was deleted.

7 changes: 3 additions & 4 deletions requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

import os.path
import socket
import socket # noqa: F401

from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
from urllib3.exceptions import HTTPError as _HTTPError
Expand Down Expand Up @@ -537,11 +537,10 @@ def send(
preload_content=False,
decode_content=False,
)
except:
finally:
# If we hit any problems here, clean up the connection.
# Then, reraise so that we can handle the actual exception.
# Then, raise so that we can handle the actual exception.
low_conn.close()
raise

except (ProtocolError, OSError) as err:
raise ConnectionError(err, request=request)
Expand Down
2 changes: 1 addition & 1 deletion requests/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def sha512_utf8(x):

hash_utf8 = sha512_utf8

KD = lambda s, d: hash_utf8(f"{s}:{d}")
KD = lambda s, d: hash_utf8(f"{s}:{d}") # noqa:E731

if hash_utf8 is None:
return None
Expand Down
9 changes: 4 additions & 5 deletions requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Import encoding now, to avoid implicit import later.
# Implicit import within threads may cause LookupError when standard library is in a ZIP,
# such as in Embedded Python. See https://github.com/psf/requests/issues/3578.
import encodings.idna
import encodings.idna # noqa: F401
from io import UnsupportedOperation

from urllib3.exceptions import (
Expand Down Expand Up @@ -979,17 +979,16 @@ def links(self):

header = self.headers.get("link")

# l = MultiDict()
l = {}
resolved_links = {}

if header:
links = parse_header_links(header)

for link in links:
key = link.get("rel") or link.get("url")
l[key] = link
resolved_links[key] = link

return l
return resolved_links

def raise_for_status(self):
"""Raises :class:`HTTPError`, if one occurred."""
Expand Down
9 changes: 7 additions & 2 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
from .hooks import default_hooks, dispatch_hook

# formerly defined here, reexposed here for backward compatibility
from .models import DEFAULT_REDIRECT_LIMIT, REDIRECT_STATI, PreparedRequest, Request
from .models import ( # noqa: F401
DEFAULT_REDIRECT_LIMIT,
REDIRECT_STATI,
PreparedRequest,
Request,
)
from .status_codes import codes
from .structures import CaseInsensitiveDict
from .utils import (
from .utils import ( # noqa: F401
DEFAULT_PORTS,
default_headers,
get_auth_from_url,
Expand Down
5 changes: 3 additions & 2 deletions requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .__version__ import __version__

# to_native_string is unused here, but imported here for backwards compatibility
from ._internal_utils import to_native_string
from ._internal_utils import to_native_string # noqa: F401
from .compat import (
Mapping,
basestring,
Expand Down Expand Up @@ -764,7 +764,8 @@ def should_bypass_proxies(url, no_proxy):
"""
# Prioritize lowercase environment variables over uppercase
# to keep a consistent behaviour with other http projects (curl, wget).
get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())
def get_proxy(key):
return os.environ.get(key) or os.environ.get(key.upper())

# First check whether no_proxy is defined. If it is, check that the URL
# we're getting isn't in the no_proxy list.
Expand Down
8 changes: 5 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ requires-dist =
idna>=2.5,<4
urllib3>=1.21.1,<1.27

[isort]
profile = black
honor_noqa = true
[flake8]
ignore = E203, E501, W503
per-file-ignores =
requests/__init__.py:E402, F401
requests/compat.py:E402, F401
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import warnings

import urllib3
from urllib3.exceptions import SNIMissingWarning

# urllib3 sets SNIMissingWarning to only go off once,
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from SimpleHTTPServer import SimpleHTTPRequestHandler

import ssl
import tempfile
import threading

import pytest
Expand Down
4 changes: 0 additions & 4 deletions tests/test_help.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import sys

import pytest

from requests.help import info


Expand Down
23 changes: 13 additions & 10 deletions tests/test_lowlevel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import threading

import pytest
from tests.testserver.server import Server, consume_socket_content

import requests
from tests.testserver.server import Server, consume_socket_content

from .utils import override_environ

Expand Down Expand Up @@ -51,7 +51,7 @@ def incomplete_chunked_response_handler(sock):
with server as (host, port):
url = f"http://{host}:{port}/"
with pytest.raises(requests.exceptions.ChunkedEncodingError):
r = requests.get(url)
requests.get(url)
close_server.set() # release server block


Expand Down Expand Up @@ -98,12 +98,14 @@ def test_conflicting_content_lengths():

def multiple_content_length_response_handler(sock):
request_content = consume_socket_content(sock, timeout=0.5)

sock.send(b"HTTP/1.1 200 OK\r\n" +
b"Content-Type: text/plain\r\n" +
b"Content-Length: 16\r\n" +
b"Content-Length: 32\r\n\r\n" +
b"-- Bad Actor -- Original Content\r\n")
response = (
b"HTTP/1.1 200 OK\r\n"
b"Content-Type: text/plain\r\n" +
b"Content-Length: 16\r\n" +
b"Content-Length: 32\r\n\r\n" +
b"-- Bad Actor -- Original Content\r\n"
)
sock.send(response)

return request_content

Expand All @@ -113,7 +115,7 @@ def multiple_content_length_response_handler(sock):
with server as (host, port):
url = f"http://{host}:{port}/"
with pytest.raises(requests.exceptions.InvalidHeader):
r = requests.get(url)
requests.get(url)
close_server.set()


Expand Down Expand Up @@ -329,6 +331,7 @@ def redirect_resp_handler(sock):

close_server.set()


def test_fragment_not_sent_with_request():
"""Verify that the fragment portion of a URI isn't sent to the server."""
def response_handler(sock):
Expand Down Expand Up @@ -358,6 +361,7 @@ def response_handler(sock):

close_server.set()


def test_fragment_update_on_redirect():
"""Verify we only append previous fragment if one doesn't exist on new
location. If a new fragment is encountered in a Location header, it should
Expand Down Expand Up @@ -388,7 +392,6 @@ def response_handler(sock):
with server as (host, port):
url = f'http://{host}:{port}/path/to/thing/#view=edit&token=hunter2'
r = requests.get(url)
raw_request = r.content

assert r.status_code == 200
assert len(r.history) == 2
Expand Down
41 changes: 23 additions & 18 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
ConnectTimeout,
ContentDecodingError,
InvalidHeader,
InvalidJSONError,
InvalidProxyURL,
InvalidSchema,
InvalidURL,
Expand Down Expand Up @@ -89,7 +88,7 @@ def test_entry_points(self):
requests.patch
requests.post
# Not really an entry point, but people rely on it.
from requests.packages.urllib3.poolmanager import PoolManager
from requests.packages.urllib3.poolmanager import PoolManager # noqa:F401

@pytest.mark.parametrize(
"exception, url",
Expand Down Expand Up @@ -934,9 +933,8 @@ def test_invalid_ssl_certificate_files(self, httpbin_secure):

with pytest.raises(IOError) as e:
requests.get(httpbin_secure(), cert=(".", INVALID_PATH))
assert (
str(e.value)
== f"Could not find the TLS key file, invalid path: {INVALID_PATH}"
assert str(e.value) == (
f"Could not find the TLS key file, invalid path: {INVALID_PATH}"
)

@pytest.mark.parametrize(
Expand Down Expand Up @@ -1112,7 +1110,9 @@ def hook(resp, **kwargs):
s.send(prep)

def test_session_hooks_are_used_with_no_request_hooks(self, httpbin):
hook = lambda x, *args, **kwargs: x
def hook(*args, **kwargs):
pass

s = requests.Session()
s.hooks["response"].append(hook)
r = requests.Request("GET", httpbin())
Expand All @@ -1121,8 +1121,12 @@ def test_session_hooks_are_used_with_no_request_hooks(self, httpbin):
assert prep.hooks["response"] == [hook]

def test_session_hooks_are_overridden_by_request_hooks(self, httpbin):
hook1 = lambda x, *args, **kwargs: x
hook2 = lambda x, *args, **kwargs: x
def hook1(*args, **kwargs):
pass

def hook2(*args, **kwargs):
pass

assert hook1 is not hook2
s = requests.Session()
s.hooks["response"].append(hook2)
Expand Down Expand Up @@ -1691,15 +1695,15 @@ def test_header_value_not_str(self, httpbin):

# Test for int
with pytest.raises(InvalidHeader) as excinfo:
r = requests.get(httpbin("get"), headers=headers_int)
requests.get(httpbin("get"), headers=headers_int)
assert "foo" in str(excinfo.value)
# Test for dict
with pytest.raises(InvalidHeader) as excinfo:
r = requests.get(httpbin("get"), headers=headers_dict)
requests.get(httpbin("get"), headers=headers_dict)
assert "bar" in str(excinfo.value)
# Test for list
with pytest.raises(InvalidHeader) as excinfo:
r = requests.get(httpbin("get"), headers=headers_list)
requests.get(httpbin("get"), headers=headers_list)
assert "baz" in str(excinfo.value)

def test_header_no_return_chars(self, httpbin):
Expand All @@ -1712,13 +1716,13 @@ def test_header_no_return_chars(self, httpbin):

# Test for newline
with pytest.raises(InvalidHeader):
r = requests.get(httpbin("get"), headers=headers_ret)
requests.get(httpbin("get"), headers=headers_ret)
# Test for line feed
with pytest.raises(InvalidHeader):
r = requests.get(httpbin("get"), headers=headers_lf)
requests.get(httpbin("get"), headers=headers_lf)
# Test for carriage return
with pytest.raises(InvalidHeader):
r = requests.get(httpbin("get"), headers=headers_cr)
requests.get(httpbin("get"), headers=headers_cr)

def test_header_no_leading_space(self, httpbin):
"""Ensure headers containing leading whitespace raise
Expand All @@ -1729,10 +1733,11 @@ def test_header_no_leading_space(self, httpbin):

# Test for whitespace
with pytest.raises(InvalidHeader):
r = requests.get(httpbin("get"), headers=headers_space)
requests.get(httpbin("get"), headers=headers_space)

# Test for tab
with pytest.raises(InvalidHeader):
r = requests.get(httpbin("get"), headers=headers_tab)
requests.get(httpbin("get"), headers=headers_tab)

@pytest.mark.parametrize("files", ("foo", b"foo", bytearray(b"foo")))
def test_can_send_objects_with_files(self, httpbin, files):
Expand Down Expand Up @@ -2655,7 +2660,7 @@ def test_preparing_bad_url(self, url):
@pytest.mark.parametrize("url, exception", (("http://localhost:-1", InvalidURL),))
def test_redirecting_to_bad_url(self, httpbin, url, exception):
with pytest.raises(exception):
r = requests.get(httpbin("redirect-to"), params={"url": url})
requests.get(httpbin("redirect-to"), params={"url": url})

@pytest.mark.parametrize(
"input, expected",
Expand Down Expand Up @@ -2730,7 +2735,7 @@ def test_parameters_for_nonstandard_schemes(self, input, params, expected):
def test_post_json_nan(self, httpbin):
data = {"foo": float("nan")}
with pytest.raises(requests.exceptions.InvalidJSONError):
r = requests.post(httpbin("post"), json=data)
requests.post(httpbin("post"), json=data)

def test_json_decode_compatibility(self, httpbin):
r = requests.get(httpbin("bytes/20"))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_testserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import time

import pytest
from tests.testserver.server import Server

import requests
from tests.testserver.server import Server


class TestTestServer:
Expand Down

0 comments on commit 0e51d67

Please sign in to comment.