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 29, 2022
1 parent 6ae0f59 commit 37012f7
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 141 deletions.
18 changes: 8 additions & 10 deletions .pre-commit-config.yaml
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
71 changes: 15 additions & 56 deletions docs/dev/contributing.rst
Expand Up @@ -93,6 +93,21 @@ event that you object to the code review feedback, you should make your case
clearly and calmly. If, after doing so, the feedback is judged to still apply,
you must either apply the feedback or withdraw your contribution.

Code Style
~~~~~~~~~~

Requests uses a collection of tools to ensure the code base has a consistent
style as it grows. We have these orchestrated using a tool called
`pre-commit`_. This can be installed locally and run over your changes prior
to opening a PR, and will also be run as part of the CI approval process
before a change is merged.

You can find the full list of formatting requirements specified in the
`.pre-commit-config.yaml`_ at the top level directory of Requests.

.. _pre-commit: https://pre-commit.com/
.. _.pre-commit-config.yaml: https://github.com/psf/requests/blob/main/.pre-commit-config.yaml

New Contributors
~~~~~~~~~~~~~~~~

Expand All @@ -103,62 +118,6 @@ asking for help.

Please also check the :ref:`early-feedback` section.

Kenneth Reitz's Code Style™
~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Requests codebase uses the `PEP 8`_ code style.

In addition to the standards outlined in PEP 8, we have a few guidelines:

- Line-length can exceed 79 characters, to 100, when convenient.
- Line-length can exceed 100 characters, when doing otherwise would be *terribly* inconvenient.
- Always use single-quoted strings (e.g. ``'#flatearth'``), unless a single-quote occurs within the string.

Additionally, one of the styles that PEP8 recommends for `line continuations`_
completely lacks all sense of taste, and is not to be permitted within
the Requests codebase::

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)

No. Just don't. Please. This is much better::

foo = long_function_name(
var_one,
var_two,
var_three,
var_four,
)

Docstrings are to follow the following syntaxes::

def the_earth_is_flat():
"""NASA divided up the seas into thirty-three degrees."""
pass

::

def fibonacci_spiral_tool():
"""With my feet upon the ground I lose myself / between the sounds
and open wide to suck it in. / I feel it move across my skin. / I'm
reaching up and reaching out. / I'm reaching for the random or
whatever will bewilder me. / Whatever will bewilder me. / And
following our will and wind we may just go where no one's been. /
We'll ride the spiral to the end and may just go where no one's
been.

Spiral out. Keep going...
"""
pass

All functions, methods, and classes are to contain docstrings. Object data
model methods (e.g. ``__repr__``) are typically the exception to this rule.

Thanks for helping to make the world a better place!

.. _PEP 8: https://pep8.org/
.. _line continuations: https://www.python.org/dev/peps/pep-0008/#indentation

Documentation Contributions
---------------------------
Expand Down
13 changes: 13 additions & 0 deletions pyproject.toml
@@ -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.

6 changes: 3 additions & 3 deletions requests/adapters.py
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,9 +537,9 @@ def send(
preload_content=False,
decode_content=False,
)
except:
except Exception:
# 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

Expand Down
2 changes: 1 addition & 1 deletion requests/auth.py
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
5 changes: 3 additions & 2 deletions requests/cookies.py
Expand Up @@ -476,8 +476,9 @@ def create_cookie(name, value, **kwargs):

badargs = set(kwargs) - set(result)
if badargs:
err = "create_cookie() got unexpected keyword arguments: %s"
raise TypeError(err % list(badargs))
raise TypeError(
f"create_cookie() got unexpected keyword arguments: {list(badargs)}"
)

result.update(kwargs)
result["port_specified"] = bool(result["port"])
Expand Down
11 changes: 6 additions & 5 deletions requests/models.py
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 @@ -965,6 +965,8 @@ def json(self, **kwargs):
# and the server didn't bother to tell us what codec *was*
# used.
pass
except JSONDecodeError as e:
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

try:
return complexjson.loads(self.text, **kwargs)
Expand All @@ -979,17 +981,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
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
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
9 changes: 6 additions & 3 deletions setup.cfg
Expand Up @@ -9,6 +9,9 @@ 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
tests/compat.py:F401
1 change: 0 additions & 1 deletion tests/__init__.py
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
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
@@ -1,7 +1,3 @@
import sys

import pytest

from requests.help import info


Expand Down

0 comments on commit 37012f7

Please sign in to comment.